-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: LarsKristoHellheadsContextController WIP
- Loading branch information
Showing
12 changed files
with
922 additions
and
7 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
5 changes: 5 additions & 0 deletions
5
app/src/context/evm/larskristo-hellheads/LarskristoHellheadsContext.tsx
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,5 @@ | ||
import { createContext } from "react"; | ||
|
||
import { LarskristoHellheadsContextType } from "./LarskristoHellheadsContext.types"; | ||
|
||
export const LarskristoHellheadsContext = createContext<LarskristoHellheadsContextType | undefined>(undefined); |
22 changes: 22 additions & 0 deletions
22
app/src/context/evm/larskristo-hellheads/LarskristoHellheadsContext.types.ts
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,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>; | ||
}; |
65 changes: 65 additions & 0 deletions
65
app/src/context/evm/larskristo-hellheads/LarskristoHellheadsContextController.tsx
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,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>; | ||
}; |
13 changes: 13 additions & 0 deletions
13
app/src/context/evm/larskristo-hellheads/useLarskristoHellheadsContext.tsx
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,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; | ||
}; |
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,9 @@ | ||
import { createWalletClient, http } from "viem"; | ||
import { sepolia } from "viem/chains"; | ||
|
||
const client = createWalletClient({ | ||
chain: sepolia, | ||
transport: http(), | ||
}); | ||
|
||
export default client; |
Oops, something went wrong.