Skip to content

Instantly share code, notes, and snippets.

@dvgui
Created March 15, 2023 11:17
Show Gist options
  • Save dvgui/13dcce8a6618414c75bbb5914fe5e3b0 to your computer and use it in GitHub Desktop.
Save dvgui/13dcce8a6618414c75bbb5914fe5e3b0 to your computer and use it in GitHub Desktop.
Util function for ERC20 permit generation in TS (adapted from uniswap's)
import { SignerWithAddress } from '@nomiclabs/hardhat-ethers/signers';
import { BigNumberish, constants, Signature, Contract } from 'ethers';
import { splitSignature } from 'ethers/lib/utils';
import { TestToken } from '../typechain-types';
export async function getPermitSignature(
wallet: SignerWithAddress,
token: TestToken | Contract,
spender: string,
value: BigNumberish = constants.MaxUint256,
deadline = constants.MaxUint256,
permitConfig?: { nonce?: BigNumberish; name?: string; chainId?: number; version?: string }
): Promise<Signature> {
const [nonce, name, version, chainId] = await Promise.all([
permitConfig?.nonce ?? token.nonces(wallet.address),
permitConfig?.name ?? token.name(),
permitConfig?.version ?? '1',
permitConfig?.chainId ?? wallet.getChainId()
]);
return splitSignature(
await wallet._signTypedData(
{
name,
version,
chainId,
verifyingContract: token.address
},
{
Permit: [
{
name: 'owner',
type: 'address'
},
{
name: 'spender',
type: 'address'
},
{
name: 'value',
type: 'uint256'
},
{
name: 'nonce',
type: 'uint256'
},
{
name: 'deadline',
type: 'uint256'
}
]
},
{
owner: wallet.address,
spender,
value,
nonce,
deadline
}
)
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment