1

我正在尝试使用solc version 0.5.1web3 version 1.0.0-beta.36. Number合约被部署并contractAddress在收到时返回receipt

虽然看起来很麻烦,但为了更容易重现,我已经在这里编写了所有代码。我遇到的问题是在最后一个块中(与合同交互)。

const solc = require('solc');
const Web3 = require('web3');

const input = {
    language: 'Solidity',
    sources: {
        solContract: {
            content: `
            pragma solidity ^0.5.1;
            contract Number {
                uint private num = 100;
                function getNum() public view returns(uint) {
                    return num;
                }
                function setNum(uint _num) public returns(bool success) {
                    num = _num;
                    return true;
                }
            }`
        }
    },
    settings: {
        outputSelection: {
            '*': {
                '*': ['*']
            }
        }
    }
}

const output = JSON.parse(solc.compile(JSON.stringify(input)))
const abi = output.contracts.solContract.Number.abi
const bytecode = `0x${output.contracts.solContract.Number.evm.bytecode.object}`

const web3 = new Web3('http://127.0.0.1:8555');
const privateKey = '0x82f74b773d7f948153d7eb8c192bd9819e3e94073d8bdc0e03d659aa42cd34ba';
// const account = web3.eth.accounts.privateKeyToAccount(privateKey);

const contract = new web3.eth.Contract(abi);

web3.eth.personal.unlockAccount('0x27f4cD26d7e4eAde2052ec5B61f6594D1481C4A2', 'passwordstring', 600)
    .then(
        contract.deploy({
            data: bytecode,
            arguments: []
        })
            .send({
                from: '0x27f4cD26d7e4eAde2052ec5B61f6594D1481C4A2',
                gas: 0x47b760
            }, (error, transactionHash) => {
                if (error) {
                    console.log(error)
                } else if (transactionHash) {
                    web3.eth.getTransaction(transactionHash)
                        .then((data) => {
                            console.log(data)
                        })
                }
            })
            .on('receipt', (receipt) => {
                console.log(receipt.contractAddress)
            }))

// Interacting with deployed contract
// contract address returned from above
// 0x101ea6703717Fa3CF70e96FD3C84FE74ddca50eB
contract.options.address = '0x101ea6703717Fa3CF70e96FD3C84FE74ddca50eB'

contract.methods.getNum().call({
    from: 0x27f4cD26d7e4eAde2052ec5B61f6594D1481C4A2,
    gas: 0x47b760
}, (err, data) => {
    err ? console.log(err) : console.log(data)
})
4

1 回答 1

1

您在这里遇到的问题是,当您执行最后一段代码时,实际上尚未部署合约。

您需要在on()块内执行对 getNum() 的调用,例如:

            .on('receipt', (receipt) => {
                console.log("CONTRACT CREATED, ADDRESS = ", receipt.contractAddress)
                performGetNum(receipt.contractAddress)
            })
            
function performGetNum(contractAddress) {
    sampleContract.options.address = contractAddress
    sampleContract.methods.getNum().call({
        from: "0xed9d02e382b34818e88b88a309c7fe71e65f419d",
        gas: 0x47b760
    }, (err, data) => {
        err ? console.log(err) : console.log("getNum() returned value: ", data)
    })
}

于 2019-07-08T16:00:14.850 回答