Skip to main content

Wallet Provider

The WalletProvider follows the EIP-1193 standard and supports the EventEmitter interface defined in it.

sdk.getWalletProvider()

Initializes the walletProvider, allowing developers to use various wallet features.

const walletProvider = sdk.getWalletProvider();

Parameters

  • N/A

Responses

WalletProvider

walletProvider.getWalletType()

Returns the type of the currently connected wallet.

const walletType = walletProvider.getWalletType();

Parameters

  • N/A

Responses

enum WalletType {
Web = "Web",
Liff = "Liff",
Extension = "Extension",
Mobile = "Mobile",
OKX = "OKX",
BITGET = "BITGET"
}

walletProvider.request()

This function provides JSON-RPC API format on request. You can send request to retrieve healthy status of chain and sign transaction with wallet. If you send it before wallet connection, user will see a screen to select wallet type to connect.

You can find the available KAIA-related methods, their parameters, and corresponding responses in the table below. RPC methods that are not included in the table will be requested directly to the chain node and please refer to Kaia docs' RPC API Reference.

const getAccount = async() => {
   const accounts = await walletProvider.request({ method: 'kaia_accounts' }) as string[]; 
   return accounts[0]; 
} 
 
const requestAccount = async () => {
   const addresses = await walletProvider.request({ method: 'kaia_requestAccounts' }) as string[];
   return accounts[0];
}
 
const connectAndSign = async (msg:string) => {
   const [account, signature] = await walletProvider.request({ method: 'kaia_connectAndSign', params: [msg]}) as string[];
   return [account, signature];
}
 
const getBalance = async(params: [account:string,blockNumberOrHash:'latest' | 'earliest'])=>{
   return await walletProvider.request({ method: 'kaia_getBalance', params: params });
}
 
const transaction = {
  from: 0xYourWalletAddress, //The currently connected wallet account can be retrieved using the kaia_accounts method.
  to: '0xRecipientAddress', //Please replace it with a valid wallet address.
  value: '0x10',
  gas: '0x5208', //general gas usage for Kaia transaction
};
 
const sendTransaction = async(transaction) => {
    const transactionHash = await walletProvider.request({ method: 'kaia_sendTransaction', params: [transaction]});
    return transactionHash;
};

Parameters

  • RequestArguments *required · object
    • method *required · string
    • params unknown[]

Responses

Promise<unknown>
methodparamsResponses
kaia_accounts

Returns the list of addresses currently connected to the wallet. If no wallet is connected, an empty array is returned.
null
['Account1']
kaia_requestAccounts

Initiates wallet connection. During the process, a window is displayed for the user to select a wallet provider. Returns a list of addresses associated with the selected wallet.
null
['Account1']
personal_sign (EIP-191)

Initiate sign procedure. We recommend to use personal_sign to get compatibility with various wallets including OKX Wallet.
[message: string, account: string]
"0xa3f20717a250c2b0b729b7e5becbff67fdaef7e0699da4de7ca5895b02a170a12d887fd3b17bfdce3481f10bea41f45ba9f709d39ce8325427b57afcfc994cee1b"
signature
kaia_connectAndSign (EIP-191)recommended

Initiates wallet connection and signing. Prompts the user to select a wallet provider, then signs the provided message. Returns [account, signature] as an array

[message: string]
["account","0xa3f20717a250c2b0b729b7e5becbff67fdaef7e0699da4de7ca5895b02a170a12d887fd3b17bfdce3481f10bea41f45ba9f709d39ce8325427b57afcfc994cee1b"]
account and signature as Array
kaia_getBalance

Returns the kaia balance.
[account: string, blockNumberOrHash: string]

blockNumberOrHash can be set as latest or earliest
  • latest: the most recent block
  • earliest: the genesis block (block 0)
"0x00000000000000000"
the balance value in kei, the smallest unit of the KAIA blockchain.
1 KAIA = 10^18 kei
kaia_sendTransaction

