Merge pull request #222 from lobotomoe/main

fix: params count in swap funct; feat: added comments
This commit is contained in:
kigastu
2024-01-04 15:24:23 +03:00
committed by GitHub
5 changed files with 33 additions and 6 deletions

View File

@@ -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",

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

@@ -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<DepositParams, 'unit'>
type PureWithdrawParams = Omit<WithdrawParams, 'unit'>
type PureGetSwapMarketInfoParams = Omit<GetSwapInfoParams, 'blockchainService' | 'aggregator'>
type PureGenerateSwapCalldataParams = Omit<GenerateSwapCalldataWithUnitParams, 'unit'>
type PureSwapLimitParams = Omit<SwapLimitParams, 'unit'>
type PureSwapMarketParams = Omit<SwapMarketParams, 'unit'>
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,
});
}
}

View File

@@ -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,
);

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');
@@ -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,
);