Skip to content

Tron

在 Bitget Wallet App 及安装 Chrome Extension 的 Chrome 浏览器中运行 DApp 时,可获得全局对象 window.bitkeep.tronLinkwindow.bitkeep.tronWeb 并进行后续的 API 调用。

js
const tronLink = window.bitkeep.tronLink;
const tronWeb = window.bitkeep.tronWeb;
const tronLink = window.bitkeep.tronLink;
const tronWeb = window.bitkeep.tronWeb;

注入对象的属性和方法

  • sign - (transaction, privateKey = false, useTronHeader = true, callback = false) => object:交易签名
  • setAddress - (address = '', name = 'primaryAccountOnly', type = 1) => void:设置地址
  • setNode - (node) => void:设置节点
  • isConnected - () => Boolean:获取连接状态
  • signMessageV2 - (message) => void:签署消息
  • multiSign - (transaction, privateKey, permissionId = 0) => void:多签
  • disconnect - () => void:断开连接
  • getVerison - () => string:获取版本信息

获取账户信息

js
try {
  await tronLink.request({ method: "tron_requestAccounts" });
  const address = tronWeb.defaultAddress.base58;
  const balance = await tronWeb.trx.getBalance(address);
} catch {}
try {
  await tronLink.request({ method: "tron_requestAccounts" });
  const address = tronWeb.defaultAddress.base58;
  const balance = await tronWeb.trx.getBalance(address);
} catch {}

钱包连接状态

js
tronWeb.ready;
tronWeb.ready;

发送交易

交易

js
var tx = await tronweb.transactionBuilder.sendTrx(
  "TW8u1VSwbXY7o7H9kC8HmCNTiSXvD69Uiw",
  1000000,
  tronWeb.defaultAddress.base58
);
var signedTx = await tronweb.trx.sign(tx);
var broastTx = await tronweb.trx.sendRawTransaction(signedTx);
console.log(broastTx);
console.log(broastTx.txid);

//Token
let decimal = 18;
let Contract = await tronWeb
  .contract()
  .at("TLa2f6VPqDgRE67v1736s7bJ8Ray5wYjU7"); //WIN
const decimalCall = Contract.decimals || Contract.DECIMALS;
if (decimalCall) {
  decimal = await decimalCall().call();
}
let broastTx = await Contract.transfer(
  "TW8u1VSwbXY7o7H9kC8HmCNTiSXvD69Uiw",
  // "0xde0b6b3a7640000"
  tronWeb.toHex(2 * Math.pow(10, decimal))
).send(); // { feeLimit: 10000000 }

console.log(broastTx);
var tx = await tronweb.transactionBuilder.sendTrx(
  "TW8u1VSwbXY7o7H9kC8HmCNTiSXvD69Uiw",
  1000000,
  tronWeb.defaultAddress.base58
);
var signedTx = await tronweb.trx.sign(tx);
var broastTx = await tronweb.trx.sendRawTransaction(signedTx);
console.log(broastTx);
console.log(broastTx.txid);

//Token
let decimal = 18;
let Contract = await tronWeb
  .contract()
  .at("TLa2f6VPqDgRE67v1736s7bJ8Ray5wYjU7"); //WIN
const decimalCall = Contract.decimals || Contract.DECIMALS;
if (decimalCall) {
  decimal = await decimalCall().call();
}
let broastTx = await Contract.transfer(
  "TW8u1VSwbXY7o7H9kC8HmCNTiSXvD69Uiw",
  // "0xde0b6b3a7640000"
  tronWeb.toHex(2 * Math.pow(10, decimal))
).send(); // { feeLimit: 10000000 }

console.log(broastTx);