Search code examples
pythonethereumbinanceweb3py

Can gas fees be paid from another account when transferring tokens using Web3?


bsc = 'https://bsc-dataseed.binance.org/'
        web3 = Web3(Web3.HTTPProvider(bsc))
        # print(web3.isConnected())
        account_1 = '0xXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
        private_key1 = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
        account_2 = '0xXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
        balance = web3.eth.get_balance(account_1)
        human_readable = web3.fromWei(balance, 'ether')
        print("Balance : " + str(human_readable))
        if balance > 0.01:
            nonce = web3.eth.getTransactionCount(account_1)
            tx = {
                'nonce': nonce,
                'to': account_2,
                'value': web3.toWei(balance, 'ether'),
                'gas': 21000,
                'gasPrice': web3.toWei(5, 'gwei')
            }
            signed_tx = web3.eth.account.sign_transaction(tx, private_key1)
            tx_hash = web3.eth.sendRawTransaction(signed_tx.rawTransaction)
            print(web3.toHex(tx_hash))

I am transferring with this code. However, while doing these transactions, I want to pay the transfer fee with another account. Is it possible. Contract, approve ...


Solution

  • I am transferring with this code. However, while doing these transactions, I want to pay the transfer fee with another account. Is it possible. Contract, approve

    You can read about meta transactions and gasless transactions here.

    They are not implemented on BNB Chain, so it is not possible.