Klaytn Provider

Kaikas injects a global API into websites visited by its users at window.klaytn. This API allows websites to request user login, load data from blockchains, and create transactions. You can use this API to detect whether the user uses Kaikas.

if (typeof window.klaytn !== "undefined") {
  // Kaikas user detected. You can now use the provider.
  const provider = window["klaytn"];
}

Properties

These properties can be used to check the current state of the connected user, which can be important things to verify before sending a transaction.

klaytn.networkVersion

Returns a numeric string representing the current blockchain's network ID.

'1001': Baobab Test Network
'8217': Cypress Main Network

Baobab and Cypress are names for Klaytn test network and main network respectively.

klaytn.selectedAddress

Returns a hex-prefixed string representing the current user's selected address, ex: "0xfdea65c8e26263f6d9a1b5de9555d2931a33b825".

klaytn.isKaikas

Returns true or false, depending on whether the user has Kaikas installed.

Methods

klaytn.enable()

Send connection request to Kaikas user. Returns a promise of an array of hex-prefixed Klaytn address strings. If the user accepts the request, BApp is granted access to user's account information.

try {
  const accounts = await klaytn.enable();
  // You now have an array of accounts!
  // Currently only one:
  // ['0xFDEa65C8e26263F6d9A1B5de9555D2931A33b825']
} catch (error) {
  // Handle error. Likely the user rejected the login
  console.error(error);
}

klaytn.sendAsync(options, callback)

Sends a message to the Kaikas browser. Message format maps to the format of Klaytn JSON-RPC API.

Here's an example of klay_sendTransaction

const transactionParameters = {
  gas: "0x2710",
  to: "0x0000000000000000000000000000000000000000",
  from: klaytn.selectedAddress,
  value: "0xff",
};

klaytn.sendAsync(
  {
    method: "klay_sendTransaction",
    params: [transactionParameters],
    from: klaytn.selectedAddress,
  },
  callback
);

wallet_watchAsset

wallet_watchAsset is a special method for registering tokens. For detailed information, please refer to registering your token

klaytn.sendAsync(
  {
    method: "wallet_watchAsset",
    params: {
      type: "ERC20", // Initially only supports ERC20, but eventually more!
      options: {
        address: tokenAddress, // The address that the token is at.
        symbol: tokenSymbol, // A ticker symbol or shorthand, up to 5 chars.
        decimals: tokenDecimals, // The number of decimals in the token
        image: tokenImage, // A string url of the token logo
      },
    },
    id: Math.round(Math.random() * 100000),
  },
  (err, result) => console.log(err, result)
);

wallet_switchKlaytnChain

"wallet_switchEthereumChain" method is also supported.

wallet_switchKlaytnChain is a method for requesting to switch the chain of the wallet.

klaytn.sendAsync(
  {
    method: "wallet_switchKlaytnChain",
    params: [
      {
        chainId: "0x3e9",
      },
    ],
  },
  (err, result) => console.log(err, result)
);

wallet_addKlaytnChain

"wallet_addEthereumChain" method is also supported.

wallet_addKlaytnChain is a method for adding a chain to the wallet. When the chain is added (or already exists), Kaikas will show a pop up to switch the chain.

klaytn.sendAsync(
  {
    method: "wallet_addKlaytnChain",
    params: [
      {
        chainId: "0x1",
        rpcUrls: ["https://..."],
        chainName: "Ethereum mainnet",
        nativeCurrency: { name: "ETH", symbol: "ETH", decimals: 18 },
        blockExplorerUrls: ["https://..."],
      },
    ],
  },
  (err, result) => console.log(err, result)
);

klaytn.request(options)

This method is the same as klaytn.sendAsync(). The difference is that request() is based on a Promise. See the klaytn.sendAsync() section for details on the options.

Sends a message to the Kaikas browser.

try {
  const result = await klaytn.request({
    method: "wallet_switchKlaytnChain",
    params: [
      {
        chainId: "0x3e9",
      },
    ],
  });
} catch (err) {
  console.error(err);
}

klaytn.autoRefreshOnNetworkChange

When the network is changed, Kaikas will reload any pages that have made requests to the provider.

To disable auto-refresh on a network change you can do:

klaytn.autoRefreshOnNetworkChange = false;

This can be toggled on or off at any time. (Default value is true)

klaytn.on(eventName, callback)

The provider supports listening for some events:

  • accountsChanged, returns updated account array.

  • networkChanged, returns network ID string.

  • disconnected, returns nothing.

klaytn.on("accountsChanged", function (accounts) {
  // Time to reload your interface with accounts[0]!
});

klaytn.on("networkChanged", function (networkId) {
  // `networkChanged` event is only useful when auto-refresh on network is disabled
  // Otherwise, Kaikas will auto-reload pages upon network change
});

klaytn.on("disconnected", function () {
  // Use the `disconnected` event to check the wallet connection status.
});

klaytn._kaikas

For developers' convenience, Kaikas provides 3 useful methods under _kaikas namespace.

klaytn._kaikas.isEnabled()

This method returns a Boolean indicating if the current domain has access to user accounts. This is useful for determining whether the user has approved account access for the current session.

klaytn._kaikas.isApproved()

This method returns a Promise that resolves to a Boolean indicating if the current domain has a cached approval. This is useful for determining if an approval popup will show when klaytn.enable() is called, since it indicates if a past approval exists

klaytn._kaikas.isUnlocked()

This method returns a Promise that resolves to a Boolean indicating whether Kaikas is unlocked. Note that this does not indicate whether the user has approved account exposure.

Last updated