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 the OKX Web3 Wallet, you can use the okxwallet.request
method to initiate a transaction.
Parameters#
This section mainly introduces the transaction parameters covered in this document. Most of the transaction parameters mentioned here will be handled by the OKX Web3 Wallet. Transactions are categorized into legacy transactions and EIP-1559 transactions, which will be discussed in order.
Legacy Transactions#
const transactionParameters = {
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.
EIP-1559 Transactions#
const transactionParameters = {
maxPriorityFeePerGas: "0x0", // Maximum fee, in wei, the sender is willing to pay per gas above the base fee.
maxFeePerGas: "0x6f4d3132b", // Maximum total fee (base fee + priority fee), in wei, the sender is willing to pay per gas.
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.
};
For EIP-1559 transactions, the key difference from legacy transactions is the use of maxPriorityFeePerGas
and maxFeePerGas
instead of gasPrice
.
maxPriorityFeePerGas [Optional]
The additional tip the user is willing to pay to the miner/validator for prioritizing the transaction.
maxFeePerGas [Optional]
The maximum total amount the user is willing to pay per unit of gas, including both the base fee and the priority fee.
Example#
Open in codeopen.
<button class="connectEthereumButton btn">Connect Ethereum</button>
<button class="signTransactionButton btn">Send Transaction</button>