diff --git a/package-lock.json b/package-lock.json index e5dea1e..0de3861 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@orionprotocol/sdk", - "version": "0.20.28", + "version": "0.20.29", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@orionprotocol/sdk", - "version": "0.20.28", + "version": "0.20.29", "hasInstallScript": true, "license": "ISC", "dependencies": { 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/swapLimit.ts b/src/Unit/Exchange/swapLimit.ts index e9d7492..d69a9e0 100644 --- a/src/Unit/Exchange/swapLimit.ts +++ b/src/Unit/Exchange/swapLimit.ts @@ -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..0557e56 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'); @@ -384,7 +385,6 @@ export default async function swapMarket({ walletAddress, matcherAddress, feeAssetAddress, - false, signer, chainId, );