Created
September 10, 2020 03:15
-
-
Save Ankarrr/b23a9174ddee1634f9b682b9870cb9d8 to your computer and use it in GitHub Desktop.
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
// Prepare parameters | |
const PERMIT_TYPEHASH = '0x6e71edae12b1b97f4d1f60370fef10105fa2faae0126114a169c64845d6126c9'; | |
const usdcVault = '0xBdF726e6eBA19342478415aF22ec097efc94258f'; // With USDC | |
const relayer = '0xC5fdf4076b8F3A5357c5E395ab970B5B54098Fef'; // With ETH | |
const nonce = await usdcContract.methods.nonces(usdcVault).call(); | |
const value = 1000000000; | |
const MAX_UINT256 = "0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"; | |
// Encode data | |
const data = web3.eth.abi.encodeParameters(['bytes32','address','address','uint256','uint256','uint256'], [PERMIT_TYPEHASH, usdcVault, relayer, value, nonce, MAX_UINT256]); | |
// Create digest | |
const digest2 = web3.utils.soliditySha3('0x1901', DOMAIN_SEPARATOR, web3.utils.keccak256(data)); | |
// Sign | |
const sig = ecSign(digest2, usdcVaultKey); | |
// Call Permit | |
const tx = await usdcContractEthers.permit(usdcVault, relayer, value, MAX_UINT256, sig.v, sig.r, sig.s); | |
/* Helper functions */ | |
const strip0x = (string) => { | |
return string.replace(/^0x/, ""); | |
}; | |
const bufferFromHexString = (string) => { | |
return Buffer.from(strip0x(string), "hex"); | |
}; | |
const hexStringFromBuffer = (buffer) => { | |
return "0x" + buffer.toString("hex"); | |
}; | |
const ecSign = (digest, key) => { | |
const sig = ecsign(bufferFromHexString(digest), bufferFromHexString(key)); | |
return { v: sig.v, r: hexStringFromBuffer(sig.r), s: hexStringFromBuffer(sig.s)} | |
;} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment