Skip to content

Commit

Permalink
core: add option to override useContract provider
Browse files Browse the repository at this point in the history
  • Loading branch information
swaggymarie authored Nov 23, 2023
1 parent fec367a commit 0b57d08
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
12 changes: 8 additions & 4 deletions packages/core/src/hooks/useContract.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useMemo } from "react";
import { Abi, Contract } from "starknet";
import { Abi, Contract, ProviderInterface } from "starknet";

import { useStarknet } from "~/context/starknet";

Expand All @@ -9,6 +9,8 @@ export interface UseContractArgs {
abi?: Abi;
/** The contract address. */
address?: string;
/** The provider, by default it will be the current one. */
provider?: ProviderInterface | null
}

/** Value returned from `useContract`. */
Expand Down Expand Up @@ -40,15 +42,17 @@ export interface UseContractResult {
export function useContract({
abi,
address,
provider: providedProvider
}: UseContractArgs): UseContractResult {
const { provider } = useStarknet();

const { provider:currentProvider } = useStarknet();
const contract = useMemo(() => {
const provider = providedProvider ? providedProvider : currentProvider;
if (abi && address && provider) {
return new Contract(abi, address, provider);
}
return undefined;
}, [abi, address, provider]);
}, [abi, address, providedProvider, currentProvider]);

return { contract };
}
13 changes: 10 additions & 3 deletions website/content/hooks/useContract.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ Get a contract.
## Usage

```ts
"use client"
import { useContract } from "@starknet-react/core";

const testAddress = "0x49d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7";
const abi = [
{
Expand Down Expand Up @@ -48,10 +50,11 @@ const abi = [
},
];
export default function Component() {
const { contract } = useContract({ abi, address: testAddress })

const { contract } = useContract({ abi: abi, address: testAddress })
return (
<div>{contract?.address}</div>
<div>
{contract?.address}
</div>
)
}
```
Expand All @@ -65,6 +68,10 @@ export default function Component() {
* __address__`: string`
- The contract address.

* __provider__`?: ProviderInterface | null`
- Override the provider used by the contract.
- `ProviderInterface` is from starknet.js.

## Returns

* __contract?__`: Contract`
Expand Down

0 comments on commit 0b57d08

Please sign in to comment.