@t2000/sdk 0.5.5 → 0.6.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.
@@ -191,6 +191,8 @@ var SUI_SYSTEM_STATE = "0x05";
191
191
  var NAVI_BALANCE_DECIMALS = 9;
192
192
  var CONFIG_API = "https://open-api.naviprotocol.io/api/navi/config?env=prod";
193
193
  var POOLS_API = "https://open-api.naviprotocol.io/api/navi/pools?env=prod";
194
+ var PACKAGE_API = "https://open-api.naviprotocol.io/api/package";
195
+ var packageCache = null;
194
196
  function toBigInt(v) {
195
197
  if (typeof v === "bigint") return v;
196
198
  return BigInt(String(v));
@@ -215,9 +217,22 @@ async function fetchJson(url) {
215
217
  const json = await res.json();
216
218
  return json.data ?? json;
217
219
  }
220
+ async function getLatestPackageId() {
221
+ if (packageCache && Date.now() - packageCache.ts < CACHE_TTL) return packageCache.id;
222
+ const res = await fetch(PACKAGE_API);
223
+ if (!res.ok) throw new T2000Error("PROTOCOL_UNAVAILABLE", `NAVI package API error: ${res.status}`);
224
+ const json = await res.json();
225
+ if (!json.packageId) throw new T2000Error("PROTOCOL_UNAVAILABLE", "NAVI package API returned no packageId");
226
+ packageCache = { id: json.packageId, ts: Date.now() };
227
+ return json.packageId;
228
+ }
218
229
  async function getConfig(fresh = false) {
219
230
  if (configCache && !fresh && Date.now() - configCache.ts < CACHE_TTL) return configCache.data;
220
- const data = await fetchJson(CONFIG_API);
231
+ const [data, latestPkg] = await Promise.all([
232
+ fetchJson(CONFIG_API),
233
+ getLatestPackageId()
234
+ ]);
235
+ data.package = latestPkg;
221
236
  configCache = { data, ts: Date.now() };
222
237
  return data;
223
238
  }
@@ -291,14 +306,13 @@ async function fetchCoins(client, owner, coinType) {
291
306
  }
292
307
  return all;
293
308
  }
294
- function mergeCoinsPtb(tx, coins, amount) {
309
+ function mergeCoins(tx, coins) {
295
310
  if (coins.length === 0) throw new T2000Error("INSUFFICIENT_BALANCE", "No coins to merge");
296
311
  const primary = tx.object(coins[0].coinObjectId);
297
312
  if (coins.length > 1) {
298
313
  tx.mergeCoins(primary, coins.slice(1).map((c) => tx.object(c.coinObjectId)));
299
314
  }
300
- const [split] = tx.splitCoins(primary, [amount]);
301
- return split;
315
+ return primary;
302
316
  }
303
317
  async function buildSaveTx(client, address, amount, options = {}) {
304
318
  const rawAmount = Number(usdcToRaw(amount));
@@ -307,7 +321,7 @@ async function buildSaveTx(client, address, amount, options = {}) {
307
321
  if (coins.length === 0) throw new T2000Error("INSUFFICIENT_BALANCE", "No USDC coins found");
308
322
  const tx = new Transaction();
309
323
  tx.setSender(address);
310
- const coinObj = mergeCoinsPtb(tx, coins, rawAmount);
324
+ const coinObj = mergeCoins(tx, coins);
311
325
  if (options.collectFee) {
312
326
  addCollectFeeToTx(tx, coinObj, "save");
313
327
  }
@@ -341,8 +355,8 @@ async function buildWithdrawTx(client, address, amount) {
341
355
  const rawAmount = Number(usdcToRaw(effectiveAmount));
342
356
  const tx = new Transaction();
343
357
  tx.setSender(address);
344
- tx.moveCall({
345
- target: `${config.package}::incentive_v3::entry_withdraw_v2`,
358
+ const [balance] = tx.moveCall({
359
+ target: `${config.package}::incentive_v3::withdraw_v2`,
346
360
  arguments: [
347
361
  tx.object(CLOCK),
348
362
  tx.object(config.oracle.priceOracle),
@@ -353,8 +367,15 @@ async function buildWithdrawTx(client, address, amount) {
353
367
  tx.object(config.incentiveV2),
354
368
  tx.object(config.incentiveV3),
355
369
  tx.object(SUI_SYSTEM_STATE)
356
- ]
370
+ ],
371
+ typeArguments: [pool.suiCoinType]
357
372
  });
373
+ const [coin] = tx.moveCall({
374
+ target: "0x2::coin::from_balance",
375
+ arguments: [balance],
376
+ typeArguments: [pool.suiCoinType]
377
+ });
378
+ tx.transferObjects([coin], address);
358
379
  return { tx, effectiveAmount };
359
380
  }
360
381
  async function buildBorrowTx(client, address, amount, options = {}) {
@@ -362,8 +383,8 @@ async function buildBorrowTx(client, address, amount, options = {}) {
362
383
  const [config, pool] = await Promise.all([getConfig(), getUsdcPool()]);
363
384
  const tx = new Transaction();
364
385
  tx.setSender(address);
365
- tx.moveCall({
366
- target: `${config.package}::incentive_v3::entry_borrow_v2`,
386
+ const [balance] = tx.moveCall({
387
+ target: `${config.package}::incentive_v3::borrow_v2`,
367
388
  arguments: [
368
389
  tx.object(CLOCK),
369
390
  tx.object(config.oracle.priceOracle),
@@ -374,8 +395,15 @@ async function buildBorrowTx(client, address, amount, options = {}) {
374
395
  tx.object(config.incentiveV2),
375
396
  tx.object(config.incentiveV3),
376
397
  tx.object(SUI_SYSTEM_STATE)
377
- ]
398
+ ],
399
+ typeArguments: [pool.suiCoinType]
400
+ });
401
+ const [borrowedCoin] = tx.moveCall({
402
+ target: "0x2::coin::from_balance",
403
+ arguments: [balance],
404
+ typeArguments: [pool.suiCoinType]
378
405
  });
406
+ tx.transferObjects([borrowedCoin], address);
379
407
  return tx;
380
408
  }
381
409
  async function buildRepayTx(client, address, amount) {
@@ -385,7 +413,7 @@ async function buildRepayTx(client, address, amount) {
385
413
  if (coins.length === 0) throw new T2000Error("INSUFFICIENT_BALANCE", "No USDC coins to repay with");
386
414
  const tx = new Transaction();
387
415
  tx.setSender(address);
388
- const coinObj = mergeCoinsPtb(tx, coins, rawAmount);
416
+ const coinObj = mergeCoins(tx, coins);
389
417
  tx.moveCall({
390
418
  target: `${config.package}::incentive_v3::entry_repay`,
391
419
  arguments: [