-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
6580e4d
commit a386d92
Showing
4 changed files
with
120 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
}); | ||
}); |