Basics of Algorand Standard Asset with JavaScript SDK

Quick Overview of ASA and AlgoSigner

Source

Demonstration

Basics of Algorand Standard Asset with JavaScript SDK 1

const algosdk=require('algosdk');

const server="https://testnet-algorand.api.purestake.io/ps2";
const port="";
const token={
	"x-api-key": "mV *** BY" // fill in yours
};

var aliceAddress = '5SL7MUMPYFNDBHUD4L7J4LJRQCQWXQUD3RDK7SBGIDWPH3ZFK47BTH43HE'; // change to yours
var bobAddress = 'BDGUP3RKI3DJVM327B3R4SYT6PQUJBMDMQWHQICKSEQQT6PWCMNAYWEY4M'; // change to yours

let client=new algosdk.Algodv2(token, server, port);

( async() => {

    let alice_account_info = (await client.accountInformation(aliceAddress).do());
    console.log("Asset of Alice: ");
    console.log(alice_account_info.assets);

    let bob_account_info = (await client.accountInformation(bobAddress).do());
    console.log("Asset of Bob: ");
    console.log(bob_account_info.assets);

})().catch(e => {
	console.log(e);
})

Step 1: Create ASA

const algosdk = require('algosdk');

const server="https://testnet-algorand.api.purestake.io/ps2";
const port="";
const token={
	"x-api-key": "mV *** BY" // fill in yours
};

var alice_mnemonic = "cash riot *** able can"; // fill in yours
var aliceAccount = algosdk.mnemonicToSecretKey(alice_mnemonic);

let client = new algosdk.Algodv2(token, server, port);

(async () => {
    let params = await client.getTransactionParams().do();
    let note = undefined; 
    let addr = aliceAccount.addr;
    let defaultFrozen = false;
    let decimals = 0;
    let totalIssuance = 1000000;
    let unitName = "KCCOIN";
    let assetName = "KC Coin";
    let assetURL = "http://someurl";
    let assetMetadataHash = "01234567890123456789012345678901";
    let manager = aliceAccount.addr;
    let reserve = aliceAccount.addr;
    let freeze = aliceAccount.addr;
    let clawback = aliceAccount.addr;
    let txn = algosdk.makeAssetCreateTxnWithSuggestedParams(addr, note,
         totalIssuance, decimals, defaultFrozen, manager, reserve, freeze,
        clawback, unitName, assetName, assetURL, assetMetadataHash, params);
    let rawSignedTxn = txn.signTxn(aliceAccount.sk);
    let tx = (await client.sendRawTransaction(rawSignedTxn).do());
    console.log("Transaction : " + tx.txId);
})().catch(e => {
    console.log(e);
});

Step 2: Transfer of ASA with Receiver not Opt-in

const algosdk = require('algosdk');

const server="https://testnet-algorand.api.purestake.io/ps2";
const port="";
const token={
	"x-api-key": "mV *** BY" // fill in yours
};

var alice_mnemonic = "cash riot *** able can"; // fill in yours
var aliceAccount = algosdk.mnemonicToSecretKey(alice_mnemonic);
var bobAddress = 'BDGUP3RKI3DJVM327B3R4SYT6PQUJBMDMQWHQICKSEQQT6PWCMNAYWEY4M'; // change to yours

let client = new algosdk.Algodv2(token, server, port);

(async () => {
    let assetID = 13300122; // change to your own assetID
    let params = await client.getTransactionParams().do();
    let sender = aliceAccount.addr;
    let recipient = bobAddress;
    let revocationTarget = undefined;
    let closeRemainderTo = undefined;
    let note = undefined;
    let amount = 200000;
    let txn = algosdk.makeAssetTransferTxnWithSuggestedParams(sender, recipient, closeRemainderTo, revocationTarget,
        amount, note, assetID, params);
    let rawSignedTxn = txn.signTxn(aliceAccount.sk)
    let tx = (await client.sendRawTransaction(rawSignedTxn).do());
    console.log("Transaction : " + tx.txId);
})().catch(e => {
    console.log(e);
});

