In addition to SDK, we also provide a UI interface. If using the UI connection, and the DApp is operating in Telegram, users can choose to open the mobile App Wallet or stay in Telegram and launch the OKX Mini Wallet.
npm install @okxconnect/ui
Before connecting to the wallet, you need to create an object for subsequent operations such as connecting to the wallet and sending transactions.
new OKXTonConnectUI(dappMetaData, buttonRootId, actionsConfiguration, uiPreferences, language, restoreConnection)
Request parameters
${string}://${string}
; Specify the return strategy for deep links when the user signs/rejects the request, if in tg, configure tg://resolve${string}://${string}
; Telegram Mini Wallet wallet, when the user signs/rejects the request, the deep link return policy, generally configure back, that means after signing and closing the wallet, it will automatically show the dapp; none means no processing after signing; default is back;Returns the value
Example
import { OKXTonConnectUI } from "@okxconnect/ui";
const okxTonConnectUI = new OKXTonConnectUI({
dappMetaData: {
name: "application name",
icon: "application icon url"
},
buttonRootId:'button-root',
actionsConfiguration:{
returnStrategy:'none',
tmaReturnUrl:'back'
},
uiPreferences: {
theme: THEME.LIGHT
},
language:'en_US',
restoreConnection: true
});
Connecting to a wallet goes to get the wallet address, which serves as the identifier and the necessary parameters used to sign the transaction. The “Connect Button” (added to buttonRootId) automatically handles the click and invokes the connection. If no buttonRootId is added, this method needs to be called.
await okxTonConnectUI.openModal();
Example
okxTonConnectUI.openModal();
Add the connection signature parameter, the If you need to set tonProof, set state:'loading', before the tonProof parameter is ready. When ready, set state to'ready' and add value;. You can also remove the loading state by setting setConnectRequestParameters(null);
Example
okxtonConnectUI.setConnectRequestParameters({ state:'loading' });
const tonProofPayload: string | null = await fetchTonProofPayloadFromBackend();
if (!tonProofPayload) {
okxtonConnectUI.setConnectRequestParameters(null);
} else {
okxtonConnectUI.setConnectRequestParameters({
state: "ready",
value: { tonProof: tonProofPayload }
});
}
Example
okxTonConnectUI.closeModal();
Get information about whether there is a currently connected wallet, and the connected wallet;
Example
const currentWallet = okxTonConnectUI.wallet;
const currentAccount = okxTonConnectUI.account;
const isConnected = okxTonConnectUI.connected;
Example
okxTonConnectUI.disconnect();
The wallet status are: connect successfully, resume connect successfully, disconnect, etc. You can use this method to get the status. Method details same as OKXTonConnect.onStatusChange
Example
import { Wallet } from "@okxconnect/ui";
const unsubscribe = okxTonConnectUI.onStatusChange((walletInfo: Wallet | null) => {
console.log('Connection status:', walletInfo);
}, (err: OKXConnectError) => {
console.log('Connection status:', err);
}
)
unsubscribe()
Method for sending a message to a wallet:
sendTransaction(transaction, actionConfigurationRequest): Promise<SendTransactionResponse>
Request parameters
transaction - object, [parameters same as OKXTonConnect.sendTransaction's transaction](/web3/build/docs/sdks/app-connect-ton-sdk#%E5%8F%91%E9%80%81% E4%BA%A4%E6%98%93)
actionConfigurationRequest - object
${string}://${string}
; The return strategy for the deep link in the App wallet when the user signs or rejects the request, if it is a Mini App in Telegram, it can be configured with tg://resolve, and if it's not configured here, the will take the returnStrategy passed by the init method, default is'none’${string}://${string}
; Telegram Mini Wallet wallet, when the user signs/rejects the request, the deep link return strategy, generally configure back, that is to say, after signing and closing the wallet, it will be displayed automatically. dapp; none means no processing after signing; default is back;** return value **
{boc: string}
: Signature Resultimport { OKXConnectError, OKX_CONNECT_ERROR_CODES } from "@okxconnect/core";
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
}
]
}
okxTonConnectUI.sendTransaction(transactionRequest, {
modals:'all',
tmaReturnUrl:'back'
}).then((result) => {
let boc = result.boc
}).catch((error) => {
if (error instanceof OKXConnectError && error.code == OKX_CONNECT_ERROR_CODES.USER_REJECTS_ERROR) {
//userReject;
} else {
//other error;
}
})
Support to modify theme, text language setting, also can add these configurations during initialisation;
Example
okxTonConnectUI.uiOptions = {
language:'zh_CN',
uiPreferences: {
theme: THEME.DARK
}
};
When the following events occur, a notification of the corresponding event will be sent, and the Dapp can add listeners as needed to handle the corresponding logic;
event
Event Name | Trigger Timing |
---|---|
OKX_UI_CONNECTION_STARTED | When the user starts to connect to the wallet |
OKX_UI_CONNECTION_COMPLETED | When the user successfully connects to the wallet |
OKX_UI_CONNECTION_ERROR | When the user canceled the connection or there was an error during the connection process |
OKX_UI_CONNECTION_RESTORING_STARTED | When the dApp starts restoring the connectio |
OKX_UI_CONNECTION_RESTORING_COMPLETED | When the dApp successfully restores the connection |
OKX_UI_CONNECTION_RESTORING_ERROR | When the dApp failed to restore the connection |
OKX_UI_DISCONNECTION | When the user starts to disconnect from the wallet |
OKX_UI_TRANSACTION_SENT_FOR_SIGNATURE | When the user sends a transaction for signature |
OKX_UI_TRANSACTION_SIGNED | When the user successfully signs a transaction |
OKX_UI_TRANSACTION_SIGNING_FAILED | When the user canceled the transaction signing or there was an error during the signing process |
Example
import { OKX_UI_CONNECTION_AND_TRANSACTION_EVENT } from "@okxconnect/ui";
window.addEventListener(OKX_UI_CONNECTION_AND_TRANSACTION_EVENT.OKX_UI_CONNECTION_STARTED, (event) => {
if (event instanceof CustomEvent) {
console.log('Transaction init', event.detail);
}
});