near-kit 0.0.0 → 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 +21 -0
- package/README.md +371 -2
- package/dist/contracts/contract.d.ts +63 -0
- package/dist/contracts/contract.d.ts.map +1 -0
- package/dist/contracts/contract.js +42 -0
- package/dist/contracts/contract.js.map +1 -0
- package/dist/contracts/index.d.ts +5 -0
- package/dist/contracts/index.d.ts.map +1 -0
- package/dist/contracts/index.js +5 -0
- package/dist/contracts/index.js.map +1 -0
- package/dist/core/actions.d.ts +193 -0
- package/dist/core/actions.d.ts.map +1 -0
- package/dist/core/actions.js +195 -0
- package/dist/core/actions.js.map +1 -0
- package/dist/core/config-schemas.d.ts +179 -0
- package/dist/core/config-schemas.d.ts.map +1 -0
- package/dist/core/config-schemas.js +169 -0
- package/dist/core/config-schemas.js.map +1 -0
- package/dist/core/constants.d.ts +43 -0
- package/dist/core/constants.d.ts.map +1 -0
- package/dist/core/constants.js +49 -0
- package/dist/core/constants.js.map +1 -0
- package/dist/core/near.d.ts +301 -0
- package/dist/core/near.d.ts.map +1 -0
- package/dist/core/near.js +504 -0
- package/dist/core/near.js.map +1 -0
- package/dist/core/nonce-manager.d.ts +39 -0
- package/dist/core/nonce-manager.d.ts.map +1 -0
- package/dist/core/nonce-manager.js +73 -0
- package/dist/core/nonce-manager.js.map +1 -0
- package/dist/core/rpc/rpc-error-handler.d.ts +60 -0
- package/dist/core/rpc/rpc-error-handler.d.ts.map +1 -0
- package/dist/core/rpc/rpc-error-handler.js +324 -0
- package/dist/core/rpc/rpc-error-handler.js.map +1 -0
- package/dist/core/rpc/rpc-schemas.d.ts +1812 -0
- package/dist/core/rpc/rpc-schemas.d.ts.map +1 -0
- package/dist/core/rpc/rpc-schemas.js +424 -0
- package/dist/core/rpc/rpc-schemas.js.map +1 -0
- package/dist/core/rpc/rpc.d.ts +117 -0
- package/dist/core/rpc/rpc.d.ts.map +1 -0
- package/dist/core/rpc/rpc.js +325 -0
- package/dist/core/rpc/rpc.js.map +1 -0
- package/dist/core/schema.d.ts +1188 -0
- package/dist/core/schema.d.ts.map +1 -0
- package/dist/core/schema.js +396 -0
- package/dist/core/schema.js.map +1 -0
- package/dist/core/transaction.d.ts +390 -0
- package/dist/core/transaction.d.ts.map +1 -0
- package/dist/core/transaction.js +649 -0
- package/dist/core/transaction.js.map +1 -0
- package/dist/core/types.d.ts +271 -0
- package/dist/core/types.d.ts.map +1 -0
- package/dist/core/types.js +9 -0
- package/dist/core/types.js.map +1 -0
- package/dist/errors/index.d.ts +226 -0
- package/dist/errors/index.d.ts.map +1 -0
- package/dist/errors/index.js +366 -0
- package/dist/errors/index.js.map +1 -0
- package/dist/index.d.ts +16 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +23 -0
- package/dist/index.js.map +1 -0
- package/dist/keys/credential-schemas.d.ts +98 -0
- package/dist/keys/credential-schemas.d.ts.map +1 -0
- package/dist/keys/credential-schemas.js +128 -0
- package/dist/keys/credential-schemas.js.map +1 -0
- package/dist/keys/file-keystore.d.ts +130 -0
- package/dist/keys/file-keystore.d.ts.map +1 -0
- package/dist/keys/file-keystore.js +266 -0
- package/dist/keys/file-keystore.js.map +1 -0
- package/dist/keys/in-memory-keystore.d.ts +71 -0
- package/dist/keys/in-memory-keystore.d.ts.map +1 -0
- package/dist/keys/in-memory-keystore.js +85 -0
- package/dist/keys/in-memory-keystore.js.map +1 -0
- package/dist/keys/index.d.ts +12 -0
- package/dist/keys/index.d.ts.map +1 -0
- package/dist/keys/index.js +18 -0
- package/dist/keys/index.js.map +1 -0
- package/dist/keys/native-keystore.d.ts +111 -0
- package/dist/keys/native-keystore.d.ts.map +1 -0
- package/dist/keys/native-keystore.js +167 -0
- package/dist/keys/native-keystore.js.map +1 -0
- package/dist/sandbox/index.d.ts +6 -0
- package/dist/sandbox/index.d.ts.map +1 -0
- package/dist/sandbox/index.js +5 -0
- package/dist/sandbox/index.js.map +1 -0
- package/dist/sandbox/sandbox.d.ts +55 -0
- package/dist/sandbox/sandbox.d.ts.map +1 -0
- package/dist/sandbox/sandbox.js +341 -0
- package/dist/sandbox/sandbox.js.map +1 -0
- package/dist/utils/amount.d.ts +76 -0
- package/dist/utils/amount.d.ts.map +1 -0
- package/dist/utils/amount.js +137 -0
- package/dist/utils/amount.js.map +1 -0
- package/dist/utils/gas.d.ts +69 -0
- package/dist/utils/gas.d.ts.map +1 -0
- package/dist/utils/gas.js +92 -0
- package/dist/utils/gas.js.map +1 -0
- package/dist/utils/index.d.ts +14 -0
- package/dist/utils/index.d.ts.map +1 -0
- package/dist/utils/index.js +14 -0
- package/dist/utils/index.js.map +1 -0
- package/dist/utils/key.d.ts +117 -0
- package/dist/utils/key.d.ts.map +1 -0
- package/dist/utils/key.js +270 -0
- package/dist/utils/key.js.map +1 -0
- package/dist/utils/nep413.d.ts +97 -0
- package/dist/utils/nep413.d.ts.map +1 -0
- package/dist/utils/nep413.js +154 -0
- package/dist/utils/nep413.js.map +1 -0
- package/dist/utils/validation.d.ts +114 -0
- package/dist/utils/validation.d.ts.map +1 -0
- package/dist/utils/validation.js +150 -0
- package/dist/utils/validation.js.map +1 -0
- package/dist/wallets/adapters.d.ts +119 -0
- package/dist/wallets/adapters.d.ts.map +1 -0
- package/dist/wallets/adapters.js +267 -0
- package/dist/wallets/adapters.js.map +1 -0
- package/dist/wallets/index.d.ts +11 -0
- package/dist/wallets/index.d.ts.map +1 -0
- package/dist/wallets/index.js +2 -0
- package/dist/wallets/index.js.map +1 -0
- package/dist/wallets/types.d.ts +99 -0
- package/dist/wallets/types.d.ts.map +1 -0
- package/dist/wallets/types.js +10 -0
- package/dist/wallets/types.js.map +1 -0
- package/package.json +78 -7
- package/index.js +0 -1
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 r-near
|
|
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
CHANGED
|
@@ -1,6 +1,375 @@
|
|
|
1
1
|
# near-kit
|
|
2
|
+
[](https://www.npmjs.com/package/near-kit)
|
|
3
|
+
[](https://opensource.org/licenses/MIT)
|
|
4
|
+
[](https://www.typescriptlang.org/)
|
|
2
5
|
|
|
3
|
-
|
|
6
|
+
A simple, intuitive TypeScript library for interacting with NEAR Protocol. Designed to feel like a modern fetch library - easy for beginners, powerful for advanced users.
|
|
4
7
|
|
|
5
|
-
|
|
8
|
+
**[📚 Full Documentation](https://kit.near.tools)**
|
|
6
9
|
|
|
10
|
+
## Features
|
|
11
|
+
|
|
12
|
+
- **Simple things should be simple** - One-line commands for common operations
|
|
13
|
+
- **Type safety everywhere** - Full TypeScript support with IDE autocomplete
|
|
14
|
+
- **Progressive complexity** - Basic API for simple needs, advanced features when required
|
|
15
|
+
- **Powerful transaction builder** - Fluent, human-readable API for transactions
|
|
16
|
+
- **Wallet-ready** - Full support for [HOT Connector](https://github.com/azbang/hot-connector) and [NEAR Wallet Selector](https://github.com/near/wallet-selector), drop-in integration
|
|
17
|
+
|
|
18
|
+
## Installation
|
|
19
|
+
|
|
20
|
+
```bash
|
|
21
|
+
npm install near-kit
|
|
22
|
+
# or
|
|
23
|
+
bun install near-kit
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
## Quick Start
|
|
27
|
+
|
|
28
|
+
```typescript
|
|
29
|
+
import { Near } from 'near-kit';
|
|
30
|
+
|
|
31
|
+
// Initialize with a private key for signing transactions
|
|
32
|
+
const near = new Near({
|
|
33
|
+
network: 'testnet',
|
|
34
|
+
privateKey: 'ed25519:...', // Your account's private key
|
|
35
|
+
defaultSignerId: 'alice.testnet' // Default account for signing
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
// View a contract method (read-only, no gas)
|
|
39
|
+
const balance = await near.view('example.testnet', 'get_balance', {
|
|
40
|
+
account_id: 'alice.testnet'
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
// Call a contract method (requires signature, costs gas)
|
|
44
|
+
await near.call('example.testnet', 'increment', {}, {
|
|
45
|
+
attachedDeposit: '0.1' // Attach 0.1 NEAR
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
// Send NEAR tokens
|
|
49
|
+
await near.send('bob.testnet', '5'); // Send 5 NEAR to Bob
|
|
50
|
+
|
|
51
|
+
// Check account balance
|
|
52
|
+
const accountBalance = await near.getBalance('alice.testnet');
|
|
53
|
+
console.log(accountBalance); // "100.00 NEAR"
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
## Core API
|
|
57
|
+
|
|
58
|
+
### Initialization
|
|
59
|
+
|
|
60
|
+
```typescript
|
|
61
|
+
// Simple - defaults to mainnet
|
|
62
|
+
const near = new Near();
|
|
63
|
+
|
|
64
|
+
// With network selection
|
|
65
|
+
const near = new Near({ network: 'testnet' });
|
|
66
|
+
|
|
67
|
+
// With custom configuration
|
|
68
|
+
const near = new Near({
|
|
69
|
+
network: 'testnet',
|
|
70
|
+
privateKey: 'ed25519:...',
|
|
71
|
+
});
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
### Basic Operations
|
|
75
|
+
|
|
76
|
+
```typescript
|
|
77
|
+
// View methods (free, no signature required)
|
|
78
|
+
const result = await near.view(
|
|
79
|
+
'contract.near',
|
|
80
|
+
'get_data',
|
|
81
|
+
{ key: 'value' }
|
|
82
|
+
);
|
|
83
|
+
|
|
84
|
+
// Check account balance
|
|
85
|
+
const balance = await near.getBalance('alice.near');
|
|
86
|
+
|
|
87
|
+
// Check if account exists
|
|
88
|
+
const exists = await near.accountExists('alice.near');
|
|
89
|
+
|
|
90
|
+
// Get network status
|
|
91
|
+
const status = await near.getStatus();
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
### Type-Safe Contracts
|
|
95
|
+
|
|
96
|
+
```typescript
|
|
97
|
+
import type { Contract } from 'near-kit';
|
|
98
|
+
|
|
99
|
+
// Define contract interface using Contract<> helper
|
|
100
|
+
type MyContract = Contract<{
|
|
101
|
+
view: {
|
|
102
|
+
get_balance: (args: { account_id: string }) => Promise<string>
|
|
103
|
+
get_info: () => Promise<{ name: string; version: string }>
|
|
104
|
+
}
|
|
105
|
+
call: {
|
|
106
|
+
// Just define args - options parameter automatically added!
|
|
107
|
+
transfer: (args: { to: string; amount: string }) => Promise<void>
|
|
108
|
+
}
|
|
109
|
+
}>
|
|
110
|
+
|
|
111
|
+
// Create type-safe contract
|
|
112
|
+
const contract = near.contract<MyContract>('example.near');
|
|
113
|
+
|
|
114
|
+
// Fully typed method calls
|
|
115
|
+
const balance = await contract.view.get_balance({ account_id: 'alice.near' });
|
|
116
|
+
const info = await contract.view.get_info();
|
|
117
|
+
|
|
118
|
+
// Call methods automatically get options parameter
|
|
119
|
+
await contract.call.transfer(
|
|
120
|
+
{ to: 'bob.near', amount: '10' },
|
|
121
|
+
{ attachedDeposit: '1 NEAR' }
|
|
122
|
+
);
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
### Transaction Builder
|
|
126
|
+
|
|
127
|
+
```typescript
|
|
128
|
+
// Alice builds a transaction with multiple actions
|
|
129
|
+
// 'alice.near' is the signer - the account that signs and pays for this transaction
|
|
130
|
+
const receipt = await near.transaction('alice.near') // Alice signs
|
|
131
|
+
.transfer('bob.near', '10') // Alice sends Bob 10 NEAR
|
|
132
|
+
.functionCall('market.near', 'buy', { id: '123' }, {
|
|
133
|
+
attachedDeposit: '5' // Alice attaches 5 NEAR to the call
|
|
134
|
+
})
|
|
135
|
+
.send();
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
### Batch Operations
|
|
139
|
+
|
|
140
|
+
```typescript
|
|
141
|
+
// Run multiple operations in parallel
|
|
142
|
+
const [balance, status, exists] = await near.batch(
|
|
143
|
+
near.getBalance('alice.near'),
|
|
144
|
+
near.getStatus(),
|
|
145
|
+
near.accountExists('bob.near')
|
|
146
|
+
);
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
## Local Testing with Sandbox
|
|
150
|
+
|
|
151
|
+
```typescript
|
|
152
|
+
import { Sandbox } from 'near-kit';
|
|
153
|
+
|
|
154
|
+
const sandbox = await Sandbox.start();
|
|
155
|
+
const near = new Near({ network: sandbox });
|
|
156
|
+
// ... run tests
|
|
157
|
+
await sandbox.stop();
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
**With test framework:**
|
|
161
|
+
```typescript
|
|
162
|
+
let sandbox: Sandbox;
|
|
163
|
+
beforeAll(async () => { sandbox = await Sandbox.start(); });
|
|
164
|
+
afterAll(async () => { await sandbox.stop(); });
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
## Key Management
|
|
168
|
+
|
|
169
|
+
```typescript
|
|
170
|
+
import { InMemoryKeyStore, FileKeyStore } from 'near-kit';
|
|
171
|
+
|
|
172
|
+
// In-memory (runtime only)
|
|
173
|
+
const near = new Near({
|
|
174
|
+
keyStore: new InMemoryKeyStore({
|
|
175
|
+
'alice.near': 'ed25519:...',
|
|
176
|
+
})
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
// File-based (persistent)
|
|
180
|
+
const near = new Near({
|
|
181
|
+
keyStore: new FileKeyStore('~/.near-credentials')
|
|
182
|
+
});
|
|
183
|
+
```
|
|
184
|
+
|
|
185
|
+
## Wallet Integration
|
|
186
|
+
|
|
187
|
+
near-kit works seamlessly with popular NEAR wallets - just pass the wallet adapter and all methods will use the wallet for signing.
|
|
188
|
+
|
|
189
|
+
### NEAR Wallet Selector
|
|
190
|
+
|
|
191
|
+
```typescript
|
|
192
|
+
import { Near, fromWalletSelector } from 'near-kit';
|
|
193
|
+
import { setupWalletSelector } from '@near-wallet-selector/core';
|
|
194
|
+
import { setupMyNearWallet } from '@near-wallet-selector/my-near-wallet';
|
|
195
|
+
import { setupHereWallet } from '@near-wallet-selector/here-wallet';
|
|
196
|
+
|
|
197
|
+
// Setup wallet selector
|
|
198
|
+
const selector = await setupWalletSelector({
|
|
199
|
+
network: 'testnet',
|
|
200
|
+
modules: [
|
|
201
|
+
setupMyNearWallet(),
|
|
202
|
+
setupHereWallet(),
|
|
203
|
+
],
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
// Get wallet instance (after user connects)
|
|
207
|
+
const wallet = await selector.wallet();
|
|
208
|
+
|
|
209
|
+
// Use with near-kit
|
|
210
|
+
const near = new Near({
|
|
211
|
+
network: 'testnet',
|
|
212
|
+
wallet: fromWalletSelector(wallet),
|
|
213
|
+
});
|
|
214
|
+
|
|
215
|
+
// All operations now use the wallet for signing
|
|
216
|
+
await near.call('contract.near', 'method', { arg: 'value' });
|
|
217
|
+
await near.send('bob.near', '10');
|
|
218
|
+
```
|
|
219
|
+
|
|
220
|
+
### HOT Connector
|
|
221
|
+
|
|
222
|
+
```typescript
|
|
223
|
+
import { Near, fromHotConnect } from 'near-kit';
|
|
224
|
+
import { NearConnector } from '@hot-labs/near-connect';
|
|
225
|
+
|
|
226
|
+
// Create connector
|
|
227
|
+
const connector = new NearConnector({ network: 'testnet' });
|
|
228
|
+
|
|
229
|
+
// Wait for user to connect
|
|
230
|
+
connector.on('wallet:signIn', async () => {
|
|
231
|
+
const near = new Near({
|
|
232
|
+
network: 'testnet',
|
|
233
|
+
wallet: fromHotConnect(connector),
|
|
234
|
+
});
|
|
235
|
+
|
|
236
|
+
// Use near-kit with the connected wallet
|
|
237
|
+
await near.call('contract.near', 'method', { arg: 'value' });
|
|
238
|
+
});
|
|
239
|
+
|
|
240
|
+
// Trigger wallet connection
|
|
241
|
+
await connector.signIn();
|
|
242
|
+
```
|
|
243
|
+
|
|
244
|
+
## Error Handling
|
|
245
|
+
|
|
246
|
+
```typescript
|
|
247
|
+
import {
|
|
248
|
+
InsufficientBalanceError,
|
|
249
|
+
FunctionCallError,
|
|
250
|
+
NetworkError,
|
|
251
|
+
} from 'near-kit';
|
|
252
|
+
|
|
253
|
+
try {
|
|
254
|
+
await near.call('contract.near', 'method', {});
|
|
255
|
+
} catch (error) {
|
|
256
|
+
if (error instanceof InsufficientBalanceError) {
|
|
257
|
+
console.log(`Need ${error.required}, have ${error.available}`);
|
|
258
|
+
} else if (error instanceof FunctionCallError) {
|
|
259
|
+
console.log(`Contract error: ${error.panic}`);
|
|
260
|
+
} else if (error instanceof NetworkError) {
|
|
261
|
+
// Retry logic
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
```
|
|
265
|
+
|
|
266
|
+
## Advanced Features
|
|
267
|
+
|
|
268
|
+
### Batch Actions (Multi-Action Transactions)
|
|
269
|
+
|
|
270
|
+
Deploy and initialize a contract in a single transaction:
|
|
271
|
+
|
|
272
|
+
```typescript
|
|
273
|
+
const contractWasm = await fs.readFile('./contract.wasm');
|
|
274
|
+
|
|
275
|
+
await near.transaction('alice.near')
|
|
276
|
+
.createAccount('contract.alice.near')
|
|
277
|
+
.transfer('contract.alice.near', '10 NEAR')
|
|
278
|
+
.deployContract('contract.alice.near', contractWasm)
|
|
279
|
+
.functionCall('contract.alice.near', 'init', { owner: 'alice.near' })
|
|
280
|
+
.send();
|
|
281
|
+
```
|
|
282
|
+
|
|
283
|
+
### NEP-413 Message Signing
|
|
284
|
+
|
|
285
|
+
Authenticate users without gas fees:
|
|
286
|
+
|
|
287
|
+
```typescript
|
|
288
|
+
const signedMessage = await near.signMessage({
|
|
289
|
+
message: 'Login to MyApp',
|
|
290
|
+
recipient: 'myapp.near',
|
|
291
|
+
nonce: crypto.getRandomValues(new Uint8Array(32))
|
|
292
|
+
});
|
|
293
|
+
|
|
294
|
+
// Send to backend for verification
|
|
295
|
+
await fetch('/api/auth', {
|
|
296
|
+
method: 'POST',
|
|
297
|
+
body: JSON.stringify(signedMessage)
|
|
298
|
+
});
|
|
299
|
+
```
|
|
300
|
+
|
|
301
|
+
### Delegate Actions (NEP-366)
|
|
302
|
+
|
|
303
|
+
Enable meta-transactions and sponsored transactions where a relayer pays the gas:
|
|
304
|
+
|
|
305
|
+
```typescript
|
|
306
|
+
// User creates and signs a delegate action (no gas cost to user)
|
|
307
|
+
const userNear = new Near({
|
|
308
|
+
network: 'testnet',
|
|
309
|
+
privateKey: 'ed25519:...' // User's key
|
|
310
|
+
});
|
|
311
|
+
|
|
312
|
+
const signedDelegateAction = await userNear
|
|
313
|
+
.transaction('user.near')
|
|
314
|
+
.transfer('recipient.near', '1 NEAR')
|
|
315
|
+
.delegate({ blockHeightOffset: 100 });
|
|
316
|
+
|
|
317
|
+
// Relayer submits the transaction (pays the gas)
|
|
318
|
+
const relayerNear = new Near({
|
|
319
|
+
network: 'testnet',
|
|
320
|
+
privateKey: 'ed25519:...' // Relayer's key
|
|
321
|
+
});
|
|
322
|
+
|
|
323
|
+
await relayerNear
|
|
324
|
+
.transaction('relayer.near')
|
|
325
|
+
.signedDelegateAction(signedDelegateAction)
|
|
326
|
+
.send();
|
|
327
|
+
```
|
|
328
|
+
|
|
329
|
+
### Automatic Nonce Management
|
|
330
|
+
|
|
331
|
+
No more nonce conflicts - the library handles nonce tracking and retries automatically:
|
|
332
|
+
|
|
333
|
+
```typescript
|
|
334
|
+
// Safe to run multiple transactions concurrently
|
|
335
|
+
await Promise.all([
|
|
336
|
+
near.send('bob.near', '1'),
|
|
337
|
+
near.send('charlie.near', '1'),
|
|
338
|
+
near.send('dave.near', '1')
|
|
339
|
+
]);
|
|
340
|
+
// Nonces are automatically managed and conflicts are retried
|
|
341
|
+
```
|
|
342
|
+
|
|
343
|
+
### Smart Retry Logic
|
|
344
|
+
|
|
345
|
+
Automatic retries for network errors with exponential backoff:
|
|
346
|
+
|
|
347
|
+
```typescript
|
|
348
|
+
try {
|
|
349
|
+
await near.call('contract.near', 'method', {});
|
|
350
|
+
} catch (error) {
|
|
351
|
+
if (error instanceof TimeoutError && error.retryable) {
|
|
352
|
+
// Already retried automatically
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
```
|
|
356
|
+
|
|
357
|
+
## Development
|
|
358
|
+
|
|
359
|
+
```bash
|
|
360
|
+
# Install dependencies
|
|
361
|
+
bun install
|
|
362
|
+
|
|
363
|
+
# Run tests
|
|
364
|
+
bun test
|
|
365
|
+
|
|
366
|
+
# Build
|
|
367
|
+
bun run build
|
|
368
|
+
|
|
369
|
+
# Run examples
|
|
370
|
+
bun run examples/basic-usage.ts
|
|
371
|
+
```
|
|
372
|
+
|
|
373
|
+
## License
|
|
374
|
+
|
|
375
|
+
MIT
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import type { Near } from "../core/near.js";
|
|
2
|
+
import type { CallOptions } from "../core/types.js";
|
|
3
|
+
/**
|
|
4
|
+
* Utility type to automatically add options parameter to call methods.
|
|
5
|
+
*
|
|
6
|
+
* Usage:
|
|
7
|
+
* ```typescript
|
|
8
|
+
* type MyContract = Contract<{
|
|
9
|
+
* view: {
|
|
10
|
+
* get_count: () => Promise<number>
|
|
11
|
+
* }
|
|
12
|
+
* call: {
|
|
13
|
+
* increment: (args: { amount: number }) => Promise<void>
|
|
14
|
+
* }
|
|
15
|
+
* }>
|
|
16
|
+
* ```
|
|
17
|
+
*
|
|
18
|
+
* The call method will automatically get an options parameter:
|
|
19
|
+
* increment: (args: { amount: number }, options?: CallOptions) => Promise<void>
|
|
20
|
+
*/
|
|
21
|
+
export type Contract<T extends {
|
|
22
|
+
view: Record<string, (...args: any[]) => any>;
|
|
23
|
+
call: Record<string, (...args: any[]) => any>;
|
|
24
|
+
}> = {
|
|
25
|
+
view: T["view"];
|
|
26
|
+
call: {
|
|
27
|
+
[K in keyof T["call"]]: T["call"][K] extends (...args: infer TArgs) => infer TReturn ? TArgs extends [infer TFirstArg, ...infer _Rest] ? (args: TFirstArg, options?: CallOptions) => TReturn : (args?: undefined, options?: CallOptions) => TReturn : never;
|
|
28
|
+
};
|
|
29
|
+
};
|
|
30
|
+
/**
|
|
31
|
+
* Base contract method interface used by {@link Contract}.
|
|
32
|
+
*
|
|
33
|
+
* Methods can be defined as:
|
|
34
|
+
* - View methods: (args?: ArgsType | Uint8Array, options?: BlockReference) => Promise<ReturnType>
|
|
35
|
+
* - Call methods: (args?: ArgsType | Uint8Array, options?: CallOptions) => Promise<ReturnType>
|
|
36
|
+
*
|
|
37
|
+
* This is a base interface without index signatures to allow
|
|
38
|
+
* extending interfaces to define specific method signatures with type safety.
|
|
39
|
+
*/
|
|
40
|
+
export interface ContractMethods {
|
|
41
|
+
view: Record<string, (...args: any[]) => Promise<any>>;
|
|
42
|
+
call: Record<string, (...args: any[]) => Promise<any>>;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Create a type-safe contract proxy for a NEAR contract.
|
|
46
|
+
*
|
|
47
|
+
* @param near - The {@link Near} client instance to use for calls.
|
|
48
|
+
* @param contractId - Account ID of the target contract.
|
|
49
|
+
*
|
|
50
|
+
* @returns A proxy implementing the given {@link ContractMethods} interface.
|
|
51
|
+
*
|
|
52
|
+
* @remarks
|
|
53
|
+
* Prefer using {@link Near.contract} instead of calling this function directly.
|
|
54
|
+
*/
|
|
55
|
+
export declare function createContract<T extends ContractMethods>(near: Near, contractId: string): T;
|
|
56
|
+
/**
|
|
57
|
+
* Helper to extend {@link Near} prototype with a `contract` method.
|
|
58
|
+
*
|
|
59
|
+
* @internal This is used to keep the core {@link Near} implementation focused;
|
|
60
|
+
* library users should call {@link Near.contract} instead of using this helper.
|
|
61
|
+
*/
|
|
62
|
+
export declare function addContractMethod(nearPrototype: typeof Near.prototype): void;
|
|
63
|
+
//# sourceMappingURL=contract.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"contract.d.ts","sourceRoot":"","sources":["../../src/contracts/contract.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,iBAAiB,CAAA;AAC3C,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAA;AAEnD;;;;;;;;;;;;;;;;;GAiBG;AACH,MAAM,MAAM,QAAQ,CAClB,CAAC,SAAS;IAER,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,GAAG,CAAC,CAAA;IAE7C,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,GAAG,CAAC,CAAA;CAC9C,IACC;IACF,IAAI,EAAE,CAAC,CAAC,MAAM,CAAC,CAAA;IACf,IAAI,EAAE;SACH,CAAC,IAAI,MAAM,CAAC,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,SAAS,CAC3C,GAAG,IAAI,EAAE,MAAM,KAAK,KACjB,MAAM,OAAO,GACd,KAAK,SAAS,CAAC,MAAM,SAAS,EAAE,GAAG,MAAM,KAAK,CAAC,GAC7C,CAAC,IAAI,EAAE,SAAS,EAAE,OAAO,CAAC,EAAE,WAAW,KAAK,OAAO,GACnD,CAAC,IAAI,CAAC,EAAE,SAAS,EAAE,OAAO,CAAC,EAAE,WAAW,KAAK,OAAO,GACtD,KAAK;KACV,CAAA;CACF,CAAA;AAED;;;;;;;;;GASG;AACH,MAAM,WAAW,eAAe;IAE9B,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,OAAO,CAAC,GAAG,CAAC,CAAC,CAAA;IAEtD,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,OAAO,CAAC,GAAG,CAAC,CAAC,CAAA;CACvD;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,cAAc,CAAC,CAAC,SAAS,eAAe,EACtD,IAAI,EAAE,IAAI,EACV,UAAU,EAAE,MAAM,GACjB,CAAC,CAiCH;AAED;;;;;GAKG;AACH,wBAAgB,iBAAiB,CAAC,aAAa,EAAE,OAAO,IAAI,CAAC,SAAS,GAAG,IAAI,CAO5E"}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Create a type-safe contract proxy for a NEAR contract.
|
|
3
|
+
*
|
|
4
|
+
* @param near - The {@link Near} client instance to use for calls.
|
|
5
|
+
* @param contractId - Account ID of the target contract.
|
|
6
|
+
*
|
|
7
|
+
* @returns A proxy implementing the given {@link ContractMethods} interface.
|
|
8
|
+
*
|
|
9
|
+
* @remarks
|
|
10
|
+
* Prefer using {@link Near.contract} instead of calling this function directly.
|
|
11
|
+
*/
|
|
12
|
+
export function createContract(near, contractId) {
|
|
13
|
+
const proxy = {
|
|
14
|
+
view: new Proxy({}, {
|
|
15
|
+
get: (_target, methodName) => {
|
|
16
|
+
return async (args, options) => {
|
|
17
|
+
return await near.view(contractId, methodName, args || {}, options);
|
|
18
|
+
};
|
|
19
|
+
},
|
|
20
|
+
}),
|
|
21
|
+
call: new Proxy({}, {
|
|
22
|
+
get: (_target, methodName) => {
|
|
23
|
+
return async (args, options) => {
|
|
24
|
+
return await near.call(contractId, methodName, args || {}, options || {});
|
|
25
|
+
};
|
|
26
|
+
},
|
|
27
|
+
}),
|
|
28
|
+
};
|
|
29
|
+
return proxy;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Helper to extend {@link Near} prototype with a `contract` method.
|
|
33
|
+
*
|
|
34
|
+
* @internal This is used to keep the core {@link Near} implementation focused;
|
|
35
|
+
* library users should call {@link Near.contract} instead of using this helper.
|
|
36
|
+
*/
|
|
37
|
+
export function addContractMethod(nearPrototype) {
|
|
38
|
+
nearPrototype.contract = function (contractId) {
|
|
39
|
+
return createContract(this, contractId);
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
//# sourceMappingURL=contract.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"contract.js","sourceRoot":"","sources":["../../src/contracts/contract.ts"],"names":[],"mappings":"AA2DA;;;;;;;;;;GAUG;AACH,MAAM,UAAU,cAAc,CAC5B,IAAU,EACV,UAAkB;IAElB,MAAM,KAAK,GAAG;QACZ,IAAI,EAAE,IAAI,KAAK,CACb,EAAE,EACF;YACE,GAAG,EAAE,CAAC,OAAO,EAAE,UAAkB,EAAE,EAAE;gBACnC,OAAO,KAAK,EACV,IAA0B,EAC1B,OAAwB,EACxB,EAAE;oBACF,OAAO,MAAM,IAAI,CAAC,IAAI,CAAC,UAAU,EAAE,UAAU,EAAE,IAAI,IAAI,EAAE,EAAE,OAAO,CAAC,CAAA;gBACrE,CAAC,CAAA;YACH,CAAC;SACF,CACF;QACD,IAAI,EAAE,IAAI,KAAK,CACb,EAAE,EACF;YACE,GAAG,EAAE,CAAC,OAAO,EAAE,UAAkB,EAAE,EAAE;gBACnC,OAAO,KAAK,EAAE,IAA0B,EAAE,OAAqB,EAAE,EAAE;oBACjE,OAAO,MAAM,IAAI,CAAC,IAAI,CACpB,UAAU,EACV,UAAU,EACV,IAAI,IAAI,EAAE,EACV,OAAO,IAAI,EAAE,CACd,CAAA;gBACH,CAAC,CAAA;YACH,CAAC;SACF,CACF;KACF,CAAA;IAED,OAAO,KAAU,CAAA;AACnB,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,iBAAiB,CAAC,aAAoC;IACpE,aAAa,CAAC,QAAQ,GAAG,UAEvB,UAAkB;QAElB,OAAO,cAAc,CAAI,IAAI,EAAE,UAAU,CAAC,CAAA;IAC5C,CAAC,CAAA;AACH,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/contracts/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,cAAc,eAAe,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/contracts/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,cAAc,eAAe,CAAA"}
|