SDK can be installed via cdn or npm
Installation via cdn You can add the following code to the HTML file, or replace "latest" with a specific version number, such as "1.3.7".
<script src='https://unpkg.com/@okxconnect/tonsdk@latest/dist/okxconnect_tonsdk.min.js'></script>
Once introduced, OKXTonConnectSDK will be available as a global object that can be directly referenced.
<script> const connector = new OKXTonConnectSDK.OKXTonConnect(); </script>
Using npm
npm install @okxconnect/tonsdk
Before connecting to the wallet, create an instance of the SDK:
new OKXTonConnect({metaData: {name, icon}})
Request Parameters
Return Value
Example
import { OKXTonConnect } from "@okxconnect/tonsdk";
const okxTonConnect = new OKXTonConnect({
metaData: {
name: "application name",
icon: "application icon url"
}
});
Connect to the wallet to get the wallet address as an identifier and the necessary parameters used to sign the transaction.
connect(request): Promise<string>;
Request Parameters
Return Value
Recommendations
Example
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');
}
}
If the user has previously connected their wallet, use this method to restore the connection state:
restoreConnection(): Promise<void>
Request parameters
None
Return Value
None
Example
okxTonConnect.restoreConnection()
Disconnects the connected wallet and deletes the current session. If you want to switch connected wallets, disconnect the current wallet first.
Example
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');
}
}
Get whether there is currently a connected wallet
Example
var connect: boolean = okxTonConnect.connected()
Method for sending a message to a wallet:
sendTransaction(transaction, options): Promise<SendTransactionResponse>
Request parameters
Return value
{boc: string}
: signed resultExample
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');
}
}
The wallet statuses are: successful connection, successful restoration of connection, disconnection, etc. You can use this method to get the status.
onStatusChange( callback: (walletInfo) => void, errorsHandler?: (err) => void ): () => void;
Request Parameters
callback - (walletInfo) => void : This callback is called when the wallet state changes;
0:<hex>
)errorsHandler - (err: OKXConnectError) => void : This errorsHandler is called when an exception occurs due to a change in the wallet state;
Return Value
Example
import { Wallet } from "@okxconnect/tonsdk";
const unsubscribe = okxTonConnect.onStatusChange((walletInfo: Wallet | null) => {
console.log('Connection status:', walletInfo);
}, (err: OKXConnectError) => {
console.log('Connection status:', err);
}
)
Call unsubscribe to save resources when you no longer need to listen for updates.
unsubscribe()
When the following events occur, corresponding event notifications will be sent, and the Dapp can add listeners as needed to handle the corresponding logic;
event
event name | trigger timing |
---|---|
OKX_TON_CONNECTION_STARTED | When the user starts to connect to the wallet |
OKX_TON_CONNECTION_COMPLETED | When the user successfully connects to the wallet |
OKX_TON_CONNECTION_ERROR | When the user canceled the connection or there was an error during the connection process |
OKX_TON_CONNECTION_RESTORING_STARTED | When the dApp starts to resume the connection |
OKX_TON_CONNECTION_RESTORING_COMPLETED | When the dApp successfully restores the connection |
OKX_TON_CONNECTION_RESTORING_ERROR | When the dApp fails to restore the connection |
OKX_TON_DISCONNECTION | When the user starts to disconnect from the wallet |
OKX_TON_TRANSACTION_SENT_FOR_SIGNATURE | When the user sends a transaction for signature |
OKX_TON_TRANSACTION_SIGNED | When the user successfully signs a transaction |
OKX_TON_TRANSACTION_SIGNING_FAILED | When the user canceled the transaction signing or there was an error during the signing process |
Example
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);
}
});
Get the currently connected account
example
import { Account } from "@okxconnect/tonsdk";
var connect: Account = okxTonConnect.account()
Get the currently connected wallet
Example
import { Wallet } from "@okxconnect/tonsdk";
var connect: Wallet = okxTonConnect.wallet()
Exceptions that may be thrown during connection, transaction, resumption of connection, and disconnection.
Exception
Error Code | description |
---|---|
OKX_CONNECT_ERROR_CODES.UNKNOWN_ERROR | Unknown Exception |
OKX_CONNECT_ERROR_CODES.ALREADY_CONNECTED_ERROR | Wallet connected |
OKX_CONNECT_ERROR_CODES.NOT_CONNECTED_ERROR | Wallet not connected |
OKX_CONNECT_ERROR_CODES.USER_REJECTS_ERROR | User Rejected |
OKX_CONNECT_ERROR_CODES.METHOD_NOT_SUPPORTED | 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,
}