如何为合同的部署事务格式化地址?

问题描述 投票:0回答:1

为什么我的部署事务在构造函数使用地址参数而不使用0参数或非地址数据类型的合约时遇到运行时错误?

我遇到了这个错误runtime error

当我尝试运行下面的代码时,即使我删除了web3.utils.toHex(),我仍然得到了同样的错误,在注释中,你可以看到我已经注意到其他参数类型的工作。地址有什么特别的需要发生的事情来正确格式化它们?

fs = require("fs")

const web3 = new (require("web3"))('HTTP://127.0.0.1:7545')
console.log("web3")

let accounts = [];
web3.eth.getAccounts().then(function(result){
    accounts = result
    console.log(accounts)
})

deployContract('false-resolve.sol', "DummyToken",[]).then(function(result){
    //THIS WORKS
    //deployContract('false-resolve.sol', "DummyToken",[])

    //THIS WORKS
    //deployContract('UintConstructor.sol', "UintConstructor", [ 1 ] ).then(function(r){
    //  console.log("UintConstructor._address", r._address) 
    //})

    deployContract('orocl.sol', "Oracle", [ web3.utils.toHex("0x786c35Ae953f94fc4Ffd31Edd0388d50fCF5Bb70") ] ).then(function(r){
        console.log("r._address", r._address)
    })
})
function deployContract(CONTRACT_FILE, contractName, args){
    const content = fs.readFileSync(CONTRACT_FILE).toString()
    const input = {
      language: 'Solidity',
      sources: {
        [CONTRACT_FILE]: {
          content: content
        }
      },
      settings: {
        outputSelection: {
          '*': {
            '*': ['*']
          }
        }
      }
    }

    var compiled = solc.compile(JSON.stringify(input))
    var output = JSON.parse(compiled)
    var abi = output.contracts[CONTRACT_FILE][contractName].abi
    var bytecode = output.contracts[CONTRACT_FILE][contractName].evm.bytecode.object.toString()
    var contract = new web3.eth.Contract(abi)

    var contractTx = contract.deploy({data: "0x"+bytecode, arguments: args});
    return contractTx.send({from: '0x786c35Ae953f94fc4Ffd31Edd0388d50fCF5Bb70', gas: 4700000})
}
web3 web3js
1个回答
1
投票

现在 bytecode 其实并不是十六进制格式的数据,你把它作为一个字符串,上面有 0x 的前面。

你可能想直接传递字节码数据。

...

var bytecode = output.contracts[CONTRACT_FILE[contractName].evm.bytecode;

...

var contractTx = contract.deploy({data: bytecode, arguments: args});

引用这个 stackoverflow问题.

© www.soinside.com 2019 - 2024. All rights reserved.