Skip to main content

Initiate a transfer transaction

The following example demonstrates how a transfer transaction may be initiated.

const wallet = AElf.wallet.createNewWallet();
let tokenContract;
// Use token contract for examples to demonstrate how to get a contract instance in different ways
// in async function
(async () => {
tokenContract = await aelf.chain.contractAt(tokenContractAddress, wallet);
})();

// promise way
aelf.chain.contractAt(tokenContractAddress, wallet).then((result) => {
tokenContract = result;
});

// callback way
aelf.chain.contractAt(tokenContractAddress, wallet, (error, result) => {
if (error) throw error;
tokenContract = result;
});

(async () => {
// get the balance of an address, this would not send a transaction,
// or store any data on the chain, or required any transaction fee, only get the balance
// with `.call` method, `aelf-sdk` will only call read-only method
const result = await tokenContract.GetBalance.call({
symbol: "ELF",
owner: "XXX",
});
console.log(result);
/**
{
"symbol": "ELF",
"owner": "XXX",
"balance": "1000000000000"
}*/
// with no `.call`, `aelf-sdk` will sign and send a transaction to the chain, and return a transaction id.
// make sure you have enough transaction fee `ELF` in your wallet
const transactionId = await tokenContract.Transfer({
symbol: "ELF",
to: "XXX",
amount: "1000000000",
memo: "transfer in demo",
});
console.log(transactionId);
/**
{
"TransactionId": "123123"
}
*/
})();