交易项
这里演示如何通过 UKey Wallet TRON Provider 签名交易、广播交易,以及使用 TronWeb 进行合约交互。
签交易
下面这段参考代码先构造交易,再请求 UKey Wallet 签名,最后由应用手动广播:
// 组装交易
const tx = await tronWeb.transactionBuilder.sendTrx(
"TR7x6W5v4U3t2S1r0Q9p8NmLyKjHgFeDcB",
tronWeb.toSun(10),
tronWeb.defaultAddress.base58,
);
// 通过 provider 完成签名
const signedTx = await provider.sign(tx);
console.log("签名完成的交易数据:", signedTx);
// 手动执行广播
const callResult = await tronWeb.trx.sendRawTransaction(signedTx);
智能合约交互
const contract = await tronWeb.contract().at(contractAddress);
// 调用只读 view 函数
const callResult = await contract.someViewFunction().call();
// 写操作(需要签名)
const tx = await contract.someWriteFunction(param1, param2).send({
feeLimit: 100_000_000, // fee cap: 100 TRX
callValue: 0,
});
触发智能合约
const tx = await tronWeb.transactionBuilder.triggerSmartContract(
contractAddress,
"transfer(address,uint256)",
{ feeLimit: 100_000_000 },
[
{ type: "address", value: recipientAddress },
{ type: "uint256", value: amount },
],
tronWeb.defaultAddress.base58,
);
const signedTx = await provider.sign(tx.transaction);
const callResult = await tronWeb.trx.sendRawTransaction(signedTx);
使用 SunWeb
SunWeb 可用于 DAppChain(侧链)相关操作:
const sunWeb = window.sunWeb;
// 从主链到侧链
await sunWeb.depositTrx(amount, depositFee, feeLimit);
// 从侧链到主链
await sunWeb.withdrawTrx(amount, withdrawFee, feeLimit);