波场Tron JS SDK TronWeb

波场Tron JS SDK TronWeb使用简介。

安装依赖

npm install tronweb

获取钱包地址

mounted(){
    if(window.tronWeb){
       let walletAddress = window.tronWeb.defaultAddress.base58;
       console.log(walletAddress);
    }
},

获取代币余额

try {
    let contract = await window.tronWeb.contract().at(this.contractAddress);
    let result = await contract.balanceOf(this.walletAddress).call();
    this.usdtBalance = Number(window.tronWeb.fromSun(result))
    console.log(this.usdtBalance);
} catch(error) {
    console.error("trigger smart contract error",error)
}    

fromSun是假定代币的decimals为6位,1 SUN = 0.000001 TRX

代币转账

async function triggerSmartContract() {
    const trc20ContractAddress = "Tcontxx...";//contract address
    var address = "Txx...";

    try {
        let contract = await window.tronWeb.contract().at(trc20ContractAddress);
        let result = await contract.transfer(
            "TVDGp...", //address _to
            1000000   //amount
        ).send({
            feeLimit: 1000000
        }).then(output => {console.log('- Output:', output, '\n');});
        console.log('result: ', result);
    } catch(error) {
        console.error("trigger smart contract error",error)
    }
}

Leave a Comment

豫ICP备19001387号-1