From a386d92df0f7f6071a5a11ec8b056df06cc98fa3 Mon Sep 17 00:00:00 2001 From: Neven Dyulgerov Date: Tue, 7 May 2024 13:12:21 +0300 Subject: [PATCH] #173 Add missing method name for deposits (#175) --- .../applications/latestInputsTable.tsx | 19 +---- apps/web/src/components/inputs/inputRow.tsx | 22 +----- apps/web/src/lib/methodResolver.ts | 48 +++++++++++++ apps/web/test/lib/methodResolver.test.ts | 69 +++++++++++++++++++ 4 files changed, 120 insertions(+), 38 deletions(-) create mode 100644 apps/web/src/lib/methodResolver.ts create mode 100644 apps/web/test/lib/methodResolver.test.ts diff --git a/apps/web/src/components/applications/latestInputsTable.tsx b/apps/web/src/components/applications/latestInputsTable.tsx index f016747b5..28900909f 100644 --- a/apps/web/src/components/applications/latestInputsTable.tsx +++ b/apps/web/src/components/applications/latestInputsTable.tsx @@ -4,11 +4,9 @@ import type { Address as AddressType } from "abitype/dist/types/abi"; import prettyMilliseconds from "pretty-ms"; import { FC, useCallback, useState } from "react"; import Address from "../address"; -import { getAddress } from "viem"; -import { erc20PortalAddress, etherPortalAddress } from "@cartesi/rollups-wagmi"; -import { MethodResolver } from "../inputs/inputRow"; import { InputItemFragment } from "../../graphql/explorer/operations"; import { TbArrowRight } from "react-icons/tb"; +import { methodResolver } from "../../lib/methodResolver"; export interface Entry { appId: AddressType; @@ -22,21 +20,6 @@ export interface LatestInputsTableProps { totalCount: number; } -const etherDepositResolver: MethodResolver = (input) => - getAddress(input.msgSender) === etherPortalAddress && "depositEther"; - -const erc20PortalResolver: MethodResolver = (input) => - getAddress(input.msgSender) === erc20PortalAddress && "depositERC20Tokens"; - -const resolvers: MethodResolver[] = [etherDepositResolver, erc20PortalResolver]; -const methodResolver: MethodResolver = (input) => { - for (const resolver of resolvers) { - const method = resolver(input); - if (method) return method; - } - return undefined; -}; - const LatestInputsTable: FC = ({ inputs, fetching, diff --git a/apps/web/src/components/inputs/inputRow.tsx b/apps/web/src/components/inputs/inputRow.tsx index 1dff7d245..bfbad7ec3 100644 --- a/apps/web/src/components/inputs/inputRow.tsx +++ b/apps/web/src/components/inputs/inputRow.tsx @@ -1,5 +1,4 @@ "use client"; -import { erc20PortalAddress, etherPortalAddress } from "@cartesi/rollups-wagmi"; import { ActionIcon, Badge, @@ -14,10 +13,11 @@ import { useDisclosure } from "@mantine/hooks"; import prettyMilliseconds from "pretty-ms"; import { FC } from "react"; import { TbArrowRight, TbFileText, TbX } from "react-icons/tb"; -import { Address as AddressType, formatUnits, getAddress } from "viem"; +import { Address as AddressType, formatUnits } from "viem"; import { InputItemFragment } from "../../graphql/explorer/operations"; import Address from "../address"; import InputDetailsView from "./inputDetailsView"; +import { methodResolver } from "../../lib/methodResolver"; export type InputRowProps = { input: InputItemFragment; @@ -25,24 +25,6 @@ export type InputRowProps = { keepDataColVisible: boolean; }; -export type MethodResolver = ( - input: InputItemFragment, -) => string | undefined | false; - -const etherDepositResolver: MethodResolver = (input) => - getAddress(input.msgSender) === etherPortalAddress && "depositEther"; -const erc20PortalResolver: MethodResolver = (input) => - getAddress(input.msgSender) === erc20PortalAddress && "depositERC20Tokens"; - -const resolvers: MethodResolver[] = [etherDepositResolver, erc20PortalResolver]; -const methodResolver: MethodResolver = (input) => { - for (const resolver of resolvers) { - const method = resolver(input); - if (method) return method; - } - return undefined; -}; - const InputRow: FC = ({ input, timeType, diff --git a/apps/web/src/lib/methodResolver.ts b/apps/web/src/lib/methodResolver.ts new file mode 100644 index 000000000..997f4942a --- /dev/null +++ b/apps/web/src/lib/methodResolver.ts @@ -0,0 +1,48 @@ +"use client"; +import { + erc1155BatchPortalAddress, + erc1155SinglePortalAddress, + erc20PortalAddress, + erc721PortalAddress, + etherPortalAddress, +} from "@cartesi/rollups-wagmi"; +import { getAddress } from "viem"; +import { InputItemFragment } from "../graphql/explorer/operations"; + +export type MethodResolver = ( + input: InputItemFragment, +) => string | undefined | false; + +const etherDepositResolver: MethodResolver = (input) => + getAddress(input.msgSender) === etherPortalAddress && "depositEther"; + +const erc20PortalResolver: MethodResolver = (input) => + getAddress(input.msgSender) === erc20PortalAddress && "depositERC20Tokens"; + +const erc721PortalResolver: MethodResolver = (input) => + getAddress(input.msgSender) === erc721PortalAddress && + "depositERC721Tokens"; + +const erc1155SinglePortalResolver: MethodResolver = (input) => + getAddress(input.msgSender) === erc1155SinglePortalAddress && + "depositERC1155SingleTokens"; + +const erc1155BatchPortalResolver: MethodResolver = (input) => + getAddress(input.msgSender) === erc1155BatchPortalAddress && + "depositERC1155BatchTokens"; + +const resolvers: MethodResolver[] = [ + etherDepositResolver, + erc20PortalResolver, + erc721PortalResolver, + erc1155SinglePortalResolver, + erc1155BatchPortalResolver, +]; + +export const methodResolver: MethodResolver = (input) => { + for (const resolver of resolvers) { + const method = resolver(input); + if (method) return method; + } + return undefined; +}; diff --git a/apps/web/test/lib/methodResolver.test.ts b/apps/web/test/lib/methodResolver.test.ts new file mode 100644 index 000000000..7486c6ea1 --- /dev/null +++ b/apps/web/test/lib/methodResolver.test.ts @@ -0,0 +1,69 @@ +import { describe, it } from "vitest"; +import { + erc1155BatchPortalAddress, + erc1155SinglePortalAddress, + erc20PortalAddress, + erc721PortalAddress, + etherPortalAddress, +} from "@cartesi/rollups-wagmi"; +import { methodResolver } from "../../src/lib/methodResolver"; +import { InputItemFragment } from "../../src/graphql/explorer/operations"; + +const defaultInput: InputItemFragment = { + id: "0x60a7048c3136293071605a4eaffef49923e981cc-40", + application: { + id: "0x60a7048c3136293071605a4eaffef49923e981cc", + __typename: "Application", + }, + index: 40, + payload: "0x123444fff0", + msgSender: "0x8fd78976f8955d13baa4fc99043208f4ec020d7e", + timestamp: "1710358644", + transactionHash: + "0x65d611065907cc7dd92ae47378427ed99e1cfe17bd6285b85f868ae7b70ed62f", + erc20Deposit: null, + __typename: "Input", +}; + +describe("methodResolver", () => { + it("should resolve correct method", () => { + let method = methodResolver(defaultInput); + + expect(method).toBe(undefined); + + method = methodResolver({ + ...defaultInput, + msgSender: etherPortalAddress, + }); + + expect(method).toBe("depositEther"); + + method = methodResolver({ + ...defaultInput, + msgSender: erc721PortalAddress, + }); + + expect(method).toBe("depositERC721Tokens"); + + method = methodResolver({ + ...defaultInput, + msgSender: erc20PortalAddress, + }); + + expect(method).toBe("depositERC20Tokens"); + + method = methodResolver({ + ...defaultInput, + msgSender: erc1155SinglePortalAddress, + }); + + expect(method).toBe("depositERC1155SingleTokens"); + + method = methodResolver({ + ...defaultInput, + msgSender: erc1155BatchPortalAddress, + }); + + expect(method).toBe("depositERC1155BatchTokens"); + }); +});