make props as object

This commit is contained in:
TheJuze
2024-05-20 12:26:38 +03:00
parent 14c2f3bb5f
commit 5a981723e1
5 changed files with 70 additions and 47 deletions

4
package-lock.json generated
View File

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

View File

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

View File

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

View File

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

View File

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