繁体   English   中英

Node JS 中 Web3 BEP20 的事务问题

[英]Transaction problem with Web3 BEP20 in Node JS

所以我正在尝试使用nodejs web3(bep-20)在BSC(testnet)上发送交易。 通过与 web3 的合同发送后,我只得到一个 TX hash 作为响应,没有收据或类似的东西。 当我在区块链上搜索交易时,它不存在。 但是当尝试发送另一笔交易时,我收到错误INTERNAL_ERROR: could not replace existing tx 如果我更改随机数,它会起作用,但我只会得到 TX 已返回不存在的 h。 也没有资金从我的地址花费。

我的代码:

const contract = new web3.eth.Contract(abiJson, contract_address, { gasPrice: "51067" });
await contract.methods.transfer(receiver_address, 10000000).send({
      from: sender_address,
      gas: 21596,
      nonce: 0,
    })
    .on("transactionHash", function (hash) {
      console.log(hash);
    })
    .on("confirmation", function (confirmationNumber, receipt) {
      console.log(confirmationNumber, receipt);
    })
    .on("receipt", function (receipt) {
      // receipt example
      console.log(receipt);
    })
    .on("error", function (error, receipt) {
      console.log(error, receipt);
    })

返回 TX hash 示例:

0x0ac1c9e3ad108068f953299c82343057d2e739fab9801e897b25d53170fe3ff0

更新

当我使用web3.eth.getTransaction(txHash); 我得到以下信息:

{
  blockHash: null,
  blockNumber: null,
  from: '0x5de622df348974877Cf7108785f67e09b97785Fc',
  gas: 21596,
  gasPrice: '51067',
  hash: '0x0ac1c9e3ad108068f953299c82343057d2e739fab9801e897b25d53170fe3ff0',  input: '0xa9059cbb000000000000000000000000ff3b0833aab74477d5efa1665632709cc8e2e0f800000000000000000000000000000000000000000000000000000000000186a0',    
  nonce: 4,
  to: '0x337610d27c682E347C9cD60BD4b3b107C9d34dDd',
  transactionIndex: null,
  value: '0',
  type: 0,
  v: '0xe5',
  r: '0x2a0c5365fff32a2314ab1aee0847aa7202b5cdd8f5e070df162e47b7e9ed65a5',   
  s: '0x4415513033acb12a484498252b8247d8f33bbb80d5093f77df4d2c515744dde7'    
}

我还是不明白为什么 TX 没有上线?

从外观上看,您创建了 tx 实例,但没有对其进行签名和发送。 这是一个完整的代码示例,可以按照您的意图进行操作。

const Web3 = require("web3");
const web3 = (new Web3(new Web3.providers.HttpProvider(`rpc address`)))
const Common = require('ethereumjs-common');
const Tx = require('ethereumjs-tx')

async function main(){
const busd = "0xe9e7CEA3DedcA5984780Bafc599bD69ADd087D56";
const abi = "put contract abi here"

const contract = new web3.eth.Contract(abi, busd)
let sender = "sender wallet"
let receiver = "receiver wallet"
let senderkey = Buffer.from("sender private key", "hex")

let data = await contract.methods.transfer(receiver, web3.utils.toHex(1000000000000000000)) //change this value to change amount to send according to decimals
let nonce = await web3.eth.getTransactionCount(sender) //to get nonce of sender address

let chain = {

    "name": "bnb",
    "networkId": 56,
    "chainId": 56
}

let rawTransaction = {
    "from": sender,
    "gasPrice": web3.utils.toHex(parseInt(Math.pow(10,9) * 5)), //5 gwei
    "gasLimit": web3.utils.toHex(500000), //500,000 gas limit
    "to": busd, //interacting with busd contract
    "data": data.encodeABI(), //our transfer data from contract instance
    "nonce":web3.utils.toHex(nonce)
};

const common1 = Common.default.forCustomChain(
    'mainnet', chain,
    'petersburg'
) // declaring that our tx is on a custom chain, bsc chain

let transaction = new Tx.Transaction(rawTransaction, {
    common: common1
}); //creating the transaction

transaction.sign(senderkey); //signing the transaction with private key

let result = await web3.eth.sendSignedTransaction(`0x${transaction.serialize().toString('hex')}`) //sending the signed transaction
console.log(`Txstatus: ${result.status}`) //return true/false
console.log(`Txhash: ${result.transactionHash}`) //return transaction hash
}
main()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM