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