Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: permit types and naming #148

Merged
merged 1 commit into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions static/scripts/rewards/render-transaction/insert-table-data.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { BigNumber, ethers } from "ethers";
import { app } from ".";
import { NftMint, Permit } from "./tx-type";
import { Erc721Permit, Erc20Permit } from "./tx-type";

export const shortenAddress = (address: string): string => {
return `${address.slice(0, 10)}...${address.slice(-8)}`;
};

export function insertPermitTableData(
permit: Permit,
export function insertErc20PermitTableData(
permit: Erc20Permit,
table: Element,
treasury: { balance: BigNumber; allowance: BigNumber; decimals: number; symbol: string },
): Element {
Expand All @@ -27,19 +27,19 @@ export function insertPermitTableData(
return requestedAmountElement;
}

export function insertNftTableData(nftMint: NftMint, table: Element): Element {
export function insertErc721PermitTableData(permit: Erc721Permit, table: Element): Element {
const requestedAmountElement = document.getElementById("rewardAmount") as Element;
renderToFields(nftMint.request.beneficiary, app.currentExplorerUrl);
renderTokenFields(nftMint.nftAddress, app.currentExplorerUrl);
const { GITHUB_REPOSITORY_NAME, GITHUB_CONTRIBUTION_TYPE, GITHUB_ISSUE_ID, GITHUB_ORGANIZATION_NAME, GITHUB_USERNAME } = nftMint.nftMetadata;
renderToFields(permit.request.beneficiary, app.currentExplorerUrl);
renderTokenFields(permit.nftAddress, app.currentExplorerUrl);
const { GITHUB_REPOSITORY_NAME, GITHUB_CONTRIBUTION_TYPE, GITHUB_ISSUE_ID, GITHUB_ORGANIZATION_NAME, GITHUB_USERNAME } = permit.nftMetadata;
renderDetailsFields([
{
name: "NFT address",
value: `<a target="_blank" rel="noopener noreferrer" href="${app.currentExplorerUrl}/address/${nftMint.nftAddress}">${nftMint.nftAddress}</a>`,
value: `<a target="_blank" rel="noopener noreferrer" href="${app.currentExplorerUrl}/address/${permit.nftAddress}">${permit.nftAddress}</a>`,
},
{
name: "Expiry",
value: nftMint.request.deadline.lte(Number.MAX_SAFE_INTEGER.toString()) ? new Date(nftMint.request.deadline.toNumber()).toLocaleString() : undefined,
value: permit.request.deadline.lte(Number.MAX_SAFE_INTEGER.toString()) ? new Date(permit.request.deadline.toNumber()).toLocaleString() : undefined,
},
{
name: "GitHub Organization",
Expand Down
18 changes: 9 additions & 9 deletions static/scripts/rewards/render-transaction/render-transaction.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { app } from "./index";
import { insertNftTableData, insertPermitTableData } from "./insert-table-data";
import { insertErc721PermitTableData, insertErc20PermitTableData } from "./insert-table-data";
import { renderEnsName } from "./render-ens-name";
import { renderNftSymbol, renderTokenSymbol } from "./render-token-symbol";
import { setClaimMessage } from "./set-claim-message";
Expand All @@ -9,8 +9,8 @@ import { Value } from "@sinclair/typebox/value";
import { Type } from "@sinclair/typebox";
import { ClaimTx } from "./tx-type";
import { handleNetwork } from "../web3/wallet";
import { mintNftHandler } from "../web3/nft-mint";
import { claimPermitHandler, fetchTreasury, generateInvalidatePermitAdminControl } from "../web3/permit";
import { claimErc721PermitHandler } from "../web3/erc721-permit";
import { claimErc20PermitHandler, fetchTreasury, generateInvalidatePermitAdminControl } from "../web3/erc20-permit";
import { removeAllEventListeners } from "./utils";

export async function init() {
Expand Down Expand Up @@ -95,11 +95,11 @@ export async function renderTransaction(nextTx?: boolean): Promise<Success> {

handleNetwork(app.currentTx.networkId);

if (app.currentTx.type === "permit") {
if (app.currentTx.type === "erc20-permit") {
const treasury = await fetchTreasury(app.currentTx);

// insert tx data into table
const requestedAmountElement = insertPermitTableData(app.currentTx, table, treasury);
const requestedAmountElement = insertErc20PermitTableData(app.currentTx, table, treasury);
table.setAttribute(`data-claim`, "ok");

renderTokenSymbol({
Expand All @@ -117,9 +117,9 @@ export async function renderTransaction(nextTx?: boolean): Promise<Success> {

generateInvalidatePermitAdminControl(app.currentTx);

claimButton.element.addEventListener("click", claimPermitHandler(app.currentTx));
} else if (app.currentTx.type === "nft-mint") {
const requestedAmountElement = insertNftTableData(app.currentTx, table);
claimButton.element.addEventListener("click", claimErc20PermitHandler(app.currentTx));
} else if (app.currentTx.type === "erc721-permit") {
const requestedAmountElement = insertErc721PermitTableData(app.currentTx, table);
table.setAttribute(`data-claim`, "ok");

renderNftSymbol({
Expand All @@ -133,7 +133,7 @@ export async function renderTransaction(nextTx?: boolean): Promise<Success> {
const toElement = document.getElementById(`rewardRecipient`) as Element;
renderEnsName({ element: toElement, address: app.currentTx.request.beneficiary });

claimButton.element.addEventListener("click", mintNftHandler(app.currentTx));
claimButton.element.addEventListener("click", claimErc721PermitHandler(app.currentTx));
}

return true;
Expand Down
14 changes: 7 additions & 7 deletions static/scripts/rewards/render-transaction/tx-type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ const TSignature = T.Transform(T.RegExp(/^0x[a-fA-F0-9]+$/))
.Decode(value => value.toLowerCase())
.Encode(value => value);

const Permit = T.Object({
type: T.Literal("permit"),
const Erc20Permit = T.Object({
type: T.Literal("erc20-permit"),
permit: T.Object({
permitted: T.Object({
token: TAddress,
Expand All @@ -36,10 +36,10 @@ const Permit = T.Object({
networkId: TNetworkId,
});

export type Permit = StaticDecode<typeof Permit>;
export type Erc20Permit = StaticDecode<typeof Erc20Permit>;

const NftMint = T.Object({
type: T.Literal("nft-mint"),
const Erc721Permit = T.Object({
type: T.Literal("erc721-permit"),
request: T.Object({
beneficiary: TAddress,
deadline: TBigNumber,
Expand All @@ -59,8 +59,8 @@ const NftMint = T.Object({
signature: TSignature,
});

export type NftMint = StaticDecode<typeof NftMint>;
export type Erc721Permit = StaticDecode<typeof Erc721Permit>;

export const ClaimTx = T.Union([Permit, NftMint]);
export const ClaimTx = T.Union([Erc20Permit, Erc721Permit]);

export type ClaimTx = StaticDecode<typeof ClaimTx>;
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import { BigNumber, BigNumberish, ethers } from "ethers";
import { erc20Abi, permit2Abi } from "../abis";
import { networkRpcs, permit2Address } from "../constants";
import { Permit } from "../render-transaction/tx-type";
import { Erc20Permit } from "../render-transaction/tx-type";
import { toaster, resetClaimButton, errorToast, loadingClaimButton, claimButton } from "../toaster";
import { shortenAddress } from "../render-transaction/insert-table-data";
import { renderTransaction } from "../render-transaction/render-transaction";
import { connectWallet } from "./wallet";
import invalidateButton from "../invalidate-component";

export async function fetchTreasury(permit: Permit): Promise<{ balance: BigNumber; allowance: BigNumber; decimals: number; symbol: string }> {
export async function fetchTreasury(permit: Erc20Permit): Promise<{ balance: BigNumber; allowance: BigNumber; decimals: number; symbol: string }> {
try {
const provider = new ethers.providers.JsonRpcProvider(networkRpcs[permit.networkId]);
const tokenAddress = permit.permit.permitted.token;
Expand All @@ -23,7 +22,7 @@ export async function fetchTreasury(permit: Permit): Promise<{ balance: BigNumbe
}
}

export function claimPermitHandler(permit: Permit) {
export function claimErc20PermitHandler(permit: Erc20Permit) {
return async function handler() {
try {
const signer = await connectWallet();
Expand Down Expand Up @@ -53,7 +52,7 @@ export function claimPermitHandler(permit: Permit) {
};
}

export async function checkPermitClaimable(permit: Permit, signer: ethers.providers.JsonRpcSigner | null) {
export async function checkPermitClaimable(permit: Erc20Permit, signer: ethers.providers.JsonRpcSigner | null) {
const claimed = await isNonceClaimed(permit);
if (claimed) {
toaster.create("error", `This reward has already been claimed or invalidated.`);
Expand Down Expand Up @@ -91,7 +90,7 @@ export async function checkPermitClaimable(permit: Permit, signer: ethers.provid
return true;
}

export async function generateInvalidatePermitAdminControl(permit: Permit) {
export async function generateInvalidatePermitAdminControl(permit: Erc20Permit) {
const signer = await connectWallet();
if (!signer) {
return;
Expand Down Expand Up @@ -126,7 +125,7 @@ export async function generateInvalidatePermitAdminControl(permit: Permit) {
});
}

export async function isNonceClaimed(permit: Permit): Promise<boolean> {
export async function isNonceClaimed(permit: Erc20Permit): Promise<boolean> {
const provider = new ethers.providers.JsonRpcProvider(networkRpcs[permit.networkId]);
const permit2Contract = new ethers.Contract(permit2Address, permit2Abi, provider);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
import { ethers } from "ethers";
import { NftMint } from "../render-transaction/tx-type";
import { Erc721Permit } from "../render-transaction/tx-type";
import { claimButton, errorToast, loadingClaimButton, resetClaimButton, toaster } from "../toaster";
import { connectWallet } from "./wallet";
import { nftRewardAbi } from "../abis/nftRewardAbi";
import { TransactionResponse } from "@ethersproject/providers";
import { networkRpcs } from "../constants";
import { renderTransaction } from "../render-transaction/render-transaction";

export function mintNftHandler(nftMint: NftMint) {
export function claimErc721PermitHandler(permit: Erc721Permit) {
return async function claimButtonHandler() {
const signer = await connectWallet();
if (!signer) {
return;
}

if ((await signer.getAddress()).toLowerCase() !== nftMint.request.beneficiary) {
if ((await signer.getAddress()).toLowerCase() !== permit.request.beneficiary) {
toaster.create("warning", `This NFT is not for you.`);
resetClaimButton();
return;
}

if (nftMint.request.deadline.lt(Math.floor(Date.now() / 1000))) {
if (permit.request.deadline.lt(Math.floor(Date.now() / 1000))) {
toaster.create("error", `This NFT has expired.`);
resetClaimButton();
return;
}

const reedemed = await isNonceRedeemed(nftMint);
const reedemed = await isNonceRedeemed(permit);
if (reedemed) {
toaster.create("error", `This NFT has already been redeemed.`);
resetClaimButton();
Expand All @@ -35,9 +35,9 @@ export function mintNftHandler(nftMint: NftMint) {

loadingClaimButton();
try {
const nftContract = new ethers.Contract(nftMint.nftAddress, nftRewardAbi, signer);
const nftContract = new ethers.Contract(permit.nftAddress, nftRewardAbi, signer);

const tx: TransactionResponse = await nftContract.safeMint(nftMint.request, nftMint.signature);
const tx: TransactionResponse = await nftContract.safeMint(permit.request, permit.signature);
toaster.create("info", `Transaction sent. Waiting for confirmation...`);
const receipt = await tx.wait();
toaster.create("success", `Claim Complete: ${receipt.transactionHash}`);
Expand All @@ -53,7 +53,7 @@ export function mintNftHandler(nftMint: NftMint) {
};
}

export async function isNonceRedeemed(nftMint: NftMint): Promise<boolean> {
export async function isNonceRedeemed(nftMint: Erc721Permit): Promise<boolean> {
const provider = new ethers.providers.JsonRpcProvider(networkRpcs[nftMint.networkId]);
const nftContract = new ethers.Contract(nftMint.nftAddress, nftRewardAbi, provider);
return nftContract.nonceRedeemed(nftMint.request.nonce);
Expand Down
Loading