Skip to main content
changed swap function
Source Link
noot
  • 96
  • 5

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][msg.sender]permissionGiven[_c][_a] && permissionGiven[_a][_c]) {
           _c.transfer(ethBalance[msg.sender]ethBalance[_a]);
           transfer(msg.sender_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.

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 _c) public {
        if (permissionGiven[_c][msg.sender]) {
           _c.transfer(ethBalance[msg.sender]);
           transfer(msg.sender, 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.

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.

Source Link
noot
  • 96
  • 5

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 _c) public {
        if (permissionGiven[_c][msg.sender]) {
           _c.transfer(ethBalance[msg.sender]);
           transfer(msg.sender, 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.