react-near-ts 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,23 @@
1
+ import type {
2
+ AccountId,
3
+ GetAccountInfoOutput,
4
+ GetAccountInfoError,
5
+ BlockReference,
6
+ PartialTransportPolicy,
7
+ } from 'near-api-ts';
8
+ import type { UseQueryResult } from '@tanstack/react-query';
9
+
10
+ type UseAccountInfoArgs = {
11
+ accountId?: AccountId;
12
+ atMomentOf?: BlockReference;
13
+ policies?: {
14
+ transport?: PartialTransportPolicy;
15
+ };
16
+ query?: {
17
+ enabled?: boolean;
18
+ };
19
+ };
20
+
21
+ export type UseAccountInfo = (
22
+ args: UseAccountInfoArgs,
23
+ ) => UseQueryResult<GetAccountInfoOutput, GetAccountInfoError>;
@@ -0,0 +1,78 @@
1
+ import type { UseQueryResult } from '@tanstack/react-query';
2
+ import type {
3
+ AccountId,
4
+ BaseDeserializeResultFn,
5
+ BaseSerializeArgsFn,
6
+ BlockReference,
7
+ CallContractReadFunctionError,
8
+ CallContractReadFunctionOutput,
9
+ MaybeBaseDeserializeResultFn,
10
+ MaybeBaseSerializeArgsFn,
11
+ MaybeJsonLikeValue,
12
+ PartialTransportPolicy,
13
+ ContractFunctionName,
14
+ } from 'near-api-ts';
15
+ import type { KeyIf } from '../_common.ts';
16
+
17
+ export type BaseUseContractReadFunctionArgs = {
18
+ contractAccountId?: AccountId;
19
+ functionName?: ContractFunctionName;
20
+ withStateAt?: BlockReference;
21
+ policies?: {
22
+ transport?: PartialTransportPolicy;
23
+ };
24
+ query?: {
25
+ enabled?: boolean;
26
+ };
27
+ };
28
+
29
+ export type InnerUseContractReadFunctionArgs = BaseUseContractReadFunctionArgs & {
30
+ functionArgs?: any;
31
+ options?: {
32
+ serializeArgs?: BaseSerializeArgsFn<any>;
33
+ deserializeResult?: BaseDeserializeResultFn;
34
+ };
35
+ };
36
+
37
+ type Options<A, SR extends MaybeBaseSerializeArgsFn<A>, DR extends MaybeBaseDeserializeResultFn> = [
38
+ SR,
39
+ DR,
40
+ ] extends [undefined, undefined]
41
+ ? {
42
+ options?: { serializeArgs?: never; deserializeResult?: never };
43
+ }
44
+ : {
45
+ options: KeyIf<'serializeArgs', SR> & KeyIf<'deserializeResult', DR>;
46
+ };
47
+
48
+ // Return type of functionArgs or undefined
49
+ type FunctionArgsOf<SA> = SA extends (args: { functionArgs: infer T }) => Uint8Array
50
+ ? T
51
+ : undefined;
52
+
53
+ type CallOutput<DR extends MaybeBaseDeserializeResultFn> = [DR] extends [BaseDeserializeResultFn]
54
+ ? CallContractReadFunctionOutput<ReturnType<DR>>
55
+ : CallContractReadFunctionOutput<unknown>;
56
+
57
+ type FunctionArgs<A> = KeyIf<'functionArgs', A>;
58
+
59
+ export type UseContractReadFunction = {
60
+ // #1
61
+ <A extends MaybeJsonLikeValue = undefined>(
62
+ args: BaseUseContractReadFunctionArgs &
63
+ FunctionArgs<A> &
64
+ Options<undefined, undefined, undefined>,
65
+ ): UseQueryResult<CallOutput<undefined>, CallContractReadFunctionError>;
66
+ // #2
67
+ <DR extends BaseDeserializeResultFn, A extends MaybeJsonLikeValue = undefined>(
68
+ args: BaseUseContractReadFunctionArgs & FunctionArgs<A> & Options<undefined, undefined, DR>,
69
+ ): UseQueryResult<CallOutput<DR>, CallContractReadFunctionError>;
70
+ // #3
71
+ <
72
+ SA extends BaseSerializeArgsFn<A>,
73
+ DR extends MaybeBaseDeserializeResultFn = undefined,
74
+ A = FunctionArgsOf<SA>,
75
+ >(
76
+ args: BaseUseContractReadFunctionArgs & FunctionArgs<A> & Options<A, SA, DR>,
77
+ ): UseQueryResult<CallOutput<DR>, CallContractReadFunctionError>;
78
+ };
@@ -0,0 +1,14 @@
1
+ import type { UseMutationResult } from '@tanstack/react-query';
2
+ import type { ExecuteTransactionOutput } from '../services/_common.ts';
3
+ import type { TransactionIntent } from 'near-api-ts';
4
+
5
+ export type ExecuteTransactionArgs = {
6
+ intent: TransactionIntent;
7
+ };
8
+
9
+ export type UseExecuteTransaction = () => UseMutationResult<
10
+ ExecuteTransactionOutput,
11
+ Error,
12
+ ExecuteTransactionArgs,
13
+ unknown
14
+ >;
@@ -0,0 +1,6 @@
1
+ import type { UseMutationResult } from '@tanstack/react-query';
2
+
3
+ export type UseNearConnect = () => {
4
+ connect: UseMutationResult<void, Error, void, unknown>;
5
+ disconnect: UseMutationResult<void, Error, void, unknown>;
6
+ };
@@ -0,0 +1,36 @@
1
+ import type { TransactionIntent, AccountId, Client } from 'near-api-ts';
2
+ import type { Result } from '../_common.ts';
3
+
4
+ export type ServiceId = string;
5
+
6
+ export type Service<
7
+ ServiceId extends string = string,
8
+ ServiceBox extends Record<string, unknown> = Record<string, unknown>,
9
+ > = {
10
+ serviceId: ServiceId;
11
+ serviceBox: ServiceBox;
12
+ };
13
+
14
+ export type ExecuteTransactionOutput = {
15
+ rawRpcResult: unknown;
16
+ };
17
+
18
+ export type Signer<ServiceId extends string = string> = {
19
+ serviceId: ServiceId;
20
+ safeExecuteTransaction: (args: {
21
+ intent: TransactionIntent;
22
+ }) => Promise<Result<ExecuteTransactionOutput, unknown>>;
23
+ };
24
+
25
+ export type ServiceCreator<
26
+ ServiceId extends string = string,
27
+ ServiceBox extends Record<string, unknown> = Record<string, unknown>,
28
+ > = {
29
+ serviceId: ServiceId;
30
+ createService(): Service<ServiceId, ServiceBox>;
31
+ createSigner(args: {
32
+ signerAccountId: AccountId;
33
+ serviceBox: ServiceBox;
34
+ client: Client;
35
+ }): Signer<ServiceId>;
36
+ };
@@ -0,0 +1,99 @@
1
+ import type { NearConnector } from '@hot-labs/near-connect';
2
+ import type { TransactionIntent, PublicKey, AccountId } from 'near-api-ts';
3
+ import type { Result } from '../_common.ts';
4
+ import type { ServiceCreator } from './_common.ts';
5
+
6
+ type CreateAccountAction = {
7
+ createAccount: {};
8
+ };
9
+
10
+ type TransferAction = {
11
+ transfer: {
12
+ deposit: bigint;
13
+ };
14
+ };
15
+
16
+ type AddKeyAction = {
17
+ addKey: {
18
+ publicKey: PublicKey;
19
+ accessKey: {
20
+ nonce: bigint;
21
+ permission:
22
+ | { fullAccess: {} }
23
+ | {
24
+ functionCall: {
25
+ receiverId: string;
26
+ allowance?: bigint;
27
+ methodNames?: string[];
28
+ };
29
+ };
30
+ };
31
+ };
32
+ };
33
+
34
+ type FunctionCallAction = {
35
+ functionCall: {
36
+ methodName: string;
37
+ args: Uint8Array;
38
+ gas: bigint;
39
+ deposit: bigint;
40
+ };
41
+ };
42
+
43
+ type DeployContractAction = {
44
+ deployContract: {
45
+ code: Uint8Array;
46
+ };
47
+ };
48
+
49
+ type StakeAction = {
50
+ stake: {
51
+ stake: bigint;
52
+ publicKey: PublicKey;
53
+ };
54
+ };
55
+
56
+ type DeleteKeyAction = {
57
+ deleteKey: {
58
+ publicKey: PublicKey;
59
+ };
60
+ };
61
+
62
+ type DeleteAccountAction = {
63
+ deleteAccount: {
64
+ beneficiaryId: AccountId;
65
+ };
66
+ };
67
+
68
+ export type NearConnectAction =
69
+ | CreateAccountAction
70
+ | TransferAction
71
+ | AddKeyAction
72
+ | FunctionCallAction
73
+ | DeployContractAction
74
+ | StakeAction
75
+ | DeleteKeyAction
76
+ | DeleteAccountAction;
77
+
78
+ type ExecuteTransactionArgs = {
79
+ intent: TransactionIntent;
80
+ };
81
+
82
+ type ExecuteTransactionOutput = {
83
+ rawRpcResult: unknown;
84
+ };
85
+
86
+ export type CreateSafeExecuteTransaction = (
87
+ connector: NearConnector,
88
+ ) => (args: ExecuteTransactionArgs) => Promise<Result<ExecuteTransactionOutput, unknown>>;
89
+
90
+ export type NearConnectNetworkId = 'mainnet' | 'testnet';
91
+
92
+ export type NearConnectServiceCreator = ServiceCreator<
93
+ 'nearConnector',
94
+ { connector: NearConnector }
95
+ >;
96
+
97
+ export type CreateNearConnectorService = (args: {
98
+ networkId: NearConnectNetworkId;
99
+ }) => NearConnectServiceCreator;
package/types/store.ts ADDED
@@ -0,0 +1,30 @@
1
+ import type { Client, AccountId } from 'near-api-ts';
2
+ import type { Service, ServiceCreator, Signer, ServiceId } from './services/_common';
3
+ import type { StoreApi } from 'zustand';
4
+ import type { NearConnectServiceCreator } from './services/nearConnect.ts';
5
+
6
+ export type StoreContext = {
7
+ serviceCreators: ServiceCreator[];
8
+ client: Client;
9
+ services: Record<ServiceId, Service>;
10
+ signers: Signer[];
11
+ };
12
+
13
+ export type NearState = {
14
+ networkId: string;
15
+ connectedAccountId?: AccountId;
16
+ getContext: () => StoreContext;
17
+ setSigners: (connectedAccountId: AccountId) => void;
18
+ clearSigners: () => void;
19
+ setConnectedAccountId: (connectedAccountId?: AccountId) => void;
20
+ };
21
+
22
+ export type CreateNearStoreArgs = {
23
+ networkId: string;
24
+ clientCreator: () => Client;
25
+ serviceCreator: NearConnectServiceCreator;
26
+ storeName?: string;
27
+ };
28
+
29
+ export type NearStore = StoreApi<NearState>;
30
+ export type CreateNearStore = (args: CreateNearStoreArgs) => NearStore;