diff --git a/package.json b/package.json index b2aa81c..2e0f74e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@orionprotocol/sdk", - "version": "0.20.38", + "version": "0.20.39", "description": "Orion Protocol SDK", "main": "./lib/index.cjs", "module": "./lib/index.js", diff --git a/src/BalanceGuard.ts b/src/BalanceGuard.ts index f466c82..9d0b881 100644 --- a/src/BalanceGuard.ts +++ b/src/BalanceGuard.ts @@ -9,6 +9,8 @@ import type { import { denormalizeNumber } from './utils/index.js'; import arrayEquals from './utils/arrayEquals.js'; +// BalanceGuard helps to check if there is enough balance to perform a swap +// Also it can fix some balance issues (e.g. approve tokens) export default class BalanceGuard { private readonly balances: Partial< Record< @@ -55,6 +57,8 @@ export default class BalanceGuard { assetBalance[source] = assetBalance[source].plus(amount); } + // Reset is approve some token to zero + // This operation may be required by some tokens, e.g. USDT private async checkResetRequired( assetAddress: string, spenderAddress: string, @@ -109,6 +113,7 @@ export default class BalanceGuard { }, []); } + // This method can fix some balance issues (e.g. approve tokens) private readonly fixAllAutofixableBalanceIssues = async ( balanceIssues: BalanceIssue[], ) => { @@ -163,6 +168,7 @@ export default class BalanceGuard { return balanceIssues.filter((item) => !autofixableBalanceIssues.includes(item)); }; + // Check that all balance requirements are fulfilled async check(fixAutofixable?: boolean) { this.logger?.(`Balance requirements: ${this.requirements .map((requirement) => `${requirement.amount} ${requirement.asset.name} ` + @@ -173,6 +179,7 @@ export default class BalanceGuard { const remainingBalances = clone(this.balances); const aggregatedRequirements = BalanceGuard.aggregateBalanceRequirements(this.requirements); + // DO NOT IGNORE THIS COMMENT. THIS IS VERY IMPORTANT TO UNDERSTAND THIS CODE // Balance absorption order is important! // 1. Exchange-contract only // 2. Exchange + wallet (can produce approves requirements) @@ -321,6 +328,7 @@ export default class BalanceGuard { const walletTokensAggregatedRequirements = flattedAggregatedRequirements .filter(({ sources, asset }) => sources[0] === 'wallet' && asset.name !== this.nativeCryptocurrency.name); + // This requirements can be fulfilled by wallet await Promise.all(walletTokensAggregatedRequirements .map(async ({ asset, spenderAddress, items }) => { const remainingBalance = remainingBalances[asset.name]; diff --git a/src/Unit/Exchange/index.ts b/src/Unit/Exchange/index.ts index 23e71da..217afb3 100644 --- a/src/Unit/Exchange/index.ts +++ b/src/Unit/Exchange/index.ts @@ -1,13 +1,19 @@ import type Unit from '../index.js'; import deposit, { type DepositParams } from './deposit.js'; import getSwapInfo, { type GetSwapInfoParams } from './getSwapInfo.js'; -import {generateSwapCalldataWithUnit, type GenerateSwapCalldataWithUnitParams } from './generateSwapCalldata.js'; +import { generateSwapCalldataWithUnit, type GenerateSwapCalldataWithUnitParams } from './generateSwapCalldata.js'; import withdraw, { type WithdrawParams } from './withdraw.js'; +import type { SwapLimitParams } from './swapLimit.js'; +import swapLimit from './swapLimit.js'; +import swapMarket from './swapMarket.js'; +import type { SwapMarketParams } from './swapMarket.js'; type PureDepositParams = Omit type PureWithdrawParams = Omit type PureGetSwapMarketInfoParams = Omit type PureGenerateSwapCalldataParams = Omit +type PureSwapLimitParams = Omit +type PureSwapMarketParams = Omit export default class Exchange { private readonly unit: Unit; @@ -44,4 +50,18 @@ export default class Exchange { unit: this.unit }) } + + public swapLimit(params: PureSwapLimitParams) { + return swapLimit({ + ...params, + unit: this.unit, + }); + } + + public swapMarket(params: PureSwapMarketParams) { + return swapMarket({ + ...params, + unit: this.unit, + }); + } } diff --git a/src/Unit/Exchange/swapLimit.ts b/src/Unit/Exchange/swapLimit.ts index e9d7492..8ea85c2 100644 --- a/src/Unit/Exchange/swapLimit.ts +++ b/src/Unit/Exchange/swapLimit.ts @@ -334,7 +334,7 @@ export default async function swapLimit({ options?.logger?.(`Transaction sent. Tx hash: ${swapThroughOrionPoolTxResponse.hash}`); return { amountOut: swapInfo.amountOut, - wait: swapThroughOrionPoolTxResponse.wait, + wait: swapThroughOrionPoolTxResponse.wait.bind(swapThroughOrionPoolTxResponse), through: 'pool', txHash: swapThroughOrionPoolTxResponse.hash, }; @@ -426,7 +426,6 @@ export default async function swapLimit({ walletAddress, matcherAddress, feeAssetAddress, - false, signer, chainId, ); diff --git a/src/Unit/Exchange/swapMarket.ts b/src/Unit/Exchange/swapMarket.ts index 90c6bd4..afcbaf0 100644 --- a/src/Unit/Exchange/swapMarket.ts +++ b/src/Unit/Exchange/swapMarket.ts @@ -45,6 +45,7 @@ export default async function swapMarket({ options, }: SwapMarketParams): Promise { if (options?.developer) options.logger?.('YOU SPECIFIED A DEVELOPER OPTIONS. BE CAREFUL!'); + if (amount === '') throw new Error('Amount can not be empty'); if (assetIn === '') throw new Error('AssetIn can not be empty'); if (assetOut === '') throw new Error('AssetOut can not be empty'); @@ -284,7 +285,7 @@ export default async function swapMarket({ options?.logger?.(`Transaction sent. Tx hash: ${swapThroughOrionPoolTxResponse.hash}`); return { amountOut: swapInfo.amountOut, - wait: swapThroughOrionPoolTxResponse.wait, + wait: swapThroughOrionPoolTxResponse.wait.bind(swapThroughOrionPoolTxResponse), through: 'pool', txHash: swapThroughOrionPoolTxResponse.hash, }; @@ -384,7 +385,6 @@ export default async function swapMarket({ walletAddress, matcherAddress, feeAssetAddress, - false, signer, chainId, );