@t2000/sdk 0.2.5 → 0.2.6

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.
Files changed (2) hide show
  1. package/README.md +88 -25
  2. package/package.json +1 -1
package/README.md CHANGED
@@ -24,7 +24,10 @@ yarn add @t2000/sdk
24
24
  ```typescript
25
25
  import { T2000 } from '@t2000/sdk';
26
26
 
27
- // Create or load a bank account
27
+ // Create a new bank account
28
+ const { agent, address } = await T2000.init({ pin: 'my-secret' });
29
+
30
+ // Or load an existing one
28
31
  const agent = await T2000.create({ pin: 'my-secret' });
29
32
 
30
33
  // Check balance
@@ -46,30 +49,53 @@ await agent.borrow({ amount: 20, asset: 'USDC' });
46
49
 
47
50
  ## API Reference
48
51
 
49
- ### `T2000.create(options)`
52
+ ### `T2000.init(options)` — Create a new wallet
53
+
54
+ Creates a new bank account (generates keypair, encrypts, and saves to disk).
55
+
56
+ ```typescript
57
+ const { agent, address, sponsored } = await T2000.init({
58
+ pin: 'my-secret', // Required — encrypts the key
59
+ keyPath: '~/.t2000/wallet.key', // Optional — custom key file path
60
+ name: 'my-agent', // Optional — agent name for sponsor registration
61
+ sponsored: true, // Optional — register with gas station (default: true)
62
+ });
63
+ ```
64
+
65
+ ### `T2000.create(options)` — Load an existing wallet
50
66
 
51
- Creates a new bank account or loads an existing one.
67
+ Loads an existing bank account from an encrypted key file. Throws `WALLET_NOT_FOUND` if no wallet exists.
52
68
 
53
69
  ```typescript
54
70
  const agent = await T2000.create({
55
- pin: 'my-secret', // Required — encrypts/decrypts the key
56
- network: 'mainnet', // 'mainnet' | 'testnet' (default: 'mainnet')
57
- rpcUrl: 'https://...', // Custom RPC endpoint (optional)
58
- keyPath: '~/.t2000/wallet.key', // Custom key file path (optional)
71
+ pin: 'my-secret', // Required — decrypts the key
72
+ keyPath: '~/.t2000/wallet.key', // Optional custom key file path
73
+ rpcUrl: 'https://...', // Optional — custom Sui RPC endpoint
59
74
  });
