Last active
April 29, 2021 17:21
-
-
Save aleph-v/c4215aceadcfa1e82c9778020d9f6850 to your computer and use it in GitHub Desktop.
An async permit signing function
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
// Uses a default infinite expiration time | |
async function getPermitSignature( | |
token: ERC20Permit, | |
sourceAddr: string, | |
spenderAddr: string, | |
spenderAmount: BigNumberish, | |
version: string | |
) { | |
// To be replaced in front end with signer type from ethers | |
const signer = ethers.provider.getSigner(0); | |
const name = await token.name(); | |
const chainId = await signer.getChainId(); | |
const domain = { | |
name: name, | |
version: version, | |
chainId: chainId, | |
verifyingContract: token.address | |
}; | |
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 token.nonces(sourceAddr); | |
const data = { | |
owner: sourceAddr, | |
spender: spenderAddr, | |
value: spenderAmount, | |
nonce: nonce, | |
deadline: ethers.constants.MaxUint256 | |
}; | |
let sigStringPromise = signer._signTypedData(domain, types, data); | |
return sigStringPromise.then((value: string) => { | |
return parseSigString(value) | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment