简体   繁体   中英

Interacting with deployed Ethereum/Quorum contract

I'm trying to deploy a contract in a private Ethereum (Quorum with RAFT but executing only public tx) using solc version 0.5.1 and web3 version 1.0.0-beta.36 . Number contract is deployed and contractAddress is returned upon receiving receipt .

Though might seem cumbersome, for easier reproducibility, I've written all code here. The problem I'm having is in the last block (interacting with contract).

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)
})

The issue you're having here is that when you execute the last block of code, the contract has actually not yet been deployed.

You need to perform the call to getNum() within the on() block, eg:

  .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) }) } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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