In MainContract, I need to perform an atomic transaction which includes the following:

  1. Transfer eth from Contract1 (which is an ERC20 contract) into a given address
  2. Change a state variable in Contract2

I'm having problem implementing the first part.

At present, what I have in mind is to add this in Contract1:

address public mainContract;

constructor(address _mainContract) public {
    mainContract = _mainContract;

function withdraw(address _to, uint256 _value) external {
     require(msg.sender == mainContract);

And then to call function withdraw from a function in MainContract:

function execute(address _to, uint256 _value) external {
     require(msg.sender == owner);
     contract1.withdraw(_to, _value);

But since Contract1 is an ERC20 contract, I was hoping there would be a cleaner way.

Given that I'm the owner of all contracts here, is there a way for me to directly transfer eth from contract1 to the specified address _to?

Something like _to.transfer(_value, {from: address(contract1)}) perhaps?

Thank you!

Note: The reason why I've mentioned the second part (contract2) here, is because otherwise you could tell me something like - "well, just do it from the offchain and get it over with". I need both operations to take place within a single transaction, in order to make sure that nothing else happens in between.

Your Answer

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

Browse other questions tagged or ask your own question.