-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove outdated eth-permit in favor of custom code
- Loading branch information
1 parent
e8a66c1
commit 42ebed4
Showing
5 changed files
with
69 additions
and
15 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { ethers } from 'ethers' | ||
|
||
export async function signERC2612Permit( | ||
provider: any, | ||
token: string | { name: string; version: string; chainId: number; verifyingContract: string }, | ||
owner: string, | ||
spender: string, | ||
value: string, | ||
deadline: number | ||
) { | ||
const tokenAddress = typeof token === 'string' ? token : token.verifyingContract | ||
const tokenContract = new ethers.Contract( | ||
tokenAddress, | ||
['function name() view returns (string)', 'function nonces(address) view returns (uint256)'], | ||
provider | ||
) | ||
|
||
let name: string, version: string, chainId: number | ||
if (typeof token === 'string') { | ||
name = await tokenContract.name() | ||
version = '1' | ||
chainId = Number((await provider.getNetwork()).chainId) | ||
} else { | ||
;({ name, version, chainId } = token) | ||
} | ||
|
||
const domain = { | ||
name, | ||
version, | ||
chainId, | ||
verifyingContract: tokenAddress, | ||
} | ||
|
||
const types = { | ||
Permit: [ | ||
{ name: 'owner', type: 'address' }, | ||
{ name: 'spender', type: 'address' }, | ||
{ name: 'value', type: 'uint256' }, | ||
{ name: 'nonce', type: 'uint256' }, | ||
{ name: 'deadline', type: 'uint256' }, | ||
], | ||
} | ||
|
||
const nonce = await tokenContract.nonces(owner) | ||
|
||
const values = { | ||
owner, | ||
spender, | ||
value, | ||
nonce, | ||
deadline, | ||
} | ||
|
||
const signer = await provider.getSigner(owner) | ||
const signature = await signer.signTypedData(domain, types, values) | ||
const { v, r, s } = ethers.Signature.from(signature) | ||
|
||
return { | ||
owner, | ||
spender, | ||
value, | ||
deadline, | ||
v, | ||
r, | ||
s, | ||
} | ||
} |
This file contains 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