@t2000/sdk 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 +221 -0
- package/dist/index.cjs +1551 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +394 -0
- package/dist/index.d.ts +394 -0
- package/dist/index.js +1513 -0
- package/dist/index.js.map +1 -0
- package/package.json +61 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,394 @@
|
|
|
1
|
+
import { EventEmitter } from 'eventemitter3';
|
|
2
|
+
import { SuiClient } from '@mysten/sui/client';
|
|
3
|
+
import { Ed25519Keypair } from '@mysten/sui/keypairs/ed25519';
|
|
4
|
+
import { Transaction } from '@mysten/sui/transactions';
|
|
5
|
+
|
|
6
|
+
interface T2000Options {
|
|
7
|
+
keyPath?: string;
|
|
8
|
+
passphrase?: string;
|
|
9
|
+
network?: 'mainnet' | 'testnet';
|
|
10
|
+
rpcUrl?: string;
|
|
11
|
+
sponsored?: boolean;
|
|
12
|
+
name?: string;
|
|
13
|
+
}
|
|
14
|
+
interface GasReserve {
|
|
15
|
+
sui: number;
|
|
16
|
+
usdEquiv: number;
|
|
17
|
+
}
|
|
18
|
+
interface BalanceResponse {
|
|
19
|
+
available: number;
|
|
20
|
+
savings: number;
|
|
21
|
+
gasReserve: GasReserve;
|
|
22
|
+
total: number;
|
|
23
|
+
assets: Record<string, number>;
|
|
24
|
+
}
|
|
25
|
+
type GasMethod = 'self-funded' | 'sponsored' | 'auto-topup';
|
|
26
|
+
interface SendResult {
|
|
27
|
+
success: boolean;
|
|
28
|
+
tx: string;
|
|
29
|
+
amount: number;
|
|
30
|
+
to: string;
|
|
31
|
+
gasCost: number;
|
|
32
|
+
gasCostUnit: string;
|
|
33
|
+
gasMethod: GasMethod;
|
|
34
|
+
balance: BalanceResponse;
|
|
35
|
+
}
|
|
36
|
+
interface SaveResult {
|
|
37
|
+
success: boolean;
|
|
38
|
+
tx: string;
|
|
39
|
+
amount: number;
|
|
40
|
+
apy: number;
|
|
41
|
+
fee: number;
|
|
42
|
+
gasCost: number;
|
|
43
|
+
gasMethod: GasMethod;
|
|
44
|
+
}
|
|
45
|
+
interface WithdrawResult {
|
|
46
|
+
success: boolean;
|
|
47
|
+
tx: string;
|
|
48
|
+
amount: number;
|
|
49
|
+
gasCost: number;
|
|
50
|
+
gasMethod: GasMethod;
|
|
51
|
+
}
|
|
52
|
+
interface BorrowResult {
|
|
53
|
+
success: boolean;
|
|
54
|
+
tx: string;
|
|
55
|
+
amount: number;
|
|
56
|
+
fee: number;
|
|
57
|
+
healthFactor: number;
|
|
58
|
+
gasCost: number;
|
|
59
|
+
gasMethod: GasMethod;
|
|
60
|
+
}
|
|
61
|
+
interface RepayResult {
|
|
62
|
+
success: boolean;
|
|
63
|
+
tx: string;
|
|
64
|
+
amount: number;
|
|
65
|
+
remainingDebt: number;
|
|
66
|
+
gasCost: number;
|
|
67
|
+
gasMethod: GasMethod;
|
|
68
|
+
}
|
|
69
|
+
interface SwapResult {
|
|
70
|
+
success: boolean;
|
|
71
|
+
tx: string;
|
|
72
|
+
fromAmount: number;
|
|
73
|
+
fromAsset: string;
|
|
74
|
+
toAmount: number;
|
|
75
|
+
toAsset: string;
|
|
76
|
+
priceImpact: number;
|
|
77
|
+
fee: number;
|
|
78
|
+
gasCost: number;
|
|
79
|
+
gasMethod: GasMethod;
|
|
80
|
+
}
|
|
81
|
+
interface HealthFactorResult {
|
|
82
|
+
healthFactor: number;
|
|
83
|
+
supplied: number;
|
|
84
|
+
borrowed: number;
|
|
85
|
+
maxBorrow: number;
|
|
86
|
+
liquidationThreshold: number;
|
|
87
|
+
}
|
|
88
|
+
interface MaxWithdrawResult {
|
|
89
|
+
maxAmount: number;
|
|
90
|
+
healthFactorAfter: number;
|
|
91
|
+
currentHF: number;
|
|
92
|
+
}
|
|
93
|
+
interface MaxBorrowResult {
|
|
94
|
+
maxAmount: number;
|
|
95
|
+
healthFactorAfter: number;
|
|
96
|
+
currentHF: number;
|
|
97
|
+
}
|
|
98
|
+
interface RatesResult {
|
|
99
|
+
USDC: {
|
|
100
|
+
saveApy: number;
|
|
101
|
+
borrowApy: number;
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
interface PositionEntry {
|
|
105
|
+
protocol: string;
|
|
106
|
+
asset: string;
|
|
107
|
+
type: 'save' | 'borrow';
|
|
108
|
+
amount: number;
|
|
109
|
+
apy: number;
|
|
110
|
+
}
|
|
111
|
+
interface PositionsResult {
|
|
112
|
+
positions: PositionEntry[];
|
|
113
|
+
}
|
|
114
|
+
interface EarningsResult {
|
|
115
|
+
totalYieldEarned: number;
|
|
116
|
+
currentApy: number;
|
|
117
|
+
dailyEarning: number;
|
|
118
|
+
supplied: number;
|
|
119
|
+
}
|
|
120
|
+
interface FundStatusResult {
|
|
121
|
+
supplied: number;
|
|
122
|
+
apy: number;
|
|
123
|
+
earnedToday: number;
|
|
124
|
+
earnedAllTime: number;
|
|
125
|
+
projectedMonthly: number;
|
|
126
|
+
}
|
|
127
|
+
interface DepositInfo {
|
|
128
|
+
address: string;
|
|
129
|
+
network: string;
|
|
130
|
+
supportedAssets: string[];
|
|
131
|
+
instructions: string;
|
|
132
|
+
}
|
|
133
|
+
interface TransactionRecord {
|
|
134
|
+
digest: string;
|
|
135
|
+
action: string;
|
|
136
|
+
amount?: number;
|
|
137
|
+
asset?: string;
|
|
138
|
+
timestamp: number;
|
|
139
|
+
gasMethod?: GasMethod;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
type T2000ErrorCode = 'INSUFFICIENT_BALANCE' | 'INSUFFICIENT_GAS' | 'INVALID_ADDRESS' | 'INVALID_AMOUNT' | 'WALLET_NOT_FOUND' | 'WALLET_LOCKED' | 'WALLET_EXISTS' | 'SPONSOR_FAILED' | 'SPONSOR_RATE_LIMITED' | 'GAS_STATION_UNAVAILABLE' | 'GAS_FEE_EXCEEDED' | 'SIMULATION_FAILED' | 'TRANSACTION_FAILED' | 'ASSET_NOT_SUPPORTED' | 'SLIPPAGE_EXCEEDED' | 'HEALTH_FACTOR_TOO_LOW' | 'WITHDRAW_WOULD_LIQUIDATE' | 'NO_COLLATERAL' | 'PROTOCOL_PAUSED' | 'PROTOCOL_UNAVAILABLE' | 'RPC_ERROR' | 'RPC_UNREACHABLE' | 'SPONSOR_UNAVAILABLE' | 'AUTO_TOPUP_FAILED' | 'PRICE_EXCEEDS_LIMIT' | 'UNSUPPORTED_NETWORK' | 'PAYMENT_EXPIRED' | 'DUPLICATE_PAYMENT' | 'FACILITATOR_REJECTION' | 'FACILITATOR_TIMEOUT' | 'UNKNOWN';
|
|
143
|
+
interface T2000ErrorData {
|
|
144
|
+
reason?: string;
|
|
145
|
+
[key: string]: unknown;
|
|
146
|
+
}
|
|
147
|
+
declare class T2000Error extends Error {
|
|
148
|
+
readonly code: T2000ErrorCode;
|
|
149
|
+
readonly data?: T2000ErrorData;
|
|
150
|
+
readonly retryable: boolean;
|
|
151
|
+
constructor(code: T2000ErrorCode, message: string, data?: T2000ErrorData, retryable?: boolean);
|
|
152
|
+
toJSON(): {
|
|
153
|
+
retryable: boolean;
|
|
154
|
+
data?: T2000ErrorData | undefined;
|
|
155
|
+
error: T2000ErrorCode;
|
|
156
|
+
message: string;
|
|
157
|
+
};
|
|
158
|
+
}
|
|
159
|
+
declare function mapWalletError(error: unknown): T2000Error;
|
|
160
|
+
declare function mapMoveAbortCode(code: number): string;
|
|
161
|
+
|
|
162
|
+
interface T2000Events {
|
|
163
|
+
balanceChange: (event: {
|
|
164
|
+
asset: string;
|
|
165
|
+
previous: number;
|
|
166
|
+
current: number;
|
|
167
|
+
cause: string;
|
|
168
|
+
tx?: string;
|
|
169
|
+
}) => void;
|
|
170
|
+
healthWarning: (event: {
|
|
171
|
+
healthFactor: number;
|
|
172
|
+
threshold: number;
|
|
173
|
+
severity: 'warning';
|
|
174
|
+
}) => void;
|
|
175
|
+
healthCritical: (event: {
|
|
176
|
+
healthFactor: number;
|
|
177
|
+
threshold: number;
|
|
178
|
+
severity: 'critical';
|
|
179
|
+
}) => void;
|
|
180
|
+
yield: (event: {
|
|
181
|
+
earned: number;
|
|
182
|
+
total: number;
|
|
183
|
+
apy: number;
|
|
184
|
+
timestamp: number;
|
|
185
|
+
}) => void;
|
|
186
|
+
gasAutoTopUp: (result: {
|
|
187
|
+
usdcSpent: number;
|
|
188
|
+
suiReceived: number;
|
|
189
|
+
}) => void;
|
|
190
|
+
gasStationFallback: (event: {
|
|
191
|
+
reason: string;
|
|
192
|
+
method: string;
|
|
193
|
+
suiUsed: number;
|
|
194
|
+
}) => void;
|
|
195
|
+
error: (error: T2000Error) => void;
|
|
196
|
+
}
|
|
197
|
+
declare class T2000 extends EventEmitter<T2000Events> {
|
|
198
|
+
private readonly keypair;
|
|
199
|
+
private readonly client;
|
|
200
|
+
private readonly _address;
|
|
201
|
+
private _lastGasMethod;
|
|
202
|
+
private constructor();
|
|
203
|
+
static create(options?: T2000Options): Promise<T2000>;
|
|
204
|
+
static fromPrivateKey(privateKey: string, options?: {
|
|
205
|
+
network?: 'mainnet' | 'testnet';
|
|
206
|
+
rpcUrl?: string;
|
|
207
|
+
}): T2000;
|
|
208
|
+
static init(options: {
|
|
209
|
+
passphrase: string;
|
|
210
|
+
keyPath?: string;
|
|
211
|
+
name?: string;
|
|
212
|
+
sponsored?: boolean;
|
|
213
|
+
}): Promise<{
|
|
214
|
+
agent: T2000;
|
|
215
|
+
address: string;
|
|
216
|
+
sponsored: boolean;
|
|
217
|
+
}>;
|
|
218
|
+
/**
|
|
219
|
+
* Ensure the agent has enough SUI for gas.
|
|
220
|
+
* If SUI is low and USDC is available, auto-swaps $1 USDC → SUI.
|
|
221
|
+
*/
|
|
222
|
+
private ensureGas;
|
|
223
|
+
/** SuiClient used by this agent — exposed for x402 and other integrations. */
|
|
224
|
+
get suiClient(): SuiClient;
|
|
225
|
+
/** Ed25519Keypair used by this agent — exposed for x402 and other integrations. */
|
|
226
|
+
get signer(): Ed25519Keypair;
|
|
227
|
+
address(): string;
|
|
228
|
+
send(params: {
|
|
229
|
+
to: string;
|
|
230
|
+
amount: number;
|
|
231
|
+
asset?: string;
|
|
232
|
+
}): Promise<SendResult>;
|
|
233
|
+
balance(): Promise<BalanceResponse>;
|
|
234
|
+
history(params?: {
|
|
235
|
+
limit?: number;
|
|
236
|
+
}): Promise<TransactionRecord[]>;
|
|
237
|
+
deposit(): Promise<DepositInfo>;
|
|
238
|
+
exportKey(): string;
|
|
239
|
+
save(params: {
|
|
240
|
+
amount: number | 'all';
|
|
241
|
+
asset?: string;
|
|
242
|
+
}): Promise<SaveResult>;
|
|
243
|
+
withdraw(params: {
|
|
244
|
+
amount: number | 'all';
|
|
245
|
+
asset?: string;
|
|
246
|
+
}): Promise<WithdrawResult>;
|
|
247
|
+
maxWithdraw(): Promise<MaxWithdrawResult>;
|
|
248
|
+
borrow(params: {
|
|
249
|
+
amount: number;
|
|
250
|
+
asset?: string;
|
|
251
|
+
}): Promise<BorrowResult>;
|
|
252
|
+
repay(params: {
|
|
253
|
+
amount: number | 'all';
|
|
254
|
+
asset?: string;
|
|
255
|
+
}): Promise<RepayResult>;
|
|
256
|
+
maxBorrow(): Promise<MaxBorrowResult>;
|
|
257
|
+
healthFactor(): Promise<HealthFactorResult>;
|
|
258
|
+
swap(params: {
|
|
259
|
+
from: string;
|
|
260
|
+
to: string;
|
|
261
|
+
amount: number;
|
|
262
|
+
maxSlippage?: number;
|
|
263
|
+
}): Promise<SwapResult>;
|
|
264
|
+
swapQuote(params: {
|
|
265
|
+
from: string;
|
|
266
|
+
to: string;
|
|
267
|
+
amount: number;
|
|
268
|
+
}): Promise<{
|
|
269
|
+
expectedOutput: number;
|
|
270
|
+
priceImpact: number;
|
|
271
|
+
poolPrice: number;
|
|
272
|
+
fee: {
|
|
273
|
+
amount: number;
|
|
274
|
+
rate: number;
|
|
275
|
+
};
|
|
276
|
+
}>;
|
|
277
|
+
positions(): Promise<PositionsResult>;
|
|
278
|
+
rates(): Promise<RatesResult>;
|
|
279
|
+
earnings(): Promise<EarningsResult>;
|
|
280
|
+
fundStatus(): Promise<FundStatusResult>;
|
|
281
|
+
private emitBalanceChange;
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
declare const MIST_PER_SUI = 1000000000n;
|
|
285
|
+
declare const SUI_DECIMALS = 9;
|
|
286
|
+
declare const USDC_DECIMALS = 6;
|
|
287
|
+
declare const BPS_DENOMINATOR = 10000n;
|
|
288
|
+
declare const CLOCK_ID = "0x6";
|
|
289
|
+
declare const SUPPORTED_ASSETS: {
|
|
290
|
+
readonly USDC: {
|
|
291
|
+
readonly type: "0xdba34672e30cb065b1f93e3ab55318768fd6fef66c15942c9f7cb846e2f900e7::usdc::USDC";
|
|
292
|
+
readonly decimals: 6;
|
|
293
|
+
readonly symbol: "USDC";
|
|
294
|
+
};
|
|
295
|
+
readonly SUI: {
|
|
296
|
+
readonly type: "0x2::sui::SUI";
|
|
297
|
+
readonly decimals: 9;
|
|
298
|
+
readonly symbol: "SUI";
|
|
299
|
+
};
|
|
300
|
+
};
|
|
301
|
+
type SupportedAsset = keyof typeof SUPPORTED_ASSETS;
|
|
302
|
+
declare const DEFAULT_NETWORK: "mainnet";
|
|
303
|
+
|
|
304
|
+
declare function validateAddress(address: string): string;
|
|
305
|
+
declare function truncateAddress(address: string): string;
|
|
306
|
+
|
|
307
|
+
declare function mistToSui(mist: bigint): number;
|
|
308
|
+
declare function suiToMist(sui: number): bigint;
|
|
309
|
+
declare function usdcToRaw(amount: number): bigint;
|
|
310
|
+
declare function rawToUsdc(raw: bigint): number;
|
|
311
|
+
declare function formatUsd(amount: number): string;
|
|
312
|
+
declare function formatSui(amount: number): string;
|
|
313
|
+
|
|
314
|
+
declare function generateKeypair(): Ed25519Keypair;
|
|
315
|
+
declare function keypairFromPrivateKey(privateKey: string): Ed25519Keypair;
|
|
316
|
+
declare function saveKey(keypair: Ed25519Keypair, passphrase: string, keyPath?: string): Promise<string>;
|
|
317
|
+
declare function loadKey(passphrase: string, keyPath?: string): Promise<Ed25519Keypair>;
|
|
318
|
+
declare function walletExists(keyPath?: string): Promise<boolean>;
|
|
319
|
+
declare function exportPrivateKey(keypair: Ed25519Keypair): string;
|
|
320
|
+
declare function getAddress(keypair: Ed25519Keypair): string;
|
|
321
|
+
|
|
322
|
+
declare function solveHashcash(challenge: string): string;
|
|
323
|
+
|
|
324
|
+
type FeeOperation = 'save' | 'swap' | 'borrow';
|
|
325
|
+
interface ProtocolFeeInfo {
|
|
326
|
+
amount: number;
|
|
327
|
+
asset: string;
|
|
328
|
+
rate: number;
|
|
329
|
+
rawAmount: bigint;
|
|
330
|
+
}
|
|
331
|
+
declare function calculateFee(operation: FeeOperation, amount: number): ProtocolFeeInfo;
|
|
332
|
+
|
|
333
|
+
interface SimulationResult {
|
|
334
|
+
success: boolean;
|
|
335
|
+
gasEstimateSui: number;
|
|
336
|
+
error?: {
|
|
337
|
+
moveAbortCode?: number;
|
|
338
|
+
moveModule?: string;
|
|
339
|
+
reason: string;
|
|
340
|
+
rawError: string;
|
|
341
|
+
};
|
|
342
|
+
}
|
|
343
|
+
declare function simulateTransaction(client: SuiClient, tx: Transaction, sender: string): Promise<SimulationResult>;
|
|
344
|
+
declare function throwIfSimulationFailed(sim: SimulationResult): void;
|
|
345
|
+
|
|
346
|
+
declare function getPoolPrice(client: SuiClient): Promise<number>;
|
|
347
|
+
declare function getSwapQuote(client: SuiClient, fromAsset: 'USDC' | 'SUI', toAsset: 'USDC' | 'SUI', amount: number): Promise<{
|
|
348
|
+
expectedOutput: number;
|
|
349
|
+
priceImpact: number;
|
|
350
|
+
poolPrice: number;
|
|
351
|
+
}>;
|
|
352
|
+
|
|
353
|
+
declare function getRates(client: SuiClient): Promise<RatesResult>;
|
|
354
|
+
|
|
355
|
+
interface GasExecutionResult {
|
|
356
|
+
digest: string;
|
|
357
|
+
effects: unknown;
|
|
358
|
+
gasMethod: GasMethod;
|
|
359
|
+
gasCostSui: number;
|
|
360
|
+
}
|
|
361
|
+
/**
|
|
362
|
+
* Gas resolution chain:
|
|
363
|
+
* 1. Self-funded (agent has enough SUI)
|
|
364
|
+
* 2. Auto-topup (swap USDC→SUI, then self-fund)
|
|
365
|
+
* 3. Gas Station sponsored (fallback)
|
|
366
|
+
* 4. Fail with INSUFFICIENT_GAS
|
|
367
|
+
*/
|
|
368
|
+
declare function executeWithGas(client: SuiClient, keypair: Ed25519Keypair, buildTx: () => Transaction | Promise<Transaction>): Promise<GasExecutionResult>;
|
|
369
|
+
|
|
370
|
+
interface AutoTopUpResult {
|
|
371
|
+
success: boolean;
|
|
372
|
+
tx: string;
|
|
373
|
+
usdcSpent: number;
|
|
374
|
+
suiReceived: number;
|
|
375
|
+
}
|
|
376
|
+
declare function shouldAutoTopUp(client: SuiClient, address: string): Promise<boolean>;
|
|
377
|
+
declare function executeAutoTopUp(client: SuiClient, keypair: Ed25519Keypair): Promise<AutoTopUpResult>;
|
|
378
|
+
|
|
379
|
+
type GasRequestType = 'bootstrap' | 'auto-topup' | 'fallback';
|
|
380
|
+
interface GasSponsorResponse {
|
|
381
|
+
txBytes: string;
|
|
382
|
+
sponsorSignature: string;
|
|
383
|
+
gasEstimateUsd: number;
|
|
384
|
+
type: GasRequestType;
|
|
385
|
+
}
|
|
386
|
+
interface GasStatusResponse {
|
|
387
|
+
circuitBreaker: boolean;
|
|
388
|
+
suiPrice: number;
|
|
389
|
+
bootstrapUsed?: number;
|
|
390
|
+
bootstrapRemaining?: number;
|
|
391
|
+
}
|
|
392
|
+
declare function getGasStatus(address?: string): Promise<GasStatusResponse>;
|
|
393
|
+
|
|
394
|
+
export { type AutoTopUpResult, BPS_DENOMINATOR, type BalanceResponse, type BorrowResult, CLOCK_ID, DEFAULT_NETWORK, type DepositInfo, type EarningsResult, type FeeOperation, type FundStatusResult, type GasExecutionResult, type GasMethod, type GasRequestType, type GasReserve, type GasSponsorResponse, type GasStatusResponse, type HealthFactorResult, MIST_PER_SUI, type MaxBorrowResult, type MaxWithdrawResult, type PositionEntry, type PositionsResult, type ProtocolFeeInfo, type RatesResult, type RepayResult, SUI_DECIMALS, SUPPORTED_ASSETS, type SaveResult, type SendResult, type SimulationResult, type SupportedAsset, type SwapResult, T2000, T2000Error, type T2000ErrorCode, type T2000ErrorData, type T2000Options, type TransactionRecord, USDC_DECIMALS, type WithdrawResult, calculateFee, executeAutoTopUp, executeWithGas, exportPrivateKey, formatSui, formatUsd, generateKeypair, getAddress, getGasStatus, getPoolPrice, getRates, getSwapQuote, keypairFromPrivateKey, loadKey, mapMoveAbortCode, mapWalletError, mistToSui, rawToUsdc, saveKey, shouldAutoTopUp, simulateTransaction, solveHashcash, suiToMist, throwIfSimulationFailed, truncateAddress, usdcToRaw, validateAddress, walletExists };
|