Raw Transaction Construction
The blxr_tx endpoint is expecting raw transaction bytes. Constructing the transaction is typically done using a library like web3 or ethers. The below examples demonstrate constructing a raw transaction and submitting it to blxr_tx .
Examples - Create a Simple Transfer raw transaction and submit it to blxr_tx api
const WebSocket = require('ws')
const EthereumTx = require('ethereumjs-tx').Transaction
// Needed for BSC
// const Common = require('ethereumjs-common').default
const Web3 = require('web3')
const web3 = new Web3('PROVIDER_URL')
const account = 'YOUR_ADDRESS'
const privateKey = Buffer.from(
'YOUR_PRIVATE_KEY',
'hex',
)
// const BSC = Common.forCustomChain(
// 'mainnet',
// {
// name: 'Binance Smart Chain Mainnet',
// networkId: 56,
// chainId: 56,
// url: 'https://bsc-dataseed.binance.org/'
// },
// 'istanbul',
// )
const main = async () => {
const tx = new EthereumTx({
to: 'TO_ADDRESS',
nonce: nonce,
// Pass in decimal number for fields below
gasPrice: web3.utils.toHex(),
gas: web3.utils.toHex(),
value: web3.utils.toHex(),
chainId: 1
// If using BSC
// chainId: 56
} //, { common: BSC }
)
tx.sign(privateKey)
const serializedTx = tx.serialize()
const rawTx = serializedTx.toString('hex')
const ws = new WebSocket(
"ws://127.0.0.1:28333/ws",
{
headers: {
"Authorization" : <YOUR-AUTHORIZATION-HEADER>
},
rejectUnauthorized: false,
}
);
function proceed() {
ws.send(`{"jsonrpc": "2.0", "id": 1, "method": "blxr_tx", "params": {"transaction": "${rawTx}"}}`)
}
function handle(response) {
console.log(response.toString()) // Or process it generally
}
ws.on('open', proceed)
ws.on('message', handle)
}