Skip to content

Ton Provider

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

js
const provider = window.bitkeep.ton;
const provider = window.bitkeep.ton;

注入对象的属性和方法

  • send - (paylod, callback) => any:通过使用不同的 payload.method(如:eth_accounts、eth_coinbase、net_version、eth_chainId)调用不同的方法
  • isConnected - () => string:获取连接状态
  • _sendAsync - (payload) => Promise:通过使用不同的 payload.method(如:ton_connect、ton_disconnect、ton_requestAccounts、ton_requestWallets、ton_getBalance、ton_sendTransaction、ton_rawSign、ton_personalSign、ton_getChain、wallet_switchChain)调用不同的链方法
  • signTypedMessage - (data) => void:签署 Typed 类型消息
  • signPersonalTypedMessage - (data) => void:签署 PersonalTyped 类型消息
  • walletSwitchChain - (network) => void:切换链
  • signTransaction - (data) => object:交易签名
  • disconnect - () => void:断开连接

Support version

PlatformVersion
Chrome Extension>=v2.4.1
App(IOS)>= 8.10.0
App(Android)>= 8.10.0

链接钱包

js
const result = await provider.send('ton_requestWallets');

result = [{
    address: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
    publicKey: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
    version: "v4R2"
}]
const result = await provider.send('ton_requestWallets');

result = [{
    address: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
    publicKey: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
    version: "v4R2"
}]

获取钱包当前账号

js
const accounts = await provider.send("ton_requestAccounts");
const account = accounts[0];
const accounts = await provider.send("ton_requestAccounts");
const account = accounts[0];

账号切换监听事件

js
provider.on("accountsChanged", function (accounts) {
  const account = accounts[0];

  console.log("accountsChanged", accounts);
});
provider.on("accountsChanged", function (accounts) {
  const account = accounts[0];

  console.log("accountsChanged", accounts);
});

获取当前网络

mainnettestnet

js
provider
  .send("ton_getChain")
  .then((chainId) => console.log({ chainId }))
  .catch((error) => {
    console.error(error);
  });
provider
  .send("ton_getChain")
  .then((chainId) => console.log({ chainId }))
  .catch((error) => {
    console.error(error);
  });

切换当前网络

js
await provider.send("wallet_switchChain", "mainnet");

or;

await provider.send("wallet_switchChain", "testnet");
await provider.send("wallet_switchChain", "mainnet");

or;

await provider.send("wallet_switchChain", "testnet");

网络切换监听事件

js
provider.on("chainChanged", (chainId) => {
  //window.location.reload();
});
provider.on("chainChanged", (chainId) => {
  //window.location.reload();
});

获取钱包地址余额

js
provider
  .send("ton_getBalance", "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
  .then(handleBalance)
  .catch((error) => {
    console.error(error);
  });
provider
  .send("ton_getBalance", "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
  .then(handleBalance)
  .catch((error) => {
    console.error(error);
  });

获取当前 address 的余额

js
provider
  .send("ton_getBalance")
  .then(handleBalance)
  .catch((error) => {
    console.error(error);
  });
provider
  .send("ton_getBalance")
  .then(handleBalance)
  .catch((error) => {
    console.error(error);
  });

ton_rawSign 方式获取签名结果

js
const signature = await provider.send("ton_rawSign", [
  {
    data: "ABCDEF0123456789ABC56789ABCDEF0123456789",
  },
]);
const signature = await provider.send("ton_rawSign", [
  {
    data: "ABCDEF0123456789ABC56789ABCDEF0123456789",
  },
]);

ton_personalSign 方式获取签名结果

js
const signature = await provider.send("ton_personalSign", {
  data: "Hello world",
});
const signature = await provider.send("ton_personalSign", {
  data: "Hello world",
});

发送交易

js
const seqNo = provider.send("ton_sendTransaction", [
  {
    to: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
    value: "10000", // 10000 nanotons = 0.00001 TONs
    data: "dapp for bitget",
    dataType: "text",
  },
]);
const seqNo = provider.send("ton_sendTransaction", [
  {
    to: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
    value: "10000", // 10000 nanotons = 0.00001 TONs
    data: "dapp for bitget",
    dataType: "text",
  },
]);