Create a custom CLI Tool, using Deno and Fetch to connect to anvil API, taking few configurable parameters, it is possible to mint a collection using a binary with a custom implementation.
This a a very specific example, the goal is to show how flexible and versatile the Anvil API is.
This example will be entirely done into one file named index.ts
Objectives
Build a simple CLI tool to mint assets
Create a Policy (an NFT Collection)
Create a custom function to customize the asset metadata
Create 2 wallets, one for the policy and one to act as the customer
Submit transaction to the network
This is only an example showing the anvil api versatility
Meaning that the policy has to be signed by the wallet defined in the policy wallet path parameter AND all mutations must be done before the DateTime defined.
Create Metadata
This function is where you have to define your own data and configuration per asset.
// NOTE: Be sure to send ADA in this address, it will be used to pay the tx fee.
const customerWallet = JSON.parse(
Deno.readTextFileSync(args.customerWalletPath)
);
// Wallet to create the policy with, no ADA is required for this one.
const policyWallet = JSON.parse(Deno.readTextFileSync(args.policyWalletPath));
// Expiration date, you can interact with the policy until this DateTime is reached.
// After that the policy is locked.
const slot = timeToSlot(new Date(args.expireDate));
const keyhash = get_keyhash(policyWallet.skey);
if (!keyhash) {
throw new Error("Unable to get key hash for policy, missing or invalid skey");
}
const policyAnvilApi = create_policy_script(keyhash, slot);
const txToSubmitOnChain = FixedTransaction.from_bytes(
Buffer.from(transactionToSignWithCustomerKey.to_hex(), "hex")
);
// This sign the tx and add vkeys to the txToSubmitOnChain, so in submit we don't need to provide signautres
txToSubmitOnChain.sign_and_add_vkey_signature(
PrivateKey.from_bech32(customerWallet.skey)
);
if (args.submit) {
const urlSubmit =
"https://preprod.api.ada-anvil.app/v2/services/transactions/submit";
const submitted = await fetch(urlSubmit, {
method: "POST",
body: JSON.stringify({
signatures: [], // This empty because the txToSubmitOnChain has the vkeys
transaction: txToSubmitOnChain.to_hex(),
}),
headers: {
"Content-Type": "application/json",
"X-Api-Key": "testnet_EyrkvCWDZqjkfLSe1pxaF0hXxUcByHEhHuXIBjt9",
},
}).then((res) => res.json());
console.debug(submitted);
} else {
console.log(txToSubmitOnChain.to_hex());
}