可以通过cdn或者npm安装SDK
通过cdn安装 将如下代码添加到HTML文件中,也可以将latest替换为特定的版本号例如1.3.7。
<script src="https://unpkg.com/@okxconnect/tonsdk@latest/dist/okxconnect_tonsdk.min.js"></script>
引入后,OKXTonConnectSDK会作为全局对象可以直接引用
<script> const connector = new OKXTonConnectSDK.OKXTonConnect(); </script>
通过npm安装
npm install @okxconnect/tonsdk
连接钱包之前,需要先创建一个对象,用于后续连接钱包、发送交易等操作。
new OKXTonConnect({metaData: {name, icon}})
请求参数
返回值
示例
import { OKXTonConnect } from "@okxconnect/tonsdk";
const okxTonConnect = new OKXTonConnect({
metaData: {
name: "application name",
icon: "application icon url"
}
});
连接钱包去获取钱包地址,作为标识符和用于签名交易的必要参数。
connect(request): Promise<string>;
请求参数
返回值
建议
示例
import { OKXConnectError } from "@okxconnect/tonsdk";
try {
okxTonConnect.connect({
tonProof: "signmessage",
redirect: "tg://resolve",
openUniversalLink: true
})
} catch (error) {
if (error instanceof OKXConnectError) {
if (error.code === OKX_CONNECT_ERROR_CODES.USER_REJECTS_ERROR) {
alert('User reject');
} else if (error.code === OKX_CONNECT_ERROR_CODES.ALREADY_CONNECTED_ERROR) {
alert('Already connected');
} else {
alert('Unknown error happened');
}
} else {
alert('Unknown error happened');
}
}
如果用户之前连接过钱包,在再次进入或页面刷新时可调用此方法,用来恢复之前的连接状态。
restoreConnection(): Promise<void>
请求参数
无
返回值
无
示例
okxTonConnect.restoreConnection()
断开已连接钱包,并删除当前会话,如果要切换连接钱包,请先断开当前钱包。
示例
import { OKX_CONNECT_ERROR_CODES } from "@okxconnect/tonsdk";
try {
await okxTonConnect.disconnect()
} catch (error) {
if (error instanceof OKXConnectError) {
switch (error.code) {
case OKX_CONNECT_ERROR_CODES.NOT_CONNECTED_ERROR:
alert('Not connected');
break;
default:
alert('Unknown error happened');
break;
}
} else {
alert('Unknown error happened');
}
}
获取当前是否有连接钱包
示例
var connect: boolean = okxTonConnect.connected()
向钱包发送消息的方法:
sendTransaction(transaction, options): Promise<SendTransactionResponse>
请求参数
返回值
{boc: string}
: 签名结果示例
import { OKXConnectError } from "@okxconnect/tonsdk";
let transactionRequest = {
"validUntil": Date.now() / 1000 + 360,
"from": "0:348bcf827469c5fc38541c77fdd91d4e347eac200f6f2d9fd62dc08885f0415f",
"messages": [
{
"address": "0:412410771DA82CBA306A55FA9E0D43C9D245E38133CB58F1457DFB8D5CD8892F",
"amount": "20000000",
"stateInit": "base64bocblahblahblah==" //deploy contract
}, {
"address": "0:E69F10CC84877ABF539F83F879291E5CA169451BA7BCE91A37A5CED3AB8080D3",
"amount": "60000000",
"payload": "base64bocblahblahblah==" //transfer nft to new deployed account 0:412410771DA82CBA306A55FA9E0D43C9D245E38133CB58F1457DFB8D5CD8892F
}
]
}
let requestOptions = {
onRequestSent: () => {
//requestMsgSend
}
}
try {
const result = await okxTonConnect.sendTransaction(transactionRequest, requestOptions);
} catch (error) {
if (error instanceof OKXConnectError) {
switch (error.code) {
case OKX_CONNECT_ERROR_CODES.USER_REJECTS_ERROR:
alert('You rejected the transaction.');
break;
case OKX_CONNECT_ERROR_CODES.NOT_CONNECTED_ERROR:
alert('Not connected');
break;
default:
alert('Unknown error happened');
break;
}
} else {
alert('Unknown error happened');
}
}
钱包状态有:连接成功、恢复连接成功、断开连接等,都可以用此方法获取状态。
onStatusChange( callback: (walletInfo) => void, errorsHandler?: (err) => void ): () => void;
请求参数
callback - (walletInfo) => void : 钱包状态发生变化时候,该callback 会被调用;
0:<hex>
)errorsHandler - (err: OKXConnectError) => void : 钱包状态发生变化出现异常的时候,该errorsHandler 会被调用;
返回值
示例
import { Wallet } from "@okxconnect/tonsdk";
const unsubscribe = okxTonConnect.onStatusChange((walletInfo: Wallet | null) => {
console.log('Connection status:', walletInfo);
}, (err: OKXConnectError) => {
console.log('Connection status:', err);
}
)
当不再需要监听更新时,调用unsubscribe节省资源。
unsubscribe()
在以下事件发生时,会发送对应事件通知,Dapp可以根据需要添加监听,来处理对应的逻辑;
event事件
事件名称 | 触发时机 |
---|---|
OKX_TON_CONNECTION_STARTED | 当用户开始连接钱包时 |
OKX_TON_CONNECTION_COMPLETED | 当用户成功连接钱包时 |
OKX_TON_CONNECTION_ERROR | 当用户取消连接或连接过程中出现错误时 |
OKX_TON_CONNECTION_RESTORING_STARTED | 当 dApp 开始恢复连接时 |
OKX_TON_CONNECTION_RESTORING_COMPLETED | 当 dApp 成功恢复连接时 |
OKX_TON_CONNECTION_RESTORING_ERROR | 当 dApp 无法恢复连接时 |
OKX_TON_DISCONNECTION | 当用户开始断开钱包连接时 |
OKX_TON_TRANSACTION_SENT_FOR_SIGNATURE | 当用户发送交易以供签名时 |
OKX_TON_TRANSACTION_SIGNED | 当用户成功签署交易时 |
OKX_TON_TRANSACTION_SIGNING_FAILED | 当用户取消交易签名或签名过程中出现错误时 |
示例
import { OKX_TON_CONNECTION_AND_TRANSACTION_EVENT } from "@okxconnect/tonsdk";
window.addEventListener(OKX_TON_CONNECTION_AND_TRANSACTION_EVENT.OKX_TON_CONNECTION_STARTED, (event) => {
if (event instanceof CustomEvent) {
console.log('Transaction init', event.detail);
}
});
获取当前连接的 account
示例
import { Account } from "@okxconnect/tonsdk";
var connect: Account = okxTonConnect.account()
获取当前连接的 wallet
示例
import { Wallet } from "@okxconnect/tonsdk";
var connect: Wallet = okxTonConnect.wallet()
在连接,交易,恢复连接,断开连接的过程中可能抛出的异常;
异常
错误码 | 描述 |
---|---|
OKX_CONNECT_ERROR_CODES.UNKNOWN_ERROR | 未知异常 |
OKX_CONNECT_ERROR_CODES.ALREADY_CONNECTED_ERROR | 钱包已连接 |
OKX_CONNECT_ERROR_CODES.NOT_CONNECTED_ERROR | 钱包未连接 |
OKX_CONNECT_ERROR_CODES.USER_REJECTS_ERROR | 用户拒绝 |
OKX_CONNECT_ERROR_CODES.METHOD_NOT_SUPPORTED | 方法不支持 |
export enum OKX_CONNECT_ERROR_CODES {
UNKNOWN_ERROR = 0,
ALREADY_CONNECTED_ERROR = 11,
NOT_CONNECTED_ERROR = 12,
USER_REJECTS_ERROR = 300,
METHOD_NOT_SUPPORTED = 400,
}