Skip to content

Commit 912e73c

Browse files
committed
feat: LarsKristoHellheadsContextController WIP
1 parent 519e885 commit 912e73c

12 files changed

+922
-7
lines changed

app/.eslintrc.js

+1
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ module.exports = {
169169
"unicorn/no-array-reduce": "off",
170170
"unicorn/consistent-destructuring": "off",
171171
"unicorn/no-array-for-each": "off",
172+
"unicorn/no-abusive-eslint-disable": "off",
172173

173174
// a11y
174175
"jsx-a11y/anchor-is-valid": "off",

app/package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"@supabase/supabase-js": "^2.33.1",
2222
"@tanstack/react-query": "^5.29.0",
2323
"@types/js-cookie": "^3.0.3",
24+
"@wagmi/core": "^2.6.17",
2425
"@web3modal/siwe": "^4.1.5",
2526
"@web3modal/wagmi": "^4.1.5",
2627
"ag-grid-community": "^27.3.0",
@@ -49,7 +50,7 @@
4950
"rxjs": "^7.8.1",
5051
"siwe": "^2.1.4",
5152
"uuid": "^9.0.0",
52-
"viem": "^2.9.13",
53+
"viem": "^2.9.20",
5354
"wagmi": "^2.5.19",
5455
"winston": "^3.9.0",
5556
"ws": "^8.13.0"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { createContext } from "react";
2+
3+
import { LarskristoHellheadsContextType } from "./LarskristoHellheadsContext.types";
4+
5+
export const LarskristoHellheadsContext = createContext<LarskristoHellheadsContextType | undefined>(undefined);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { ReactNode } from "react";
2+
3+
import { LarsKristoHellheads } from "providers/evm/contracts/larskristohellheads/LarsKristoHellheads";
4+
5+
export type LarskristoHellheadsContextControllerProps = {
6+
children: ReactNode;
7+
};
8+
9+
export type LarskristoHellheadsContextActions = {
10+
fetchContractValues: { isLoading: boolean };
11+
};
12+
13+
export type LarskristoHellheadsContractValues = {
14+
name: LarsKristoHellheads["name"];
15+
symbol: LarsKristoHellheads["symbol"];
16+
};
17+
18+
export type LarskristoHellheadsContextType = {
19+
contractValues?: LarskristoHellheadsContractValues;
20+
actions: LarskristoHellheadsContextActions;
21+
fetchContractValues: (address: string) => Promise<void>;
22+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import React, { useState } from "react";
2+
import { getContract } from "viem";
3+
4+
import { LarsKristoHellheads__factory } from "providers/evm/contracts/larskristohellheads/LarsKristoHellheads__factory";
5+
import evm from "providers/evm";
6+
7+
import { LarskristoHellheadsContext } from "./LarskristoHellheadsContext";
8+
import {
9+
LarskristoHellheadsContextActions,
10+
LarskristoHellheadsContextControllerProps,
11+
LarskristoHellheadsContextType,
12+
LarskristoHellheadsContractValues,
13+
} from "./LarskristoHellheadsContext.types";
14+
15+
export const LarskristoHellheadsContextController = ({ children }: LarskristoHellheadsContextControllerProps) => {
16+
const [contractValues, setContractValues] = useState<LarskristoHellheadsContractValues>();
17+
const [actions, setActions] = useState<LarskristoHellheadsContextActions>({
18+
fetchContractValues: {
19+
isLoading: false,
20+
},
21+
});
22+
23+
const fetchContractValues = async (address: string) => {
24+
setActions((prev) => ({
25+
...prev,
26+
fetchContractValues: {
27+
isLoading: true,
28+
},
29+
}));
30+
31+
try {
32+
const contract = getContract({
33+
address: address as `0x{string}`,
34+
abi: LarsKristoHellheads__factory.abi,
35+
client: evm.client,
36+
});
37+
38+
const [name, symbol] = await Promise.all([contract.read.name(), contract.read.symbol()]);
39+
40+
const values: LarskristoHellheadsContractValues = {
41+
name,
42+
symbol,
43+
};
44+
45+
setContractValues({ ...values });
46+
} catch (error) {
47+
console.error(error);
48+
}
49+
50+
setActions((prev) => ({
51+
...prev,
52+
fetchContractValues: {
53+
isLoading: false,
54+
},
55+
}));
56+
};
57+
58+
const props: LarskristoHellheadsContextType = {
59+
fetchContractValues,
60+
contractValues,
61+
actions,
62+
};
63+
64+
return <LarskristoHellheadsContext.Provider value={props}>{children}</LarskristoHellheadsContext.Provider>;
65+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { useContext } from "react";
2+
3+
import { LarskristoHellheadsContext } from "./LarskristoHellheadsContext";
4+
5+
export const useLarskristoHellheadsContext = () => {
6+
const context = useContext(LarskristoHellheadsContext);
7+
8+
if (context === undefined) {
9+
throw new Error("useLarskristoHellheadsContext must be used within a LarskristoHellheadsContext");
10+
}
11+
12+
return context;
13+
};

app/src/providers/evm/client.ts

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { createWalletClient, http } from "viem";
2+
import { sepolia } from "viem/chains";
3+
4+
const client = createWalletClient({
5+
chain: sepolia,
6+
transport: http(),
7+
});
8+
9+
export default client;

0 commit comments

Comments
 (0)