Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: prevent silent failing during contract creation #59

Merged
merged 4 commits into from
Feb 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/real-bats-enjoy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@scio-labs/use-inkathon': minor
---

throws Error when trying to access unavailable contract deployment
1 change: 0 additions & 1 deletion src/helpers/getDeployment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ export const getDeploymentContract = (
contractId: string,
networkId: string,
) => {
if (!api) return undefined
const deployment = getDeployment(deployments || [], contractId, networkId)
if (!deployment) return undefined
return new ContractPromise(api, deployment?.abi, deployment?.address)
Expand Down
8 changes: 6 additions & 2 deletions src/hooks/useContract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,12 @@ export const useContract = (
setContract(undefined)
return
}
const contract = new ContractPromise(api, abi, address)
setContract(contract)
try {
const contract = new ContractPromise(api, abi, address)
setContract(contract)
} catch (error) {
console.error('Error during Contract initialization', error)
}
}
useEffect(() => {
initialize()
Expand Down
9 changes: 8 additions & 1 deletion src/hooks/useRegisteredContract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,14 @@ import { useContract } from './useContract'
*/
export const useRegisteredContract = (contractId: string, networkId?: string) => {
const { deployments, activeChain } = useInkathon()

networkId = networkId || activeChain?.network || ''

const deployment = getDeployment(deployments || [], contractId, networkId)
return useContract(deployment?.abi, deployment?.address)

if (!deployment) {
throw new Error(`No deployment found for contractId ${contractId} on network ${networkId}`)
}

return useContract(deployment.abi, deployment.address)
}
2 changes: 1 addition & 1 deletion src/provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ export const UseInkathonProvider: FC<UseInkathonProviderProps> = ({
}
}, [api])

// Initialze
// Initialize
useEffect(() => {
if (isInitialized.current || isInitializing.current) return
connectOnInit ? connect() : initialize()
Expand Down