Skip to content

Commit

Permalink
core: fix type and api inconsistencies
Browse files Browse the repository at this point in the history
  • Loading branch information
fracek committed Oct 25, 2023
1 parent ca15368 commit ba30369
Show file tree
Hide file tree
Showing 19 changed files with 243 additions and 68 deletions.
1 change: 0 additions & 1 deletion packages/core/src/hooks/useAccount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { AccountInterface } from "starknet";

import { Connector } from "~/connectors";
import { useStarknetAccount } from "~/context/account";
import { useStarknet } from "~/context/starknet";

import { useConnect } from "./useConnect";

Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/hooks/useBalance.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, expect, it } from "vitest";
import { account } from "../../test/devnet";
import { accounts } from "../../test/devnet";
import { renderHook, waitFor } from "../../test/react";

import { useBalance } from "./useBalance";
Expand Down Expand Up @@ -28,7 +28,7 @@ describe("useBalance", () => {
// Some issue with the RPC provider.
it.skip("returns the balance", async () => {
const { result } = renderHook(() =>
useBalance({ address: account.address }),
useBalance({ address: accounts.goerli[0].address }),
);

await waitFor(() => {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/hooks/useBalance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import {
import { z } from "zod";

import { UseQueryProps, UseQueryResult, useQuery } from "~/query";

import { useContract } from "./useContract";
import { useInvalidateOnBlock } from "./useInvalidateOnBlock";

import { useNetwork } from "./useNetwork";

export type Balance = {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/hooks/useContract.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ describe("useContract", () => {
"type": "function",
},
],
"address": "0x049d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7",
"address": "0x49d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7",
"callData": CallData {
"abi": [
{
Expand Down
61 changes: 61 additions & 0 deletions packages/core/src/hooks/useContractRead.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { Call, Contract } from "starknet";
import { describe, expect, it } from "vitest";
import { accounts, defaultConnector, tokenAddress } from "../../test/devnet";
import { act, renderHook, waitFor } from "../../test/react";

import { useContractRead } from "./useContractRead";

const abi = [
{
members: [
{
name: "low",
offset: 0,
type: "felt",
},
{
name: "high",
offset: 1,
type: "felt",
},
],
name: "Uint256",
size: 2,
type: "struct",
},
{
name: "balanceOf",
type: "function",
inputs: [
{
name: "account",
type: "felt",
},
],
outputs: [
{
name: "balance",
type: "Uint256",
},
],
stateMutability: "view",
},
];

describe("useContractRead", () => {
it.skip("returns the contract read result", async () => {
const { result } = renderHook(() =>
useContractRead({
functionName: "balanceOf",
args: [accounts.goerli[0].address],
abi,
address: tokenAddress,
watch: true,
}),
);

await waitFor(() => {
expect(result.current.isSuccess).toBeTruthy();
});
});
});
5 changes: 3 additions & 2 deletions packages/core/src/hooks/useContractRead.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ export function useContractRead({
parseArgs,
parseResult,
watch = false,
enabled: enabled_ = true,
...props
}: UseContractReadProps): UseContractReadResult {
const { chain } = useNetwork();
Expand All @@ -70,8 +71,8 @@ export function useContractRead({
);

const enabled = useMemo(
() => Boolean(props.enabled && contract && functionName && args),
[props.enabled, contract, functionName, args],
() => Boolean(enabled_ && contract && functionName && args),
[enabled_, contract, functionName, args],
);

useInvalidateOnBlock({
Expand Down
91 changes: 91 additions & 0 deletions packages/core/src/hooks/useContractWrite.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import { Call, Contract } from "starknet";
import { describe, expect, it } from "vitest";
import { accounts, defaultConnector, tokenAddress } from "../../test/devnet";
import { act, renderHook, waitFor } from "../../test/react";
import { useConnect } from "./useConnect";

import { useContractWrite } from "./useContractWrite";

const abi = [
{
members: [
{
name: "low",
offset: 0,
type: "felt",
},
{
name: "high",
offset: 1,
type: "felt",
},
],
name: "Uint256",
size: 2,
type: "struct",
},
{
inputs: [
{
name: "recipient",
type: "felt",
},
{
name: "amount",
type: "Uint256",
},
],
name: "transfer",
outputs: [
{
name: "success",
type: "felt",
},
],
type: "function",
},
];

function useTestHook({ calls }: { calls: Call[] }) {
const writeResult = useContractWrite({
calls,
});
const { connectAsync, connector } = useConnect();
return {
...writeResult,
connectAsync,
connector,
};
}

describe("useContractWrite", () => {
it.skip("returns the transaction hash in the response", async () => {
const contract = new Contract(abi, tokenAddress);
const { result } = renderHook(() =>
useTestHook({
calls: [
contract.populateTransaction.transfer(accounts.goerli[0].address, {
low: 1,
high: 0,
}),
],
}),
);

await act(async () => {
await result.current.connectAsync({ connector: defaultConnector });
});

await waitFor(() => {
expect(result.current.connector).toBeDefined();
});

await act(async () => {
await result.current.writeAsync();
});

await waitFor(() => {
// expect(result.current).toMatchInlineSnapshot(``);
});
});
});
43 changes: 32 additions & 11 deletions packages/core/src/hooks/useContractWrite.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { useCallback } from "react";
import {
Abi,
AccountInterface,
Expand All @@ -23,7 +24,7 @@ export type ContractWriteVariables = {
export type UseContractWriteProps = ContractWriteVariables &
UseMutationProps<InvokeFunctionResponse, Error, ContractWriteVariables>;

type MutationResult = UseMutationResult<
export type MutationResult = UseMutationResult<
InvokeFunctionResponse,
Error,
ContractWriteVariables
Expand Down Expand Up @@ -69,13 +70,39 @@ export function useContractWrite({
variables,
} = useMutation({
mutationKey: mutationKey({ account, calls, abis, options }),
mutationFn: mutationFn({ account, calls, abis, options }),
mutationFn: mutationFn({ account }),
...props,
});

const write = useCallback(
(args?: ContractWriteVariables) => {
return mutate({
...(args ?? {
calls,
abis,
options,
}),
});
},
[mutate, calls, abis, options],
);

const writeAsync = useCallback(
(args?: ContractWriteVariables) => {
return mutateAsync({
...(args ?? {
calls,
abis,
options,
}),
});
},
[mutateAsync, calls, abis, options],
);

return {
write: mutate,
writeAsync: mutateAsync,
write,
writeAsync,
data,
error,
isError,
Expand Down Expand Up @@ -105,16 +132,10 @@ function mutationKey({

function mutationFn({
account,
calls,
abis,
options,
}: {
account?: AccountInterface;
calls?: Call[];
abis?: Abi[];
options?: InvocationsDetails;
}) {
return async function () {
return async function ({ calls, abis, options }: ContractWriteVariables) {
if (!account) throw new Error("account is required");
if (!calls || calls.length === 0) throw new Error("calls are required");
return await account?.execute(calls, abis, options);
Expand Down
65 changes: 34 additions & 31 deletions packages/core/src/hooks/useSign.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { AccountInterface, Signature, TypedData } from "starknet";
import { UseMutationProps, UseMutationResult, useMutation } from "~/query";
import { useAccount } from "./useAccount";

type Variables = { account?: AccountInterface } & Partial<TypedData>;
type Variables = Partial<TypedData>;

type MutationResult = UseMutationResult<Signature, Error, Variables>;

Expand Down Expand Up @@ -45,32 +45,34 @@ export function useSignTypedData({
variables,
} = useMutation({
mutationKey: mutationKey({ domain, types, message, primaryType }),
mutationFn: mutateFn,
mutationFn: mutateFn({ account }),
...props,
});

const signTypedData = useCallback(
(args?: Partial<TypedData>) =>
mutate({
domain: args?.domain ?? domain,
types: args?.types ?? types,
message: args?.message ?? message,
primaryType: args?.primaryType ?? primaryType,
account,
}),
[mutate, account, domain, types, message, primaryType],
mutate(
args ?? {
domain,
types,
message,
primaryType,
},
),
[mutate, domain, types, message, primaryType],
);

const signTypedDataAsync = useCallback(
(args?: Partial<TypedData>) =>
mutateAsync({
domain: args?.domain ?? domain,
types: args?.types ?? types,
message: args?.message ?? message,
primaryType: args?.primaryType ?? primaryType,
account,
}),
[mutateAsync, account, domain, types, message, primaryType],
mutateAsync(
args ?? {
domain,
types,
message,
primaryType,
},
),
[mutateAsync, domain, types, message, primaryType],
);

return {
Expand Down Expand Up @@ -106,17 +108,18 @@ function mutationKey({
] as const;
}

function mutateFn({
account,
domain,
types,
message,
primaryType,
}: Variables): Promise<Signature> {
if (!account) throw new Error("account is required");
if (!domain) throw new Error("domain is required");
if (!types) throw new Error("types is required");
if (!message) throw new Error("message is required");
if (!primaryType) throw new Error("primaryType is required");
return account.signMessage({ domain, types, message, primaryType });
function mutateFn({ account }: { account?: AccountInterface }) {
return function ({
domain,
types,
message,
primaryType,
}: Variables): Promise<Signature> {
if (!account) throw new Error("account is required");
if (!domain) throw new Error("domain is required");
if (!types) throw new Error("types is required");
if (!message) throw new Error("message is required");
if (!primaryType) throw new Error("primaryType is required");
return account.signMessage({ domain, types, message, primaryType });
};
}
Loading

0 comments on commit ba30369

Please sign in to comment.