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
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:
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
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.
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.
-
-
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– AlbertApr 18, 2018 at 15:09
-
1Yea, but you have to write a simple algorithm to do this. Check web3. Or you can write such logic via smart contracts I think.– someartApr 18, 2018 at 16:17
-
1
-
1My instructions are not strict, I'll just point you the idea of how to implement that. Indeed, you can adapt it as you want.– someartApr 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) {
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