代码之家  ›  专栏  ›  技术社区  ›  sunwarr10r

NodeJS web3:正在获取返回的事务哈希,但无法在块资源管理器上找到它

  •  0
  • sunwarr10r  · 技术社区  · 8 年前

    一直出现以下错误后:

    没有足够的资金。需要25000000000000,得到:0。

    gacPrice 从我的 txData 具体如下:

    const txData = {
            nonce: web3.utils.toHex(txCount),
            gasLimit: web3.utils.toHex(40000),
            //gasPrice: web3.utils.toHex(10e9),
            to: addressTo,
            from: addressFrom,
            data: functionAbi
    }
    

    tx hash 结果返回,但是 tx哈希 Kovan ? 这是我的智能合约:

    pragma solidity ^0.4.24;
    
    contract Test2 {
        address public bank;
    
        struct Box {
            uint size;
        }
        Box public box;
    
        constructor() public {
            box.size = 3;
            bank = 0xa2079636...;
        }
    
        function changeBox(uint _change) public {
            box.size = _change;
        }
    
        function getBox() public returns (uint) {
            return box.size;
        }  
    }  
    

    下面是我如何通过 web3.js (完整代码):

    const Web3 = require('web3')
    const Tx = require('ethereumjs-tx')
    
    const web3 = new Web3(new Web3.providers.HttpProvider('https://kovan.infura.io/apikey'))
    const addressFrom = '0x002D18...'
    const privKey = '240462...'
    const addressTo = '0x36075430619b21Fff798454e2D5C81E9C18DEe81'
    
    var contractABI = new web3.eth.Contract(
        [...abi...], addressTo);
    
    const contractFunction = contractABI.methods.changeBox(5);
    const functionAbi = contractFunction.encodeABI();
    
    function sendSigned(txData, callback) {
        //const privateKey = new Buffer(config.privKey, 'hex')
        const privateKey = Buffer.from(privKey, 'hex');
        const transaction = new Tx(txData)
        transaction.sign(privateKey)
        const serializedTx = transaction.serialize().toString('hex')
        web3.eth.sendSignedTransaction('0x' + serializedTx, callback)
    }
    
    web3.eth.getTransactionCount(addressFrom).then(txCount => {
        // construct the transaction data
        const txData = {
            nonce: web3.utils.toHex(txCount),
            gasLimit: web3.utils.toHex(40000),
            //gasPrice: web3.utils.toHex(10e9), // 10 Gwei
            to: addressTo,
            from: addressFrom,
            data: functionAbi
            //value: web3.utils.toHex(web3.utils.toWei(123, 'wei'))
      }
    
        sendSigned(txData, function(err, result) {
            if (err) return console.log('error', err)
            console.log('sent', result)
        })
    
    })
    
    1 回复  |  直到 8 年前
        1
  •  0
  •   Diwa    8 年前

    从web3接收事务地址并不总是意味着成功事务处理.Web3返回所有提交事务的事务哈希。