-
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 #94 from aragon/feat/lock-to-veto-permit
Lock to veto permit
- Loading branch information
Showing
9 changed files
with
785 additions
and
32 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
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
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,93 @@ | ||
import { useEffect } from "react"; | ||
import { | ||
useReadContract, | ||
useWaitForTransactionReceipt, | ||
useWriteContract, | ||
} from "wagmi"; | ||
import { OptimisticTokenVotingPluginAbi } from "../artifacts/OptimisticTokenVotingPlugin.sol"; | ||
import { AlertContextProps, useAlertContext } from "@/context/AlertContext"; | ||
import { useRouter } from "next/router"; | ||
import { PUB_CHAIN, PUB_LOCK_TO_VOTE_PLUGIN_ADDRESS } from "@/constants"; | ||
|
||
export function useProposalExecute(proposalId: string) { | ||
const { reload } = useRouter(); | ||
const { addAlert } = useAlertContext() as AlertContextProps; | ||
|
||
const { | ||
data: canExecute, | ||
isError: isCanVoteError, | ||
isLoading: isCanVoteLoading, | ||
} = useReadContract({ | ||
address: PUB_LOCK_TO_VOTE_PLUGIN_ADDRESS, | ||
abi: OptimisticTokenVotingPluginAbi, | ||
chainId: PUB_CHAIN.id, | ||
functionName: "canExecute", | ||
args: [proposalId], | ||
}); | ||
const { | ||
writeContract: executeWrite, | ||
data: executeTxHash, | ||
error: executingError, | ||
status: executingStatus, | ||
} = useWriteContract(); | ||
const { isLoading: isConfirming, isSuccess: isConfirmed } = | ||
useWaitForTransactionReceipt({ hash: executeTxHash }); | ||
|
||
const executeProposal = () => { | ||
if (!canExecute) return; | ||
|
||
executeWrite({ | ||
chainId: PUB_CHAIN.id, | ||
abi: OptimisticTokenVotingPluginAbi, | ||
address: PUB_LOCK_TO_VOTE_PLUGIN_ADDRESS, | ||
functionName: "execute", | ||
args: [proposalId], | ||
}); | ||
}; | ||
|
||
useEffect(() => { | ||
if (executingStatus === "idle" || executingStatus === "pending") return; | ||
else if (executingStatus === "error") { | ||
if (executingError?.message?.startsWith("User rejected the request")) { | ||
addAlert("Transaction rejected by the user", { | ||
timeout: 4 * 1000, | ||
}); | ||
} else { | ||
console.error(executingError); | ||
addAlert("Could not execute the proposal", { | ||
type: "error", | ||
description: | ||
"The proposal may contain actions with invalid operations", | ||
}); | ||
} | ||
return; | ||
} | ||
|
||
// success | ||
if (!executeTxHash) return; | ||
else if (isConfirming) { | ||
addAlert("Proposal submitted", { | ||
description: "Waiting for the transaction to be validated", | ||
type: "info", | ||
txHash: executeTxHash, | ||
}); | ||
return; | ||
} else if (!isConfirmed) return; | ||
|
||
addAlert("Proposal executed", { | ||
description: "The transaction has been validated", | ||
type: "success", | ||
txHash: executeTxHash, | ||
}); | ||
|
||
setTimeout(() => reload(), 1000 * 2); | ||
}, [executingStatus, executeTxHash, isConfirming, isConfirmed]); | ||
|
||
return { | ||
executeProposal, | ||
canExecute: | ||
!isCanVoteError && !isCanVoteLoading && !isConfirmed && !!canExecute, | ||
isConfirming, | ||
isConfirmed, | ||
}; | ||
} |
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
Oops, something went wrong.