I'm having trouble compiling a contract that calls a function of an imported contract.
SilverCoin.sol
:
pragma solidity ^0.4.11;
contract SilverCoin {
function abc() constant returns (uint) {
return 0;
}
}
Banker.sol
:
pragma solidity ^0.4.11;
import "./SilverCoin.sol";
contract Banker {
function abc_1() constant returns (uint) {
return SilverCoin.abc();
}
}
Migration script for truffle:
var SilverCoin = artifacts.require("./SilverCoin.sol");
var Banker = artifacts.require("./Banker.sol");
module.exports = function(deployer) {
deployer.deploy(SilverCoin);
deployer.deploy(Banker);
};
Error:
$ truffle compile
Compiling ./contracts/Banker.sol...
Compiling ./contracts/SilverCoin.sol...
/path/to/file/contracts/Banker.sol:7:16: : Member "abc" not found or not visible after argument-dependent lookup in type(contract SilverCoin)
return SilverCoin.abc();
^------------^
Compiliation failed. See above.
How do I fix this?
Best Answer
You are making a reference to the contract itself while you should refer to the contract instance (deployed to a given address).
First of all, you need to store the contract instance address into the Banker contract. For example, you can pass the SilverCoin instance address into the Banker constructor:
contracts/Banker.sol
Then you have to refer to this particular instance when you make the call
contracts/Banker.sol
Finally, when you deploy SilverCoin and Banker, you can pass the SilverCoin contract address in the Bank contract deployment:
migrations/2_deploy_contracts.js
And finally, a quick test to validate all of that
test/test.js
Command Result:
I pushed the code in Git: https://github.com/gjeanmart/stackexchange/tree/master/20750