@t2000/sdk 0.15.1 → 0.15.3

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/dist/index.js CHANGED
@@ -3054,9 +3054,12 @@ To access invested funds: t2000 invest sell ${params.amount} ${asset}`,
3054
3054
  }
3055
3055
  async balance() {
3056
3056
  const bal = await queryBalance(this.client, this._address);
3057
+ const earningAssets = new Set(
3058
+ this.portfolio.getPositions().filter((p) => p.earning).map((p) => p.asset)
3059
+ );
3057
3060
  try {
3058
3061
  const positions = await this.positions();
3059
- const savings = positions.positions.filter((p) => p.type === "save").reduce((sum, p) => sum + p.amount, 0);
3062
+ const savings = positions.positions.filter((p) => p.type === "save").filter((p) => !earningAssets.has(p.asset)).reduce((sum, p) => sum + p.amount, 0);
3060
3063
  const debt = positions.positions.filter((p) => p.type === "borrow").reduce((sum, p) => sum + p.amount, 0);
3061
3064
  bal.savings = savings;
3062
3065
  bal.debt = debt;
@@ -3084,7 +3087,14 @@ To access invested funds: t2000 invest sell ${params.amount} ${asset}`,
3084
3087
  for (const pos of portfolioPositions) {
3085
3088
  if (!(pos.asset in INVESTMENT_ASSETS)) continue;
3086
3089
  const price = assetPrices[pos.asset] ?? 0;
3087
- if (pos.asset === "SUI") {
3090
+ if (pos.earning) {
3091
+ investmentValue += pos.totalAmount * price;
3092
+ investmentCostBasis += pos.costBasis;
3093
+ if (pos.asset === "SUI") {
3094
+ const gasSui = Math.max(0, bal.gasReserve.sui);
3095
+ bal.gasReserve = { sui: gasSui, usdEquiv: gasSui * price };
3096
+ }
3097
+ } else if (pos.asset === "SUI") {
3088
3098
  const actualHeld = Math.min(pos.totalAmount, bal.gasReserve.sui);
3089
3099
  investmentValue += actualHeld * price;
3090
3100
  if (actualHeld < pos.totalAmount && pos.totalAmount > 0) {
@@ -3944,26 +3954,20 @@ To sell investment: t2000 invest sell ${params.amount} ${fromAsset}`,
3944
3954
  if (depositAmount <= 0) {
3945
3955
  throw new T2000Error("INSUFFICIENT_BALANCE", `No ${params.asset} available to deposit (wallet: ${walletAmount}, gas reserve: ${gasReserve})`);
3946
3956
  }
3947
- const { tx } = await adapter.buildSaveTx(this._address, depositAmount, params.asset);
3948
- const result = await this.client.signAndExecuteTransaction({
3949
- signer: this.keypair,
3950
- transaction: tx,
3951
- options: { showEffects: true }
3957
+ const gasResult = await executeWithGas(this.client, this.keypair, async () => {
3958
+ const { tx } = await adapter.buildSaveTx(this._address, depositAmount, params.asset);
3959
+ return tx;
3952
3960
  });
3953
- await this.client.waitForTransaction({ digest: result.digest });
3954
- const gasCost = result.effects?.gasUsed ? Math.abs(
3955
- (Number(result.effects.gasUsed.computationCost) + Number(result.effects.gasUsed.storageCost) - Number(result.effects.gasUsed.storageRebate)) / 1e9
3956
- ) : 0;
3957
3961
  this.portfolio.recordEarn(params.asset, adapter.id, rate.saveApy);
3958
3962
  return {
3959
3963
  success: true,
3960
- tx: result.digest,
3964
+ tx: gasResult.digest,
3961
3965
  asset: params.asset,
3962
3966
  amount: depositAmount,
3963
3967
  protocol: adapter.name,
3964
3968
  apy: rate.saveApy,
3965
- gasCost,
3966
- gasMethod: "self-funded"
3969
+ gasCost: gasResult.gasCostSui,
3970
+ gasMethod: gasResult.gasMethod
3967
3971
  };
3968
3972
  }
3969
3973
  async investUnearn(params) {
@@ -3982,27 +3986,23 @@ To sell investment: t2000 invest sell ${params.amount} ${fromAsset}`,
3982
3986
  const positions = await adapter.getPositions(this._address);
3983
3987
  const supply = positions.supplies.find((s) => s.asset === params.asset);
3984
3988
  const withdrawAmount = supply?.amount ?? pos.totalAmount;
3985
- const { tx, effectiveAmount } = await adapter.buildWithdrawTx(this._address, withdrawAmount, params.asset);
3986
- const result = await this.client.signAndExecuteTransaction({
3987
- signer: this.keypair,
3988
- transaction: tx,
3989
- options: { showEffects: true }
3990
- });
3991
- await this.client.waitForTransaction({ digest: result.digest });
3992
- const gasCost = result.effects?.gasUsed ? Math.abs(
3993
- (Number(result.effects.gasUsed.computationCost) + Number(result.effects.gasUsed.storageCost) - Number(result.effects.gasUsed.storageRebate)) / 1e9
3994
- ) : 0;
3995
3989
  const protocolName = adapter.name;
3990
+ let effectiveAmount = withdrawAmount;
3991
+ const gasResult = await executeWithGas(this.client, this.keypair, async () => {
3992
+ const result = await adapter.buildWithdrawTx(this._address, withdrawAmount, params.asset);
3993
+ effectiveAmount = result.effectiveAmount;
3994
+ return result.tx;
3995
+ });
3996
3996
  this.portfolio.recordUnearn(params.asset);
3997
3997
  return {
3998
3998
  success: true,
3999
- tx: result.digest,
3999
+ tx: gasResult.digest,
4000
4000
  asset: params.asset,
4001
4001
  amount: effectiveAmount,
4002
4002
  protocol: protocolName,
4003
4003
  apy: 0,
4004
- gasCost,
4005
- gasMethod: "self-funded"
4004
+ gasCost: gasResult.gasCostSui,
4005
+ gasMethod: gasResult.gasMethod
4006
4006
  };
4007
4007
  }
4008
4008
  async getPortfolio() {
@@ -4027,7 +4027,7 @@ To sell investment: t2000 invest sell ${params.amount} ${fromAsset}`,
4027
4027
  const currentPrice = prices[pos.asset] ?? 0;
4028
4028
  let totalAmount = pos.totalAmount;
4029
4029
  let costBasis = pos.costBasis;
4030
- if (pos.asset in INVESTMENT_ASSETS) {
4030
+ if (pos.asset in INVESTMENT_ASSETS && !pos.earning) {
4031
4031
  try {
4032
4032
  const assetInfo = SUPPORTED_ASSETS[pos.asset];
4033
4033
  const bal = await this.client.getBalance({ owner: this._address, coinType: assetInfo.type });