fix: params count in swap funct; feat: added comments

This commit is contained in:
Alex Kraiz
2023-12-11 13:39:23 +04:00
parent 14998f2531
commit 47d671a9f7
4 changed files with 11 additions and 4 deletions

4
package-lock.json generated
View File

@@ -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": {

View File

@@ -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];

View File

@@ -426,7 +426,6 @@ export default async function swapLimit({
walletAddress,
matcherAddress,
feeAssetAddress,
false,
signer,
chainId,
);

View File

@@ -45,6 +45,7 @@ export default async function swapMarket({
options,
}: SwapMarketParams): Promise<Swap> {
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,
);