1

I am trying to send Ether to 2 Solidity contracts (version 0.5.3) using Remix IDE at the time of object creation. I am using following 2 techniques but getting compilation error:

First Method:

contract testSuite {

    /// 'beforeAll' runs before all other tests
    /// More special functions are: 'beforeEach', 'beforeAll', 'afterEach' & 'afterAll'
    F5  obj1;
    STFB5 payable obj2;

    function beforeAll() public payable {
       uint a = 10;
       obj1 = new F5();
       payable (address(obj1)).transfer(a);
       obj2 = new STFB5();
       payable (address(obj2)).transfer(a);
    }
}

Giving Error:

contracts/STFB5_test.sol:21:11: ParserError: Expected identifier but got 'payable'
STFB5 payable obj2;
^-----^

Also I tried:

contract testSuite {

    /// 'beforeAll' runs before all other tests
    /// More special functions are: 'beforeEach', 'beforeAll', 'afterEach' & 'afterAll'
    F5  obj1;
    STFB5 obj2;

    function beforeAll() public payable {
        // <instantiate contract>
       //obj1 = new F5();
       //obj2 = new STFB5();
       uint a = 10;
       //obj1 = new F5();
       //payable (address(obj1)).transfer(a);
       //obj2 = new STFB5();
       //payable (address(obj2)).transfer(a);
       obj1 = new F5.value(a)(); 
       obj2 = new STFB5.value(a)(); 

    }

    }

Gives error:

contracts/STFB5_test.sol:32:19: DeclarationError: Identifier not found or not unique.
obj1 = new F5.value(a)();
^------^

Somebody please guide me the solution for this problem. Zulfi.

1 Answer 1

1

Use address payable instead of contacts. For example:

import "./IContract.sol"; // interface of the contract with contractAddress
constract testSuite {
  address payable _contactAddress;
  constructor(address payable contractAddress) {
     _contractAddress = contractAddress;
     _contractAddress.transfer(1);
  }
  
  // Now, when you want to use the functions defined by the contract use explict cast:

  function useContract() public {
      IContract(_contractAddress).mint(); // contract has function mint
  }
}
3
  • Hi my friend, my work is related to: ethereum.stackexchange.com/questions/117140/…, in which testSuite does not have constructor and there are two SCs which have to receive Ether from testSuite contract and I have to test the received Ether in testSuite functions. I can't understand how to incorporate your code. Kindly see the link.
    – zak100
    Dec 26, 2021 at 3:18
  • 1
    Just use payable addresses instead of contract types. And when calling functions of the contracts, you explicitly cast the addresses to the contracts. The code above is not for incorporation, it's just an example.
    – rlib
    Dec 26, 2021 at 9:21
  • Hi, kindly tell me the contents of IContract.sol?
    – zak100
    Dec 27, 2021 at 2:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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