Connect to Extension Wallet
Send transactions

Send transactions#

eth_sendTransaction

Description

Transactions are formal actions on a blockchain. They're always initiated in OKX Wallet by calling eth_sendTransaction method. They include simply sending Ether, sending tokens, creating new smart contract, or changing the state of on the blockchain in any way. They're always initiated by a signature from an external account, or a simple key pair.

In OKX Wallet, using the okxwallet.request method to initiate a transaction will form an object like this:

Parameters

Many transaction parameters have already been handled for you by OKX Wallet, but it's always good to know what all the parameters do.

const transactionParameters = {
  nonce: '0x00', // ignored by OKX
  gasPrice: '0x09184e72a000', // customizable by user during OKX confirmation.
  gas: '0x2710', // customizable by user during OKX confirmation.
  to: '0x0000000000000000000000000000000000000000', // Required except during contract publications.
  from: okxwallet.selectedAddress, // must match user's active address.
  value: '0x00', // Only required to send ether to the recipient from the initiating external account.
  data:
    '0x7f7465737432000000000000000000000000000000000000000000000000000000600057', // Optional, but used for defining smart contract creation and interaction.
  chainId: '0x3', // Used to prevent transaction reuse across blockchains. Auto-filled by OKX.
};

Gas price [optional]

Optional parameter - best used on private blockchains

In Ethereum, every transaction specifies a price for the gas it'll consume. To maximize their profit, block producers will pick pending transactions with higher gas prices first when creating the next block. This means that a high gas price will usually cause your transaction to be processed faster at the cost of higher transaction fees. Note that this may not be true for Layer 2 networks which may have a fixed gas price or no gas price at all.

In other words, while you can ignore this parameter on OKX Wallet's default networks, you may want to include it in situations where your application knows more about the target network than we do. On our default networks, OKX Wallet allows you to choose between "slow," "medium," and "fast" options for your gas price.

Gas Limit [optional]

Optional parameter. Rarely useful to DApp developers.

Gas limit is a highly optional parameter, and we automatically calculate a reasonable price for it. You'll probably know if, for some reason, your smart contract benefits from a custom gas limit.

To [optional]

A hex-encoded Ethereum address. Required for transactions with a recipient (all transactions except for contract creation).

Contract creation occurs when there's no to value but there's a data value.

Value [optional]

Hex-encoded value of the network's native currency to be sent. On the main Ethereum network, that currency is Ether, denominated in Wei which is 1e-18 Ether.

Note that these numbers frequently used in Ethereum are far more precise than native JavaScript numbers, and can cause unpredictable behaviors if they're not anticipated. For this reason, we highly recommend using BN.js when manipulating values intended for blockchain.

Hex-encoded value of the network's native currency to send. On the Main Ethereum network, this is ether, which is denominated in wei, which is 1e-18 ether.

Please note that these numbers often used in Ethereum are far higher precision than native JavaScript numbers, and can cause unpredictable behavior if not anticipated. For this reason, we highly recommend using BN.js when manipulating values intended for the blockchain.

Data [optional]

Required for smart contract creation.

This field is also used for specifying contract methods and their parameters. You can learn more about how that data is encoded on the solidity ABI spec.

Chain ID [currently ignored]

Chain ID is currently derived from the user's selected network at okxwallet.networkVersion.

Return value

DATA, 32 bytes - transaction hash. If the transaction is not available yet, it is a zero hash.

When you create a contract, after the transaction is mined, use eth_getTransactionReceipt to get the contract address.

Example

Open in codeopen.

HTML
JavaScript
<button class="connectEthereumButton btn">Connect Ethereum</button>
<button class="signTransactionButton btn">Send Transaction</button>
const ethereumButton = document.querySelector('.connectEthereumButton');
const signTransactionButton = document.querySelector('.signTransactionButton');

let accounts = [];

signTransactionButton.addEventListener('click', () => {
  okxwallet
    .request({
      method: 'eth_sendTransaction',
      params: [
        {
          from: accounts[0],
          to: '0x2f318C334780961FB129D2a6c30D0763d9a5C970',
          value: '0x29a2241af62c0000',
          gasPrice: '0x09184e72a000',
          gas: '0x2710',
        },
      ],
    })
    .then((txHash) => console.log(txHash))
    .catch((error) => console.error);
});

ethereumButton.addEventListener('click', () => {
  getAccount();
});

async function getAccount() {
  try{
    accounts = await okxwallet.request({ method: 'eth_requestAccounts' });
  }catch(error){
    console.log(error);
  }
}