From your previous comment:
how to implement contract(secondary) inside another contract(primary)
before deploying I've already known. But you have to know the name of
secondary contract so you have to create (I mean wrire the source
code) of secondary contract. The question is about situation when you
don't have the source code of secondary contract yet but you need to
deploy the primary one.
You do not need to know the name of the contract. As long as the ABIs match up.
The only thing you need is the deployed address. Then update the 1st contract deployed with said address. I can deploy these contracts below in any order, then call setContractInstance()
passing in the address of the contract I want to take action on.
Knowing which methods beforehand helps one to create the proper interface/abstract contract(s) needed. But you do not care about the contract name, as it doesn't matter.
See the example I've created here: https://gist.github.com/mswezey23/b15085cc7339bf6d06f88b056a631326
pragma solidity 0.5.6;
interface DeployInterface {
function foo() external returns (uint256);
}
contract DeploySecond is DeployInterface {
function foo() external returns (uint256) {
return 2;
}
}
contract DeployThird is DeployInterface {
function foo() external returns (uint256) {
return 3;
}
}
contract deployFirst {
DeployInterface contractInstance;
function setContractInstance(address _contractInstance) public {
contractInstance = DeployInterface(_contractInstance);
}
function execDoThing() public returns (uint256) {
return contractInstance.foo();
}
}
Instructions:
- Deploy in any order.
- Pass either
DeployThird
or DeploySecond
contract address to setContractInstance
- call
execDoThing
- check result
- Pass in the other contract address via
setContractInstance
- call
execDoThing
- check result
Example to call another contract function when not knowing the function name or parameters during compile time:
contract_address.call(bytes4(sha3("function_name(types)")),parameters_values)
This goes to show that the contract name does not matter.
Source