0

I'm building an escrow contract and using web3js to interact with it. Currently, I am trying to approve and send an amount of custom ERC20 token from a wallet to the escrow contract. The problem is, the approve function doesn't work, but the transferFrom function that I call from the escrow contract itself does. I know the approve function doesn't work because I check the allowance from Remix and it shows none. Why does one transaction work and not the other?

Transaction Data:

//This doesnt work
    let approveSpender = async(batch=false) => {

                const txOptions = {
                    from: address_from,
                    nonce: await web3.eth.getTransactionCount(address_from, "pending"),
                    gasLimit: web3.toHex(210000),
                    gasPrice:web3.toHex(web3.toWei('30', 'gwei')),
                    to: gntAddress,
                    data :gntContract.approve.getData(escrowAddress, (40e18)),
                    value: "0x0",
                };
                let transaction = new EthereumTx(txOptions);

                if(batch) return sendRaw(transaction);

                const txResult = await sendRaw(transaction);
                if(!txResult) {console.error('Something messed up')}
                else console.log('Actually worked?');
            };
//This works
    let depositToEscrow = async(batch=false) => {
                let nonce = await web3.eth.getTransactionCount(address_from, "pending");
                if(batch) nonce += 1;

                const txOptions = {
                    from: address_from,
                    nonce: nonce,
                    gasLimit: web3.toHex(210000),
                    gasPrice:web3.toHex(web3.toWei('30', 'gwei')),
                    to: escrowAddress,
                    data :escrowContract.deposit.getData((40*1e18)),
                    value: "0x0",

                };
                let transaction = new EthereumTx(txOptions);

                if(batch) return sendRaw(transaction);

                const txResult = await sendRaw(transaction);
                if(!txResult) {console.error('Something messed up')}
                else console.log('Actually worked?')
            };
  • Can you show your code? What happens when you send the approve transaction? Is it going through? – rhlsthrm Mar 8 at 20:13
  • The approve transaction is going through and is successful as reported on etherscan. Please check the post above, I have updated it with code. – Abhi Thosar Mar 8 at 20:21
  • Do you have a transaction hash? The code looks reasonable. – Ismael Mar 9 at 20:42
  • @Ismael yessir, here is the transaction hash for the approval function that doesn't work, after that is the hash for the transferFrom function: 0x75d86705c8c4bf8fca818e92c6f2f0303851afb57d1aa1efe5d8666d05dca9ed, 0x03745369cb750f4739486ddd1e4a40339e133bbf11373a700969291d68488542 – Abhi Thosar Mar 12 at 16:08
  • @Ismael I should mention that the transferFrom function is called from the escrow contract, the function in the escrow contract that calls transferFrom is named deposit() which is what you'll see as the function run in the second transaction hash. Please let me know if you would like to see the smart contract code as well. – Abhi Thosar Mar 12 at 16:17

Your Answer

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

Browse other questions tagged or ask your own question.