@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/dist/index.d.cts
CHANGED
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -272,12 +272,14 @@ async function buildSendTx({
|
|
|
272
272
|
}
|
|
273
273
|
|
|
274
274
|
// src/wallet/balance.ts
|
|
275
|
-
var _cachedSuiPrice =
|
|
275
|
+
var _cachedSuiPrice = 0;
|
|
276
276
|
var _priceLastFetched = 0;
|
|
277
277
|
var PRICE_CACHE_TTL_MS = 6e4;
|
|
278
278
|
async function fetchSuiPrice(client) {
|
|
279
279
|
const now = Date.now();
|
|
280
|
-
if (now - _priceLastFetched < PRICE_CACHE_TTL_MS)
|
|
280
|
+
if (_cachedSuiPrice > 0 && now - _priceLastFetched < PRICE_CACHE_TTL_MS) {
|
|
281
|
+
return _cachedSuiPrice;
|
|
282
|
+
}
|
|
281
283
|
try {
|
|
282
284
|
const pool = await client.getObject({
|
|
283
285
|
id: CETUS_USDC_SUI_POOL,
|
|
@@ -290,7 +292,7 @@ async function fetchSuiPrice(client) {
|
|
|
290
292
|
const Q64 = 2n ** 64n;
|
|
291
293
|
const sqrtPriceFloat = Number(currentSqrtPrice) / Number(Q64);
|
|
292
294
|
const rawPrice = sqrtPriceFloat * sqrtPriceFloat;
|
|
293
|
-
const price =
|
|
295
|
+
const price = 1e3 / rawPrice;
|
|
294
296
|
if (price > 0.01 && price < 1e3) {
|
|
295
297
|
_cachedSuiPrice = price;
|
|
296
298
|
_priceLastFetched = now;
|
|
@@ -722,10 +724,11 @@ function solveHashcash(challenge) {
|
|
|
722
724
|
|
|
723
725
|
// src/gas/gasStation.ts
|
|
724
726
|
async function requestGasSponsorship(txJson, sender, type) {
|
|
727
|
+
const txBytes = Buffer.from(txJson).toString("base64");
|
|
725
728
|
const res = await fetch(`${API_BASE_URL}/api/gas`, {
|
|
726
729
|
method: "POST",
|
|
727
730
|
headers: { "Content-Type": "application/json" },
|
|
728
|
-
body: JSON.stringify({ txJson, sender, type })
|
|
731
|
+
body: JSON.stringify({ txJson, txBytes, sender, type })
|
|
729
732
|
});
|
|
730
733
|
const data = await res.json();
|
|
731
734
|
if (!res.ok) {
|
|
@@ -1055,6 +1058,10 @@ var T2000 = class _T2000 extends EventEmitter {
|
|
|
1055
1058
|
}
|
|
1056
1059
|
// -- Savings --
|
|
1057
1060
|
async save(params) {
|
|
1061
|
+
const asset = (params.asset ?? "USDC").toUpperCase();
|
|
1062
|
+
if (asset !== "USDC") {
|
|
1063
|
+
throw new T2000Error("ASSET_NOT_SUPPORTED", `Only USDC is supported for save. Got: ${asset}`);
|
|
1064
|
+
}
|
|
1058
1065
|
let amount;
|
|
1059
1066
|
if (params.amount === "all") {
|
|
1060
1067
|
const bal = await queryBalance(this.client, this._address);
|
|
@@ -1079,6 +1086,12 @@ var T2000 = class _T2000 extends EventEmitter {
|
|
|
1079
1086
|
const rates = await getRates(this.client);
|
|
1080
1087
|
reportFee(this._address, "save", fee.amount, fee.rate, gasResult.digest);
|
|
1081
1088
|
this.emitBalanceChange("USDC", saveAmount, "save", gasResult.digest);
|
|
1089
|
+
let savingsBalance = saveAmount - fee.amount;
|
|
1090
|
+
try {
|
|
1091
|
+
const positions = await this.positions();
|
|
1092
|
+
savingsBalance = positions.positions.filter((p) => p.type === "save").reduce((sum, p) => sum + p.amount, 0);
|
|
1093
|
+
} catch {
|
|
1094
|
+
}
|
|
1082
1095
|
return {
|
|
1083
1096
|
success: true,
|
|
1084
1097
|
tx: gasResult.digest,
|
|
@@ -1086,10 +1099,15 @@ var T2000 = class _T2000 extends EventEmitter {
|
|
|
1086
1099
|
apy: rates.USDC.saveApy,
|
|
1087
1100
|
fee: fee.amount,
|
|
1088
1101
|
gasCost: gasResult.gasCostSui,
|
|
1089
|
-
gasMethod: gasResult.gasMethod
|
|
1102
|
+
gasMethod: gasResult.gasMethod,
|
|
1103
|
+
savingsBalance
|
|
1090
1104
|
};
|
|
1091
1105
|
}
|
|
1092
1106
|
async withdraw(params) {
|
|
1107
|
+
const asset = (params.asset ?? "USDC").toUpperCase();
|
|
1108
|
+
if (asset !== "USDC") {
|
|
1109
|
+
throw new T2000Error("ASSET_NOT_SUPPORTED", `Only USDC is supported for withdraw. Got: ${asset}`);
|
|
1110
|
+
}
|
|
1093
1111
|
let amount;
|
|
1094
1112
|
if (params.amount === "all") {
|
|
1095
1113
|
const maxResult = await this.maxWithdraw();
|
|
@@ -1136,6 +1154,10 @@ var T2000 = class _T2000 extends EventEmitter {
|
|
|
1136
1154
|
}
|
|
1137
1155
|
// -- Borrowing --
|
|
1138
1156
|
async borrow(params) {
|
|
1157
|
+
const asset = (params.asset ?? "USDC").toUpperCase();
|
|
1158
|
+
if (asset !== "USDC") {
|
|
1159
|
+
throw new T2000Error("ASSET_NOT_SUPPORTED", `Only USDC is supported for borrow. Got: ${asset}`);
|
|
1160
|
+
}
|
|
1139
1161
|
const maxResult = await this.maxBorrow();
|
|
1140
1162
|
if (params.amount > maxResult.maxAmount) {
|
|
1141
1163
|
throw new T2000Error("HEALTH_FACTOR_TOO_LOW", `Max safe borrow: $${maxResult.maxAmount.toFixed(2)}`, {
|
|
@@ -1164,6 +1186,10 @@ var T2000 = class _T2000 extends EventEmitter {
|
|
|
1164
1186
|
};
|
|
1165
1187
|
}
|
|
1166
1188
|
async repay(params) {
|
|
1189
|
+
const asset = (params.asset ?? "USDC").toUpperCase();
|
|
1190
|
+
if (asset !== "USDC") {
|
|
1191
|
+
throw new T2000Error("ASSET_NOT_SUPPORTED", `Only USDC is supported for repay. Got: ${asset}`);
|
|
1192
|
+
}
|
|
1167
1193
|
let amount;
|
|
1168
1194
|
if (params.amount === "all") {
|
|
1169
1195
|
const hf2 = await this.healthFactor();
|