Skip to content

Instantly share code, notes, and snippets.

@SanariSan
Forked from WP-LKL/BscRawTx.ts
Created February 10, 2023 14:26
Show Gist options
  • Save SanariSan/ece3700a36345a2a6a672281563fd376 to your computer and use it in GitHub Desktop.
Save SanariSan/ece3700a36345a2a6a672281563fd376 to your computer and use it in GitHub Desktop.
Binance Smart Chain transaction using web3 and ethereumjs with custom chain and commandline for python.
// Ex. $ npx ts-node BscRawTx.ts --txData=<txData>
// Consult: https://github.com/WP-LKL/bscValueDefi-Exploit, for python use-case
const Tx = require('ethereumjs-tx').Transaction;
const Web3 = require('web3');
import Common from 'ethereumjs-common';
import {parse} from 'ts-command-line-args';
interface input {
txData: string;
}
export const inputs = parse<input>({
txData: {
type: String,
optional: true,
}
})
console.log(inputs["txData"])
const web3 = new Web3("https://bsc-dataseed.binance.org");
const privKey = Buffer.from("SECRETKEY", 'hex');
const addressFrom = "0xMYADDRESS";
const addressTo = '0xRECEPIENT/CONTRACT_ADDRESS';
web3.eth.getTransactionCount(addressFrom, (err, txCount) => {
const common = Common.forCustomChain('mainnet', {
name: 'bnb',
networkId: 56,
chainId: 56
}, 'petersburg');
const txObject = {
nonce: web3.utils.toHex(txCount),
to: addressTo,
value: web3.utils.toHex(web3.utils.toWei('0.0', 'ether')),
gasLimit: web3.utils.toHex(700000),
gasPrice: web3.utils.toHex(web3.utils.toWei('10', 'gwei')),
data: inputs["txData"],
};
const tx = new Tx(txObject, {common});
tx.sign(privKey);
const serializedTrans = tx.serialize();
const raw = '0x' + serializedTrans.toString('hex');
web3.eth.sendSignedTransaction(raw, (err, txHash) => {
console.log('txHash:', txHash)
});
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment