Skip to content

Commit

Permalink
#173 Add missing method name for deposits (#175)
Browse files Browse the repository at this point in the history
  • Loading branch information
nevendyulgerov committed May 7, 2024
1 parent 6580e4d commit a386d92
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 38 deletions.
19 changes: 1 addition & 18 deletions apps/web/src/components/applications/latestInputsTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<LatestInputsTableProps> = ({
inputs,
fetching,
Expand Down
22 changes: 2 additions & 20 deletions apps/web/src/components/inputs/inputRow.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
"use client";
import { erc20PortalAddress, etherPortalAddress } from "@cartesi/rollups-wagmi";
import {
ActionIcon,
Badge,
Expand All @@ -14,35 +13,18 @@ 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;
timeType: string;
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<InputRowProps> = ({
input,
timeType,
Expand Down
48 changes: 48 additions & 0 deletions apps/web/src/lib/methodResolver.ts
Original file line number Diff line number Diff line change
@@ -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;
};
69 changes: 69 additions & 0 deletions apps/web/test/lib/methodResolver.test.ts
Original file line number Diff line number Diff line change
@@ -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");
});
});

0 comments on commit a386d92

Please sign in to comment.