-
Notifications
You must be signed in to change notification settings - Fork 18
/
useContract.ts
33 lines (30 loc) · 933 Bytes
/
useContract.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
import { useInkathon } from '@/provider'
import { Abi, ContractPromise } from '@polkadot/api-contract'
import { AccountId } from '@polkadot/types/interfaces'
import { useEffect, useState } from 'react'
/**
* React Hook that returns a `ContractPromise` object configured with
* the active api & chain as well as the given `abi` and `address`.
*/
export const useContract = (
abi?: string | Record<string, unknown> | Abi,
address?: string | AccountId,
) => {
const { api, isConnecting } = useInkathon()
const [contract, setContract] = useState<ContractPromise | undefined>()
const initialize = async () => {
if (isConnecting || !api || !abi || !address) {
setContract(undefined)
return
}
const contract = new ContractPromise(api, abi, address)
setContract(contract)
}
useEffect(() => {
initialize()
}, [api, isConnecting, abi, address])
return {
contract,
address,
}
}