Clients Setup & Usage See code on GitHub

Setup Algod and Indexer clients and fetch transactions parameters


const algodServer = 'https://testnet-algorand.api.purestake.io/ps2';
const indexerServer = 'https://testnet-algorand.api.purestake.io/idx2';
const token = { 'X-API-Key': 'YOUR API KEY HERE' };
const port = '';

algodClient = new algosdk.Algodv2(token, algodServer, port);
indexerClient = new algosdk.Indexer(token, indexerServer, port);

algodClient.getTransactionParams().do()
.then(d => {
  console.log(d);
  txnParams = d;
})
.catch(e => { 
  console.error(e); 
});
          
code snippet output

Enable account discovery See code on GitHub

Request from the user which accounts will be shared with the dApp


algorand.enable({ genesisID: txnParams.genesisID })
.then((d) => {
  console.log(d);
  sharedAccounts = d.accounts;
})
.catch((e) => {
  console.error(e);
});
          
code snippet output

Sign Asset Config Transaction See code on GitHub

Ask the user to sign an asset creation transaction using the signTxns method


const unsignedTxn = algosdk.makeAssetCreateTxnWithSuggestedParamsFromObject({
  from: document.getElementById('from').value,
  manager: document.getElementById('from').value,
  assetName: document.getElementById('name').value,
  unitName: document.getElementById('unit-name').value,
  total: +document.getElementById('total').value,
  decimals: +document.getElementById('decimals').value,
  note: algorand.encoding.stringToByteArray(document.getElementById('note').value),
  suggestedParams: {...txnParams}
});
console.log(unsignedTxn._getDictForDisplay());

// Use the provided library to make the transaction base64-encoded
let b64EncodedTxn = algorand.encoding.msgpackToBase64(unsignedTxn.toByte());

algorand.signTxns([{txn: b64EncodedTxn}])
.then((d) => {
  console.log(d);
  signedTxs = d;
})
.catch((e) => {
  console.error(e);
});
          

code snippet output

Post Transaction to Network See code on GitHub

Post the previously signed transaction to the network


console.log('Waiting for network confirmation...');

algorand.postTxns(signedTxns)
.then((d) => {
  console.log(d);
  txnID = d.txnIDs[0];
})
.catch((e) => {
  console.error(e);
});
          
code snippet output

Sign and Post Asset Destroy transaction See code on GitHub

Request AlgoSigner to post the asset destroy transaction to the network as soon as it's signed


console.log('Fetching created asset ID...');
const txnInfo = await indexerClient.lookupTransactionByID(txnID).do();
const assetIndex = txnInfo.transaction['created-asset-index'];

let unsignedTxn = algosdk.makePaymentTxnWithSuggestedParamsFromObject({
  from: document.getElementById('from').value,
  assetIndex: assetIndex,
  note: algorand.encoding.stringToByteArray('sent without returning'),
  suggestedParams: {...txnParams}
});
console.log(unsignedTxn._getDictForDisplay());

// Use the provided library to make the transaction base64-encoded
let b64EncodedTxn = algorand.encoding.msgpackToBase64(unsignedTxn.toByte());

algorand.signAndPostTxns([{txn: b64EncodedTxn}])
.then((d) => {
  console.log(d);
})
.catch((e) => {
  console.error(e);
});
          
code snippet output