From 47d671a9f73236503184b1138be7283abc7833e4 Mon Sep 17 00:00:00 2001 From: Alex Kraiz Date: Mon, 11 Dec 2023 13:39:23 +0400 Subject: [PATCH 1/3] fix: params count in swap funct; feat: added comments --- package-lock.json | 4 ++-- src/BalanceGuard.ts | 8 ++++++++ src/Unit/Exchange/swapLimit.ts | 1 - src/Unit/Exchange/swapMarket.ts | 2 +- 4 files changed, 11 insertions(+), 4 deletions(-) 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, ); From 994e6d44fa989e32698094de53eec5113f248a87 Mon Sep 17 00:00:00 2001 From: Alex Kraiz Date: Thu, 4 Jan 2024 02:59:34 +0400 Subject: [PATCH 2/3] feat: return back swap market / limit --- package.json | 2 +- src/Unit/Exchange/index.ts | 14 +++++++++++++- src/Unit/Exchange/swapLimit.ts | 2 +- src/Unit/Exchange/swapMarket.ts | 4 ++-- 4 files changed, 17 insertions(+), 5 deletions(-) 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/Unit/Exchange/index.ts b/src/Unit/Exchange/index.ts index 23e71da..284ddef 100644 --- a/src/Unit/Exchange/index.ts +++ b/src/Unit/Exchange/index.ts @@ -1,8 +1,12 @@ 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 @@ -44,4 +48,12 @@ export default class Exchange { unit: this.unit }) } + + public swapLimit(params: SwapLimitParams) { + return swapLimit(params); + } + + public swapMarket(params: SwapMarketParams) { + return swapMarket(params); + } } diff --git a/src/Unit/Exchange/swapLimit.ts b/src/Unit/Exchange/swapLimit.ts index d69a9e0..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, }; diff --git a/src/Unit/Exchange/swapMarket.ts b/src/Unit/Exchange/swapMarket.ts index 0557e56..afcbaf0 100644 --- a/src/Unit/Exchange/swapMarket.ts +++ b/src/Unit/Exchange/swapMarket.ts @@ -45,7 +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'); @@ -285,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, }; From 0633f9c1139fd7b4bf4b7d80453a14bbc900c790 Mon Sep 17 00:00:00 2001 From: Alex Kraiz Date: Thu, 4 Jan 2024 11:21:04 +0400 Subject: [PATCH 3/3] fix: swapMarket / swapLimit --- src/Unit/Exchange/index.ts | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/Unit/Exchange/index.ts b/src/Unit/Exchange/index.ts index 284ddef..217afb3 100644 --- a/src/Unit/Exchange/index.ts +++ b/src/Unit/Exchange/index.ts @@ -12,6 +12,8 @@ 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; @@ -49,11 +51,17 @@ export default class Exchange { }) } - public swapLimit(params: SwapLimitParams) { - return swapLimit(params); + public swapLimit(params: PureSwapLimitParams) { + return swapLimit({ + ...params, + unit: this.unit, + }); } - public swapMarket(params: SwapMarketParams) { - return swapMarket(params); + public swapMarket(params: PureSwapMarketParams) { + return swapMarket({ + ...params, + unit: this.unit, + }); } }