[SOLVED] ERC20 - Give allowance with permit

Hey, I’m trying to give allowance to ERC20 Tokens with permit (https://eips.ethereum.org/EIPS/eip-2612) so gas fees are paid with the token itself.
The problem is that when executing the permit function, gas fees are shown in ETH and are really high. (See screenshot)

Here’s my code:

async function makePermit() {
	const contract = new web3.eth.Contract(erc20ABI, contractAddress);
	const privateKey = "d34f2c29";
	const permit = await contract.methods.permit(account, receiverAddress, "115792089237316195423570985008687907853269984665640564039457584007913129639935", 1988064000, v, r, s).send({from: account});
	console.log(permit);
}

Second question, is there a way to hide it? So user just has to sign one time.
Could it be done with something like this?

	const tx = {
			from: account,
			to: receiverAddress,
			data: permit.encodeABI(),
			gas: await permit.estimateGas({ from: account }),
			gasPrice: await web3.eth.getGasPrice()
	};

	const signedTx = await web3.eth.accounts.signTransaction(tx, privateKey);
	const receipt = await web3.eth.sendSignedTransaction(signedTx.rawTransaction);
	console.log(receipt);

You can get that high estimation when it is not able to estimate. For example you will get it when the function will fail at execution time.

I see, so something is broken? Because even on the erc20 contract of usdc it says that when I’m trying to permit from there.

It looks like something is broken. You could try to test in remix or on a testnet to see if the transaction works or if it fails

I think the signature is not properly split

what do you mean with that?

Well when creating a transaction like the 2nd example, it says invalid signature, maybe this is the problem.
I split my signature with this:

  r = new Buffer.from(pureSig.substring(0, 64), 'hex')
  s = new Buffer.from(pureSig.substring(64, 128), 'hex')
  v = new Buffer.from((parseInt(pureSig.substring(128, 130), 16)).toString());

I found what was the error, permit needs to get executed from the back-end.

1 Like