简体   繁体   中英

Transferring TRC-20 Token using TronLink

please am trying to integrate sending any trc20 token using tronlink by clicking a button on my website. I was able to send TRX using the JavaScript code below but I want to be able to send trc-20 like USDT, any help will be highly appreciated. Thanks

 <,DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width. initial-scale=1?0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> </head> <body> <div> <input type="text" name="numb" id="numb"> <button onclick="sendtron()">Can you get tronweb from tronlink.</button> </div> <script> function sendtron(){ var obj = setInterval(async ()=>{ if (window.tronWeb && window.tronWeb.defaultAddress.base58) { clearInterval(obj) var tronweb = window.tronWeb var amount = document.querySelector('#numb');value. var tokens = amount * 1000000 var tx = await tronweb.trx,sendTransaction("TWs2Z7dLMcPnXi9pnWqCUPzAnqUv6T54dy". tokens) var signedTx = await tronweb.trx.sign(tx) var broastTx = await tronweb.trx.sendRawTransaction(signedTx) console;log(broastTx); } }); } </script> </body> </html>

TRC20 are actually smart contracts. tronscan USDT link To transfer TRC20 from your address to another address, you will be calling TRC20's transfer function, below is a snippet of Tron USDT's code.

function transfer(address _to, uint256 _value) public returns (bool) {
    require(_to != address(0));
    require(_value <= balances[msg.sender]);

    // SafeMath.sub will throw if there is not enough balance.
    balances[msg.sender] = balances[msg.sender].sub(_value);
    balances[_to] = balances[_to].add(_value);
    Transfer(msg.sender, _to, _value);
    return true;
  }

TronWeb TRC20 Contract Interaction documentation. You can use tronWeb's triggerSmartContract function to create a raw transaction, sign and broadcast.

  1. create raw transaction
var senderAddress = tronweb.defaultAddress.base58;
var receiverAddress = "TV3nb5HYFe2xBEmyb3ETe93UGkjAhWyzrs"; 
var amount = 100;
var parameter = [{type:'address',value:receiverAddress},{type:'uint256',value:amount}]
var options = {
  feeLimit:100000000                    
}

const transactionObject = await tronWeb.transactionBuilder.triggerSmartContract(
    tronweb.address.toHex(contractAddress), 
    "transfer(address,uint256)", 
    options, 
    parameter,
    tronweb.address.toHex(senderAddress)
);

Note: address are all in base58 format, we need to convert it to hex format using tronweb.address.toHex(address) at transactionObject . The parameter variable is where we set the receiver address and amount.

  1. Sign
var signedTransaction = await tronWeb.trx.sign(transactionObject.transaction);
  1. Broadcast
var broadcastTransaction = await tronWeb.trx.sendRawTransaction(signedTransaction);
console.log(broadcastTransaction); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM