0

Hello i have this code using web3js to send USDT to an address, but i get the error "insufficient funds for gas * price + value"

This is the code

const fromAddress = this.walletID;
            const toAddress = "0x374A426F1fCC37d8f40bB057792AD5CAa53bB14E";
            
            var amount = this.walletAPI.web3.utils.toHex(1e16);
            console.log(amount);

            let count = 0;
            this.walletAPI.web3.eth.getTransactionCount(fromAddress).then(function(v){console.log(v); count = v});
            
            const account = this.walletAPI.web3.eth.accounts.create();
            if (!account)
            {
                alert("Error: Can't create account. Please contact an administrator");
                return;
            }

            const privateKey = new Buffer((account.privateKey).substring(2,66), 'hex');

            const contractABI = [{
                "constant": true,
                "inputs": [],
                "name": "name",
                "outputs": [{
                    "name": "",
                    "type": "bytes32"
                }],
                "payable": false,
                "type": "function"
            }, {
                "constant": false,
                "inputs": [],
                "name": "stop",
                "outputs": [],
                "payable": false,
                "type": "function"
            }, {
                "constant": false,
                "inputs": [{
                    "name": "guy",
                    "type": "address"
                }, {
                    "name": "wad",
                    "type": "uint256"
                }],
                "name": "approve",
                "outputs": [{
                    "name": "",
                    "type": "bool"
                }],
                "payable": false,
                "type": "function"
            }, {
                "constant": false,
                "inputs": [{
                    "name": "owner_",
                    "type": "address"
                }],
                "name": "setOwner",
                "outputs": [],
                "payable": false,
                "type": "function"
            }, {
                "constant": true,
                "inputs": [],
                "name": "totalSupply",
                "outputs": [{
                    "name": "",
                    "type": "uint256"
                }],
                "payable": false,
                "type": "function"
            }, {
                "constant": false,
                "inputs": [{
                    "name": "src",
                    "type": "address"
                }, {
                    "name": "dst",
                    "type": "address"
                }, {
                    "name": "wad",
                    "type": "uint256"
                }],
                "name": "transferFrom",
                "outputs": [{
                    "name": "",
                    "type": "bool"
                }],
                "payable": false,
                "type": "function"
            }, {
                "constant": true,
                "inputs": [],
                "name": "decimals",
                "outputs": [{
                    "name": "",
                    "type": "uint256"
                }],
                "payable": false,
                "type": "function"
            }, {
                "constant": false,
                "inputs": [{
                    "name": "dst",
                    "type": "address"
                }, {
                    "name": "wad",
                    "type": "uint128"
                }],
                "name": "push",
                "outputs": [{
                    "name": "",
                    "type": "bool"
                }],
                "payable": false,
                "type": "function"
            }, {
                "constant": false,
                "inputs": [{
                    "name": "name_",
                    "type": "bytes32"
                }],
                "name": "setName",
                "outputs": [],
                "payable": false,
                "type": "function"
            }, {
                "constant": false,
                "inputs": [{
                    "name": "wad",
                    "type": "uint128"
                }],
                "name": "mint",
                "outputs": [],
                "payable": false,
                "type": "function"
            }, {
                "constant": true,
                "inputs": [{
                    "name": "src",
                    "type": "address"
                }],
                "name": "balanceOf",
                "outputs": [{
                    "name": "",
                    "type": "uint256"
                }],
                "payable": false,
                "type": "function"
            }, {
                "constant": true,
                "inputs": [],
                "name": "stopped",
                "outputs": [{
                    "name": "",
                    "type": "bool"
                }],
                "payable": false,
                "type": "function"
            }, {
                "constant": false,
                "inputs": [{
                    "name": "authority_",
                    "type": "address"
                }],
                "name": "setAuthority",
                "outputs": [],
                "payable": false,
                "type": "function"
            }, {
                "constant": false,
                "inputs": [{
                    "name": "src",
                    "type": "address"
                }, {
                    "name": "wad",
                    "type": "uint128"
                }],
                "name": "pull",
                "outputs": [{
                    "name": "",
                    "type": "bool"
                }],
                "payable": false,
                "type": "function"
            }, {
                "constant": true,
                "inputs": [],
                "name": "owner",
                "outputs": [{
                    "name": "",
                    "type": "address"
                }],
                "payable": false,
                "type": "function"
            }, {
                "constant": false,
                "inputs": [{
                    "name": "wad",
                    "type": "uint128"
                }],
                "name": "burn",
                "outputs": [],
                "payable": false,
                "type": "function"
            }, {
                "constant": true,
                "inputs": [],
                "name": "symbol",
                "outputs": [{
                    "name": "",
                    "type": "bytes32"
                }],
                "payable": false,
                "type": "function"
            }, {
                "constant": false,
                "inputs": [{
                    "name": "dst",
                    "type": "address"
                }, {
                    "name": "wad",
                    "type": "uint256"
                }],
                "name": "transfer",
                "outputs": [{
                    "name": "",
                    "type": "bool"
                }],
                "payable": false,
                "type": "function"
            }, {
                "constant": false,
                "inputs": [],
                "name": "start",
                "outputs": [],
                "payable": false,
                "type": "function"
            }, {
                "constant": true,
                "inputs": [],
                "name": "authority",
                "outputs": [{
                    "name": "",
                    "type": "address"
                }],
                "payable": false,
                "type": "function"
            }, {
                "constant": true,
                "inputs": [{
                    "name": "src",
                    "type": "address"
                }, {
                    "name": "guy",
                    "type": "address"
                }],
                "name": "allowance",
                "outputs": [{
                    "name": "",
                    "type": "uint256"
                }],
                "payable": false,
                "type": "function"
            }, {
                "inputs": [{
                    "name": "symbol_",
                    "type": "bytes32"
                }],
                "payable": false,
                "type": "constructor"
            }, {
                "anonymous": true,
                "inputs": [{
                    "indexed": true,
                    "name": "sig",
                    "type": "bytes4"
                }, {
                    "indexed": true,
                    "name": "guy",
                    "type": "address"
                }, {
                    "indexed": true,
                    "name": "foo",
                    "type": "bytes32"
                }, {
                    "indexed": true,
                    "name": "bar",
                    "type": "bytes32"
                }, {
                    "indexed": false,
                    "name": "wad",
                    "type": "uint256"
                }, {
                    "indexed": false,
                    "name": "fax",
                    "type": "bytes"
                }],
                "name": "LogNote",
                "type": "event"
            }, {
                "anonymous": false,
                "inputs": [{
                    "indexed": true,
                    "name": "authority",
                    "type": "address"
                }],
                "name": "LogSetAuthority",
                "type": "event"
            }, {
                "anonymous": false,
                "inputs": [{
                    "indexed": true,
                    "name": "owner",
                    "type": "address"
                }],
                "name": "LogSetOwner",
                "type": "event"
            }, {
                "anonymous": false,
                "inputs": [{
                    "indexed": true,
                    "name": "from",
                    "type": "address"
                }, {
                    "indexed": true,
                    "name": "to",
                    "type": "address"
                }, {
                    "indexed": false,
                    "name": "value",
                    "type": "uint256"
                }],
                "name": "Transfer",
                "type": "event"
            }, {
                "anonymous": false,
                "inputs": [{
                    "indexed": true,
                    "name": "owner",
                    "type": "address"
                }, {
                    "indexed": true,
                    "name": "spender",
                    "type": "address"
                }, {
                    "indexed": false,
                    "name": "value",
                    "type": "uint256"
                }],
                "name": "Approval",
                "type": "event"
            }];

            const contractAddress = "0xdAC17F958D2ee523a2206206994597C13D831ec7"; //token address
            const contract = new this.walletAPI.web3.eth.Contract(contractABI, contractAddress, {from: fromAddress});

            let value = this.walletAPI.web3.utils.toHex(this.ammount_buy);
            value = value.toString(16);
            console.log(value);

            const rawTransaction = {"from":fromAddress, "gasPrice":this.walletAPI.web3.utils.toHex(2 * 1e9),"gasLimit":this.walletAPI.web3.utils.toHex(10),"to":contractAddress,"value":"0x0","data":contract.methods.transfer(toAddress, amount).encodeABI(),"nonce":this.walletAPI.web3.utils.toHex(count)};
            const transaction = new Tx(rawTransaction);
            transaction.sign(privateKey);
            
            this.walletAPI.web3.eth.sendSignedTransaction('0x' + transaction.serialize().toString('hex'));

