-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #90 from aragon/feat/lock-to-veto
Adding the Lock To Vote plugin
- Loading branch information
Showing
27 changed files
with
3,913 additions
and
0 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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,109 @@ | ||
import { useEffect } from "react"; | ||
import { | ||
useReadContracts, | ||
useSignTypedData, | ||
useAccount, | ||
} from "wagmi"; | ||
import { hexToSignature, Address } from "viem"; | ||
import { ERC20PermitAbi } from "@/artifacts/ERC20Permit.sol"; | ||
import { useAlertContext, AlertContextProps } from "@/context/AlertContext"; | ||
import { PUB_CHAIN, PUB_TOKEN_ADDRESS } from "@/constants"; | ||
|
||
export function usePermit() { | ||
const { addAlert } = useAlertContext() as AlertContextProps; | ||
|
||
const account_address = useAccount().address!; | ||
const erc20Contract = { | ||
address: PUB_TOKEN_ADDRESS, | ||
abi: ERC20PermitAbi, | ||
}; | ||
const { data: erc20data, refetch: erc20refetch } = useReadContracts({ | ||
contracts: [{ | ||
...erc20Contract, | ||
functionName: "nonces", | ||
args: [account_address], | ||
},{ | ||
...erc20Contract, | ||
functionName: "name", | ||
},{ | ||
...erc20Contract, | ||
functionName: "version", | ||
}] | ||
}); | ||
const [nonceResult, nameResult, versionResult] = erc20data || []; | ||
|
||
const { signTypedDataAsync: permitSign, status: permitSignStatus, error: permitSignError } = useSignTypedData(); | ||
|
||
useEffect(() => { | ||
switch (permitSignStatus) { | ||
case "idle": | ||
case "pending": | ||
return; | ||
case "error": | ||
if (permitSignError?.message?.startsWith("User rejected the request")) { | ||
addAlert("Transaction rejected by the user", { | ||
timeout: 4 * 1000, | ||
}); | ||
} else { | ||
addAlert("Could not sign the permit", { type: "error", timeout: 1500 }); | ||
} | ||
return; | ||
case "success": | ||
addAlert("Permit signed", { type: "success", timeout: 1500 }); | ||
return; | ||
} | ||
}, [permitSignStatus]); | ||
|
||
const signPermit = async (dest: Address, value: BigInt, deadline: BigInt = BigInt(Math.floor(Date.now() / 1000) + 60 * 60)) => { | ||
if (!nonceResult || !nameResult || !versionResult) return; | ||
|
||
const nonce = BigInt(Number(nonceResult?.result)); | ||
const erc20_name = String(nameResult?.result); | ||
/* We assume 1 if permit version is not specified */ | ||
const versionFromContract = String(versionResult?.result ?? '1'); | ||
|
||
const domain = { | ||
chainId: PUB_CHAIN.id, | ||
name: erc20_name, | ||
version: versionFromContract, | ||
verifyingContract: PUB_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 message = { | ||
owner: account_address, | ||
spender: dest, | ||
value, | ||
nonce, | ||
deadline, | ||
}; | ||
|
||
try { | ||
let sig = await permitSign({ | ||
account: account_address, | ||
types, | ||
domain, | ||
primaryType: 'Permit', | ||
message, | ||
}); | ||
|
||
return hexToSignature(sig); | ||
} catch (e) { | ||
return; | ||
} | ||
}; | ||
|
||
return { | ||
refetchPermitData: erc20refetch, | ||
signPermit, | ||
}; | ||
} |
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
Oops, something went wrong.