Skip to content

Commit

Permalink
format erc1155 and fix actions
Browse files Browse the repository at this point in the history
  • Loading branch information
kyscott18 committed Sep 20, 2023
1 parent 03fad2d commit a864d65
Show file tree
Hide file tree
Showing 33 changed files with 1,322 additions and 818 deletions.
3 changes: 2 additions & 1 deletion docs/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
node_modules
.vitepress/.*
142 changes: 140 additions & 2 deletions docs/.vitepress/config.mts
Original file line number Diff line number Diff line change
Expand Up @@ -182,12 +182,150 @@ export default defineConfig({
{
text: "ERC721",
collapsed: true,
items: [{ text: "Introduction", link: "" }],
items: [
{ text: "Introduction", link: "" },
{
text: "Utilities",
items: [
{
text: "createERC721",
link: "",
},
{
text: "createERC721Data",
link: "",
},
{
text: "createERC721DataID",
link: "",
},
],
},
{
text: "Public Actions",
items: [
{
text: "getERC721",
link: "",
},
{
text: "getERC721Approved",
link: "",
},
{
text: "getERC721IsApprovedForAll",
link: "",
},
{
text: "getERC721BalanceOf",
link: "",
},
{
text: "getERC721Name",
link: "",
},
{
text: "getERC721Symbol",
link: "",
},
{
text: "getERC721OwnerOf",
link: "",
},
{
text: "getERC721SupportsInterface",
link: "",
},
{
text: "getERC721TokenURI",
link: "",
},
{
text: "getERC721Data",
link: "",
},
{
text: "getERC721DataID",
link: "",
},
],
},
{
text: "Wallet Actions",
items: [
{
text: "simulateERC721Transfer",
link: "",
},
{
text: "simulateERC721Approve",
link: "",
},
{
text: "simulateERC721SetApprovalForAll",
link: "",
},
],
},
],
},
{
text: "ERC1155",
collapsed: true,
items: [{ text: "Introduction", link: "" }],
items: [
{ text: "Introduction", link: "" },
{
text: "Utilities",
items: [
{
text: "createERC1155",
link: "",
},
{
text: "createERC1155Data",
link: "",
},
],
},
{
text: "Public Actions",
items: [
{
text: "getERC1155",
link: "",
},
{
text: "getERC1155IsApprovedForAll",
link: "",
},
{
text: "getERC1155BalanceOf",
link: "",
},
{
text: "getERC1155URI",
link: "",
},
],
},
{
text: "Wallet Actions",
items: [
{
text: "simulateERC1155Transfer",
link: "",
},
{
text: "simulateERC1155TransferBatch",
link: "",
},
{
text: "simulateERC1155SetApprovalForAll",
link: "",
},
],
},
],
},
{
text: "Amount",
Expand Down
20 changes: 12 additions & 8 deletions examples/next-interface/hooks/useBalance.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useQuery } from "@tanstack/react-query";
import { type ERC20, erc20BalanceOf, getQueryKey } from "reverse-mirage";
import { type ERC20, getERC20BalanceOf, getQueryKey } from "reverse-mirage";
import { type Address, usePublicClient } from "wagmi";
import type { HookArg } from "./internal/types";
import { userRefectchInterval } from "./internal/utils";
Expand All @@ -14,19 +14,23 @@ export const useBalance = <TERC20 extends ERC20>(

return useQuery({
queryKey: getQueryKey(
erc20BalanceOf,
getERC20BalanceOf,
{ erc20: erc20!, address: address! },
chainID,
),
enabled: [erc20, address].some((a) => a === undefined) ? false : true,
queryFn: () =>
erc20BalanceOf({
args: { erc20: erc20!, address: address! },
}).read(publicClient),
getERC20BalanceOf(
publicClient,
{ erc20: erc20!, address: address! },
"select",
).read(),
select: (data) =>
erc20BalanceOf({
args: { erc20: erc20!, address: address! },
}).parse(data),
getERC20BalanceOf(
publicClient,
{ erc20: erc20!, address: address! },
"select",
).parse(data),
staleTime: Infinity,
refetchInterval: userRefectchInterval,
});
Expand Down
11 changes: 5 additions & 6 deletions examples/next-interface/hooks/useTransfer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import { useMemo } from "react";
import {
type BaseERC20,
type ERC20Amount,
erc20BalanceOf,
erc20Transfer,
getERC20BalanceOf,
getQueryKey,
simulateERC20Transfer,
} from "reverse-mirage";
import { getAddress } from "viem";
import { type Address, useWalletClient } from "wagmi";
Expand Down Expand Up @@ -36,9 +36,8 @@ export const useTransfer = <TERC20 extends BaseERC20>(
} & {
toast: TxToast;
}) => {
const { request } = await erc20Transfer(client, {
to,
amount,
const { request } = await simulateERC20Transfer(client, {
args: { to, amount },
});
const hash = await walletClient.data!.writeContract(request);

Expand All @@ -59,7 +58,7 @@ export const useTransfer = <TERC20 extends BaseERC20>(
await Promise.all([
queryClient.invalidateQueries({
queryKey: getQueryKey(
erc20BalanceOf,
getERC20BalanceOf,
{
erc20: input.amount.token,
address: getAddress(data.from),
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/_test/constants.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { foundry } from "viem/chains";
import type { NativeCurrency } from "../native/types.js";
import type { Token } from "../types.js";
import type { Token } from "../types/token.js";

// Test accounts
export const ACCOUNTS = [
Expand Down
17 changes: 17 additions & 0 deletions packages/core/src/erc1155/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
export type {
ERC1155,
ERC1155Data,
} from "./types.js";

export { createERC1155, createERC1155Data } from "./utils.js";

export { solmateErc1155ABI as solmateERC1155ABI } from "../generated.js";

export { getERC1155 } from "./publicActions/getERC1155.js";
export { getERC1155BalanceOf } from "./publicActions/getERC1155BalanceOf.js";
export { getERC1155IsApprovedForAll } from "./publicActions/getERC1155IsApprovedForAll.js";
export { getERC1155URI } from "./publicActions/getERC1155URI.js";

export { simulateERC1155Transfer } from "./walletActions/simulateERC1155Transfer.js";
export { simulateERC1155TransferBatch } from "./walletActions/simulateERC1155TransferBatch.js";
export { simulateERC1155SetApprovalForAll } from "./walletActions/simulateERC1155SetApprovalForAll.js";
43 changes: 43 additions & 0 deletions packages/core/src/erc1155/publicActions/getERC1155.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import invariant from "tiny-invariant";
import { type Hex } from "viem";
import { foundry } from "viem/chains";
import { beforeEach, expect, test } from "vitest";
import ERC1155Bytecode from "../../../../../contracts/out/ERC1155.sol/ERC1155.json";
import { publicClient, testClient, walletClient } from "../../_test/utils.js";
import { erc1155ABI } from "../../generated.js";
import type { ERC1155 } from "../types.js";
import { createERC1155 } from "../utils.js";
import { getERC1155 } from "./getERC1155.js";

let id: Hex | undefined = undefined;

let erc1155: ERC1155;

beforeEach(async () => {
if (id === undefined) {
const deployHash = await walletClient.deployContract({
abi: erc1155ABI,
bytecode: ERC1155Bytecode.bytecode.object as Hex,
args: ["https://mitch.com"],
});

const { contractAddress } = await publicClient.waitForTransactionReceipt({
hash: deployHash,
});
invariant(contractAddress);
erc1155 = createERC1155(
contractAddress,
0n,
"https://mitch.com",
foundry.id,
);
} else {
await testClient.revert({ id });
}

id = await testClient.snapshot();
});

test("getERC1155", async () => {
expect(await getERC1155(publicClient, { erc1155 })).toStrictEqual(erc1155);
});
57 changes: 57 additions & 0 deletions packages/core/src/erc1155/publicActions/getERC1155.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import type { Chain, Client, ReadContractParameters, Transport } from "viem";
import { readContract } from "viem/contract";
import { solmateErc1155ABI as solmateERC1155ABI } from "../../generated.js";
import type { ReverseMirage } from "../../types/rm.js";
import type { ERC1155 } from "../types.js";
import { createERC1155 } from "../utils.js";

export type GetERC1155Parameters = Omit<
ReadContractParameters<typeof solmateERC1155ABI, "uri">,
"address" | "abi" | "functionName" | "args"
> & {
erc1155: Pick<ERC1155, "address" | "id" | "chainID"> &
Partial<Pick<ERC1155, "blockCreated">>;
};

export type GetERC1155ReturnType = ERC1155;

export const getERC1155 = <
TChain extends Chain | undefined,
T extends "select" | undefined,
>(
client: Client<Transport, TChain>,
args: GetERC1155Parameters,
type?: T,
): ReverseMirage<string, GetERC1155ReturnType, T> =>
(type === undefined
? readContract(client, {
abi: solmateERC1155ABI,
address: args.erc1155.address,
functionName: "uri",
args: [args.erc1155.id],
}).then((data) =>
createERC1155(
args.erc1155.address,
args.erc1155.id,
data,
args.erc1155.chainID,
args.blockNumber,
),
)
: {
read: () =>
readContract(client, {
abi: solmateERC1155ABI,
address: args.erc1155.address,
functionName: "uri",
args: [args.erc1155.id],
}),
parse: (data) =>
createERC1155(
args.erc1155.address,
args.erc1155.id,
data,
args.erc1155.chainID,
args.blockNumber,
),
}) as ReverseMirage<string, GetERC1155ReturnType, T>;
Loading

0 comments on commit a864d65

Please sign in to comment.