@t2000/sdk 2.13.2 → 2.14.1

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.
@@ -4303,26 +4303,6 @@ function resolveAssetInfo(asset) {
4303
4303
  }
4304
4304
  throw new T2000Error("ASSET_NOT_SUPPORTED", `Unknown asset: ${asset}`);
4305
4305
  }
4306
- async function fetchCoins(client, owner, coinType) {
4307
- const all = [];
4308
- let cursor;
4309
- let hasNext = true;
4310
- while (hasNext) {
4311
- const page = await client.getCoins({ owner, coinType, cursor: cursor ?? void 0 });
4312
- all.push(...page.data.map((c) => ({ coinObjectId: c.coinObjectId, balance: c.balance })));
4313
- cursor = page.nextCursor;
4314
- hasNext = page.hasNextPage;
4315
- }
4316
- return all;
4317
- }
4318
- function mergeCoins(tx, coins) {
4319
- if (coins.length === 0) throw new T2000Error("INSUFFICIENT_BALANCE", "No coins to merge");
4320
- const primary = tx.object(coins[0].coinObjectId);
4321
- if (coins.length > 1) {
4322
- tx.mergeCoins(primary, coins.slice(1).map((c) => tx.object(c.coinObjectId)));
4323
- }
4324
- return primary;
4325
- }
4326
4306
  async function getPositions(client, address) {
4327
4307
  try {
4328
4308
  const naviPositions = await $e(address, {
@@ -4413,13 +4393,15 @@ async function buildSaveTx(client, address, amount, options = {}) {
4413
4393
  }
4414
4394
  const asset = options.asset ?? "USDC";
4415
4395
  const assetInfo = resolveAssetInfo(asset);
4416
- const coins = await fetchCoins(client, address, assetInfo.type);
4417
- if (coins.length === 0) throw new T2000Error("INSUFFICIENT_BALANCE", `No ${assetInfo.displayName} coins found`);
4418
- const totalBalance = coins.reduce((sum, c) => sum + BigInt(c.balance), 0n);
4396
+ const balResp = await client.getBalance({ owner: address, coinType: assetInfo.type });
4397
+ const totalBalance = BigInt(balResp.totalBalance);
4398
+ if (totalBalance === 0n) {
4399
+ throw new T2000Error("INSUFFICIENT_BALANCE", `No ${assetInfo.displayName} balance found`);
4400
+ }
4419
4401
  const tx = new transactions.Transaction();
4420
4402
  tx.setSender(address);
4421
- const coinObj = mergeCoins(tx, coins);
4422
4403
  const rawAmount = Math.min(Number(stableToRaw(amount, assetInfo.decimals)), Number(totalBalance));
4404
+ const coinObj = transactions.coinWithBalance({ type: assetInfo.type, balance: BigInt(rawAmount) })(tx);
4423
4405
  try {
4424
4406
  await Ce(tx, assetInfo.type, coinObj, {
4425
4407
  ...sdkOptions(client),
@@ -4551,18 +4533,19 @@ async function buildRepayTx(client, address, amount, options = {}) {
4551
4533
  }
4552
4534
  const asset = options.asset ?? "USDC";
4553
4535
  const assetInfo = resolveAssetInfo(asset);
4554
- const coins = await fetchCoins(client, address, assetInfo.type);
4555
- if (coins.length === 0) throw new T2000Error("INSUFFICIENT_BALANCE", `No ${assetInfo.displayName} coins to repay with. Withdraw some savings first to get cash.`);
4556
- const totalBalance = coins.reduce((sum, c) => sum + BigInt(c.balance), 0n);
4536
+ const balResp = await client.getBalance({ owner: address, coinType: assetInfo.type });
4537
+ const totalBalance = BigInt(balResp.totalBalance);
4538
+ if (totalBalance === 0n) {
4539
+ throw new T2000Error("INSUFFICIENT_BALANCE", `No ${assetInfo.displayName} to repay with. Withdraw some savings first to get cash.`);
4540
+ }
4557
4541
  const rawRequested = Number(stableToRaw(amount, assetInfo.decimals));
4558
4542
  if (Number(totalBalance) < rawRequested && Number(totalBalance) < 1e3) {
4559
4543
  throw new T2000Error("INSUFFICIENT_BALANCE", `Not enough ${assetInfo.displayName} to repay (need $${amount.toFixed(2)}, wallet has ~$${(Number(totalBalance) / 10 ** assetInfo.decimals).toFixed(4)}). Withdraw some savings first.`);
4560
4544
  }
4561
4545
  const tx = new transactions.Transaction();
4562
4546
  tx.setSender(address);
4563
- const coinObj = mergeCoins(tx, coins);
4564
4547
  const rawAmount = Math.min(rawRequested, Number(totalBalance));
4565
- const [repayCoin] = tx.splitCoins(coinObj, [rawAmount]);
4548
+ const repayCoin = transactions.coinWithBalance({ type: assetInfo.type, balance: BigInt(rawAmount) })(tx);
4566
4549
  await refreshOracle(tx, client, address, {
4567
4550
  skipOracle: options.skipOracle,
4568
4551
  skipPythUpdate: options.skipPythUpdate