Skip to content

Commit 4f6e165

Browse files
committed
fix: refinements around login switching and permit claim or invalidator buttons
1 parent 26321d7 commit 4f6e165

File tree

6 files changed

+56
-41
lines changed

6 files changed

+56
-41
lines changed

static/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@
162162
<div>Void</div>
163163
<svg xmlns="http://www.w3.org/2000/svg" height="24" viewBox="0 -960 960 960" width="24">
164164
<path
165-
d="M256-213.847 213.847-256l224-224-224-224L256-746.153l224 224 224-table46.153-704l-224 224 224 224L704-213.847l-224-224-224 224Z"
165+
d="M256-213.847 213.847-256l224-224-224-224L256-746.153l224 224 224-224L746.153-704l-224 224 224 224L704-213.847l-224-224-224 224Z"
166166
/>
167167
</svg>
168168
</button>

static/scripts/rewards/render-transaction/read-claim-data-from-url.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@ import { Value } from "@sinclair/typebox/value";
33
import { createClient } from "@supabase/supabase-js";
44
import { AppState, app } from "../app-state";
55
import { useFastestRpc } from "../rpc-optimization/get-optimal-provider";
6+
import { buttonController, toaster } from "../toaster";
67
import { connectWallet } from "../web3/connect-wallet";
8+
import { checkRenderInvalidatePermitAdminControl, checkRenderMakeClaimControl } from "../web3/erc20-permit";
79
import { verifyCurrentNetwork } from "../web3/verify-current-network";
810
import { claimRewardsPagination } from "./claim-rewards-pagination";
911
import { renderTransaction } from "./render-transaction";
1012
import { setClaimMessage } from "./set-claim-message";
1113
import { RewardPermit, claimTxT } from "./tx-type";
12-
import { buttonController, toaster } from "../toaster";
1314

1415
declare const SUPABASE_URL: string;
1516
declare const SUPABASE_ANON_KEY: string;
@@ -33,7 +34,10 @@ export async function readClaimDataFromUrl(app: AppState) {
3334
app.provider = await useFastestRpc(app);
3435
if (window.ethereum) {
3536
app.signer = await connectWallet().catch(console.error);
36-
window.ethereum.on("accountsChanged", () => buttonController.showMakeClaim());
37+
window.ethereum.on("accountsChanged", () => {
38+
checkRenderMakeClaimControl(app).catch(console.error);
39+
checkRenderInvalidatePermitAdminControl(app).catch(console.error);
40+
});
3741
} else {
3842
buttonController.hideAll();
3943
toaster.create("info", "Please use a web3 enabled browser to collect this reward.");

static/scripts/rewards/render-transaction/render-transaction.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { app } from "../app-state";
22
import { networkExplorers } from "../constants";
33
import { buttonController, getMakeClaimButton, viewClaimButton } from "../toaster";
4-
import { claimErc20PermitHandlerWrapper, fetchTreasury, generateInvalidatePermitAdminControl } from "../web3/erc20-permit";
4+
import { checkRenderInvalidatePermitAdminControl, claimErc20PermitHandlerWrapper, fetchTreasury } from "../web3/erc20-permit";
55
import { claimErc721PermitHandler } from "../web3/erc721-permit";
66
import { verifyCurrentNetwork } from "../web3/verify-current-network";
77
import { insertErc20PermitTableData, insertErc721PermitTableData } from "./insert-table-data";
@@ -47,7 +47,7 @@ export async function renderTransaction(): Promise<Success> {
4747
renderEnsName({ element: toElement, address: app.reward.transferDetails.to }).catch(console.error);
4848

4949
if (app.provider) {
50-
generateInvalidatePermitAdminControl(app).catch(console.error);
50+
checkRenderInvalidatePermitAdminControl(app).catch(console.error);
5151
}
5252

5353
if (app.claimTxs[app.reward.permit.nonce.toString()] !== undefined) {

static/scripts/rewards/web3/erc20-permit.ts

Lines changed: 45 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import { JsonRpcSigner, TransactionResponse } from "@ethersproject/providers";
22
import { BigNumber, BigNumberish, Contract, ethers } from "ethers";
33
import { erc20Abi, permit2Abi } from "../abis";
4-
import { app, AppState } from "../app-state";
4+
import { AppState, app } from "../app-state";
55
import { permit2Address } from "../constants";
66
import { supabase } from "../render-transaction/read-claim-data-from-url";
77
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";
99

1010
export async function fetchTreasury(
1111
permit: Erc20Permit | Erc721Permit
@@ -138,7 +138,7 @@ export async function checkPermitClaimable(app: AppState): Promise<boolean> {
138138
}
139139

140140
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.`);
142142
buttonController.showViewClaim();
143143
return false;
144144
}
@@ -186,46 +186,67 @@ export async function checkPermitClaimable(app: AppState): Promise<boolean> {
186186
return true;
187187
}
188188

189-
export async function generateInvalidatePermitAdminControl(app: AppState) {
189+
export async function checkRenderMakeClaimControl(app: AppState) {
190190
try {
191191
const address = await app.signer.getAddress();
192192
const user = address.toLowerCase();
193193

194194
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();
197198
return;
198199
}
199200
}
200201
} catch (error) {
201202
console.error("Error getting address from signer");
202203
console.error(error);
203204
}
205+
buttonController.showMakeClaim();
206+
}
204207

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) {
212216
buttonController.hideInvalidator();
213217
return;
214218
}
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-
}
223219
}
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();
227225
}
228226

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+
229250
//mimics https://github.com/Uniswap/permit2/blob/a7cd186948b44f9096a35035226d7d70b9e24eaf/src/SignatureTransfer.sol#L150
230251
export async function isNonceClaimed(app: AppState): Promise<boolean> {
231252
const provider = app.provider;

static/scripts/rewards/web3/handle-if-on-correct-network.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import { buttonController } from "../toaster";
22

33
export function handleIfOnCorrectNetwork(currentNetworkId: number, desiredNetworkId: number) {
44
if (desiredNetworkId === currentNetworkId) {
5-
buttonController.showInvalidator();
5+
buttonController.showMakeClaim();
66
} else {
7-
buttonController.hideInvalidator();
7+
buttonController.hideMakeClaim();
88
}
99
}

static/styles/rewards/claim-table.css

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@ table #controls {
3131
table #controls > button {
3232
flex: 1;
3333
padding: 0 24px;
34-
justify-content: center;
35-
align-items: center;
3634
}
3735
table aside {
3836
text-transform: uppercase;
@@ -310,8 +308,6 @@ thead {
310308
transition: 0.25s all ease-in-out;
311309
border: 1px solid var(--border-color);
312310
transform: translate(-50%, -50%);
313-
/* padding: 0 16px; */
314-
/* border-radius: 3px; */
315311
width: 100%;
316312
max-width: 563px;
317313
overflow: hidden;
@@ -324,15 +320,9 @@ table * {
324320
@keyframes thead-fade-in {
325321
0% {
326322
opacity: 0;
327-
/* transform: translate(-50%, -50%) scale(1.1); */
328-
/* filter: blur(16px); */
329-
/* background: #fff */
330323
}
331324
100% {
332325
opacity: 1;
333-
/* transform: translate(-50%, -50%) scale(1); */
334-
/* filter: blur(0px); */
335-
/* background: unset */
336326
}
337327
}
338328

0 commit comments

Comments
 (0)