Skip to content

Commit

Permalink
feat!: update permit schema (#224)
Browse files Browse the repository at this point in the history
* feat!: updated permit type to match package

* chore: removed unused file for permits types

* chore: updated ubiquibot permit config version

* chore: fixed reward mint structure for erc721 permits
  • Loading branch information
gentlementlegen authored Apr 20, 2024
1 parent 941d461 commit efff1d7
Show file tree
Hide file tree
Showing 10 changed files with 85 additions and 5,571 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"@ethersproject/providers": "^5.7.2",
"@sinclair/typebox": "^0.32.15",
"@supabase/supabase-js": "^2.39.8",
"@ubiquibot/permit-generation": "1.2.2",
"@uniswap/permit2-sdk": "^1.2.0",
"axios": "^1.6.7",
"dotenv": "^16.4.4",
Expand Down
10 changes: 5 additions & 5 deletions static/scripts/rewards/app-state.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { JsonRpcProvider, JsonRpcSigner } from "@ethersproject/providers";
import { Permit } from "@ubiquibot/permit-generation/types";
import { networkExplorers } from "./constants";
import { RewardPermit } from "./render-transaction/tx-type";

export class AppState {
public claims: RewardPermit[] = [];
public claims: Permit[] = [];
public claimTxs: Record<string, string> = {};
private _provider!: JsonRpcProvider;
private _currentIndex = 0;
Expand Down Expand Up @@ -33,7 +33,7 @@ export class AppState {
return this._currentIndex;
}

get reward(): RewardPermit {
get reward(): Permit {
return this.rewardIndex < this.claims.length ? this.claims[this.rewardIndex] : this.claims[0];
}

Expand All @@ -48,12 +48,12 @@ export class AppState {
return networkExplorers[this.reward.networkId] || "https://etherscan.io";
}

nextPermit(): RewardPermit | null {
nextPermit(): Permit | null {
this._currentIndex = Math.min(this.claims.length - 1, this.rewardIndex + 1);
return this.reward;
}

previousPermit(): RewardPermit | null {
previousPermit(): Permit | null {
this._currentIndex = Math.max(0, this._currentIndex - 1);
return this.reward;
}
Expand Down
26 changes: 13 additions & 13 deletions static/scripts/rewards/render-transaction/insert-table-data.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ERC20Permit, ERC721Permit } from "@ubiquibot/permit-generation/types";
import { BigNumber, ethers } from "ethers";
import { AppState, app } from "../app-state";
import { Erc20Permit, Erc721Permit } from "./tx-type";
import { app, AppState } from "../app-state";

function shortenAddress(address: string): string {
return `${address.slice(0, 10)}...${address.slice(-8)}`;
Expand All @@ -11,16 +11,16 @@ export function insertErc20PermitTableData(
table: Element,
treasury: { balance: BigNumber; allowance: BigNumber; decimals: number; symbol: string }
): Element {
const reward = app.reward as Erc20Permit;
const reward = app.reward as ERC20Permit;
const requestedAmountElement = document.getElementById("rewardAmount") as Element;
renderToFields(reward.transferDetails.to, app.currentExplorerUrl);
renderTokenFields(reward.permit.permitted.token, app.currentExplorerUrl);
renderToFields(reward.beneficiary, app.currentExplorerUrl);
renderTokenFields(reward.tokenAddress, app.currentExplorerUrl);
renderDetailsFields([
{ name: "From", value: `<a target="_blank" rel="noopener noreferrer" href="${app.currentExplorerUrl}/address/${reward.owner}">${reward.owner}</a>` },
{
name: "Expiry",
value: (() => {
const deadline = BigNumber.isBigNumber(reward.permit.deadline) ? reward.permit.deadline : BigNumber.from(reward.permit.deadline);
const deadline = BigNumber.isBigNumber(reward.deadline) ? reward.deadline : BigNumber.from(reward.deadline);
return deadline.lte(Number.MAX_SAFE_INTEGER.toString()) ? new Date(deadline.toNumber()).toLocaleString() : undefined;
})(),
},
Expand All @@ -31,19 +31,19 @@ export function insertErc20PermitTableData(
return requestedAmountElement;
}

export function insertErc721PermitTableData(reward: Erc721Permit, table: Element): Element {
export function insertErc721PermitTableData(reward: ERC721Permit, table: Element): Element {
const requestedAmountElement = document.getElementById("rewardAmount") as Element;
renderToFields(reward.transferDetails.to, app.currentExplorerUrl);
renderTokenFields(reward.permit.permitted.token, app.currentExplorerUrl);
const { GITHUB_REPOSITORY_NAME, GITHUB_CONTRIBUTION_TYPE, GITHUB_ISSUE_ID, GITHUB_ORGANIZATION_NAME, GITHUB_USERNAME } = reward.nftMetadata;
renderToFields(reward.beneficiary, app.currentExplorerUrl);
renderTokenFields(reward.tokenAddress, app.currentExplorerUrl);
const { GITHUB_REPOSITORY_NAME, GITHUB_CONTRIBUTION_TYPE, GITHUB_ISSUE_ID, GITHUB_ORGANIZATION_NAME, GITHUB_USERNAME } = reward.erc721Request?.metadata || {};
renderDetailsFields([
{
name: "NFT address",
value: `<a target="_blank" rel="noopener noreferrer" href="${app.currentExplorerUrl}/address/${reward.permit.permitted.token}">${reward.permit.permitted.token}</a>`,
value: `<a target="_blank" rel="noopener noreferrer" href="${app.currentExplorerUrl}/address/${reward.tokenAddress}">${reward.tokenAddress}</a>`,
},
{
name: "Expiry",
value: reward.permit.deadline.lte(Number.MAX_SAFE_INTEGER.toString()) ? new Date(reward.permit.deadline.toNumber()).toLocaleString() : undefined,
value: BigNumber.from(reward.deadline).lte(Number.MAX_SAFE_INTEGER.toString()) ? new Date(Number(reward.deadline)).toLocaleString() : undefined,
},
{
name: "GitHub Organization",
Expand All @@ -61,7 +61,7 @@ export function insertErc721PermitTableData(reward: Erc721Permit, table: Element
name: "GitHub Username",
value: `<a target="_blank" rel="noopener noreferrer" href="https://github.com/${GITHUB_USERNAME}">${GITHUB_USERNAME}</a>`,
},
{ name: "Contribution Type", value: GITHUB_CONTRIBUTION_TYPE.split(",").join(", ") },
{ name: "Contribution Type", value: GITHUB_CONTRIBUTION_TYPE?.split(",").join(", ") },
]);
table.setAttribute(`data-make-claim-rendered`, "true");
return requestedAmountElement;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Type } from "@sinclair/typebox";
import { Value } from "@sinclair/typebox/value";
import { createClient } from "@supabase/supabase-js";
import { AppState, app } from "../app-state";
import { decodePermits } from "@ubiquibot/permit-generation/handlers";
import { Permit } from "@ubiquibot/permit-generation/types";
import { app, AppState } from "../app-state";
import { useFastestRpc } from "../rpc-optimization/get-optimal-provider";
import { buttonController, toaster } from "../toaster";
import { connectWallet } from "../web3/connect-wallet";
Expand All @@ -10,7 +10,6 @@ import { verifyCurrentNetwork } from "../web3/verify-current-network";
import { claimRewardsPagination } from "./claim-rewards-pagination";
import { renderTransaction } from "./render-transaction";
import { setClaimMessage } from "./set-claim-message";
import { RewardPermit, claimTxT } from "./tx-type";

declare const SUPABASE_URL: string;
declare const SUPABASE_ANON_KEY: string;
Expand All @@ -29,7 +28,7 @@ export async function readClaimDataFromUrl(app: AppState) {
return;
}

app.claims = decodeClaimData(base64encodedTxData).flat();
app.claims = decodeClaimData(base64encodedTxData);
app.claimTxs = await getClaimedTxs(app);
try {
app.provider = await useFastestRpc(app);
Expand Down Expand Up @@ -64,21 +63,21 @@ export async function readClaimDataFromUrl(app: AppState) {
async function getClaimedTxs(app: AppState): Promise<Record<string, string>> {
const txs: Record<string, string> = Object.create(null);
for (const claim of app.claims) {
const { data } = await supabase.from("permits").select("transaction").eq("nonce", claim.permit.nonce.toString());
const { data } = await supabase.from("permits").select("transaction").eq("nonce", claim.nonce.toString());

if (data?.length == 1 && data[0].transaction !== null) {
txs[claim.permit.nonce.toString()] = data[0].transaction as string;
txs[claim.nonce.toString()] = data[0].transaction as string;
}
}
return txs;
}

function decodeClaimData(base64encodedTxData: string): RewardPermit[] {
function decodeClaimData(base64encodedTxData: string): Permit[] {
let permit;

try {
permit = JSON.parse(atob(base64encodedTxData));
return [Value.Decode(Type.Array(claimTxT), permit)];
permit = decodePermits(base64encodedTxData);
return permit;
} catch (error) {
console.error(error);
setClaimMessage({ type: "Error", message: `Invalid claim data passed in URL` });
Expand Down
22 changes: 11 additions & 11 deletions static/scripts/rewards/render-transaction/render-transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { verifyCurrentNetwork } from "../web3/verify-current-network";
import { insertErc20PermitTableData, insertErc721PermitTableData } from "./insert-table-data";
import { renderEnsName } from "./render-ens-name";
import { renderNftSymbol, renderTokenSymbol } from "./render-token-symbol";
import { Erc20Permit, RewardPermit } from "./tx-type";
import { ERC20Permit, Permit, TokenType } from "@ubiquibot/permit-generation/types";

const carousel = document.getElementById("carousel") as Element;
const table = document.querySelector(`table`) as HTMLTableElement;
Expand All @@ -28,31 +28,31 @@ export async function renderTransaction(): Promise<Success> {

verifyCurrentNetwork(app.reward.networkId).catch(console.error);

if (permitCheck(app.reward)) {
if (isErc20Permit(app.reward)) {
const treasury = await fetchTreasury(app.reward);

// insert tx data into table
const requestedAmountElement = insertErc20PermitTableData(app, table, treasury);

renderTokenSymbol({
tokenAddress: app.reward.permit.permitted.token,
tokenAddress: app.reward.tokenAddress,
ownerAddress: app.reward.owner,
amount: app.reward.transferDetails.requestedAmount,
amount: app.reward.amount,
explorerUrl: networkExplorers[app.reward.networkId],
table,
requestedAmountElement,
}).catch(console.error);

const toElement = document.getElementById(`rewardRecipient`) as Element;
renderEnsName({ element: toElement, address: app.reward.transferDetails.to }).catch(console.error);
renderEnsName({ element: toElement, address: app.reward.beneficiary }).catch(console.error);

if (app.provider) {
checkRenderInvalidatePermitAdminControl(app).catch(console.error);
}

if (app.claimTxs[app.reward.permit.nonce.toString()] !== undefined) {
if (app.claimTxs[app.reward.nonce.toString()] !== undefined) {
buttonController.showViewClaim();
viewClaimButton.addEventListener("click", () => window.open(`${app.currentExplorerUrl}/tx/${app.claimTxs[app.reward.permit.nonce.toString()]}`));
viewClaimButton.addEventListener("click", () => window.open(`${app.currentExplorerUrl}/tx/${app.claimTxs[app.reward.nonce.toString()]}`));
} else if (window.ethereum) {
// requires wallet connection to claim
buttonController.showMakeClaim();
Expand All @@ -64,21 +64,21 @@ export async function renderTransaction(): Promise<Success> {
const requestedAmountElement = insertErc721PermitTableData(app.reward, table);
table.setAttribute(`data-make-claim`, "ok");
renderNftSymbol({
tokenAddress: app.reward.permit.permitted.token,
tokenAddress: app.reward.tokenAddress,
explorerUrl: networkExplorers[app.reward.networkId],
table,
requestedAmountElement,
}).catch(console.error);

const toElement = document.getElementById(`rewardRecipient`) as Element;
renderEnsName({ element: toElement, address: app.reward.transferDetails.to }).catch(console.error);
renderEnsName({ element: toElement, address: app.reward.beneficiary }).catch(console.error);

getMakeClaimButton().addEventListener("click", claimErc721PermitHandler(app.reward));
}

return true;
}

function permitCheck(permit: RewardPermit): permit is Erc20Permit {
return permit.type === "erc20-permit";
function isErc20Permit(permit: Permit): permit is ERC20Permit {
return permit.tokenType === TokenType.ERC20;
}
80 changes: 0 additions & 80 deletions static/scripts/rewards/render-transaction/tx-type.ts

This file was deleted.

Loading

0 comments on commit efff1d7

Please sign in to comment.