@t2000/sdk 0.2.0 → 0.2.4
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/README.md +6 -6
- package/dist/index.cjs +31 -5
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +31 -5
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -35,13 +35,13 @@ console.log(`$${balance.available} USDC available`);
|
|
|
35
35
|
await agent.send({ to: '0x...', amount: 10 });
|
|
36
36
|
|
|
37
37
|
// Save (earn yield via NAVI Protocol)
|
|
38
|
-
await agent.save({ amount: 50 });
|
|
38
|
+
await agent.save({ amount: 50, asset: 'USDC' });
|
|
39
39
|
|
|
40
40
|
// Swap USDC → SUI (via Cetus DEX)
|
|
41
41
|
await agent.swap({ from: 'USDC', to: 'SUI', amount: 5 });
|
|
42
42
|
|
|
43
43
|
// Borrow against savings
|
|
44
|
-
await agent.borrow({ amount: 20 });
|
|
44
|
+
await agent.borrow({ amount: 20, asset: 'USDC' });
|
|
45
45
|
```
|
|
46
46
|
|
|
47
47
|
## API Reference
|
|
@@ -65,11 +65,11 @@ const agent = await T2000.create({
|
|
|
65
65
|
|--------|-------------|---------|
|
|
66
66
|
| `agent.balance()` | Available USDC + savings + gas reserve | `BalanceResponse` |
|
|
67
67
|
| `agent.send({ to, amount })` | Transfer USDC to any Sui address | `SendResult` |
|
|
68
|
-
| `agent.save({ amount })` | Deposit USDC to NAVI Protocol (earn APY) | `SaveResult` |
|
|
69
|
-
| `agent.withdraw({ amount })` | Withdraw USDC from savings | `WithdrawResult` |
|
|
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
70
|
| `agent.swap({ from, to, amount })` | Swap via Cetus CLMM DEX | `SwapResult` |
|
|
71
|
-
| `agent.borrow({ amount })` | Borrow USDC against collateral | `BorrowResult` |
|
|
72
|
-
| `agent.repay({ amount })` | Repay outstanding borrows | `RepayResult` |
|
|
71
|
+
| `agent.borrow({ amount, asset: 'USDC' })` | Borrow USDC against collateral | `BorrowResult` |
|
|
72
|
+
| `agent.repay({ amount, asset: 'USDC' })` | Repay outstanding borrows | `RepayResult` |
|
|
73
73
|
|
|
74
74
|
### Query Methods
|
|
75
75
|
|
package/dist/index.cjs
CHANGED
|
@@ -274,12 +274,14 @@ async function buildSendTx({
|
|
|
274
274
|
}
|
|
275
275
|
|
|
276
276
|
// src/wallet/balance.ts
|
|
277
|
-
var _cachedSuiPrice =
|
|
277
|
+
var _cachedSuiPrice = 0;
|
|
278
278
|
var _priceLastFetched = 0;
|
|
279
279
|
var PRICE_CACHE_TTL_MS = 6e4;
|
|
280
280
|
async function fetchSuiPrice(client) {
|
|
281
281
|
const now = Date.now();
|
|
282
|
-
if (now - _priceLastFetched < PRICE_CACHE_TTL_MS)
|
|
282
|
+
if (_cachedSuiPrice > 0 && now - _priceLastFetched < PRICE_CACHE_TTL_MS) {
|
|
283
|
+
return _cachedSuiPrice;
|
|
284
|
+
}
|
|
283
285
|
try {
|
|
284
286
|
const pool = await client.getObject({
|
|
285
287
|
id: CETUS_USDC_SUI_POOL,
|
|
@@ -292,7 +294,7 @@ async function fetchSuiPrice(client) {
|
|
|
292
294
|
const Q64 = 2n ** 64n;
|
|
293
295
|
const sqrtPriceFloat = Number(currentSqrtPrice) / Number(Q64);
|
|
294
296
|
const rawPrice = sqrtPriceFloat * sqrtPriceFloat;
|
|
295
|
-
const price =
|
|
297
|
+
const price = 1e3 / rawPrice;
|
|
296
298
|
if (price > 0.01 && price < 1e3) {
|
|
297
299
|
_cachedSuiPrice = price;
|
|
298
300
|
_priceLastFetched = now;
|
|
@@ -724,10 +726,11 @@ function solveHashcash(challenge) {
|
|
|
724
726
|
|
|
725
727
|
// src/gas/gasStation.ts
|
|
726
728
|
async function requestGasSponsorship(txJson, sender, type) {
|
|
729
|
+
const txBytes = Buffer.from(txJson).toString("base64");
|
|
727
730
|
const res = await fetch(`${API_BASE_URL}/api/gas`, {
|
|
728
731
|
method: "POST",
|
|
729
732
|
headers: { "Content-Type": "application/json" },
|
|
730
|
-
body: JSON.stringify({ txJson, sender, type })
|
|
733
|
+
body: JSON.stringify({ txJson, txBytes, sender, type })
|
|
731
734
|
});
|
|
732
735
|
const data = await res.json();
|
|
733
736
|
if (!res.ok) {
|
|
@@ -1057,6 +1060,10 @@ var T2000 = class _T2000 extends eventemitter3.EventEmitter {
|
|
|
1057
1060
|
}
|
|
1058
1061
|
// -- Savings --
|
|
1059
1062
|
async save(params) {
|
|
1063
|
+
const asset = (params.asset ?? "USDC").toUpperCase();
|
|
1064
|
+
if (asset !== "USDC") {
|
|
1065
|
+
throw new T2000Error("ASSET_NOT_SUPPORTED", `Only USDC is supported for save. Got: ${asset}`);
|
|
1066
|
+
}
|
|
1060
1067
|
let amount;
|
|
1061
1068
|
if (params.amount === "all") {
|
|
1062
1069
|
const bal = await queryBalance(this.client, this._address);
|
|
@@ -1081,6 +1088,12 @@ var T2000 = class _T2000 extends eventemitter3.EventEmitter {
|
|
|
1081
1088
|
const rates = await getRates(this.client);
|
|
1082
1089
|
reportFee(this._address, "save", fee.amount, fee.rate, gasResult.digest);
|
|
1083
1090
|
this.emitBalanceChange("USDC", saveAmount, "save", gasResult.digest);
|
|
1091
|
+
let savingsBalance = saveAmount - fee.amount;
|
|
1092
|
+
try {
|
|
1093
|
+
const positions = await this.positions();
|
|
1094
|
+
savingsBalance = positions.positions.filter((p) => p.type === "save").reduce((sum, p) => sum + p.amount, 0);
|
|
1095
|
+
} catch {
|
|
1096
|
+
}
|
|
1084
1097
|
return {
|
|
1085
1098
|
success: true,
|
|
1086
1099
|
tx: gasResult.digest,
|
|
@@ -1088,10 +1101,15 @@ var T2000 = class _T2000 extends eventemitter3.EventEmitter {
|
|
|
1088
1101
|
apy: rates.USDC.saveApy,
|
|
1089
1102
|
fee: fee.amount,
|
|
1090
1103
|
gasCost: gasResult.gasCostSui,
|
|
1091
|
-
gasMethod: gasResult.gasMethod
|
|
1104
|
+
gasMethod: gasResult.gasMethod,
|
|
1105
|
+
savingsBalance
|
|
1092
1106
|
};
|
|
1093
1107
|
}
|
|
1094
1108
|
async withdraw(params) {
|
|
1109
|
+
const asset = (params.asset ?? "USDC").toUpperCase();
|
|
1110
|
+
if (asset !== "USDC") {
|
|
1111
|
+
throw new T2000Error("ASSET_NOT_SUPPORTED", `Only USDC is supported for withdraw. Got: ${asset}`);
|
|
1112
|
+
}
|
|
1095
1113
|
let amount;
|
|
1096
1114
|
if (params.amount === "all") {
|
|
1097
1115
|
const maxResult = await this.maxWithdraw();
|
|
@@ -1138,6 +1156,10 @@ var T2000 = class _T2000 extends eventemitter3.EventEmitter {
|
|
|
1138
1156
|
}
|
|
1139
1157
|
// -- Borrowing --
|
|
1140
1158
|
async borrow(params) {
|
|
1159
|
+
const asset = (params.asset ?? "USDC").toUpperCase();
|
|
1160
|
+
if (asset !== "USDC") {
|
|
1161
|
+
throw new T2000Error("ASSET_NOT_SUPPORTED", `Only USDC is supported for borrow. Got: ${asset}`);
|
|
1162
|
+
}
|
|
1141
1163
|
const maxResult = await this.maxBorrow();
|
|
1142
1164
|
if (params.amount > maxResult.maxAmount) {
|
|
1143
1165
|
throw new T2000Error("HEALTH_FACTOR_TOO_LOW", `Max safe borrow: $${maxResult.maxAmount.toFixed(2)}`, {
|
|
@@ -1166,6 +1188,10 @@ var T2000 = class _T2000 extends eventemitter3.EventEmitter {
|
|
|
1166
1188
|
};
|
|
1167
1189
|
}
|
|
1168
1190
|
async repay(params) {
|
|
1191
|
+
const asset = (params.asset ?? "USDC").toUpperCase();
|
|
1192
|
+
if (asset !== "USDC") {
|
|
1193
|
+
throw new T2000Error("ASSET_NOT_SUPPORTED", `Only USDC is supported for repay. Got: ${asset}`);
|
|
1194
|
+
}
|
|
1169
1195
|
let amount;
|
|
1170
1196
|
if (params.amount === "all") {
|
|
1171
1197
|
const hf2 = await this.healthFactor();
|