New fetchWithValidation / introducing simpleFetch

This commit is contained in:
Aleksandr Kraiz
2022-05-10 11:44:09 +04:00
parent edc91ecefe
commit 883bcf928f
16 changed files with 350 additions and 117 deletions

View File

@@ -10,6 +10,7 @@ import {
} from '../../constants';
import { normalizeNumber } from '../../utils';
import getNativeCryptocurrency from '../../utils/getNativeCryptocurrency';
import simpleFetch from '../../simpleFetch';
export type DepositParams = {
asset: string,
@@ -38,12 +39,12 @@ export default async function deposit({
const {
exchangeContractAddress,
assetToAddress,
} = await orionBlockchain.getInfo();
} = await simpleFetch(orionBlockchain.getInfo)();
const nativeCryptocurrency = getNativeCryptocurrency(assetToAddress);
const exchangeContract = contracts.Exchange__factory.connect(exchangeContractAddress, provider);
const gasPriceWei = await orionBlockchain.getGasPriceWei();
const gasPriceWei = await simpleFetch(orionBlockchain.getGasPriceWei)();
const assetAddress = assetToAddress[asset];
if (!assetAddress) throw new Error(`Asset '${asset}' not found`);

View File

@@ -1,3 +1,4 @@
/* eslint-disable @typescript-eslint/unbound-method */
/* eslint-disable max-len */
import BigNumber from 'bignumber.js';
import { ethers } from 'ethers';
@@ -8,6 +9,7 @@ import OrionUnit from '..';
import { contracts, crypt, utils } from '../..';
import { INTERNAL_ORION_PRECISION, NATIVE_CURRENCY_PRECISION, SWAP_THROUGH_ORION_POOL_GAS_LIMIT } from '../../constants';
import getNativeCryptocurrency from '../../utils/getNativeCryptocurrency';
import simpleFetch from '../../simpleFetch';
export type SwapMarketParams = {
type: 'exactSpend' | 'exactReceive',
@@ -74,13 +76,13 @@ export default async function swapMarket({
exchangeContractAddress,
matcherAddress,
assetToAddress,
} = await orionBlockchain.getInfo();
const nativeCryptocurrency = getNativeCryptocurrency(assetToAddress);
} = await simpleFetch(orionBlockchain.getInfo)();
const nativeCryptocurrency = getNativeCryptocurrency(assetToAddress);
const exchangeContract = contracts.Exchange__factory.connect(exchangeContractAddress, provider);
const feeAssets = await orionBlockchain.getTokensFee();
const pricesInOrn = await orionBlockchain.getPrices();
const gasPriceWei = await orionBlockchain.getGasPriceWei();
const feeAssets = await simpleFetch(orionBlockchain.getTokensFee)();
const pricesInOrn = await simpleFetch(orionBlockchain.getPrices)();
const gasPriceWei = await simpleFetch(orionBlockchain.getGasPriceWei)();
const gasPriceGwei = ethers.utils.formatUnits(gasPriceWei, 'gwei').toString();
@@ -111,7 +113,7 @@ export default async function swapMarket({
signer,
);
const swapInfo = await orionAggregator.getSwapInfo(type, assetIn, assetOut, amount.toString());
const swapInfo = await simpleFetch(orionAggregator.getSwapInfo)(type, assetIn, assetOut, amount.toString());
if (swapInfo.type === 'exactReceive' && amountBN.lt(swapInfo.minAmountOut)) {
throw new Error(`Amount is too low. Min amountOut is ${swapInfo.minAmountOut} ${assetOut}`);
@@ -240,7 +242,7 @@ export default async function swapMarket({
.toString();
const [baseAssetName, quoteAssetName] = swapInfo.orderInfo.assetPair.split('-');
const pairConfig = await orionAggregator.getPairConfig(`${baseAssetName}-${quoteAssetName}`);
const pairConfig = await simpleFetch(orionAggregator.getPairConfig)(`${baseAssetName}-${quoteAssetName}`);
if (!pairConfig) throw new Error(`Pair config ${baseAssetName}-${quoteAssetName} not found`);
const baseAssetAddress = assetToAddress[baseAssetName];
@@ -336,7 +338,7 @@ export default async function swapMarket({
const orderIsOk = await exchangeContract.validateOrder(signedOrder);
if (!orderIsOk) throw new Error('Order is not valid');
const { orderId } = await orionAggregator.placeOrder(signedOrder, false);
const { orderId } = await simpleFetch(orionAggregator.placeOrder)(signedOrder, false);
return {
through: 'aggregator',
id: orderId,

View File

@@ -10,6 +10,7 @@ import {
} from '../../constants';
import { normalizeNumber } from '../../utils';
import getNativeCryptocurrency from '../../utils/getNativeCryptocurrency';
import simpleFetch from '../../simpleFetch';
export type WithdrawParams = {
asset: string,
@@ -38,11 +39,11 @@ export default async function withdraw({
const {
exchangeContractAddress,
assetToAddress,
} = await orionBlockchain.getInfo();
} = await simpleFetch(orionBlockchain.getInfo)();
const nativeCryptocurrency = getNativeCryptocurrency(assetToAddress);
const exchangeContract = contracts.Exchange__factory.connect(exchangeContractAddress, provider);
const gasPriceWei = await orionBlockchain.getGasPriceWei();
const gasPriceWei = await simpleFetch(orionBlockchain.getGasPriceWei)();
const assetAddress = assetToAddress[asset];
if (!assetAddress) throw new Error(`Asset '${asset}' not found`);

View File

@@ -4,6 +4,7 @@ import OrionUnit from '..';
import { contracts } from '../..';
import BalanceGuard from '../../BalanceGuard';
import { ADD_LIQUIDITY_GAS_LIMIT, INTERNAL_ORION_PRECISION, NATIVE_CURRENCY_PRECISION } from '../../constants';
import simpleFetch from '../../simpleFetch';
import { denormalizeNumber, normalizeNumber } from '../../utils';
import getBalances from '../../utils/getBalances';
import getNativeCryptocurrency from '../../utils/getNativeCryptocurrency';
@@ -46,7 +47,7 @@ export default class FarmingManager {
exchangeContractAddress,
assetToAddress,
assetToDecimals,
} = await this.orionUnit.orionBlockchain.getInfo();
} = await simpleFetch(this.orionUnit.orionBlockchain.getInfo)();
const walletAddress = await signer.getAddress();
@@ -85,7 +86,7 @@ export default class FarmingManager {
signer,
);
const poolsConfig = await this.orionUnit.orionBlockchain.getPoolsConfig();
const poolsConfig = await simpleFetch(this.orionUnit.orionBlockchain.getPoolsConfig)();
const pool = poolsConfig.pools[poolName];
if (!pool) throw new Error(`Pool ${poolName} not found`);
@@ -223,7 +224,7 @@ export default class FarmingManager {
assetToAddress,
assetToDecimals,
exchangeContractAddress,
} = await this.orionUnit.orionBlockchain.getInfo();
} = await simpleFetch(this.orionUnit.orionBlockchain.getInfo)();
const assetAAddress = assetToAddress[assetA];
if (!assetAAddress) throw new Error(`Asset '${assetA}' not found`);
@@ -235,7 +236,7 @@ export default class FarmingManager {
const assetBDecimals = assetToDecimals[assetB];
if (!assetBDecimals) throw new Error(`Decimals for asset '${assetB}' not found`);
const poolsConfig = await this.orionUnit.orionBlockchain.getPoolsConfig();
const poolsConfig = await simpleFetch(this.orionUnit.orionBlockchain.getPoolsConfig)();
const pool = poolsConfig.pools[poolName];
if (!pool) throw new Error(`Pool ${poolName} not found`);