Step 3: Opt-in and Successful Transfer of ASA

const algosdk = require('algosdk');

const server="https://testnet-algorand.api.purestake.io/ps2";
const port="";
const token={
	"x-api-key": "mV *** BY" // fill in yours
};

var bob_mnemonic = "truth erase *** above magic"; // fill in yours
var bobAccount = algosdk.mnemonicToSecretKey(bob_mnemonic);

let client = new algosdk.Algodv2(token, server, port);

(async () => {
    let assetID = 13300122; // change to your own assetID
    let params = await client.getTransactionParams().do();
    let sender = bobAccount.addr;
    let recipient = sender;
    let revocationTarget = undefined;
    let closeRemainderTo = undefined;
    let note = undefined;
    let amount = 0;
    let txn = algosdk.makeAssetTransferTxnWithSuggestedParams(sender, recipient, closeRemainderTo, revocationTarget,
        amount,  note, assetID, params);
    let rawSignedTxn = txn.signTxn(bobAccount.sk)
    let tx = (await client.sendRawTransaction(rawSignedTxn).do());
    console.log("Transaction : " + tx.txId);
})().catch(e => {
    console.log(e);
});
After opt-in Asset is seen in Bob’s account with amount zero.

Step 4: Observation in AlgoSigner

Both accounts are imported to my AlgoSigner wallet. Alice’s account Bob’s account
Bob’s account

Step 5: Alice sends 100k KCCOINs to Bob with AlgoSigner

Alice sends 100k KCCOINs to Bob in AlgoSigner

Step 6: Destroy ASA with Outstanding ASAs

const algosdk = require('algosdk');

const server="https://testnet-algorand.api.purestake.io/ps2";
const port="";
const token={
	"x-api-key": "mV *** BY" // fill in yours
};

var alice_mnemonic = "cash riot *** able can"; // fill in yours
var aliceAccount = algosdk.mnemonicToSecretKey(alice_mnemonic);

let client = new algosdk.Algodv2(token, server, port);

(async () => {
    let assetID = 13300122; // change to your own asset
    let params = await client.getTransactionParams().do();
    let addr = aliceAccount.addr;
    let note = undefined;
    let txn = algosdk.makeAssetDestroyTxnWithSuggestedParams(addr, note, assetID, params);
    let rawSignedTxn = txn.signTxn(aliceAccount.sk);
    let tx = (await client.sendRawTransaction(rawSignedTxn).do());
    console.log("Transaction : " + tx.txId);
})().catch(e => {
    console.log(e);
});

Step 7: Return all ASA to Creator

Bob sends 300k KCCOINs to Alice in AlgoSigner

Step 8: Destroy ASA

Step 9: Remove Opt-in

const algosdk = require('algosdk');

const server="https://testnet-algorand.api.purestake.io/ps2";
const port="";
const token={
	"x-api-key": "mV *** BY"
};

var bob_mnemonic = "truth erase *** above magic"; // fill in yours

var bobAccount = algosdk.mnemonicToSecretKey(bob_mnemonic);
var aliceAddress = '5SL7MUMPYFNDBHUD4L7J4LJRQCQWXQUD3RDK7SBGIDWPH3ZFK47BTH43HE'; // change to yours

let client = new algosdk.Algodv2(token, server, port);

(async () => {
    let assetID = 13300122; // change to your own assetID
    let params = await client.getTransactionParams().do();
    let sender = bobAccount.addr;
    let recipient = aliceAddress;
    let revocationTarget = undefined;
    let closeRemainderTo = aliceAddress;
    let note = undefined;
    let amount = 0;
    let txn = algosdk.makeAssetTransferTxnWithSuggestedParams(sender, recipient, closeRemainderTo, revocationTarget,
        amount,  note, assetID, params);
    let rawSignedTxn = txn.signTxn(bobAccount.sk)
    let tx = (await client.sendRawTransaction(rawSignedTxn).do());
    console.log("Transaction : " + tx.txId);
})().catch(e => {
    console.log(e);
});

Summary

This article has been published from the source link without modifications to the text. Only the headline has been changed.

Source link