Last active
August 7, 2022 21:14
-
-
Save johnpm-12/dc3f059176ebf43f8cacd4c4b76d5786 to your computer and use it in GitHub Desktop.
EIP-2612 ethers example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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