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.
- package/LICENSE.md +21 -0
- package/README.md +187 -0
- package/dist/index.d.mts +175 -0
- package/dist/index.mjs +283 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +73 -0
- package/src/_common/utils/result.ts +6 -0
- package/src/hooks/useAccountInfo.ts +23 -0
- package/src/hooks/useConnectedAccount.ts +9 -0
- package/src/hooks/useContractReadFunction.ts +33 -0
- package/src/hooks/useExecuteTransaction.ts +34 -0
- package/src/hooks/useNearConnector.ts +51 -0
- package/src/index.ts +44 -0
- package/src/providers/MainnetNearProvider.tsx +16 -0
- package/src/providers/NearProvider.tsx +24 -0
- package/src/providers/TestnetNearProdiver.tsx +16 -0
- package/src/services/memory/memory.ts +22 -0
- package/src/services/nearConnector/executeTransaction/executeTransaction.ts +19 -0
- package/src/services/nearConnector/executeTransaction/toNearConnectActions.ts +83 -0
- package/src/services/nearConnector/nearConnector.ts +24 -0
- package/src/store/NearStoreProvider.tsx +25 -0
- package/src/store/nearStore.ts +68 -0
- package/types/_common.ts +7 -0
- package/types/hooks/useAccountInfo.ts +23 -0
- package/types/hooks/useContractReadFunction.ts +78 -0
- package/types/hooks/useExecuteTransaction.ts +14 -0
- package/types/hooks/useNearConnect.ts +6 -0
- package/types/services/_common.ts +36 -0
- package/types/services/nearConnect.ts +99 -0
- package/types/store.ts +30 -0
package/LICENSE.md
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright(c) 2025–2026 Volodymyr Bilyk (volodymyr@lantstool.dev)
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
# react-near-ts
|
|
2
|
+
|
|
3
|
+
TypeScript-first React wrapper for `near-api-ts` with built-in wallet connection
|
|
4
|
+
via `@hot-labs/near-connect`.
|
|
5
|
+
|
|
6
|
+
## Installation
|
|
7
|
+
|
|
8
|
+
```bash
|
|
9
|
+
pnpm add react-near-ts react react-dom @tanstack/react-query zod
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
## Quick Start
|
|
13
|
+
|
|
14
|
+
For mainnet:
|
|
15
|
+
|
|
16
|
+
```tsx
|
|
17
|
+
import { MainnetNearProvider } from 'react-near-ts';
|
|
18
|
+
|
|
19
|
+
export const App = () => (
|
|
20
|
+
<MainnetNearProvider>
|
|
21
|
+
<h1>Hello, Near!</h1>
|
|
22
|
+
</MainnetNearProvider>
|
|
23
|
+
);
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
For testnet:
|
|
27
|
+
|
|
28
|
+
```tsx
|
|
29
|
+
import { TestnetNearProvider } from 'react-near-ts';
|
|
30
|
+
|
|
31
|
+
export const App = () => (
|
|
32
|
+
<TestnetNearProvider>
|
|
33
|
+
<h1>Hello, Near!</h1>
|
|
34
|
+
</TestnetNearProvider>
|
|
35
|
+
);
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
### Custom setup
|
|
39
|
+
|
|
40
|
+
```tsx
|
|
41
|
+
import {
|
|
42
|
+
NearProvider,
|
|
43
|
+
createNearStore,
|
|
44
|
+
createClient,
|
|
45
|
+
createNearConnectorService,
|
|
46
|
+
} from 'react-near-ts';
|
|
47
|
+
|
|
48
|
+
const clientCreator = () => createClient({
|
|
49
|
+
transport: {
|
|
50
|
+
rpcEndpoints: {
|
|
51
|
+
regular: [{ url: 'https://free.rpc.fastnear.com' }],
|
|
52
|
+
archival: [{ url: 'https://1rpc.io/near' }],
|
|
53
|
+
},
|
|
54
|
+
},
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
const nearStore = createNearStore({
|
|
58
|
+
networkId: 'mainnet',
|
|
59
|
+
clientCreator,
|
|
60
|
+
serviceCreator: createNearConnectorService({ networkId: 'mainnet' }),
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
export const App = ({ children }: { children: React.ReactNode }) => (
|
|
64
|
+
<NearProvider nearStore={nearStore}>{children}</NearProvider>
|
|
65
|
+
);
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
## Hooks
|
|
69
|
+
|
|
70
|
+
### `useNearConnector`
|
|
71
|
+
|
|
72
|
+
Connect/disconnect wallet.
|
|
73
|
+
|
|
74
|
+
```tsx
|
|
75
|
+
import { useNearConnector } from 'react-near-ts';
|
|
76
|
+
|
|
77
|
+
const { connect, disconnect } = useNearConnector();
|
|
78
|
+
|
|
79
|
+
<button onClick={() => connect.mutate()}>Connect</button>
|
|
80
|
+
<button onClick={() => disconnect.mutate()}>Disconnect</button>
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
### `useConnectedAccount`
|
|
84
|
+
|
|
85
|
+
Read current connected account id.
|
|
86
|
+
|
|
87
|
+
```tsx
|
|
88
|
+
import { useConnectedAccount } from 'react-near-ts';
|
|
89
|
+
|
|
90
|
+
const { connectedAccountId, isConnectedAccount } = useConnectedAccount();
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
### `useAccountInfo`
|
|
94
|
+
|
|
95
|
+
Fetch account info via JSON RPC.
|
|
96
|
+
|
|
97
|
+
```tsx
|
|
98
|
+
import { useAccountInfo } from 'react-near-ts';
|
|
99
|
+
|
|
100
|
+
const accountInfo = useAccountInfo({ accountId: 'example.testnet' });
|
|
101
|
+
|
|
102
|
+
if (accountInfo.isSuccess) {
|
|
103
|
+
console.log(accountInfo.data.accountInfo.balance.total.near);
|
|
104
|
+
}
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
### `useContractReadFunction`
|
|
108
|
+
|
|
109
|
+
Call read-only contract methods.
|
|
110
|
+
|
|
111
|
+
```tsx
|
|
112
|
+
import {
|
|
113
|
+
useContractReadFunction,
|
|
114
|
+
fromJsonBytes,
|
|
115
|
+
type DeserializeResultFnArgs,
|
|
116
|
+
} from 'react-near-ts';
|
|
117
|
+
import * as z from 'zod/mini';
|
|
118
|
+
|
|
119
|
+
const ResultSchema = z.array(z.string());
|
|
120
|
+
|
|
121
|
+
const deserializeResult = ({ rawResult }: DeserializeResultFnArgs) =>
|
|
122
|
+
ResultSchema.parse(fromJsonBytes(rawResult));
|
|
123
|
+
|
|
124
|
+
const records = useContractReadFunction({
|
|
125
|
+
contractAccountId: 'react-near-ts.lantstool.testnet',
|
|
126
|
+
functionName: 'get_records',
|
|
127
|
+
functionArgs: { author_id: 'example.testnet' },
|
|
128
|
+
withStateAt: 'LatestOptimisticBlock',
|
|
129
|
+
options: { deserializeResult },
|
|
130
|
+
});
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
### `useExecuteTransaction`
|
|
134
|
+
|
|
135
|
+
Send signed transaction from connected wallet.
|
|
136
|
+
|
|
137
|
+
```tsx
|
|
138
|
+
import {
|
|
139
|
+
transfer,
|
|
140
|
+
functionCall,
|
|
141
|
+
useExecuteTransaction,
|
|
142
|
+
} from 'react-near-ts';
|
|
143
|
+
|
|
144
|
+
const executeTransaction = useExecuteTransaction();
|
|
145
|
+
|
|
146
|
+
// Transfer
|
|
147
|
+
executeTransaction.mutate({
|
|
148
|
+
intent: {
|
|
149
|
+
action: transfer({ amount: { near: '0.1' } }),
|
|
150
|
+
receiverAccountId: 'receiver.testnet',
|
|
151
|
+
},
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
// Function call
|
|
155
|
+
executeTransaction.mutate({
|
|
156
|
+
intent: {
|
|
157
|
+
action: functionCall({
|
|
158
|
+
functionName: 'add_record',
|
|
159
|
+
functionArgs: { record: 'hello' },
|
|
160
|
+
gasLimit: { teraGas: '10' },
|
|
161
|
+
}),
|
|
162
|
+
receiverAccountId: 'react-near-ts.lantstool.testnet',
|
|
163
|
+
},
|
|
164
|
+
});
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
## Re-exports from `near-api-ts`
|
|
168
|
+
|
|
169
|
+
`react-near-ts` also re-exports common client creators, action creators and
|
|
170
|
+
utils, including:
|
|
171
|
+
|
|
172
|
+
- `createMainnetClient`, `createTestnetClient`, `createClient`
|
|
173
|
+
- `transfer`, `functionCall`, `createAccount`, `stake`, ...
|
|
174
|
+
- `near`, `yoctoNear`, `teraGas`, `fromJsonBytes`, `toJsonBytes`, ...
|
|
175
|
+
|
|
176
|
+
## Playground
|
|
177
|
+
|
|
178
|
+
See a full working example (Next.js App Router):
|
|
179
|
+
|
|
180
|
+
- `playgrounds/react-near-ts/next-app-router`
|
|
181
|
+
|
|
182
|
+
It demonstrates:
|
|
183
|
+
|
|
184
|
+
- wallet connect/disconnect
|
|
185
|
+
- account info fetch
|
|
186
|
+
- token transfer
|
|
187
|
+
- contract read/write flows
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
import { UseMutationResult, UseQueryResult } from "@tanstack/react-query";
|
|
2
|
+
import { ReactNode } from "react";
|
|
3
|
+
import * as react_jsx_runtime0 from "react/jsx-runtime";
|
|
4
|
+
import { NearConnector } from "@hot-labs/near-connect";
|
|
5
|
+
import { AccountId, BaseDeserializeResultFn, BaseSerializeArgsFn, BlockReference, CallContractReadFunctionError, CallContractReadFunctionOutput, Client, ContractFunctionName, DeserializeResultFnArgs, GetAccountInfoError, GetAccountInfoOutput, MaybeBaseDeserializeResultFn, MaybeBaseSerializeArgsFn, MaybeJsonLikeValue, PartialTransportPolicy, TransactionIntent, addFullAccessKey, addFunctionCallKey, createAccount, createClient, createMainnetClient, createTestnetClient, deleteAccount, deleteKey, deployContract, fromJsonBytes, functionCall, gas, keyPair, near, nearGas, nearToken, randomEd25519KeyPair, randomSecp256k1KeyPair, stake, teraGas, toJsonBytes, transfer, yoctoNear } from "near-api-ts";
|
|
6
|
+
import { StoreApi } from "zustand";
|
|
7
|
+
|
|
8
|
+
//#region types/hooks/useAccountInfo.d.ts
|
|
9
|
+
type UseAccountInfoArgs = {
|
|
10
|
+
accountId?: AccountId;
|
|
11
|
+
atMomentOf?: BlockReference;
|
|
12
|
+
policies?: {
|
|
13
|
+
transport?: PartialTransportPolicy;
|
|
14
|
+
};
|
|
15
|
+
query?: {
|
|
16
|
+
enabled?: boolean;
|
|
17
|
+
};
|
|
18
|
+
};
|
|
19
|
+
type UseAccountInfo = (args: UseAccountInfoArgs) => UseQueryResult<GetAccountInfoOutput, GetAccountInfoError>;
|
|
20
|
+
//#endregion
|
|
21
|
+
//#region src/hooks/useAccountInfo.d.ts
|
|
22
|
+
declare const useAccountInfo: UseAccountInfo;
|
|
23
|
+
//#endregion
|
|
24
|
+
//#region src/hooks/useConnectedAccount.d.ts
|
|
25
|
+
declare const useConnectedAccount: () => {
|
|
26
|
+
connectedAccountId: string | undefined;
|
|
27
|
+
isConnectedAccount: boolean;
|
|
28
|
+
};
|
|
29
|
+
//#endregion
|
|
30
|
+
//#region types/_common.d.ts
|
|
31
|
+
type KeyIf<K extends PropertyKey, V> = [V] extends [undefined] ? { [P in K]?: never } : { [P in K]: V };
|
|
32
|
+
type ResultOk<V> = {
|
|
33
|
+
ok: true;
|
|
34
|
+
value: V;
|
|
35
|
+
};
|
|
36
|
+
type ResultErr<E> = {
|
|
37
|
+
ok: false;
|
|
38
|
+
error: E;
|
|
39
|
+
};
|
|
40
|
+
type Result<V, E> = ResultOk<V> | ResultErr<E>;
|
|
41
|
+
//#endregion
|
|
42
|
+
//#region types/hooks/useContractReadFunction.d.ts
|
|
43
|
+
type BaseUseContractReadFunctionArgs = {
|
|
44
|
+
contractAccountId?: AccountId;
|
|
45
|
+
functionName?: ContractFunctionName;
|
|
46
|
+
withStateAt?: BlockReference;
|
|
47
|
+
policies?: {
|
|
48
|
+
transport?: PartialTransportPolicy;
|
|
49
|
+
};
|
|
50
|
+
query?: {
|
|
51
|
+
enabled?: boolean;
|
|
52
|
+
};
|
|
53
|
+
};
|
|
54
|
+
type Options<A, SR extends MaybeBaseSerializeArgsFn<A>, DR extends MaybeBaseDeserializeResultFn> = [SR, DR] extends [undefined, undefined] ? {
|
|
55
|
+
options?: {
|
|
56
|
+
serializeArgs?: never;
|
|
57
|
+
deserializeResult?: never;
|
|
58
|
+
};
|
|
59
|
+
} : {
|
|
60
|
+
options: KeyIf<'serializeArgs', SR> & KeyIf<'deserializeResult', DR>;
|
|
61
|
+
};
|
|
62
|
+
type FunctionArgsOf<SA> = SA extends ((args: {
|
|
63
|
+
functionArgs: infer T;
|
|
64
|
+
}) => Uint8Array) ? T : undefined;
|
|
65
|
+
type CallOutput<DR extends MaybeBaseDeserializeResultFn> = [DR] extends [BaseDeserializeResultFn] ? CallContractReadFunctionOutput<ReturnType<DR>> : CallContractReadFunctionOutput<unknown>;
|
|
66
|
+
type FunctionArgs<A> = KeyIf<'functionArgs', A>;
|
|
67
|
+
type UseContractReadFunction = {
|
|
68
|
+
<A extends MaybeJsonLikeValue = undefined>(args: BaseUseContractReadFunctionArgs & FunctionArgs<A> & Options<undefined, undefined, undefined>): UseQueryResult<CallOutput<undefined>, CallContractReadFunctionError>;
|
|
69
|
+
<DR extends BaseDeserializeResultFn, A extends MaybeJsonLikeValue = undefined>(args: BaseUseContractReadFunctionArgs & FunctionArgs<A> & Options<undefined, undefined, DR>): UseQueryResult<CallOutput<DR>, CallContractReadFunctionError>;
|
|
70
|
+
<SA extends BaseSerializeArgsFn<A>, DR extends MaybeBaseDeserializeResultFn = undefined, A = FunctionArgsOf<SA>>(args: BaseUseContractReadFunctionArgs & FunctionArgs<A> & Options<A, SA, DR>): UseQueryResult<CallOutput<DR>, CallContractReadFunctionError>;
|
|
71
|
+
};
|
|
72
|
+
//#endregion
|
|
73
|
+
//#region src/hooks/useContractReadFunction.d.ts
|
|
74
|
+
declare const useContractReadFunction: UseContractReadFunction;
|
|
75
|
+
//#endregion
|
|
76
|
+
//#region types/services/_common.d.ts
|
|
77
|
+
type ServiceId = string;
|
|
78
|
+
type Service<ServiceId extends string = string, ServiceBox extends Record<string, unknown> = Record<string, unknown>> = {
|
|
79
|
+
serviceId: ServiceId;
|
|
80
|
+
serviceBox: ServiceBox;
|
|
81
|
+
};
|
|
82
|
+
type ExecuteTransactionOutput = {
|
|
83
|
+
rawRpcResult: unknown;
|
|
84
|
+
};
|
|
85
|
+
type Signer<ServiceId extends string = string> = {
|
|
86
|
+
serviceId: ServiceId;
|
|
87
|
+
safeExecuteTransaction: (args: {
|
|
88
|
+
intent: TransactionIntent;
|
|
89
|
+
}) => Promise<Result<ExecuteTransactionOutput, unknown>>;
|
|
90
|
+
};
|
|
91
|
+
type ServiceCreator<ServiceId extends string = string, ServiceBox extends Record<string, unknown> = Record<string, unknown>> = {
|
|
92
|
+
serviceId: ServiceId;
|
|
93
|
+
createService(): Service<ServiceId, ServiceBox>;
|
|
94
|
+
createSigner(args: {
|
|
95
|
+
signerAccountId: AccountId;
|
|
96
|
+
serviceBox: ServiceBox;
|
|
97
|
+
client: Client;
|
|
98
|
+
}): Signer<ServiceId>;
|
|
99
|
+
};
|
|
100
|
+
//#endregion
|
|
101
|
+
//#region types/hooks/useExecuteTransaction.d.ts
|
|
102
|
+
type ExecuteTransactionArgs = {
|
|
103
|
+
intent: TransactionIntent;
|
|
104
|
+
};
|
|
105
|
+
type UseExecuteTransaction = () => UseMutationResult<ExecuteTransactionOutput, Error, ExecuteTransactionArgs, unknown>;
|
|
106
|
+
//#endregion
|
|
107
|
+
//#region src/hooks/useExecuteTransaction.d.ts
|
|
108
|
+
declare const useExecuteTransaction: UseExecuteTransaction;
|
|
109
|
+
//#endregion
|
|
110
|
+
//#region types/hooks/useNearConnect.d.ts
|
|
111
|
+
type UseNearConnect = () => {
|
|
112
|
+
connect: UseMutationResult<void, Error, void, unknown>;
|
|
113
|
+
disconnect: UseMutationResult<void, Error, void, unknown>;
|
|
114
|
+
};
|
|
115
|
+
//#endregion
|
|
116
|
+
//#region src/hooks/useNearConnector.d.ts
|
|
117
|
+
declare const useNearConnector: UseNearConnect;
|
|
118
|
+
//#endregion
|
|
119
|
+
//#region src/providers/MainnetNearProvider.d.ts
|
|
120
|
+
declare const MainnetNearProvider: (props: {
|
|
121
|
+
children: ReactNode;
|
|
122
|
+
}) => react_jsx_runtime0.JSX.Element;
|
|
123
|
+
//#endregion
|
|
124
|
+
//#region types/store.d.ts
|
|
125
|
+
type StoreContext = {
|
|
126
|
+
serviceCreators: ServiceCreator[];
|
|
127
|
+
client: Client;
|
|
128
|
+
services: Record<ServiceId, Service>;
|
|
129
|
+
signers: Signer[];
|
|
130
|
+
};
|
|
131
|
+
type NearState = {
|
|
132
|
+
networkId: string;
|
|
133
|
+
connectedAccountId?: AccountId;
|
|
134
|
+
getContext: () => StoreContext;
|
|
135
|
+
setSigners: (connectedAccountId: AccountId) => void;
|
|
136
|
+
clearSigners: () => void;
|
|
137
|
+
setConnectedAccountId: (connectedAccountId?: AccountId) => void;
|
|
138
|
+
};
|
|
139
|
+
type CreateNearStoreArgs = {
|
|
140
|
+
networkId: string;
|
|
141
|
+
clientCreator: () => Client;
|
|
142
|
+
serviceCreators: ServiceCreator[];
|
|
143
|
+
storeName?: string;
|
|
144
|
+
};
|
|
145
|
+
type NearStore = StoreApi<NearState>;
|
|
146
|
+
type CreateNearStore = (args: CreateNearStoreArgs) => NearStore;
|
|
147
|
+
//#endregion
|
|
148
|
+
//#region src/providers/NearProvider.d.ts
|
|
149
|
+
type NearProviderProps = {
|
|
150
|
+
nearStore: NearStore;
|
|
151
|
+
children: ReactNode;
|
|
152
|
+
};
|
|
153
|
+
declare const NearProvider: (props: NearProviderProps) => react_jsx_runtime0.JSX.Element;
|
|
154
|
+
//#endregion
|
|
155
|
+
//#region src/providers/TestnetNearProdiver.d.ts
|
|
156
|
+
declare const TestnetNearProvider: (props: {
|
|
157
|
+
children: ReactNode;
|
|
158
|
+
}) => react_jsx_runtime0.JSX.Element;
|
|
159
|
+
//#endregion
|
|
160
|
+
//#region src/store/nearStore.d.ts
|
|
161
|
+
declare const createNearStore: CreateNearStore;
|
|
162
|
+
//#endregion
|
|
163
|
+
//#region types/services/nearConnect.d.ts
|
|
164
|
+
type NearConnectNetworkId = 'mainnet' | 'testnet';
|
|
165
|
+
type CreateNearConnectorService = (args: {
|
|
166
|
+
networkId: NearConnectNetworkId;
|
|
167
|
+
}) => ServiceCreator<'nearConnector', {
|
|
168
|
+
connector: NearConnector;
|
|
169
|
+
}>;
|
|
170
|
+
//#endregion
|
|
171
|
+
//#region src/services/nearConnector/nearConnector.d.ts
|
|
172
|
+
declare const createNearConnectorService: CreateNearConnectorService;
|
|
173
|
+
//#endregion
|
|
174
|
+
export { type DeserializeResultFnArgs, MainnetNearProvider, NearProvider, TestnetNearProvider, addFullAccessKey, addFunctionCallKey, createAccount, createClient, createMainnetClient, createNearConnectorService, createNearStore, createTestnetClient, deleteAccount, deleteKey, deployContract, fromJsonBytes, functionCall, gas, keyPair, near, nearGas, nearToken, randomEd25519KeyPair, randomSecp256k1KeyPair, stake, teraGas, toJsonBytes, transfer, useAccountInfo, useConnectedAccount, useContractReadFunction, useExecuteTransaction, useNearConnector, yoctoNear };
|
|
175
|
+
//# sourceMappingURL=index.d.mts.map
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,283 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
|
|
3
|
+
import { QueryClient, QueryClientProvider, skipToken, useMutation, useQuery } from "@tanstack/react-query";
|
|
4
|
+
import { createContext, useContext } from "react";
|
|
5
|
+
import { useStore } from "zustand/react";
|
|
6
|
+
import { jsx } from "react/jsx-runtime";
|
|
7
|
+
import * as z from "zod/mini";
|
|
8
|
+
import { NearConnector } from "@hot-labs/near-connect";
|
|
9
|
+
import { createStore } from "zustand/vanilla";
|
|
10
|
+
import { persist } from "zustand/middleware";
|
|
11
|
+
import { addFullAccessKey, addFunctionCallKey, createAccount, createClient, createMainnetClient, createMainnetClient as createMainnetClient$1, createTestnetClient, createTestnetClient as createTestnetClient$1, deleteAccount, deleteKey, deployContract, fromJsonBytes, functionCall, gas, keyPair, near, nearGas, nearGas as nearGas$1, nearToken, nearToken as nearToken$1, randomEd25519KeyPair, randomSecp256k1KeyPair, stake, teraGas, toJsonBytes, transfer, yoctoNear } from "near-api-ts";
|
|
12
|
+
|
|
13
|
+
//#region src/store/NearStoreProvider.tsx
|
|
14
|
+
const NearStoreContext = createContext(null);
|
|
15
|
+
const NearStoreProvider = (props) => /* @__PURE__ */ jsx(NearStoreContext.Provider, {
|
|
16
|
+
value: props.nearStore,
|
|
17
|
+
children: props.children
|
|
18
|
+
});
|
|
19
|
+
const useNearStore = (selector) => {
|
|
20
|
+
const store = useContext(NearStoreContext);
|
|
21
|
+
if (!store) throw new Error("useNearStore must be used within NearStoreProvider");
|
|
22
|
+
return useStore(store, selector);
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
//#endregion
|
|
26
|
+
//#region src/hooks/useAccountInfo.ts
|
|
27
|
+
const useAccountInfo = (args) => {
|
|
28
|
+
const context = useNearStore((store) => store.getContext)();
|
|
29
|
+
const { accountId, query, ...rest } = args;
|
|
30
|
+
return useQuery({
|
|
31
|
+
queryKey: ["getAccountInfo", accountId],
|
|
32
|
+
queryFn: accountId ? (args) => context.client.getAccountInfo({
|
|
33
|
+
...rest,
|
|
34
|
+
accountId,
|
|
35
|
+
options: { signal: args.signal }
|
|
36
|
+
}) : skipToken,
|
|
37
|
+
enabled: query?.enabled ?? true
|
|
38
|
+
});
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
//#endregion
|
|
42
|
+
//#region src/hooks/useConnectedAccount.ts
|
|
43
|
+
const useConnectedAccount = () => {
|
|
44
|
+
const connectedAccountId = useNearStore((store) => store.connectedAccountId);
|
|
45
|
+
return {
|
|
46
|
+
connectedAccountId,
|
|
47
|
+
isConnectedAccount: typeof connectedAccountId === "string"
|
|
48
|
+
};
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
//#endregion
|
|
52
|
+
//#region src/hooks/useContractReadFunction.ts
|
|
53
|
+
const useContractReadFunction = (args) => {
|
|
54
|
+
const context = useNearStore((store) => store.getContext)();
|
|
55
|
+
const { query, contractAccountId, functionName, ...rest } = args;
|
|
56
|
+
return useQuery({
|
|
57
|
+
queryKey: [
|
|
58
|
+
"callContractReadFunction",
|
|
59
|
+
contractAccountId,
|
|
60
|
+
functionName,
|
|
61
|
+
rest.functionArgs
|
|
62
|
+
],
|
|
63
|
+
queryFn: contractAccountId && functionName ? ({ signal }) => context.client.callContractReadFunction({
|
|
64
|
+
...rest,
|
|
65
|
+
contractAccountId,
|
|
66
|
+
functionName,
|
|
67
|
+
options: {
|
|
68
|
+
...rest.options,
|
|
69
|
+
signal
|
|
70
|
+
}
|
|
71
|
+
}) : skipToken,
|
|
72
|
+
enabled: query?.enabled ?? true
|
|
73
|
+
});
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
//#endregion
|
|
77
|
+
//#region src/hooks/useExecuteTransaction.ts
|
|
78
|
+
const tryOnManySigners = async (args, context) => {
|
|
79
|
+
const signers = context.signers;
|
|
80
|
+
if (signers.length === 0) throw new Error("No signers available");
|
|
81
|
+
const executeTransaction = async (signerIndex) => {
|
|
82
|
+
const result = await signers[signerIndex].safeExecuteTransaction({ intent: args.intent });
|
|
83
|
+
if (result.ok) return result.value;
|
|
84
|
+
throw result.error;
|
|
85
|
+
};
|
|
86
|
+
return executeTransaction(0);
|
|
87
|
+
};
|
|
88
|
+
const useExecuteTransaction = () => {
|
|
89
|
+
const context = useNearStore((s) => s.getContext)();
|
|
90
|
+
return useMutation({ mutationFn: (args) => tryOnManySigners(args, context) });
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
//#endregion
|
|
94
|
+
//#region src/hooks/useNearConnector.ts
|
|
95
|
+
const NearConnectorServiceSchema = z.object({ nearConnector: z.object({ serviceBox: z.object({ connector: z.instanceof(NearConnector) }) }) });
|
|
96
|
+
const useNearConnector = () => {
|
|
97
|
+
const getContext = useNearStore((store) => store.getContext);
|
|
98
|
+
const setSigners = useNearStore((store) => store.setSigners);
|
|
99
|
+
const clearSigners = useNearStore((store) => store.clearSigners);
|
|
100
|
+
const setConnectedAccountId = useNearStore((store) => store.setConnectedAccountId);
|
|
101
|
+
const context = getContext();
|
|
102
|
+
return {
|
|
103
|
+
connect: useMutation({ mutationFn: async () => {
|
|
104
|
+
const connectedAccountId = (await (await NearConnectorServiceSchema.parse(context.services).nearConnector.serviceBox.connector.connect()).getAccounts())[0].accountId;
|
|
105
|
+
setSigners(connectedAccountId);
|
|
106
|
+
setConnectedAccountId(connectedAccountId);
|
|
107
|
+
} }),
|
|
108
|
+
disconnect: useMutation({ mutationFn: async () => {
|
|
109
|
+
await NearConnectorServiceSchema.parse(context.services).nearConnector.serviceBox.connector.disconnect();
|
|
110
|
+
clearSigners();
|
|
111
|
+
setConnectedAccountId(void 0);
|
|
112
|
+
} })
|
|
113
|
+
};
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
//#endregion
|
|
117
|
+
//#region src/store/nearStore.ts
|
|
118
|
+
const createNearStore = (args) => {
|
|
119
|
+
const { storeName = "react-near-ts", networkId, clientCreator, serviceCreators } = args;
|
|
120
|
+
const services = serviceCreators.reduce((acc, creator) => {
|
|
121
|
+
acc[creator.serviceId] = creator.createService();
|
|
122
|
+
return acc;
|
|
123
|
+
}, {});
|
|
124
|
+
const context = {
|
|
125
|
+
serviceCreators,
|
|
126
|
+
client: clientCreator(),
|
|
127
|
+
services,
|
|
128
|
+
signers: []
|
|
129
|
+
};
|
|
130
|
+
const getContext = () => context;
|
|
131
|
+
const setSigners = (connectedAccountId) => {
|
|
132
|
+
context.signers = context.serviceCreators.map((creator) => creator.createSigner({
|
|
133
|
+
signerAccountId: connectedAccountId,
|
|
134
|
+
client: context.client,
|
|
135
|
+
serviceBox: context.services[creator.serviceId].serviceBox
|
|
136
|
+
}));
|
|
137
|
+
};
|
|
138
|
+
const clearSigners = () => {
|
|
139
|
+
context.signers = [];
|
|
140
|
+
};
|
|
141
|
+
return createStore()(persist((set) => ({
|
|
142
|
+
networkId,
|
|
143
|
+
connectedAccountId: void 0,
|
|
144
|
+
getContext,
|
|
145
|
+
setSigners,
|
|
146
|
+
clearSigners,
|
|
147
|
+
setConnectedAccountId: (connectedAccountId) => {
|
|
148
|
+
set(() => ({ connectedAccountId }));
|
|
149
|
+
}
|
|
150
|
+
}), {
|
|
151
|
+
name: `${storeName}:${networkId}`,
|
|
152
|
+
version: 1,
|
|
153
|
+
partialize: (s) => ({ connectedAccountId: s.connectedAccountId }),
|
|
154
|
+
onRehydrateStorage: () => (state) => {
|
|
155
|
+
if (state?.connectedAccountId) state.setSigners(state.connectedAccountId);
|
|
156
|
+
}
|
|
157
|
+
}));
|
|
158
|
+
};
|
|
159
|
+
|
|
160
|
+
//#endregion
|
|
161
|
+
//#region src/_common/utils/result.ts
|
|
162
|
+
const result = {
|
|
163
|
+
ok: (value) => ({
|
|
164
|
+
ok: true,
|
|
165
|
+
value
|
|
166
|
+
}),
|
|
167
|
+
err: (error) => ({
|
|
168
|
+
ok: false,
|
|
169
|
+
error
|
|
170
|
+
})
|
|
171
|
+
};
|
|
172
|
+
|
|
173
|
+
//#endregion
|
|
174
|
+
//#region src/services/nearConnector/executeTransaction/toNearConnectActions.ts
|
|
175
|
+
const toNearConnectAction = (action) => {
|
|
176
|
+
if (action.actionType === "CreateAccount") return { createAccount: {} };
|
|
177
|
+
if (action.actionType === "Transfer") return { transfer: { deposit: nearToken$1(action.amount).yoctoNear } };
|
|
178
|
+
if (action.actionType === "AddKey") return { addKey: {
|
|
179
|
+
publicKey: action.publicKey,
|
|
180
|
+
accessKey: {
|
|
181
|
+
nonce: 0n,
|
|
182
|
+
permission: action.accessType === "FullAccess" ? { fullAccess: {} } : { functionCall: {
|
|
183
|
+
receiverId: action.contractAccountId,
|
|
184
|
+
methodNames: action.allowedFunctions,
|
|
185
|
+
allowance: action.gasBudget ? nearToken$1(action.gasBudget).yoctoNear : void 0
|
|
186
|
+
} }
|
|
187
|
+
}
|
|
188
|
+
} };
|
|
189
|
+
if (action.actionType === "FunctionCall") return { functionCall: {
|
|
190
|
+
methodName: action.functionName,
|
|
191
|
+
args: action.functionArgs,
|
|
192
|
+
gas: nearGas$1(action.gasLimit).gas,
|
|
193
|
+
deposit: action.attachedDeposit ? nearToken$1(action.attachedDeposit).yoctoNear : 0n
|
|
194
|
+
} };
|
|
195
|
+
if (action.actionType === "DeployContract") return { deployContract: { code: action.wasmBytes } };
|
|
196
|
+
if (action.actionType === "Stake") return { stake: {
|
|
197
|
+
stake: nearToken$1(action.amount).yoctoNear,
|
|
198
|
+
publicKey: action.validatorPublicKey
|
|
199
|
+
} };
|
|
200
|
+
if (action.actionType === "DeleteKey") return { deleteKey: { publicKey: action.publicKey } };
|
|
201
|
+
if (action.actionType === "DeleteAccount") return { deleteAccount: { beneficiaryId: action.beneficiaryAccountId } };
|
|
202
|
+
throw new Error(`Unsupported action type: ${action}`);
|
|
203
|
+
};
|
|
204
|
+
const toNearConnectActions = (intent) => {
|
|
205
|
+
if (intent.action) return [toNearConnectAction(intent.action)];
|
|
206
|
+
if (intent.actions) return intent.actions.map((action) => toNearConnectAction(action));
|
|
207
|
+
return [];
|
|
208
|
+
};
|
|
209
|
+
|
|
210
|
+
//#endregion
|
|
211
|
+
//#region src/services/nearConnector/executeTransaction/executeTransaction.ts
|
|
212
|
+
const createSafeExecuteTransaction = (connector) => async (args) => {
|
|
213
|
+
try {
|
|
214
|
+
const finalExecutionOutcome = await (await connector.wallet()).signAndSendTransaction({
|
|
215
|
+
actions: toNearConnectActions(args.intent),
|
|
216
|
+
receiverId: args.intent.receiverAccountId
|
|
217
|
+
});
|
|
218
|
+
return result.ok({ rawRpcResult: finalExecutionOutcome });
|
|
219
|
+
} catch (e) {
|
|
220
|
+
return result.err(e);
|
|
221
|
+
}
|
|
222
|
+
};
|
|
223
|
+
|
|
224
|
+
//#endregion
|
|
225
|
+
//#region src/services/nearConnector/nearConnector.ts
|
|
226
|
+
const serviceId = "nearConnector";
|
|
227
|
+
const createNearConnectorService = (args) => ({
|
|
228
|
+
serviceId,
|
|
229
|
+
createService: () => {
|
|
230
|
+
return {
|
|
231
|
+
serviceId,
|
|
232
|
+
serviceBox: { connector: new NearConnector({ network: args.networkId }) }
|
|
233
|
+
};
|
|
234
|
+
},
|
|
235
|
+
createSigner: (args) => {
|
|
236
|
+
return {
|
|
237
|
+
serviceId,
|
|
238
|
+
safeExecuteTransaction: createSafeExecuteTransaction(args.serviceBox.connector)
|
|
239
|
+
};
|
|
240
|
+
}
|
|
241
|
+
});
|
|
242
|
+
|
|
243
|
+
//#endregion
|
|
244
|
+
//#region src/providers/NearProvider.tsx
|
|
245
|
+
const queryClient = new QueryClient({ defaultOptions: {
|
|
246
|
+
queries: { retry: false },
|
|
247
|
+
mutations: { retry: false }
|
|
248
|
+
} });
|
|
249
|
+
const NearProvider = (props) => /* @__PURE__ */ jsx(NearStoreProvider, {
|
|
250
|
+
nearStore: props.nearStore,
|
|
251
|
+
children: /* @__PURE__ */ jsx(QueryClientProvider, {
|
|
252
|
+
client: queryClient,
|
|
253
|
+
children: props.children
|
|
254
|
+
})
|
|
255
|
+
});
|
|
256
|
+
|
|
257
|
+
//#endregion
|
|
258
|
+
//#region src/providers/MainnetNearProvider.tsx
|
|
259
|
+
const createMainnetNearStore = () => createNearStore({
|
|
260
|
+
networkId: "mainnet",
|
|
261
|
+
clientCreator: createMainnetClient$1,
|
|
262
|
+
serviceCreators: [createNearConnectorService({ networkId: "mainnet" })]
|
|
263
|
+
});
|
|
264
|
+
const MainnetNearProvider = (props) => /* @__PURE__ */ jsx(NearProvider, {
|
|
265
|
+
nearStore: createMainnetNearStore(),
|
|
266
|
+
children: props.children
|
|
267
|
+
});
|
|
268
|
+
|
|
269
|
+
//#endregion
|
|
270
|
+
//#region src/providers/TestnetNearProdiver.tsx
|
|
271
|
+
const createTestnetNearStore = () => createNearStore({
|
|
272
|
+
networkId: "testnet",
|
|
273
|
+
clientCreator: createTestnetClient$1,
|
|
274
|
+
serviceCreators: [createNearConnectorService({ networkId: "testnet" })]
|
|
275
|
+
});
|
|
276
|
+
const TestnetNearProvider = (props) => /* @__PURE__ */ jsx(NearProvider, {
|
|
277
|
+
nearStore: createTestnetNearStore(),
|
|
278
|
+
children: props.children
|
|
279
|
+
});
|
|
280
|
+
|
|
281
|
+
//#endregion
|
|
282
|
+
export { MainnetNearProvider, NearProvider, TestnetNearProvider, addFullAccessKey, addFunctionCallKey, createAccount, createClient, createMainnetClient, createNearConnectorService, createNearStore, createTestnetClient, deleteAccount, deleteKey, deployContract, fromJsonBytes, functionCall, gas, keyPair, near, nearGas, nearToken, randomEd25519KeyPair, randomSecp256k1KeyPair, stake, teraGas, toJsonBytes, transfer, useAccountInfo, useConnectedAccount, useContractReadFunction, useExecuteTransaction, useNearConnector, yoctoNear };
|
|
283
|
+
//# sourceMappingURL=index.mjs.map
|