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 ?

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

1

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? – Hrach Karapetyan Apr 18 '18 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 – Hrach Karapetyan Apr 18 '18 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. – someart Apr 18 '18 at 16:17
  • I understand I just don't get the idea of what you suggested – Hrach Karapetyan Apr 19 '18 at 5:28
  • I've updated the answer – someart Apr 19 '18 at 10:28
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.