60
75
  ```
61
76
 
77
+ ### `T2000.fromPrivateKey(key, options?)` — Load from raw key
78
+
79
+ Synchronous factory that creates an agent from a raw private key (bech32 `suiprivkey1...` or hex).
80
+
81
+ ```typescript
82
+ const agent = T2000.fromPrivateKey('suiprivkey1q...');
83
+ ```
84
+
62
85
  ### Core Methods
63
86
 
64
87
  | Method | Description | Returns |
65
88
  |--------|-------------|---------|
89
+ | `agent.address()` | Wallet Sui address | `string` |
66
90
  | `agent.balance()` | Available USDC + savings + gas reserve | `BalanceResponse` |
67
- | `agent.send({ to, amount })` | Transfer USDC to any Sui address | `SendResult` |
68
- | `agent.save({ amount, asset: 'USDC' })` | Deposit USDC to NAVI Protocol (earn APY) | `SaveResult` |
69
- | `agent.withdraw({ amount, asset: 'USDC' })` | Withdraw USDC from savings | `WithdrawResult` |
70
- | `agent.swap({ from, to, amount })` | Swap via Cetus CLMM DEX | `SwapResult` |
71
- | `agent.borrow({ amount, asset: 'USDC' })` | Borrow USDC against collateral | `BorrowResult` |
72
- | `agent.repay({ amount, asset: 'USDC' })` | Repay outstanding borrows | `RepayResult` |
91
+ | `agent.send({ to, amount, asset? })` | Transfer USDC to any Sui address | `SendResult` |
92
+ | `agent.save({ amount, asset })` | Deposit USDC to NAVI Protocol (earn APY). `amount` can be `'all'`. | `SaveResult` |
93
+ | `agent.withdraw({ amount, asset })` | Withdraw USDC from savings. `amount` can be `'all'`. | `WithdrawResult` |
94
+ | `agent.swap({ from, to, amount, maxSlippage? })` | Swap via Cetus CLMM DEX. `maxSlippage` in % (default: 3). | `SwapResult` |
95
+ | `agent.swapQuote({ from, to, amount })` | Get swap quote without executing | `SwapQuote` |
96
+ | `agent.borrow({ amount, asset })` | Borrow USDC against collateral | `BorrowResult` |
97
+ | `agent.repay({ amount, asset })` | Repay outstanding borrows. `amount` can be `'all'`. | `RepayResult` |
98
+ | `agent.exportKey()` | Export private key (bech32 format) | `string` |
73
99
 
74
100
  ### Query Methods
75
101
 
@@ -83,7 +109,7 @@ const agent = await T2000.create({
83
109
  | `agent.maxWithdraw()` | Max safe withdrawal amount | `MaxWithdrawResult` |
84
110
  | `agent.maxBorrow()` | Max safe borrow amount | `MaxBorrowResult` |
85
111
  | `agent.deposit()` | Wallet address + funding instructions | `DepositInfo` |
86
- | `agent.history()` | Transaction history | `TransactionRecord[]` |
112
+ | `agent.history({ limit? })` | Transaction history (default: all) | `TransactionRecord[]` |
87
113
 
88
114
  ### Key Management
89
115
 
@@ -93,6 +119,9 @@ import {
93
119
  keypairFromPrivateKey,
94
120
  exportPrivateKey,
95
121
  getAddress,
122
+ saveKey,
123
+ loadKey,
124
+ walletExists,
96
125
  } from '@t2000/sdk';
97
126
 
98
127
  // Generate a new keypair
@@ -106,21 +135,44 @@ const privkey = exportPrivateKey(keypair);
106
135
 
107
136
  // Get the Sui address
108
137
  const address = getAddress(keypair);
138
+
139
+ // Check if wallet exists on disk
140
+ const exists = await walletExists();
141
+
142
+ // Save/load encrypted key
143
+ await saveKey(keypair, 'my-pin');
144
+ const loaded = await loadKey('my-pin');
109
145
  ```
110
146
 
111
147
  ### Events
112
148
 
113
149
  ```typescript
114
150
  agent.on('balanceChange', (e) => {
115
- console.log(`${e.cause}: ${e.asset} changed`);
151
+ console.log(`${e.cause}: ${e.asset} ${e.previous} → ${e.current}`);
116
152
  });
117
153
 
118
154
  agent.on('healthWarning', (e) => {
119
- console.log(`Health factor: ${e.healthFactor}`);
155
+ console.log(`Health factor: ${e.healthFactor} (warning)`);
156
+ });
157
+
158
+ agent.on('healthCritical', (e) => {
159
+ console.log(`Health factor: ${e.healthFactor} (critical — below 1.2)`);
120
160
  });
121
161
 
122
162
  agent.on('yield', (e) => {
123
- console.log(`Earned: $${e.earned}`);
163
+ console.log(`Earned: $${e.earned}, total: $${e.total}`);
164
+ });
165
+
166
+ agent.on('gasAutoTopUp', (e) => {
167
+ console.log(`Auto-topped up gas: $${e.usdcSpent} USDC → ${e.suiReceived} SUI`);
168
+ });
169
+
170
+ agent.on('gasStationFallback', (e) => {
171
+ console.log(`Gas station fallback: ${e.reason}`);
172
+ });
173
+
174
+ agent.on('error', (e) => {
175
+ console.error(`Error: ${e.code} — ${e.message}`);
124
176
  });
125
177
  ```
126
178
 
