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

Get Assets from Indexer See code on GitHub

Query Indexer with the client to fetch TestNet assets


const name = document.getElementById('name').value;
const limit = document.getElementById('limit').value;

indexerClient.searchForAssets().limit(limit).name(name).do()
.then((d) => {
  console.log(d);
  assetsList = d.assets;
})
.catch((e) => {
  console.error(e);
});
          

code snippet output

Sign Asset Opt-in Transaction See code on GitHub

Ask the user to sign an asset opt-in transaction using the signTxns method


unsignedTxn = algosdk.makeAssetTransferTxnWithSuggestedParamsFromObject({
  from: document.getElementById('from').value,
  to: document.getElementById('from').value,
  assetIndex: +document.getElementById('asset').value,
  note: algorand.encoding.stringToByteArray(document.getElementById('note').value),
  amount: 0,
  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);
})
.catch((e) => {
  console.error(e);
});
          
code snippet output

Sign and Post Asset Opt-out transaction See code on GitHub

Request AlgoSigner to post the asset opt-out transaction to the network as soon as it's signed


unsignedTxn = algosdk.makeAssetTransferTxnWithSuggestedParamsFromObject({
  from: document.getElementById('from').value,
  to: document.getElementById('from').value,
  closeRemainderTo: document.getElementById('from').value,
  assetIndex: +document.getElementById('asset').value,
  note: algorand.encoding.stringToByteArray(document.getElementById('note').value),
  amount: 0,
  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