I will try to explain my idea.

Our server receive data to save from different servers, we call it S1, S2 and S3.

Our server uses a node script with web3.js to call function over a contract. Actually we're paying all the transactions from our wallet.

I'd like to be able to receive eth from our clients, saving into contract balance, having a balance x single address.

This is doable.

But then i'd like that our server will spend the clients's eth, not our eth, to call contract functions.

So the ultimate goal is that user will pay for what server does.

My problem is that users are not interacting with our server with a browser, so we have no metamask to pay costs.

Is there a way to addebitate to third party the fees?

We could do calls for our clients, we anticipate costs, then call a contract function that only we can call to move a certain amount of eth from user's personal balance to our.

I imagine I can't simply move eth from other's wallet to our. So I thinked to create something like a balance per user inside contract, accept anticipated payments, and move gas from user's balance into contract balance.

I am totally sure it will be hacked in some way. I am not entirely happy.


  1. My user should be able to send ETH to contract. This amount is sent to contract, not to contract owner

  2. When contract receive eth from an user, the contract does something like

    balance[msg.sender] += msg.value

  3. A part the initial payment, user will NO be able to interact directly to the contract in any way.

EDIT 2 : Probably I've found a practical example

Imagine like we act as a bank.

You, the user, transfer money to your bank account. The bank, our contract, actually knows the total amount of money owned by you.

But if your bank account has enough money, the bank pays your bills.

You sent initially money into your bank account, but is the bank that spends your money without your intervention.

So sending eth to my contract, users allow me to spend what they sent.

  • 1
    Where user ether, you would like to use, is stored? Is it on user personal accounts or in contract already? Are your users able to publish Ethereum transactions? Are your user use software provided by you to communicate to your application? Commented Jan 24, 2020 at 14:56
  • @MikhailVladimirov : I edited the question trying to reply to your questions
    – realtebo
    Commented Jan 24, 2020 at 15:34
  • For me, sending ether to a contract is a form of direct interaction with the contract, isn't it? Commented Jan 24, 2020 at 16:24
  • Right: A phisical user sends ether to contract address, directly. A part of this initial interaction, no more interactions will be done
    – realtebo
    Commented Jan 24, 2020 at 16:31
  • And you want to use this initial deposit to pay gas cost for transactions you execute for the user, right? Commented Jan 24, 2020 at 18:17

2 Answers 2


You just need to write your smart contract in such a way, that it will change user balance for a fee and send this fee to you to compensate gas cost. Something like this:

uint public constant FEE = 123;
mapping (address => uint) public userBalances;

function doSomethingForUser (address user, ...) public onlyowner {
  // Do something useful for user

  uint userBalance = userBalances [user];
  require (userBalance >= FEE);
  userBalances [user] = userBalance - FEE;
  msg.sender.transfer (FEE);

In this example FEE amount is static. You may make is to depend on gas price (available to the smart contract as tx.gasprice but this will allow you to charge arbitrary large fee from user by specifying absurdly large gas price.

Also, you my try to estimate actual gas usage of the transaction by calculating difference between values returned by gasleft() at the beginning and and the end of the function, and use this estimation to calculate FEE.

  • What is tx?. Your idea about gas estimation is great, I didn't think about this solutionm
    – realtebo
    Commented Jan 29, 2020 at 8:16
  • 1
    tx does not mean anything by its own, but tx.gasprice does. It is gas price of current transaction. See documentation for details. Commented Jan 29, 2020 at 8:35
  • Thanks, I didn't know that a function could know it own price. Very useful for me in this specific case
    – realtebo
    Commented Jan 30, 2020 at 11:49
  • 1
    The function cannot precisely measure full price of its own execution, but it is possible to measure the price of execution of any particular block of code. You just need to call gasleft() before and after the block, calculate the difference, and multiply it by tx.gasprice. Commented Jan 30, 2020 at 13:20

Have you heard of Proxy contracts? You could have some kind proxy contract where you hold the users ETH (safely) and at the same time, when the user wants to trigger any action, he may sign the transaction, forward it to the proxy contract, and the contract is the actual relayer of the tx, spending than the ETH from the client. Hope this helps

Check this answer

  • If I understand, this requires an user interaction. I mean user must send funds AND send transaction. I need only the payment from the user. Imagine like we act as a bank: if the bank account has enough money, the bank pays your bills. You sent money into your bank account, but is the bank that spends your money without your intervention.
    – realtebo
    Commented Jan 27, 2020 at 14:29
  • Well, in this case, if you're making use of ERC20 standard tokens, you can make use of the allowance/approve mechanisms. The standard interface for ERC20, already gives you this type of functionalities. See it here github.com/OpenZeppelin/openzeppelin-contracts/blob/master/… Commented Jan 27, 2020 at 14:44
  • 1
    This way you could allow a third party (your bank) to make use of a certain allowance prior defined by you. Commented Jan 27, 2020 at 14:45

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.