@@ -140,11 +192,20 @@ import {
140
192
 
141
193
  mistToSui(1_000_000_000n); // 1.0
142
194
  usdcToRaw(10.50); // 10_500_000n
143
- formatUsd(1234.5); // "$1,234.50"
144
- truncateAddress('0xabcd...1234'); // "0xabcd...1234"
195
+ formatUsd(1234.5); // "$1234.50"
196
+ truncateAddress('0xabcdef...1234'); // "0xabcd...1234"
145
197
  validateAddress('0x...'); // throws if invalid
146
198
  ```
147
199
 
200
+ ### Advanced: Exposed Internals
201
+
202
+ For integrations (like `@t2000/x402`), the agent exposes:
203
+
204
+ ```typescript
205
+ agent.suiClient; // SuiClient instance
206
+ agent.signer; // Ed25519Keypair
207
+ ```
208
+
148
209
  ## Gas Abstraction
149
210
 
150
211
  Every operation (send, save, borrow, repay, withdraw, swap) routes through a 3-step gas resolution chain via `executeWithGas()`. The agent never fails due to low gas if it has USDC or the Gas Station is reachable:
@@ -164,12 +225,10 @@ Every transaction result includes a `gasMethod` field (`'self-funded'` | `'auto-
164
225
 
165
226
  | Environment Variable | Description | Default |
166
227
  |---------------------|-------------|---------|
167
- | `T2000_PIN` | Bank account PIN | — |
168
- | `T2000_NETWORK` | `mainnet` or `testnet` | `mainnet` |
169
- | `T2000_RPC_URL` | Custom Sui RPC URL | Sui public fullnode |
170
- | `T2000_KEY_PATH` | Path to encrypted key file | `~/.t2000/wallet.key` |
171
228
  | `T2000_API_URL` | t2000 API base URL | `https://api.t2000.ai` |
172
229
 
230
+ Options like `pin`, `keyPath`, and `rpcUrl` are passed directly to `T2000.create()` or `T2000.init()`. The CLI handles env vars like `T2000_PIN` — see the [CLI README](https://www.npmjs.com/package/@t2000/cli).
231
+
173
232
  ## Supported Assets
174
233
 
175
234
  | Asset | Type | Decimals |
@@ -192,12 +251,12 @@ try {
192
251
  }
193
252
  ```
194
253
 
195
- Common error codes: `INSUFFICIENT_BALANCE` · `INVALID_ADDRESS` · `INVALID_AMOUNT` · `HEALTH_FACTOR_TOO_LOW` · `NO_COLLATERAL` · `WALLET_NOT_FOUND` · `SIMULATION_FAILED` · `TRANSACTION_FAILED` · `PROTOCOL_PAUSED` · `INSUFFICIENT_GAS` · `SLIPPAGE_EXCEEDED` · `ASSET_NOT_SUPPORTED` · `WITHDRAW_WOULD_LIQUIDATE`
254
+ Common error codes: `INSUFFICIENT_BALANCE` · `INVALID_ADDRESS` · `INVALID_AMOUNT` · `HEALTH_FACTOR_TOO_LOW` · `NO_COLLATERAL` · `WALLET_NOT_FOUND` · `WALLET_LOCKED` · `WALLET_EXISTS` · `SIMULATION_FAILED` · `TRANSACTION_FAILED` · `PROTOCOL_PAUSED` · `INSUFFICIENT_GAS` · `SLIPPAGE_EXCEEDED` · `ASSET_NOT_SUPPORTED` · `WITHDRAW_WOULD_LIQUIDATE` · `AUTO_TOPUP_FAILED` · `GAS_STATION_UNAVAILABLE`
196
255
 
197
256
  ## Testing
198
257
 
199
258
  ```bash
200
- # Run all SDK unit tests (92 tests)
259
+ # Run all SDK unit tests (122 tests)
201
260
  pnpm --filter @t2000/sdk test
202
261
  ```
203
262
 
@@ -210,6 +269,10 @@ pnpm --filter @t2000/sdk test
210
269
  | `keyManager.test.ts` | Key generation, encryption, decryption, import/export |
211
270
  | `errors.test.ts` | `T2000Error` construction, serialization, `mapWalletError`, `mapMoveAbortCode` |
212
271
  | `navi.test.ts` | NAVI math utilities (health factor, APY, position calculations) |
272
+ | `send.test.ts` | Send transaction building and validation |
273
+ | `manager.test.ts` | Gas resolution chain (self-fund, auto-topup, sponsored fallback) |
274
+ | `autoTopUp.test.ts` | Auto-topup threshold logic and swap execution |
275
+ | `serialization.test.ts` | Transaction JSON serialization roundtrip |
213
276
 
214
277
  ## Protocol Fees
215
278
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@t2000/sdk",
3
- "version": "0.2.5",
3
+ "version": "0.2.6",
4
4
  "description": "TypeScript SDK for AI agent bank accounts on Sui — send, save, swap, borrow",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs",