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?')
};