0

below are very simple setup of one contract calling another contract and I somehow can't get it to work.

Contract C1

pragma solidity ^0.5.0;

contract C1 {
    int public x = 1;

    function setX(int _x) public {
        x = _x;
    }

    function getX() public view returns (int y) {
        return x;
    }
}

Contract C2

pragma solidity ^0.5.0;

import { C1 } from './C1.sol';

contract C2 {
    C1 c1;
    constructor(address _c1) public {
        c1 = C1(_c1);
    }

    function setX(int _x) public {
        c1.setX(_x);
    }

    function getX() public view returns (int) {
        c1.getX();
    }
}

calling setX and getX on C1 works perfectly. But C2 methods don't work. Tried on https://remix.ethereum.org/ Tried with solidity compiler 0.5.0 and EVM version constantinople. I guess I am doing something wrong but not able to figure out what.

Edit: Add JS Code:

'use strict';

const web3 = require("web3");
const solc = require("solc");
const fs = require("fs");

const w3 = new web3(new web3.providers.WebsocketProvider("ws://***.***.***.***:8545"))

// compile contract
const contracts = {
    'C1.sol': {
        content: fs.readFileSync(__dirname + "/C1.sol", "utf-8")
    },
    'C2.sol': {
        content: fs.readFileSync(__dirname + "/C2.sol", "utf-8")
    }
}

const input = {
    language: 'Solidity',
    sources: contracts,
    settings: {
        optimizer: {
            enabled: false
        },
        outputSelection: {
            '*': {
                '*': ["abi", "evm.bytecode"]
            }
        }
    }
}

const parsedOutput = JSON.parse(solc.compile(JSON.stringify(input)));

const compiled = {};
for (let contract in contracts) {
    const name = contract.split(".")[0];
    const output = {};
    output.abi = parsedOutput.contracts[contract][name].abi;
    output.bin = parsedOutput.contracts[contract][name].evm.bytecode.object;
    compiled[name] = output;
}

// console.log(compiled);

// deploy contract

const init = async () => {
    const addresses = await w3.eth.getAccounts();
    console.log(addresses);

    const sendObj = {
        from: addresses[0],
        gas: 450000000
    };
    const callObj = {
        from: addresses[0]
    }

    // deploy C1
    const ct1 = new w3.eth.Contract(compiled["C1"].abi);
    ct1.options.data = "0x" + compiled["C1"].bin;
    const ct1Deployed = await ct1.deploy().send(sendObj);
    console.log(ct1Deployed.options.address);

    const newCt1 = new w3.eth.Contract(compiled["C1"].abi, ct1Deployed.options.address);

    console.log(await newCt1.methods.getX().call(callObj));
    await newCt1.methods.setX(123).send(sendObj);
    console.log(await newCt1.methods.getX().call(callObj));

    // deploy C2
    const ct2 = new w3.eth.Contract(compiled["C2"].abi);
    ct2.options.data = "0x" + compiled["C2"].bin;
    const ct2Deployed = await ct2.deploy({ arguments: [ct1Deployed.options.address] }).send(sendObj);
    console.log(ct2Deployed.options.address);

    const newCt2 = new w3.eth.Contract(compiled["C2"].abi, ct2Deployed.options.address);

    console.log(await newCt2.methods.getX(ct1Deployed.options.address).call(callObj));
    // await newCt2.methods.setX(123).send(sendObj);
    // console.log(await newCt2.methods.getX().call(callObj));
}

init();
2
  • Are you passing the address of a valid C1 instance when you deploy the C2 instance? – goodvibration Nov 3 '19 at 7:44
  • yes ofcourse. I am updating my question with JS code. – Aditya Goel Nov 3 '19 at 8:14
0

You missed the return keyword in C2.

function getX() public view returns (int) {
    return c1.getX();
}

Also, it's confusing to see a variable y in C1's getter, which seems to be there by mistake. Better remove it, like so:

function getX() public view returns (int) {
    return x;
}
1
  • Thanks it does work on remix now. Still can't get it to work on my quorum node. I have raised this issue with them. Thanks. – Aditya Goel Nov 4 '19 at 5:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.