const algosdk = require(‘algosdk’);
var account1_mnemonic = “your mnemonic”;
var account2_mnemonic = “your mnemonic”;
var account3_mnemonic = “your mnemonic”;
var recoveredAccount1 = algosdk.mnemonicToSecretKey(account1_mnemonic);
var recoveredAccount2 = algosdk.mnemonicToSecretKey(account2_mnemonic);
var recoveredAccount3 = algosdk.mnemonicToSecretKey(account3_mnemonic);
console.log(recoveredAccount3.addr);
const baseServer = “https://testnet-algorand.api.purestake.io/p”;
const port = “”;
const token = {
‘X-API-key’ : ‘your purestake api’,}
let algodclient = new algosdk.Algodv2(token, baseServer, port);
// Function used to wait for a tx confirmation
const waitForConfirmation = async function (algodclient, txId) {
let response = await algodclient.status().do();
let lastround = response[“last-round”];
while (true) {
const pendingInfo = await algodclient.pendingTransactionInformation(txId).do();
if (pendingInfo[“confirmed-round”] !== null && pendingInfo[“confirmed-round”] > 0) {
//Got the completed Transaction
console.log("Transaction " + txId + " confirmed in round " + pendingInfo[“confirmed-round”]);
break;
}
lastround++;
await algodclient.statusAfterBlock(lastround).do();
}
};
// Function used to print created asset for account and assetid
const printCreatedAsset = async function (algodclient, account, assetid) {
// note: if you have an indexer instance available it is easier to just use this
// let accountInfo = await indexerClient.searchAccounts()
// .assetID(assetIndex).do();
// and in the loop below use this to extract the asset for a particular account
// accountInfo[‘accounts’][idx][account]);
let accountInfo = await algodclient.accountInformation(account).do();
for (let idx = 0; idx < accountInfo[‘created-assets’].length; idx++) {
let scrutinizedAsset = accountInfo[‘created-assets’][idx];
if (scrutinizedAsset[‘index’] === assetid) {
console.log("AssetID = " + scrutinizedAsset[‘index’]);
let myparms = JSON.stringify(scrutinizedAsset[‘params’], undefined, 2);
console.log("parms = " + myparms);
break;
}
}
};
// Function used to print asset holding for account and assetid
const printAssetHolding = async function (algodclient, account, assetid) {
// note: if you have an indexer instance available it is easier to just use this
// let accountInfo = await indexerClient.searchAccounts()
// .assetID(assetIndex).do();
// and in the loop below use this to extract the asset for a particular account
// accountInfo[‘accounts’][idx][account]);
let accountInfo = await algodclient.accountInformation(account).do();
for (let idx = 0; idx < accountInfo[‘assets’].length; idx++) {
let scrutinizedAsset = accountInfo[‘assets’][idx];
if (scrutinizedAsset[‘asset-id’] === assetid) {
let myassetholding = JSON.stringify(scrutinizedAsset, undefined, 2);
console.log("assetholdinginfo = " + myassetholding);
break;
}
}
};
(async () => {
// Asset Creation:
// The first transaciton is to create a new asset
// Get last round and suggested tx fee
// We use these to get the latest round and tx fees
// These parameters will be required before every
// Transaction
// We will account for changing transaction parameters
// before every transaction in this example
let params = await algodclient.getTransactionParams().do();
//comment out the next two lines to use suggested fee
params.fee = 1000;
params.flatFee = true;
console.log(params);
let note = undefined; // arbitrary data to be stored in the transaction; here, none is stored
// Asset creation specific parameters
// The following parameters are asset specific
// Throughout the example these will be re-used.
// We will also change the manager later in the example
let addr = recoveredAccount1.addr;
// Whether user accounts will need to be unfrozen before transacting
let defaultFrozen = false;
// integer number of decimals for asset unit calculation
let decimals = 0;
// total number of this asset available for circulation
let totalIssuance = 1000;
// Used to display asset units to user
let unitName = “asset symbol”;
// Friendly name of the asset
let assetName = “asset name”
// Optional string pointing to a URL relating to the asset
let assetURL = “http://someurl”;
// Optional hash commitment of some sort relating to the asset. 32 character length.
let assetMetadataHash = “16efaa3924a6fd9d3a4824799a4ac65d”;
// The following parameters are the only ones
// that can be changed, and they have to be changed
// by the current manager
// Specified address can change reserve, freeze, clawback, and manager
let manager = recoveredAccount2.addr;
// Specified address is considered the asset reserve
// (it has no special privileges, this is only informational)
let reserve = recoveredAccount2.addr;
// Specified address can freeze or unfreeze user asset holdings
let freeze = recoveredAccount2.addr;
// Specified address can revoke user asset holdings and send
// them to other addresses
let clawback = recoveredAccount2.addr;
// signing and sending "txn" allows "addr" to create an asset
let txn = algosdk.makeAssetCreateTxnWithSuggestedParams(addr, note,
totalIssuance, decimals, defaultFrozen, manager, reserve, freeze,
clawback, unitName, assetName, assetURL, assetMetadataHash, params);
let rawSignedTxn = txn.signTxn(recoveredAccount1.sk)
let tx = (await algodclient.sendRawTransaction(rawSignedTxn).do());
console.log("Transaction : " + tx.txId);
let assetID = null;
// wait for transaction to be confirmed
await waitForConfirmation(algodclient, tx.txId);
// Get the new asset's information from the creator account
let ptx = await algodclient.pendingTransactionInformation(tx.txId).do();
assetID = ptx["asset-index"];
// console.log("AssetID = " + assetID);
await printCreatedAsset(algodclient, recoveredAccount1.addr, assetID);
await printAssetHolding(algodclient, recoveredAccount1.addr, assetID);
params = await algodclient.getTransactionParams().do();
//comment out the next two lines to use suggested fee
params.fee = 1000;
params.flatFee = true;
// Asset configuration specific parameters
// all other values are the same so we leave
// Them set.
// specified address can change reserve, freeze, clawback, and manager
manager = recoveredAccount1.addr;
// Note that the change has to come from the existing manager
let ctxn = algosdk.makeAssetConfigTxnWithSuggestedParams(recoveredAccount2.addr, note,
assetID, manager, reserve, freeze, clawback, params);
// This transaction must be signed by the current manager
rawSignedTxn = ctxn.signTxn(recoveredAccount2.sk)
let ctx = (await algodclient.sendRawTransaction(rawSignedTxn).do());
console.log("Transaction : " + ctx.txId);
// wait for transaction to be confirmed
await waitForConfirmation(algodclient, ctx.txId);
// Get the asset information for the newly changed asset
// use indexer or utiltiy function for Account info
// The manager should now be the same as the creator
await printCreatedAsset(algodclient, recoveredAccount1.addr, assetID);