From f0bef5c3c10a3d47c817a8247cc46b025bdf9e75 Mon Sep 17 00:00:00 2001 From: aphex Date: Mon, 17 Feb 2025 15:03:10 +0000 Subject: [PATCH 1/2] add support for overriding an metadata server allowlist with local file --- README.md | 1 + apps/dapp/src/allowlist/README.md | 4 ++ apps/dapp/src/allowlist/allowlists.ts | 22 +++++++++++ apps/dapp/src/allowlist/index.ts | 0 apps/dapp/src/allowlist/utils.test.ts | 34 +++++++++++++++++ apps/dapp/src/allowlist/utils.ts | 37 +++++++++++++++++++ .../src/modules/auction/hooks/use-auctions.ts | 7 +++- 7 files changed, 104 insertions(+), 1 deletion(-) create mode 100644 apps/dapp/src/allowlist/README.md create mode 100644 apps/dapp/src/allowlist/allowlists.ts create mode 100644 apps/dapp/src/allowlist/index.ts create mode 100644 apps/dapp/src/allowlist/utils.test.ts create mode 100644 apps/dapp/src/allowlist/utils.ts diff --git a/README.md b/README.md index 32ef57d..057e648 100644 --- a/README.md +++ b/README.md @@ -7,6 +7,7 @@ This app is designed for projects looking to host and manage their Token Generat ## App Configuration - add your Token Address, Chain id and other optional metadata to `./app-config.ts` +- [OPTIONAL] Overriding Allowlists: check `apps/dapp/src/allowlist/README.md` for how-to ## Dev Setup diff --git a/apps/dapp/src/allowlist/README.md b/apps/dapp/src/allowlist/README.md new file mode 100644 index 0000000..a284091 --- /dev/null +++ b/apps/dapp/src/allowlist/README.md @@ -0,0 +1,4 @@ +# Allowlist Overriding + +1. Check `allowlists.ts` and add your allowlist(s) to it. +2. That's it. diff --git a/apps/dapp/src/allowlist/allowlists.ts b/apps/dapp/src/allowlist/allowlists.ts new file mode 100644 index 0000000..cb3c953 --- /dev/null +++ b/apps/dapp/src/allowlist/allowlists.ts @@ -0,0 +1,22 @@ +/** The allowlists exported from this file will override the metadata server's allowlists + * + * The object key should be the auction id, which can be retrieved from the subgraph: + * 1) Obtain the subgraph URL for the auction chain here: https://github.com/Axis-Fi/ui-libs/tree/main/packages/deployments/src/chains + * 2) Obtain the subgraph version from https://github.com/Axis-Fi/ui-libs/blob/main/packages/deployments/src/subgraph-config.ts + * 3) Run the following query replacing the base token with your token address: + * + * query getBatchAuctionLotsByBaseTokenAddress { + batchAuctionLots(where: { baseToken: "0x0000000000000000000000000000000000000000" }) { + id + } + } + + The return id value is the key to be used for the allowlist + */ + +export default { + "blast-sepolia-0x1234567890123456789012345678901234567890-1": [ + ["0x0000000000000000000000000000000000000000", "300000000000000000"], + ["0x0000000000000000000000000000000000000001", "1777777700000000000"], + ], +} as Record>; diff --git a/apps/dapp/src/allowlist/index.ts b/apps/dapp/src/allowlist/index.ts new file mode 100644 index 0000000..e69de29 diff --git a/apps/dapp/src/allowlist/utils.test.ts b/apps/dapp/src/allowlist/utils.test.ts new file mode 100644 index 0000000..6971f7e --- /dev/null +++ b/apps/dapp/src/allowlist/utils.test.ts @@ -0,0 +1,34 @@ +import { describe, it, expect } from "vitest"; +import { overrideAllowlist } from "./utils"; +import { getBatchAuctionMock } from "src/stories/mocks/batch-auction"; + +describe("Allowlist Overrides", () => { + it("overrides an allowlist if file is present", () => { + const mockAuction = getBatchAuctionMock(); + const result = overrideAllowlist(mockAuction); + + const allowlist = result?.info?.allowlist; + + expect(Array.isArray(allowlist)).toBe(true); + + allowlist?.forEach((element) => { + expect(element).toHaveProperty("values"); + expect(Array.isArray(element.values)).toBe(true); + + element.values.forEach((value) => { + expect(typeof value).toBe("string"); + }); + }); + }); + + it("keeps auction unchanged if no allowlist for that id is present", () => { + const mockAuction = getBatchAuctionMock({ + id: "blast-sepolia-0x1234567890123456789012345678901234567890-2", + }); + const result = overrideAllowlist(mockAuction); + const allowlist = result?.info?.allowlist; + + expect(result).toEqual(mockAuction); + expect(allowlist).toBe(undefined); + }); +}); diff --git a/apps/dapp/src/allowlist/utils.ts b/apps/dapp/src/allowlist/utils.ts new file mode 100644 index 0000000..70818c7 --- /dev/null +++ b/apps/dapp/src/allowlist/utils.ts @@ -0,0 +1,37 @@ +import { Auction } from "@axis-finance/types"; +import allowlists from "./allowlists"; + +/** + * Returns the existing allowlist file if it exists locally + */ +export function getAllowlistByAuctionId(id: Auction["id"]) { + return allowlists[id]; +} + +/** + * Overrides an auction allowlist property with a local allowlist file + */ +export function overrideAllowlist(auction: Auction): Auction { + const _auction = auction; + const allowlist = getAllowlistByAuctionId(auction.id); + + if (!allowlist) return auction; + + if (auction.info === null) { + _auction.info = { + links: [], + allowlist: [], + }; + } + + const mappedAllowlist = allowlist.map((values) => ({ values })); + + return { + ..._auction, + info: { + ..._auction.info, + links: _auction.info?.links ?? [], + allowlist: mappedAllowlist, + }, + }; +} diff --git a/apps/dapp/src/modules/auction/hooks/use-auctions.ts b/apps/dapp/src/modules/auction/hooks/use-auctions.ts index da8a083..5528501 100644 --- a/apps/dapp/src/modules/auction/hooks/use-auctions.ts +++ b/apps/dapp/src/modules/auction/hooks/use-auctions.ts @@ -12,6 +12,7 @@ import { AUCTION_CHAIN_ID, AUCTION_TOKEN_ADDRESS, } from "../../../../../../app-config"; +import { overrideAllowlist } from "src/allowlist/utils"; export type AuctionsResult = { data: Auction[]; @@ -62,7 +63,11 @@ export function useAuctions(): AuctionsResult { chainId, }; - return preparedAuction as Auction; + const auctionWithOverridenAllowlist = overrideAllowlist( + preparedAuction as Auction, + ); + + return auctionWithOverridenAllowlist; }) .sort(sortAuction); From 56a2dfe23e1737fcdb314b27f0d2623a58e430e5 Mon Sep 17 00:00:00 2001 From: aphex Date: Tue, 18 Feb 2025 10:24:34 +0000 Subject: [PATCH 2/2] make overrideAllowlist function stateless --- apps/dapp/src/allowlist/allowlists.ts | 1 + apps/dapp/src/allowlist/utils.test.ts | 7 ++++++- apps/dapp/src/allowlist/utils.ts | 8 ++++++-- apps/dapp/src/modules/auction/hooks/use-auctions.ts | 6 +++++- 4 files changed, 18 insertions(+), 4 deletions(-) diff --git a/apps/dapp/src/allowlist/allowlists.ts b/apps/dapp/src/allowlist/allowlists.ts index cb3c953..84a1a5e 100644 --- a/apps/dapp/src/allowlist/allowlists.ts +++ b/apps/dapp/src/allowlist/allowlists.ts @@ -15,6 +15,7 @@ */ export default { + //Example format "blast-sepolia-0x1234567890123456789012345678901234567890-1": [ ["0x0000000000000000000000000000000000000000", "300000000000000000"], ["0x0000000000000000000000000000000000000001", "1777777700000000000"], diff --git a/apps/dapp/src/allowlist/utils.test.ts b/apps/dapp/src/allowlist/utils.test.ts index 6971f7e..f846255 100644 --- a/apps/dapp/src/allowlist/utils.test.ts +++ b/apps/dapp/src/allowlist/utils.test.ts @@ -2,10 +2,15 @@ import { describe, it, expect } from "vitest"; import { overrideAllowlist } from "./utils"; import { getBatchAuctionMock } from "src/stories/mocks/batch-auction"; +const SAMPLE_ALLOWLIST = [ + ["0x0000000000000000000000000000000000000000", "300000000000000000"], + ["0x0000000000000000000000000000000000000001", "1777777700000000000"], +]; + describe("Allowlist Overrides", () => { it("overrides an allowlist if file is present", () => { const mockAuction = getBatchAuctionMock(); - const result = overrideAllowlist(mockAuction); + const result = overrideAllowlist(mockAuction, SAMPLE_ALLOWLIST); const allowlist = result?.info?.allowlist; diff --git a/apps/dapp/src/allowlist/utils.ts b/apps/dapp/src/allowlist/utils.ts index 70818c7..ddd21c8 100644 --- a/apps/dapp/src/allowlist/utils.ts +++ b/apps/dapp/src/allowlist/utils.ts @@ -11,9 +11,13 @@ export function getAllowlistByAuctionId(id: Auction["id"]) { /** * Overrides an auction allowlist property with a local allowlist file */ -export function overrideAllowlist(auction: Auction): Auction { +export function overrideAllowlist( + auction: Auction, + allowlist?: Array, +): Auction { + if (!allowlist || allowlist.length === 0) return auction; + const _auction = auction; - const allowlist = getAllowlistByAuctionId(auction.id); if (!allowlist) return auction; diff --git a/apps/dapp/src/modules/auction/hooks/use-auctions.ts b/apps/dapp/src/modules/auction/hooks/use-auctions.ts index 5528501..461fc92 100644 --- a/apps/dapp/src/modules/auction/hooks/use-auctions.ts +++ b/apps/dapp/src/modules/auction/hooks/use-auctions.ts @@ -12,7 +12,10 @@ import { AUCTION_CHAIN_ID, AUCTION_TOKEN_ADDRESS, } from "../../../../../../app-config"; -import { overrideAllowlist } from "src/allowlist/utils"; +import { + getAllowlistByAuctionId, + overrideAllowlist, +} from "src/allowlist/utils"; export type AuctionsResult = { data: Auction[]; @@ -65,6 +68,7 @@ export function useAuctions(): AuctionsResult { const auctionWithOverridenAllowlist = overrideAllowlist( preparedAuction as Auction, + getAllowlistByAuctionId(preparedAuction.id), ); return auctionWithOverridenAllowlist;