Provider API#
What is injected provider API?#
The OKX injected provider API is a JavaScript API that OKX injects into websites visited by our users. Your DApp can use this API to request users' accounts, read data from blockchains users are connected to, and help users sign messages and transactions.
Special Notes#
The WAX API of OKX Wallet is fully compatible with the Scatter protocol. The following APIs and examples are based on this protocol. For specific usage details, developers can refer to the Scatter protocol documentation.
Connect wallet and retrieve wallet information#
import ScatterJS from '@scatterjs/core';
import ScatterEOS from '@scatterjs/eosjs2';
ScatterJS.plugins(new ScatterEOS());
ScatterJS.login().then(identity => {
const account = identity.accounts[0]
console.log(account)
})
Whether the wallet is connected#
Verify whether the wallet is connected
import ScatterJS from '@scatterjs/core';
import ScatterEOS from '@scatterjs/eosjs2';
ScatterJS.plugins(new ScatterEOS());
const isConnected = ScatterJS.isConnected()
console.log(isConnected)
Retrieve wallet information#
Retrieve information about the currently connected wallet; if no wallet is connected, it will return null
.
import ScatterJS from '@scatterjs/core';
import ScatterEOS from '@scatterjs/eosjs2';
ScatterJS.plugins(new ScatterEOS());
const isConnected = ScatterJS.isConnected()
if (isConnected) {
const identity = ScatterJS.account()
const account = identity.accounts[0]
console.log(account)
}
Sign transaction#
When signing transactions, need to use the eosjs library.
import ScatterJS from '@scatterjs/core';
import ScatterEOS from '@scatterjs/eosjs2';
import {JsonRpc, Api} from 'eosjs';
ScatterJS.plugins(new ScatterEOS());
const network = ScatterJS.Network.fromJson({
blockchain:'wax',
chainId:'1064487b3cd1a897ce03ae5b6a865651747e2e152090f99c1d19d44e01aea5a4',
host:'nodes.get-scatter.com',
port:443,
protocol:'https'
});
const rpc = new JsonRpc(network.fullhost());
ScatterJS.connect('YourAppName', {network}).then(connected => {
if(!connected) return console.error('no scatter');
const eos = ScatterJS.eos(network, Api, {rpc});
ScatterJS.login().then(identity => {
if(!identity) return console.error('no identity');
const account = identity.accounts[0]
eos.transact({
actions: []
}).then(res => {
console.log('sent: ', res);
}).catch(err => {
console.error('error: ', err);
});
});
});
Add/Remove Event Listeners#
Add/remove event listeners. Currently supported events include:
connect
: This event is triggered when the wallet is connected.disconnect
: This event is triggered when the user disconnects.accountChanged
: This event is triggered when the user switches accounts.
import ScatterJS from '@scatterjs/core';
const connect = () => {}
ScatterJS.on('connect', connect)
ScatterJS.off('connect', connect)