0

How can I architecture following in the most optimal way;
I'm trying to have a flow between contract A, B, and C.

Contract C should be sending ERC20 to contract B.
Contract A should be sending Ether to contract B.
Contract B should accept the two values from contract C & A, and perform a swap between the two only if both contract C & A gives their consent to contract B to execute the swap of their sent values to contract B.

So in this specific scenario, we are dealing with a two-phase process.

0

Contract B should accept the two values from contract C & B, and perform a swap between the two only if both contract C & B gives their consent to contract B

Do you mean two values from C & A and only if C & A give their consent, not C & B?

Assuming that's what you meant, I'd do something like this.

contract B { 
     // this mapping will store if address A has given permission for the 
     // contract to swap with address C.
     mapping (address => (address => bool)) permissionGiven;
     mapping (address => uint) ethBalance; //ether balance
     mapping (address => uint) balances; //ERC20 token balance

     function () payable { 
        ethBalance[msg.sender] = ethBalance[msg.sender].add(msg.value); 
     }

     function givePermission (address _a) public { 
        permissionGiven[msg.sender][a] = true;
     }

     function swap(address _a, address _c) public {
        if (permissionGiven[_c][_a] && permissionGiven[_a][_c]) {
           _c.transfer(ethBalance[_a]);
           transfer(_a, balances[_c]);
        }
     }

     function transfer (address _to, uint _tokens) {
         // usual ERC20 transfer function
     }
} 

In this implementation, contracts C and A can call givePermission, passing in the addresses of A and C respectively. Then, after depositing ether/ tokens and calling swap, the ether will be transferred to C and the tokens to A.

3
  • Yes, you're right Elizabeth, I'll correct the description, that's what I meant. However, both contract A & C should give permission before a swap is initiated.
    – NowsyMe
    May 5 '18 at 1:15
  • I changed the swap function if statement to reflect that. The way I had it before was assuming A had already given permission, and C had to call swap to check if A has given permission; if A had, then it would commence with the swap.
    – noot
    May 5 '18 at 1:22
  • I see. they should, however, give permission to contract B and not each other.
    – NowsyMe
    May 5 '18 at 11:07

Your Answer

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

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