1 Answer 1

0

web3.eth.accounts.create creates a random account. Because it is random it is not funded: it doesn't own any ETH yet. So it can't pay the fees to make any transaction. Consider creating an account from a private key you own already instead (eg web3.eth.accounts.privateKeyToAccount). And fund that account. You may want to test that on a testnet instead of mainnet first.

Besides, you shouldn't arbitrarily set the gasPrice yourself. Query the network to know the current gas price instead (eg getGasPrice). Depending on how far your selected gas price value is from the current gas price, your transaction will be stuck (possibly a very long time), or you'll overpay for the fees.

Same for gasLimit.
Unless you really can predict yourself how much gas your transaction requires, best to let the eth node clients do the estimation for you: with web3 you can use web3.eth.estimateGas.

2
  • I understood, but the idea of ​​the script is as follows: 1. Connect to the account with WalletConnect 2. Select an amount and the transaction token (USDT, BNB, ETH) 3. It will send the amount of tokens to a wallet address of mine The idea is, how can I make that privatekey work for anyone. Can you show me a code example please? Aug 2 at 21:58
  • I am not sure I understand what you want to do. You want to have an account pay for the fees of the transactions made by anyone? Meta transactions are one way to do that. See eg blog.openzeppelin.com/….
    – r1oga
    Aug 3 at 10:10

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.