-
Notifications
You must be signed in to change notification settings - Fork 15
/
useRegisteredTypedContract.ts
37 lines (33 loc) · 1.36 KB
/
useRegisteredTypedContract.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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 }
}