Skip to content

Instantly share code, notes, and snippets.

@johnpm-12
Last active August 7, 2022 21:14
Show Gist options
  • Select an option

  • Save johnpm-12/dc3f059176ebf43f8cacd4c4b76d5786 to your computer and use it in GitHub Desktop.

Select an option

Save johnpm-12/dc3f059176ebf43f8cacd4c4b76d5786 to your computer and use it in GitHub Desktop.
EIP-2612 ethers example
import { BigNumberish, Signature, utils, providers, Contract } from "ethers";
// this is designed to work with USDC
export async function signPermit(
signer: providers.JsonRpcSigner,
token: Contract,
chainId: BigNumberish,
spender: string,
value: BigNumberish,
deadline: BigNumberish
): Promise<Signature> {
const address = await signer.getAddress();
const rawSignature = await signer._signTypedData(
{
name: await token.name(), // unique name of EIP-712 domain
version: await token.version(), // version of domain
chainId,
verifyingContract: token.address, // address that receives permit
},
{
Permit: [
{
name: "owner",
type: "address",
},
{
name: "spender",
type: "address",
},
{
name: "value",
type: "uint256",
},
{
name: "nonce",
type: "uint256",
},
{
name: "deadline",
type: "uint256",
},
],
},
{
owner: address,
spender,
value,
nonce: await token.nonces(address), // current nonce
deadline,
}
);
return utils.splitSignature(rawSignature);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment