-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add support for type-safe contracts via new hook `useRegistered…
…TypedContract`
- Loading branch information
Showing
8 changed files
with
75 additions
and
3 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,37 @@ | ||
import { useRegisteredContract } from '@/hooks/useRegisteredContract' | ||
import { useInkathon } from '@/provider' | ||
import { TypechainContractConstructor } from '@/types' | ||
import { useEffect, useState } from 'react' | ||
|
||
/** | ||
* React Hook that returns a type-safe contract object by `typechain-polkadot`, | ||
* configured with the active api & chain for the given deployment contract id | ||
* which is looked up from the deployments registry. | ||
*/ | ||
export const useRegisteredTypedContract = <T>( | ||
contractId: string, | ||
Contract: TypechainContractConstructor<T>, | ||
networkId?: string, | ||
) => { | ||
const { api, activeAccount } = useInkathon() | ||
const registeredContract = useRegisteredContract(contractId, networkId) | ||
|
||
const [typedContract, setTypedContract] = useState<T | undefined>(undefined) | ||
useEffect(() => { | ||
if (!registeredContract?.address || !activeAccount?.address || !api) { | ||
setTypedContract(undefined) | ||
return | ||
} | ||
|
||
// IMPORTANT: Right now, only KeyringPair is supported as signer, but as we don't have | ||
// those anyways in the frontend, we can alreaday start using the new API. | ||
const typedContract = new Contract( | ||
registeredContract.address.toString(), | ||
activeAccount.address as any, | ||
api, | ||
) | ||
setTypedContract(typedContract) | ||
}, [registeredContract?.address, activeAccount?.address]) | ||
|
||
return { ...registeredContract, typedContract } | ||
} |
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,8 @@ | ||
import { ApiPromise } from '@polkadot/api' | ||
import { KeyringPair } from '@polkadot/keyring/types' | ||
|
||
export type TypechainContractConstructor<T> = new ( | ||
address: string, | ||
signer: KeyringPair, | ||
nativeAPI: ApiPromise, | ||
) => T |
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