Skip to main content

Ethereum Provider

Ethereum Provider for the Wallet SDK.

Initialization#

import EthereumProvider from '@binance-id/ethereum-provider'
const provider = new EthereumProvider({  chainId, // OPTIONAL request chain id})

Establish Connection#

connect

Description#

Request the user to approve the connection between the DApp and the Binance Defi Wallet. Return a promise that resolves the approved account list. If the user denies the request, the promise will be rejected with a 100001 or 100002 error code.

Error code#

100001: User closed connection modal at dApp side 100002: User rejected connection at Binance app side 100003: Provider is not ready 100004: Already processing the connection request 100005: Already connected

Example#

try {  const accounts = await provider.enable()} catch(error) {  if (error.code === 100001) {    // user closed connection modal at dApp side  } else if (error.code === 100002) {    // user rejected connection at Binance app side  }}

Sign Message#

eth_sign | personal_sign | eth_signTypedData | eth_signTypedData_v1 | eth_signTypedData_v2 | eth_signTypedData_v3 | eth_signTypedData_v4

Description#

Request the user to sign the message or typed data with a private key. Return a promise that resolves the signature. If the user denies the request, the promise will be rejected with a 200001 error code.

Error code#

200001: User rejected sign the information 200004: Invalid sign param

Example#

const message = 'Hello Binance SDK'const signature = await provider.signMessage(message)

Send Transaction#

eth_sendTransaction

Description#

Request the user to sign and send the transaction to the blockchain. Return a promise that resolves the transaction hash.

This promise is used to transfer native tokens or to interact with smart contracts deployed on the blockchain. Once a user initiates the transaction, it is broadcast to the blockchain and processed by nodes on the network. Miners then compete to include the transaction in the next block on the blockchain. Once the transaction is confirmed and included in a block, the transfer of Ether or interaction with the smart contract is executed, and the transaction is considered complete.

Error code#

200001: User rejected sign the information 200004: Invalid sign param

Example#

const payload = {  from, // The address the transaction is send from.  to, // The address the transaction is directed to.  gas, // OPTIONAL Integer of the gas provided for the transaction execution. It will return unused gas.  gasPrice, // OPTIONAL Integer of the gasPrice used for each paid gas  maxFeePergas, // OPTIONAL Includes both the base fee and the priority fee  maxPriorityFeePergas, // OPTIONAL The maximum priority fee that a user is willing to pay for a transaction  value, // OPTIONAL Integer of the value sent with this transaction  data // The compiled code of a contract OR the hash of the invoked method signature and encoded parameters. For details see Ethereum Contract ABI  nonce // OPTIONAL Integer of a nonce. This allows to overwrite your own pending transactions that use the same nonce.}const txHash = await provider.request({  method: 'eth_sendTransaction',  params: [payload]})

Disconnect#

disconnect

Description#

Disconnect the connection between the DApp and Binance Defi Wallet proactively. A disconnect event will be emitted.

Example#

provider.disconnect()

Events#

The SDK provides a similar events API to eventemitter3. It allows DApp developers to receive real-time updates about certain actions that have taken place on the Binance Defi Wallet, which can then be used to trigger further actions within the DApp.

It is important to note that developers should always unsubscribe from events when they are no longer needed to prevent unnecessary resource usage. This can be done using the ‘off’ method provided by the SDK.

accountsChanged#

provider.on('accountsChanged', handler: (accounts: Array<string>) => void)

Triggered when there is an account change in the Binance Defi Wallet accounts. This event is emitted as an array of the current accounts held.

Developers can listen for the accountsChanged event using the provider's ‘on’ method, passing the event name as the first argument and a callback function as the second argument. When the event is triggered, the callback function will be initiated with the new array of accounts as the only parameter.

It is important to note that the accountsChanged event is not triggered when the DApp initially connects to the wallet but only when there is a subsequent change in the connected accounts.

chainChanged#

provider.on('chainChanged', handler: (chainId: string) => void)

Triggered when there is a change in the Binance Defi Wallet's network chain. This event is emitted as a string containing the updated chain ID.

Developers can listen for the chainChanged event using the provider's ‘on’ method, passing the event name as the first argument and a callback function as the second argument. When the event is triggered, the callback function will be called with the new chain ID as the only parameter.

It is important to note that the chainChanged event is not triggered when the DApp initially connects to the wallet but only when there is a subsequent change in the connected network chain.

disconnect#

import { DisconnectType } from '@binance-id/types'
// enum DisconnectType {//   DisconnectAtWallet = 0,//   DisconnectAtClient = 1,//   NetworkError = 2,// }provider.on('disconnect', handler: (disconnectType: DisconnectType) => void);

Triggered when the connection between the DApp and Binance Defi Wallet is terminated. This event is emitted with the disconnect type.

Developers can listen for the disconnect event using the provider's ‘on’ method, passing the event name as the first argument and a callback function as the second argument. When the event is triggered, the callback function will be called with the disconnect type.