波场链通过tronWeb发送带备注的TRC – 20 转账
var contractAddress = "TRC-20 合约";//选择合约⽅法let functionSelector = "transfer(address,uint256)";//根据⽅法构造参数let parameter = [{type: "address",value: address},{type: "uint256",value: Math.ceil(number)}];//额外参数let options = {shouldPollResponse: false,feeLimit: 1000000 //1Trx};// 构造智能合约交易信息let res = await tronWeb.transactionBuilder.triggerSmartContract(contractAddress, functionSelector, options, parameter).catch(err1 => {// 构建交易信息失败return false;});// 向转账交易信息中追加备注信息await tronWeb.transactionBuilder.addUpdateData(res.transaction, "备注信息", 'utf8');// 对已经添加备注的交易信息进⾏签名let sign = await tronWeb.trx.sign(res.transaction).catch(err2 => {//签名失败return false;});// 将签名交易⼴播上链return await tronWeb.trx.sendRawTransaction(sign).catch(outputErr => {//交易⼴播出错console.log(outputErr);return false;});
波场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 addressvar address = "Txx...";try {let contract = await window.tronWeb.contract().at(trc20ContractAddress);let result = await contract.transfer("TVDGp...", //address _to1000000 //amount).send({feeLimit: 1000000}).then(output => {console.log('- Output:', output, '\n');});console.log('result: ', result);} catch(error) {console.error("trigger smart contract error",error)}}
如何将 TRC20 交易发送到地址blockchain tron tronweb我正在使用 tron web 查询地址的交易,但它不会返回发送到该地址的交易,该地址传输的令牌为 TRC20。这不起作用。我想获取某个地址上的交易并获取 TRX、trc10 和 trc20 交易。我做错了什么或怎么做?这是我的代码块:tronWeb.setDefaultBlock("latest");var result = await tronGrid.account.getTransactions(address, {only_confirmed: true,only_to: true,limit: 10});console.log(JSON.stringify(result));})();经过大量研究,我发现可以轻松地每隔一段时间查询合同事件以获取该合同地址上的交易,然后您可以根据您正在观看的地址对其进行过滤,因为您无法通过 trongrid/tronweb 实现获得 webhook 或 websocket。这是我用来实现此目的的示例文件,即使使用不同的合约地址,它也可以很好地监控多个地址。注意:在我自己的实现中,这个节点文件是从另一个文件中调用的,其他物流在另一个文件中处理,但是下面你会看到我是如何查询指定合约发出的 trasfer 事件的const TronWeb = require("tronweb");const TronGrid = require("trongrid");const tronWeb = new TronWeb({fullHost: "https://api.trongrid.io"});const tronGrid = new TronGrid(tronWeb);const argv = require("minimist")(process.argv.slice(2));var contractAddress = argv.address;var min_timestamp = Number(argv.last_timestamp) + 1; //this is stored for the last time i ran the query(async function() {tronWeb.setDefaultBlock("latest");tronWeb.setAddress("ANY TRON ADDRESS"); // maybe being the one making the query not necessarily the addresses for which you need the transactionsvar result = await tronGrid.contract.getEvents(contractAddress, {only_confirmed: true,event_name: "Transfer",limit: 100,min_timestamp: min_timestamp,order_by: "timestamp,asc"});result.data = result.data.map(tx => {tx.result.to_address = tronWeb.address.fromHex(tx.result.to); // this makes it easy for me to check the address at the other endreturn tx;});console.log(JSON.stringify(result));})();您可以自由自定义传递给该tronGrid.contract.getEvents方法的配置数据。根据您正在监控的合约上交易的频率,您应该知道什么时间间隔对您有利,以及您应该通过什么限制值。有关详细信息,请参阅https://developers.tron.network/trx兑换 @helpwm我希望这可以帮助别人