From 5a981723e1d82e91a57b29669e22116c02e20fe4 Mon Sep 17 00:00:00 2001 From: TheJuze Date: Mon, 20 May 2024 12:26:38 +0300 Subject: [PATCH] make props as object --- package-lock.json | 4 +-- package.json | 2 +- src/Unit/Exchange/swapLimit.ts | 34 +++++++++++++---------- src/Unit/Exchange/swapMarket.ts | 28 +++++++++++-------- src/crypt/signOrder.ts | 49 +++++++++++++++++++++------------ 5 files changed, 70 insertions(+), 47 deletions(-) diff --git a/package-lock.json b/package-lock.json index 87b8a5f..97d21c6 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@orionprotocol/sdk", - "version": "0.21.0-rc4", + "version": "0.21.0-rc5", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@orionprotocol/sdk", - "version": "0.21.0-rc4", + "version": "0.21.0-rc5", "hasInstallScript": true, "license": "ISC", "dependencies": { diff --git a/package.json b/package.json index d2829c9..f62133a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@orionprotocol/sdk", - "version": "0.21.0-rc4", + "version": "0.21.0-rc5", "description": "Orion Protocol SDK", "main": "./lib/index.cjs", "module": "./lib/index.js", diff --git a/src/Unit/Exchange/swapLimit.ts b/src/Unit/Exchange/swapLimit.ts index 6c9f4c8..26b3049 100644 --- a/src/Unit/Exchange/swapLimit.ts +++ b/src/Unit/Exchange/swapLimit.ts @@ -5,7 +5,11 @@ import getBalances from '../../utils/getBalances.js'; import BalanceGuard from '../../BalanceGuard.js'; import getAvailableSources from '../../utils/getAvailableFundsSources.js'; import type Unit from '../index.js'; -import { INTERNAL_PROTOCOL_PRECISION, NATIVE_CURRENCY_PRECISION, SWAP_THROUGH_ORION_POOL_GAS_LIMIT } from '../../constants/index.js'; +import { + INTERNAL_PROTOCOL_PRECISION, + NATIVE_CURRENCY_PRECISION, + SWAP_THROUGH_ORION_POOL_GAS_LIMIT +} from '../../constants/index.js'; import getNativeCryptocurrencyName from '../../utils/getNativeCryptocurrencyName.js'; import { calculateFeeInFeeAsset, denormalizeNumber, normalizeNumber } from '../../utils/index.js'; import { signOrder } from '../../crypt/index.js'; @@ -212,7 +216,7 @@ export default async function swapLimit({ if (options.developer.route === 'pool' && !priceIsBTEMP) { throw new Error( 'CONFLICT: Pool execution is not available for this swap.' + - ' Price is worse than market price. Please unset "route" option or set it to "aggregator"' + ' Price is worse than market price. Please unset "route" option or set it to "aggregator"' ); } route = options.developer.route; @@ -221,17 +225,17 @@ export default async function swapLimit({ if (!priceIsBTEMP) { throw new Error( 'CONFLICT: Pool execution is not available for this swap.' + - ' Price is worse than market price. Please disable "poolOnly" option' + ' Price is worse than market price. Please disable "poolOnly" option' ); } options.logger?.('Swap is through pool (because "poolOnly" option is true)'); route = 'pool'; } else if ( poolExchangesList.length > 0 && - swapExchanges.length === 1 && - firstSwapExchange !== undefined && - poolExchangesList.some((poolExchange) => poolExchange === firstSwapExchange) && - priceIsBTEMP + swapExchanges.length === 1 && + firstSwapExchange !== undefined && + poolExchangesList.some((poolExchange) => poolExchange === firstSwapExchange) && + priceIsBTEMP ) { options?.logger?.(`Swap is through pool [via ${firstSwapExchange}] (detected by "exchanges" field)`); route = 'pool'; @@ -439,19 +443,19 @@ export default async function swapLimit({ await balanceGuard.check(options?.autoApprove); - const signedOrder = await signOrder( + const signedOrder = await signOrder({ baseAssetAddress, quoteAssetAddress, - swapInfo.orderInfo.side, - safePriceWithAppliedPrecision.toString(), - swapInfo.orderInfo.amount, - totalFeeInFeeAsset, - walletAddress, + side: swapInfo.orderInfo.side, + price: safePriceWithAppliedPrecision.toString(), + amount: swapInfo.orderInfo.amount, + matcherFee: totalFeeInFeeAsset, + senderAddress: walletAddress, matcherAddress, - feeAssetAddress, + serviceFeeAssetAddress: feeAssetAddress, signer, chainId, - ); + }); const orderIsOk = await exchangeContract.validateOrder(signedOrder); if (!orderIsOk) throw new Error('Order is not valid'); diff --git a/src/Unit/Exchange/swapMarket.ts b/src/Unit/Exchange/swapMarket.ts index be7350a..a5c971b 100644 --- a/src/Unit/Exchange/swapMarket.ts +++ b/src/Unit/Exchange/swapMarket.ts @@ -4,7 +4,11 @@ import { Exchange__factory } from '@orionprotocol/contracts/lib/ethers-v6/index. import getBalances from '../../utils/getBalances.js'; import BalanceGuard from '../../BalanceGuard.js'; import getAvailableSources from '../../utils/getAvailableFundsSources.js'; -import { INTERNAL_PROTOCOL_PRECISION, NATIVE_CURRENCY_PRECISION, SWAP_THROUGH_ORION_POOL_GAS_LIMIT } from '../../constants'; +import { + INTERNAL_PROTOCOL_PRECISION, + NATIVE_CURRENCY_PRECISION, + SWAP_THROUGH_ORION_POOL_GAS_LIMIT +} from '../../constants'; import getNativeCryptocurrencyName from '../../utils/getNativeCryptocurrencyName.js'; import { calculateFeeInFeeAsset, denormalizeNumber, normalizeNumber } from '../../utils/index.js'; import { signOrder } from '../../crypt/index.js'; @@ -178,9 +182,9 @@ export default async function swapMarket({ route = 'pool'; } else if ( poolExchangesList.length > 0 && - swapExchanges.length === 1 && - firstSwapExchange !== undefined && - poolExchangesList.some((poolExchange) => poolExchange === firstSwapExchange) + swapExchanges.length === 1 && + firstSwapExchange !== undefined && + poolExchangesList.some((poolExchange) => poolExchange === firstSwapExchange) ) { options?.logger?.(`Swap is through pool [via ${firstSwapExchange}] (detected by "exchanges" field)`); route = 'pool'; @@ -398,19 +402,19 @@ export default async function swapMarket({ await balanceGuard.check(options?.autoApprove); - const signedOrder = await signOrder( + const signedOrder = await signOrder({ baseAssetAddress, quoteAssetAddress, - swapInfo.orderInfo.side, - safePriceWithAppliedPrecision.toString(), - swapInfo.orderInfo.amount, - totalFeeInFeeAsset, - walletAddress, + side: swapInfo.orderInfo.side, + price: safePriceWithAppliedPrecision.toString(), + amount: swapInfo.orderInfo.amount, + matcherFee: totalFeeInFeeAsset, + senderAddress: walletAddress, matcherAddress, - feeAssetAddress, + serviceFeeAssetAddress: feeAssetAddress, signer, chainId, - ); + }); const orderIsOk = await exchangeContract.validateOrder(signedOrder); if (!orderIsOk) throw new Error('Order is not valid'); diff --git a/src/crypt/signOrder.ts b/src/crypt/signOrder.ts index 533662a..331bd61 100644 --- a/src/crypt/signOrder.ts +++ b/src/crypt/signOrder.ts @@ -10,20 +10,35 @@ import generateSecret from '../utils/generateSecret'; const DEFAULT_EXPIRATION = 29 * 24 * 60 * 60 * 1000; // 29 days -export const signOrder = async ( - baseAssetAddr: string, - quoteAssetAddr: string, - side: 'BUY' | 'SELL', - price: BigNumber.Value, - amount: BigNumber.Value, - matcherFee: BigNumber.Value, - senderAddress: string, - matcherAddress: string, - serviceFeeAssetAddr: string, - signer: ethers.Signer, - chainId: SupportedChainId, - targetChainId?: SupportedChainId, -) => { +type SignOrderProps = { + baseAssetAddress: string + quoteAssetAddress: string + side: 'BUY' | 'SELL' + price: BigNumber.Value + amount: BigNumber.Value + matcherFee: BigNumber.Value + senderAddress: string + matcherAddress: string + serviceFeeAssetAddress: string + signer: ethers.Signer + chainId: SupportedChainId + targetChainId?: SupportedChainId +} + +export const signOrder = async ({ + amount, + signer, + side, + baseAssetAddress, + quoteAssetAddress, + serviceFeeAssetAddress, + matcherFee, + matcherAddress, + senderAddress, + targetChainId, + chainId, + price +}: SignOrderProps) => { const nonce = Date.now(); const expiration = nonce + DEFAULT_EXPIRATION; const secret = generateSecret(); @@ -34,9 +49,9 @@ export const signOrder = async ( const order: Order | CrossOrder = { senderAddress, matcherAddress, - baseAsset: baseAssetAddr, - quoteAsset: quoteAssetAddr, - matcherFeeAsset: serviceFeeAssetAddr, + baseAsset: baseAssetAddress, + quoteAsset: quoteAssetAddress, + matcherFeeAsset: serviceFeeAssetAddress, amount: Number(normalizeNumber( amount, INTERNAL_PROTOCOL_PRECISION,