Constructs a transaction with given parameters
[{
  from: string,
  to: string,
  value: string,
  gas: string
 }]

from field should be the account that achieved from 'kaia_accounts','kaia_requestAccounts' or 'kaia_connectAndSign'
"0xe670ec64341771606e55d6b4ca35a1a6b75ee3d5145a99d059210"
transactionHash

Error

{code: -32001, message: 'User canceled'}
CodeDescription
-32001
{
  "code": -32001,
  "message": "User canceled" 
}

//When the user dismissed the wallet connection popup.
{code: -32001, message: 'User closed popup', data: null}

//When the user clicks the dismiss button on the signature popup
{
  "code": -32001,
  "message": "User denied message signature"
}

//When the user clicks the dismiss button on the transaction popup
{
  "code": -32001,
  "message": "User denied transaction send."
}
-32004Invalid from address (Please retry methods after executing walletProvider.disconnectWallet())
-32005User logged out due to incorrect password input (Please retry methods after executing walletProvider.disconnectWallet())
-32006Wallet is not connected yet (If an error occurs while the wallet is connected, Please retry methods after executing walletProvider.disconnectWallet(). If an error occurs while the wallet is not connected, please connect wallet first)

walletProvider.disconnectWallet()

Disconnects the wallet. When this function is called, a confirmation window will appear to confirm the disconnection.

 const disconnectWallet = async ()=>{
    await walletProvider.disconnectWallet();
    window.location.reload();
}

Parameters

  • N/A

Responses

  • N/A

walletProvider.getErc20TokenBalanceWithDepositedBalance()

This method returns the total amount, which is the sum of the on-chain USDT balance and the USDT balance deposited in UNIFI.

const getErc20TokenBalanceWithDepositedBalance = async(contractAddress:string,account:string)=> {
    return await walletProvider.getErc20TokenBalanceWithDepositedBalance(contractAddress,account);
}
 
const USDTContractAddress = '0xd077a400968890eacc75cdc901f0356c943e4fdb';
const account = 'my_account_address';
getErc20TokenBalanceWithDepositedBalance(USDTContractAddress, account).then(balance => {
    const formattedUSDTBalance = Number(microUSDTHexToUSDTDecimal(balance as string)).toFixed(2);
    //microUSDTHexToUSDTDecimal is format function to transform hexadecimal string to decimal string
    //https://github.com/techreadiness/unifi-apps-starter/blob/main/src/utils/format.ts
    console.log(formattedUSDTBalance);
    //0.00
})

Parameters

  • contactAddress *required · string
  • account *required · string

Responses

64-byte hexadecimal string. The returned value includes the token’s decimal scaling according to its decimals specification.

For example, USDT uses a decimal scale of 10⁶, while DELABS uses a decimal scale of 10¹⁸.

string

walletProvider.getErc20TokenBalance()

It returns the on-chain balance of an ERC20-based token.

const getErc20TokenBalance = async(contractAddress:string,account:string)=> {
    return await walletProvider.getErc20TokenBalance(contractAddress,account);
}
 
const USDTContractAddress = '0xd077a400968890eacc75cdc901f0356c943e4fdb';
const account = 'my_account_address';
getErc20TokenBalance(USDTContractAddress, account).then(balance => {
    const formattedUSDTBalance = Number(microUSDTHexToUSDTDecimal(balance as string)).toFixed(2);
    //microUSDTHexToUSDTDecimal is format function to transform hexadecimal string to decimal string
    //https://github.com/techreadiness/dapp-starter/blob/main/src/utils/format.ts
    console.log(formattedUSDTBalance);
    //0.00
})

Parameters

  • contactAddress *required · string
  • account *required · string

Responses

64-byte hexadecimal string. The returned value includes the token’s decimal scaling according to its decimals specification.

For example, USDT uses a decimal scale of 10⁶, while DELABS uses a decimal scale of 10¹⁸.

string

Compatible Libraries