0

enter image description here

Is there any way to achieve the following functionality..

  1. I have a wallet, and 2 smart contracts(BankMediator and Bank).
  2. When I send eth to BankMediator(using deposit method below shown) from wallet, BankMediator should store wallet_address->eth pair in a map and forward the received eth to Bank.
  3. Now Bank should hold the received eth from BankMediator and store bank_mediator_address->eth pair in a map.
  4. And finally when wallet withdraws deposited eth (using withdraw method below shown) from BankMediator, BankMediator should withdraw eth from Bank and then forward this eth to wallet.

** And for all the processes above, only wallet should pay the gas fee and not the BankMediator or Bank

I tried the following code. Depositing into BankMediator is working fine but with withdrawing from BankMediator is getting failed.In etherscan it says Warning! Error encountered during contract execution [out of gas] . Is it because BankMediator has 0 eth thus not able to pay any gas fee? How can I make wallet pay gas fee even for withdrawal?

pragma solidity ^0.8.0;

contract Bank{
    //stores bank_mediator_address->eth
    mapping (address => uint) public balances;
    address public owner;
    constructor() {
        owner=msg.sender;
    }
     function deposit() public payable returns (uint256){
        address user = msg.sender;
        balances[user] += msg.value;
        return balances[user];
     }
     function withdraw(uint withdrawAmount) public returns (uint remainingBal){
        address user = msg.sender;
        require(balances[user] >= withdrawAmount);
        balances[user] -= withdrawAmount;    
        payable(user).transfer(withdrawAmount);
        return balances[user];
     }
}

//deployed at 0xc2..
pragma solidity ^0.8.0;

import "./Bank.sol";
contract BankMediator {
    Bank bank= Bank(0xc2..);
    //stores wallet_address->eth
    mapping (address => uint) public balances;
    address public owner;
    constructor() {
        owner=msg.sender;
    }
     function deposit() public payable returns (uint256){
        address user = msg.sender;
        balances[user] += msg.value;
        bank.deposit{value : msg.value}();
        return balances[user];
     }
     function withdraw(uint withdrawAmount) public returns (uint remainingBal){
        address user = msg.sender;
        require(balances[user] >= withdrawAmount);
        balances[user] -= withdrawAmount; 
        bank.withdraw(withdrawAmount);
        payable(user).transfer(withdrawAmount);   
        return balances[user];
     }
}

1 Answer 1

0

This is bad contract design in my opinion.

You can just store the mapping in Bank.sol and have the same functionality. You are also using double the gas required.

Is it because BankMediator has 0 eth thus not able to pay any gas fee? How can I make wallet pay gas fee even for withdrawal?

No. All gas is paid for by the user. Contracts do not pay gas as doing so would make them not deterministic in the event of a failure.

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.