|
1 | 1 | import { JsonRpcSigner, TransactionResponse } from "@ethersproject/providers";
|
2 | 2 | import { BigNumber, BigNumberish, Contract, ethers } from "ethers";
|
3 | 3 | import { erc20Abi, permit2Abi } from "../abis";
|
4 |
| -import { app, AppState } from "../app-state"; |
| 4 | +import { AppState, app } from "../app-state"; |
5 | 5 | import { permit2Address } from "../constants";
|
6 | 6 | import { supabase } from "../render-transaction/read-claim-data-from-url";
|
7 | 7 | import { Erc20Permit, Erc721Permit } from "../render-transaction/tx-type";
|
8 |
| -import { buttonController, errorToast, getMakeClaimButton, MetaMaskError, toaster } from "../toaster"; |
| 8 | +import { MetaMaskError, buttonController, errorToast, getMakeClaimButton, toaster } from "../toaster"; |
9 | 9 |
|
10 | 10 | export async function fetchTreasury(
|
11 | 11 | permit: Erc20Permit | Erc721Permit
|
@@ -138,7 +138,7 @@ export async function checkPermitClaimable(app: AppState): Promise<boolean> {
|
138 | 138 | }
|
139 | 139 |
|
140 | 140 | if (isClaimed) {
|
141 |
| - toaster.create("error", `Your reward for this task has already been claimed or invalidated.`); |
| 141 | + toaster.create("error", `Your reward for this task has already been claimed.`); |
142 | 142 | buttonController.showViewClaim();
|
143 | 143 | return false;
|
144 | 144 | }
|
@@ -186,46 +186,67 @@ export async function checkPermitClaimable(app: AppState): Promise<boolean> {
|
186 | 186 | return true;
|
187 | 187 | }
|
188 | 188 |
|
189 |
| -export async function generateInvalidatePermitAdminControl(app: AppState) { |
| 189 | +export async function checkRenderMakeClaimControl(app: AppState) { |
190 | 190 | try {
|
191 | 191 | const address = await app.signer.getAddress();
|
192 | 192 | const user = address.toLowerCase();
|
193 | 193 |
|
194 | 194 | if (app.reward) {
|
195 |
| - const owner = app.reward.owner.toLowerCase(); |
196 |
| - if (owner !== user) { |
| 195 | + const beneficiary = app.reward.transferDetails.to.toLowerCase(); |
| 196 | + if (beneficiary !== user) { |
| 197 | + buttonController.hideMakeClaim(); |
197 | 198 | return;
|
198 | 199 | }
|
199 | 200 | }
|
200 | 201 | } catch (error) {
|
201 | 202 | console.error("Error getting address from signer");
|
202 | 203 | console.error(error);
|
203 | 204 | }
|
| 205 | + buttonController.showMakeClaim(); |
| 206 | +} |
204 | 207 |
|
205 |
| - const invalidateButton = document.getElementById("invalidator") as HTMLDivElement; |
206 |
| - buttonController.showInvalidator(); |
207 |
| - invalidateButton.addEventListener("click", async function invalidateButtonClickHandler() { |
208 |
| - try { |
209 |
| - const isClaimed = await isNonceClaimed(app); |
210 |
| - if (isClaimed) { |
211 |
| - toaster.create("error", `This reward has already been claimed or invalidated.`); |
| 208 | +export async function checkRenderInvalidatePermitAdminControl(app: AppState) { |
| 209 | + try { |
| 210 | + const address = await app.signer.getAddress(); |
| 211 | + const user = address.toLowerCase(); |
| 212 | + |
| 213 | + if (app.reward) { |
| 214 | + const owner = app.reward.owner.toLowerCase(); |
| 215 | + if (owner !== user) { |
212 | 216 | buttonController.hideInvalidator();
|
213 | 217 | return;
|
214 | 218 | }
|
215 |
| - await invalidateNonce(app.signer, app.reward.permit.nonce); |
216 |
| - } catch (error: unknown) { |
217 |
| - if (error instanceof Error) { |
218 |
| - const e = error as unknown as MetaMaskError; |
219 |
| - console.error(e); |
220 |
| - errorToast(e, e.reason); |
221 |
| - return; |
222 |
| - } |
223 | 219 | }
|
224 |
| - toaster.create("info", "Nonce invalidation transaction sent"); |
225 |
| - buttonController.hideInvalidator(); |
226 |
| - }); |
| 220 | + } catch (error) { |
| 221 | + console.error("Error getting address from signer"); |
| 222 | + console.error(error); |
| 223 | + } |
| 224 | + buttonController.showInvalidator(); |
227 | 225 | }
|
228 | 226 |
|
| 227 | +const invalidateButton = document.getElementById("invalidator") as HTMLDivElement; |
| 228 | + |
| 229 | +invalidateButton.addEventListener("click", async function invalidateButtonClickHandler() { |
| 230 | + try { |
| 231 | + const isClaimed = await isNonceClaimed(app); |
| 232 | + if (isClaimed) { |
| 233 | + toaster.create("error", `This reward has already been claimed or invalidated.`); |
| 234 | + buttonController.hideInvalidator(); |
| 235 | + return; |
| 236 | + } |
| 237 | + await invalidateNonce(app.signer, app.reward.permit.nonce); |
| 238 | + } catch (error: unknown) { |
| 239 | + if (error instanceof Error) { |
| 240 | + const e = error as unknown as MetaMaskError; |
| 241 | + console.error(e); |
| 242 | + errorToast(e, e.reason); |
| 243 | + return; |
| 244 | + } |
| 245 | + } |
| 246 | + toaster.create("info", "Nonce invalidation transaction sent"); |
| 247 | + buttonController.hideInvalidator(); |
| 248 | +}); |
| 249 | + |
229 | 250 | //mimics https://github.com/Uniswap/permit2/blob/a7cd186948b44f9096a35035226d7d70b9e24eaf/src/SignatureTransfer.sol#L150
|
230 | 251 | export async function isNonceClaimed(app: AppState): Promise<boolean> {
|
231 | 252 | const provider = app.provider;
|
|
0 commit comments