# How to specify gas payer

how I can specify the account which will pay for gas let say 0x1 is sending tokens to 0x2 but I want 0x3 (I have private key) to pay for tx gas ?

I suppose you can't. The one who sends transaction should pay for the spent gas.

What you can do is to calculate the price of the spent gas, convert it to ether and send this value back to 0x1/0x2 from 0x3.

Let me explain this more in details:

If you want to do this automatically (to refund the tx fee) you can write a simple script to do this. I will suggest you to check the Web3.js because it's much more convenient for solving such problems. So your algorithm should work this way:

1. Set up some kind of balance watcher for first account (0x1). ( check the related answer on how to do this). So when your 0x1 account balance changes, you should check if the transaction has been sent to 0x2

2. If you see the successful new ether transfer transaction appear, use Web3 to get the transaction hash and then get the tx fee (spent gas) and convert it to Wei.

3. Send the converted amount from 0x3 to 0x1 using Web3 again.

That's the pretty straightforward and simple way of solving such problem. But pay attention, that you will spend more because 0x3 have to also pay a fee for refunds.

• thanks so it will always charge from - address? Apr 18, 2018 at 14:58
• someart btw you u please explain your last statement ? is to calculate the price of the spent gas, convert it to ether and send this value back to 0x1/0x2 from 0x3. ??? Thanks Apr 18, 2018 at 15:09
• Yea, but you have to write a simple algorithm to do this. Check web3. Or you can write such logic via smart contracts I think. Apr 18, 2018 at 16:17
• I've updated the answer Apr 19, 2018 at 10:28
• My instructions are not strict, I'll just point you the idea of how to implement that. Indeed, you can adapt it as you want. Apr 21, 2018 at 11:37

It's only possible if 0x1 is a contract. 0x3 can then call a method on 0x1 which sends the tokens to 0x2.

If 0x1 can't be a contract, check out someart's answer

I am answering my own question I was able to specify from_address for all transactions like so

``````exports.getContract = function (token_name,private_key,owner_address,contract_address,owner_private) {