0

I want to create a DApp by staking pools of Stake BNB Earn MyToken tokens.

I've been looking for some references but I'm still confused, that's why I decided to ask.

I have the following lines of code and it's running on cainId : 56 bsc when I run Error Invalid sender.

I'm really confused someone can kindly help me.

const Tx = require('@ethereumjs/tx').Transaction;
const Common = require('@ethereumjs/common').default;
import pancakeAbi from '../pancakeAbi.json';

const RPCuri = 'https://bsc-dataseed1.binance.org';
const pcsRouterV2 = '0x10ed43c718714eb63d5aa57b78b54704e256024e';
const pathToken   = [WBNBAddress, MyTokenAddress];

const web3 = new Web3(new Web3.providers.HttpProvider(RPCuri));

const amountToBuyWith = web3.utils.toHex(tokensIn);
const amountOutMin = '100' + Math.random().toString().slice(2,6);

const contract = new web3.eth.Contract(pancakeAbi, pcsRouterV2, {from: Accounts.address});
const data = await contract.methods.swapExactETHForTokens(
  web3.utils.toHex(amountOutMin), 
  pathToken,
  Accounts.address,
  web3.utils.toHex(Math.round(Date.now()/1000)+60*20)
);

const count = await web3.eth.getTransactionCount(Accounts.address);
const rawTransaction = {
  nonce: web3.utils.toHex(count),
  gasPrice: web3.utils.toHex(1000000000),
  gasLimit: web3.utils.toHex(210000),
  to: pcsRouterV2,
  from: Accounts.address,
  value: web3.utils.toHex(amountToBuyWith),
  data: data.encodeABI()
};


const privateKey = Buffer.from(Accounts.privateKey, 'hex');
const common = Common.forCustomChain ('mainnet', { networkId: 56, chainId: 56, name: 'bnb' }, 'istanbul');

console.log(rawTransaction)

const tx = new Tx(rawTransaction, { common });
tx.sign(privateKey);
const serializedTx = tx.serialize();

web3.eth.sendSignedTransaction('0x' + serializedTx.toString('hex'))
.on('transactionHash', (hash) => {
  console.log('transactionHash',hash)
})
.on('error', function(error) {
  console.log(error.message);
});

dependencies

"@ethereumjs/tx": "^3.3.0", "ethers": "^5.4.6", "web3": "1.3.0"

Someone can help me!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.