0

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 ?

3 Answers 3

2

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.

7
  • thanks so it will always charge from - address?
    – Albert
    Commented 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
    – Albert
    Commented Apr 18, 2018 at 15:09
  • 1
    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.
    – someart
    Commented Apr 18, 2018 at 16:17
  • 1
    I've updated the answer
    – someart
    Commented Apr 19, 2018 at 10:28
  • 1
    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.
    – someart
    Commented Apr 21, 2018 at 11:37
1

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

1

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) { 
       let cont_address=token_name+"_address";
       //read ABI dynamically 
       const abi = fs.readFileSync('erc20_'+token_name+'Abi.json', 'utf-8');
       // tricky part add user private key to interact with his/her wallet
       web3.eth.accounts.wallet.add(private_key);
       // This line helps to add general wallet which will pay for gas
      web3.eth.accounts.wallet.add(owner_private);
      const contract = new web3.eth.Contract(JSON.parse(abi), contract_address,{from: owner_address, gas: 1000000}); 
      return contract

In this scenarios I was able to send and receive tokens without having ehter in my wallet . Hope it helps

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.