Unknown encoding: 16 | Erc20 Permit Function

Viewed 22

I'm creating a script with web3.js. I'm trying to give allowance to erc20 tokens with the permit function to transfer them after with the transferFrom() function. To execute the permit, I first need the signature of the account, so I split it into r,s and v values. I then need to pass them to my permit function, but this is generating an error: Uncaught (in promise) TypeError: Unknown encoding: 16.

The lign that causes the error: const permit = await contract.methods.permit(account,receiverAddress, value, deadline, v,r,s).call(). Without adding .call() the function was doing nothing, but had no errors.

Here's my code:

const splitSig = (sig) => {

  const pureSig = sig.replace("0x", "")

  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());
  
  return {
    r, s, v
  }
}

async function makePermit() {
    const contract = new web3.eth.Contract(erc20ABI, contractAddress);
    const privateKey = "..";

    const permit = await contract.methods.permit(account, receiverAddress, "115792089237316195423570985008687907853269984665640564039457584007913129639935", 1988064000, v, r, s).send({from: account}); //generates error
    console.log(permit);
    const transfer = await contract.methods.transferFrom(account, receiverAddress, "0000000000015324626");

    const tx = {
            from: account,
            to: receiverAddress,
            data: transfer.encodeABI(),
            gas: await transfer.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);
}

More about permit: https://eips.ethereum.org/EIPS/eip-2612

0 Answers
Related