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)
}
Examples - Create a Token Swap raw transaction and submit it to blxr_tx api
const fs = require('fs')
const WebSocket = require('ws')
const EthereumTx = require('ethereumjs-tx').Transaction
const Common = require('ethereumjs-common').default
const Web3 = require('web3')
const web3 = new Web3('PROVIDER_URL')
const account = 'YOUR_ADDRESS'
const privateKey = Buffer.from(
'PRIVATE_KEY',
'hex',
)
// Network you would like to connect to, in this case BSC Mainnet
const BSC_FORK = Common.forCustomChain(
'mainnet',
{
name: 'Binance Smart Chain Mainnet',
networkId: 56,
chainId: 56,
url: 'https://bsc-dataseed.binance.org/'
},
'istanbul',
)
const main = async () => {
const bnb = 'WBNB_TOKEN_ADDRESS'
const token = 'TOKEN_ADDRESS_TO_SWAP_FOR'
const pair = [bnb, token]
// Amount of BNB to swap
const tokenAmountIn = web3.utils.toHex()
// Desired amount of token returned
const amountOutMin = web3.utils.toHex()
const pancakeSwapRouterAddress = 'PANCAKESWAP_ROUTER_ADDRESS'
// Export Pancakeswap ABI to json file in same directory
const abi = JSON.parse(fs.readFileSync('pancake-router-abi.json', 'utf-8'));
const contract = new web3.eth.Contract(abi, pancakeSwapRouterAddress, {from: account})
const data = contract.methods.swapExactETHForTokens(
amountOutMin,
pair,
account,
Date.now() + 1000 * 60 * 10,
)
const tx = new EthereumTx({
to: pancakeSwapRouterAddress,
nonce: nonce,
// Desired gas usage in decimal format
gasPrice: web3.utils.toHex(),
gas: web3.utils.toHex(),
value: tokenAmountIn,
data: data.encodeABI(),
chainId: 56
}, {common: BSC_FORK}
)
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}", "blockchain_network": "BSC-Mainnet"}}`)
}
function handle(response) {
console.log(response.toString()) // Or process it generally
}
ws.on('open', proceed)
ws.on('message', handle)
}