Skip to content

Commit

Permalink
feat: LarsKristoHellheadsContextController WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
netpoe committed Apr 17, 2024
1 parent 519e885 commit 912e73c
Show file tree
Hide file tree
Showing 12 changed files with 922 additions and 7 deletions.
1 change: 1 addition & 0 deletions app/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ module.exports = {
"unicorn/no-array-reduce": "off",
"unicorn/consistent-destructuring": "off",
"unicorn/no-array-for-each": "off",
"unicorn/no-abusive-eslint-disable": "off",

// a11y
"jsx-a11y/anchor-is-valid": "off",
Expand Down
3 changes: 2 additions & 1 deletion app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"@supabase/supabase-js": "^2.33.1",
"@tanstack/react-query": "^5.29.0",
"@types/js-cookie": "^3.0.3",
"@wagmi/core": "^2.6.17",
"@web3modal/siwe": "^4.1.5",
"@web3modal/wagmi": "^4.1.5",
"ag-grid-community": "^27.3.0",
Expand Down Expand Up @@ -49,7 +50,7 @@
"rxjs": "^7.8.1",
"siwe": "^2.1.4",
"uuid": "^9.0.0",
"viem": "^2.9.13",
"viem": "^2.9.20",
"wagmi": "^2.5.19",
"winston": "^3.9.0",
"ws": "^8.13.0"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { createContext } from "react";

import { LarskristoHellheadsContextType } from "./LarskristoHellheadsContext.types";

export const LarskristoHellheadsContext = createContext<LarskristoHellheadsContextType | undefined>(undefined);
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { ReactNode } from "react";

import { LarsKristoHellheads } from "providers/evm/contracts/larskristohellheads/LarsKristoHellheads";

export type LarskristoHellheadsContextControllerProps = {
children: ReactNode;
};

export type LarskristoHellheadsContextActions = {
fetchContractValues: { isLoading: boolean };
};

export type LarskristoHellheadsContractValues = {
name: LarsKristoHellheads["name"];
symbol: LarsKristoHellheads["symbol"];
};

export type LarskristoHellheadsContextType = {
contractValues?: LarskristoHellheadsContractValues;
actions: LarskristoHellheadsContextActions;
fetchContractValues: (address: string) => Promise<void>;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import React, { useState } from "react";
import { getContract } from "viem";

import { LarsKristoHellheads__factory } from "providers/evm/contracts/larskristohellheads/LarsKristoHellheads__factory";
import evm from "providers/evm";

import { LarskristoHellheadsContext } from "./LarskristoHellheadsContext";
import {
LarskristoHellheadsContextActions,
LarskristoHellheadsContextControllerProps,
LarskristoHellheadsContextType,
LarskristoHellheadsContractValues,
} from "./LarskristoHellheadsContext.types";

export const LarskristoHellheadsContextController = ({ children }: LarskristoHellheadsContextControllerProps) => {
const [contractValues, setContractValues] = useState<LarskristoHellheadsContractValues>();
const [actions, setActions] = useState<LarskristoHellheadsContextActions>({
fetchContractValues: {
isLoading: false,
},
});

const fetchContractValues = async (address: string) => {
setActions((prev) => ({
...prev,
fetchContractValues: {
isLoading: true,
},
}));

try {
const contract = getContract({
address: address as `0x{string}`,
abi: LarsKristoHellheads__factory.abi,
client: evm.client,
});

const [name, symbol] = await Promise.all([contract.read.name(), contract.read.symbol()]);

const values: LarskristoHellheadsContractValues = {
name,
symbol,
};

setContractValues({ ...values });
} catch (error) {
console.error(error);
}

setActions((prev) => ({
...prev,
fetchContractValues: {
isLoading: false,
},
}));
};

const props: LarskristoHellheadsContextType = {
fetchContractValues,
contractValues,
actions,
};

return <LarskristoHellheadsContext.Provider value={props}>{children}</LarskristoHellheadsContext.Provider>;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { useContext } from "react";

import { LarskristoHellheadsContext } from "./LarskristoHellheadsContext";

export const useLarskristoHellheadsContext = () => {
const context = useContext(LarskristoHellheadsContext);

if (context === undefined) {
throw new Error("useLarskristoHellheadsContext must be used within a LarskristoHellheadsContext");
}

return context;
};
9 changes: 9 additions & 0 deletions app/src/providers/evm/client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { createWalletClient, http } from "viem";
import { sepolia } from "viem/chains";

const client = createWalletClient({
chain: sepolia,
transport: http(),
});

export default client;
Loading

0 comments on commit 912e73c

Please sign in to comment.