Option one – using the ABI

As we did in the Ctontine contract, the regular way to interact with other contracts is to call (invoke) a function on a contract object (we borrow here the OOP terminology). For example, we can call a remote function from a remote contract, ContractA , as follows: ContractA.Remotefunction(arguments).

But to be able to make the invocation this way, we need to define (in the same Solidity file) an abstract form of ContractA. For instance, if we have a contract, ContractA, then the code is as follows:

contract ContractA {
function f(uint256 a, string s) payable returns (bool) {
//your code here
return true;
}
}

If this contract is deployed in the blockchain, let’s say under the 0x123456 address, and we want to call the f() function within a caller contract, ContractB, then we have to include the abstract form of ContractA in the ContractB file, then instantiate ContractA and execute f(). Here is how the ContractB contract file will look:

contract ContractA {
function f(uint256 a, string s) payable returns (bool);
function h() payable returns (uint);
}

contract ContractB{
address ContractAaddress = 0x123456;
ContractA ContractAInstance = ContractA(ContractAaddress);

function g() returns (bool){
return ContractAInstance.f(10, "hello");
}
}

If we invoke a payable function, we can specify the number of wei, as well as limit the amount of gas available to the invocation using the .gas() and .value() special options, respectively: ContractAInstance.h.value(10).gas(800)();.

The parentheses at the end serve to receive the arguments needed to perform the call. If the function doesn’t accept any arguments, we keep them empty.

The argument provided for the .value() function is considered by default in wei. You can indicate other units, such as finney or ether. For instance .value(10 ether) will send 10 ether instead of 10 wei.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset