Merge remote-tracking branch 'origin/main' into feat/OP-3702-add-governance-pools

This commit is contained in:
Dmitry Leleko
2023-04-12 17:00:49 +03:00
15 changed files with 137 additions and 318 deletions

View File

@@ -11,7 +11,7 @@ import {
REDEEMATOMIC_GAS_LIMIT,
WITHDRAW_GAS_LIMIT
} from '../../constants/index.js';
import getNativeCryptocurrency from '../../utils/getNativeCryptocurrency.js';
import getNativeCryptocurrencyName from '../../utils/getNativeCryptocurrencyName.js';
import { denormalizeNumber, generateSecret, normalizeNumber, toUpperCase } from '../../utils/index.js';
import type { SupportedChainId } from '../../types.js';
import type Orion from '../index.js';
@@ -100,7 +100,7 @@ export default async function swap({
assetToAddress: sourceAssetToAddress,
} = await simpleFetch(sourceOrionBlockchain.getInfo)();
const sourceChainNativeCryptocurrency = getNativeCryptocurrency(sourceAssetToAddress);
const sourceChainNativeCryptocurrency = getNativeCryptocurrencyName(sourceAssetToAddress);
const sourceExchangeContract = Exchange__factory.connect(sourceExchangeContractAddress, sourceProvider);
// const sourceChainGasPriceWei = await simpleFetch(sourceOrionBlockchain.getGasPriceWei)();
@@ -114,7 +114,7 @@ export default async function swap({
const targetChainAssetAddress = targetAssetToAddress[assetName];
if (targetChainAssetAddress === undefined) throw new Error(`Asset '${assetName}' not found in target chain`);
const targetChainNativeCryptocurrency = getNativeCryptocurrency(targetAssetToAddress);
const targetChainNativeCryptocurrency = getNativeCryptocurrencyName(targetAssetToAddress);
const targetExchangeContract = Exchange__factory.connect(targetExchangeContractAddress, targetProvider);
const sourceChainBalances = await getBalances(

View File

@@ -8,7 +8,7 @@ import {
DEPOSIT_ERC20_GAS_LIMIT, DEPOSIT_ETH_GAS_LIMIT, INTERNAL_ORION_PRECISION, NATIVE_CURRENCY_PRECISION,
} from '../../constants/index.js';
import { denormalizeNumber, normalizeNumber } from '../../utils/index.js';
import getNativeCryptocurrency from '../../utils/getNativeCryptocurrency.js';
import getNativeCryptocurrencyName from '../../utils/getNativeCryptocurrencyName.js';
import { simpleFetch } from 'simple-typed-fetch';
export type DepositParams = {
@@ -40,7 +40,7 @@ export default async function deposit({
assetToAddress,
} = await simpleFetch(orionBlockchain.getInfo)();
const nativeCryptocurrency = getNativeCryptocurrency(assetToAddress);
const nativeCryptocurrency = getNativeCryptocurrencyName(assetToAddress);
const exchangeContract = Exchange__factory.connect(exchangeContractAddress, provider);
const gasPriceWei = await simpleFetch(orionBlockchain.getGasPriceWei)();

View File

@@ -5,7 +5,7 @@ import { NATIVE_CURRENCY_PRECISION, SWAP_THROUGH_ORION_POOL_GAS_LIMIT } from '..
import type { OrionAggregator } from '../../services/OrionAggregator/index.js';
import type { OrionBlockchain } from '../../services/OrionBlockchain/index.js';
import { calculateFeeInFeeAsset, denormalizeNumber, getNativeCryptocurrency } from '../../utils/index.js';
import { calculateFeeInFeeAsset, denormalizeNumber, getNativeCryptocurrencyName } from '../../utils/index.js';
export type GetSwapInfoParams = {
type: 'exactSpend' | 'exactReceive'
@@ -43,7 +43,7 @@ export default async function getSwapInfo({
const {
assetToAddress,
} = await simpleFetch(orionBlockchain.getInfo)();
const nativeCryptocurrency = getNativeCryptocurrency(assetToAddress);
const nativeCryptocurrencyName = getNativeCryptocurrencyName(assetToAddress);
const feeAssets = await simpleFetch(orionBlockchain.getTokensFee)();
const pricesInOrn = await simpleFetch(orionBlockchain.getPrices)();
@@ -106,7 +106,7 @@ export default async function getSwapInfo({
route,
swapInfo,
fee: {
assetName: nativeCryptocurrency,
assetName: nativeCryptocurrencyName,
assetAddress: ethers.constants.AddressZero,
networkFeeInFeeAsset: denormalizedTransactionCost.toString(),
protocolFeeInFeeAsset: undefined,

View File

@@ -6,7 +6,7 @@ import BalanceGuard from '../../BalanceGuard.js';
import getAvailableSources from '../../utils/getAvailableFundsSources.js';
import type OrionUnit from '../index.js';
import { INTERNAL_ORION_PRECISION, NATIVE_CURRENCY_PRECISION, SWAP_THROUGH_ORION_POOL_GAS_LIMIT } from '../../constants/index.js';
import getNativeCryptocurrency from '../../utils/getNativeCryptocurrency.js';
import getNativeCryptocurrencyName from '../../utils/getNativeCryptocurrencyName.js';
import { calculateFeeInFeeAsset, denormalizeNumber, normalizeNumber } from '../../utils/index.js';
import { signOrder } from '../../crypt/index.js';
import type orderSchema from '../../services/OrionAggregator/schemas/orderSchema.js';
@@ -86,7 +86,7 @@ export default async function swapLimit({
matcherAddress,
assetToAddress,
} = await simpleFetch(orionBlockchain.getInfo)();
const nativeCryptocurrency = getNativeCryptocurrency(assetToAddress);
const nativeCryptocurrency = getNativeCryptocurrencyName(assetToAddress);
const exchangeContract = Exchange__factory.connect(exchangeContractAddress, provider);
const feeAssets = await simpleFetch(orionBlockchain.getTokensFee)();

View File

@@ -5,7 +5,7 @@ import getBalances from '../../utils/getBalances.js';
import BalanceGuard from '../../BalanceGuard.js';
import getAvailableSources from '../../utils/getAvailableFundsSources.js';
import { INTERNAL_ORION_PRECISION, NATIVE_CURRENCY_PRECISION, SWAP_THROUGH_ORION_POOL_GAS_LIMIT } from '../../constants/index.js';
import getNativeCryptocurrency from '../../utils/getNativeCryptocurrency.js';
import getNativeCryptocurrencyName from '../../utils/getNativeCryptocurrencyName.js';
import { calculateFeeInFeeAsset, denormalizeNumber, normalizeNumber } from '../../utils/index.js';
import { signOrder } from '../../crypt/index.js';
import type orderSchema from '../../services/OrionAggregator/schemas/orderSchema.js';
@@ -71,7 +71,7 @@ export default async function swapMarket({
matcherAddress,
assetToAddress,
} = await simpleFetch(orionBlockchain.getInfo)();
const nativeCryptocurrency = getNativeCryptocurrency(assetToAddress);
const nativeCryptocurrency = getNativeCryptocurrencyName(assetToAddress);
const exchangeContract = Exchange__factory.connect(exchangeContractAddress, provider);
const feeAssets = await simpleFetch(orionBlockchain.getTokensFee)();

View File

@@ -8,7 +8,7 @@ import {
INTERNAL_ORION_PRECISION, NATIVE_CURRENCY_PRECISION, WITHDRAW_GAS_LIMIT,
} from '../../constants/index.js';
import { denormalizeNumber, normalizeNumber } from '../../utils/index.js';
import getNativeCryptocurrency from '../../utils/getNativeCryptocurrency.js';
import getNativeCryptocurrencyName from '../../utils/getNativeCryptocurrencyName.js';
import { simpleFetch } from 'simple-typed-fetch';
export type WithdrawParams = {
@@ -40,7 +40,7 @@ export default async function withdraw({
assetToAddress,
} = await simpleFetch(orionBlockchain.getInfo)();
const nativeCryptocurrency = getNativeCryptocurrency(assetToAddress);
const nativeCryptocurrency = getNativeCryptocurrencyName(assetToAddress);
const exchangeContract = Exchange__factory.connect(exchangeContractAddress, provider);
const gasPriceWei = await simpleFetch(orionBlockchain.getGasPriceWei)();

View File

@@ -7,7 +7,7 @@ import BalanceGuard from '../../BalanceGuard.js';
import { ADD_LIQUIDITY_GAS_LIMIT, INTERNAL_ORION_PRECISION, NATIVE_CURRENCY_PRECISION } from '../../constants/index.js';
import { denormalizeNumber, normalizeNumber } from '../../utils/index.js';
import getBalances from '../../utils/getBalances.js';
import getNativeCryptocurrency from '../../utils/getNativeCryptocurrency.js';
import getNativeCryptocurrencyName from '../../utils/getNativeCryptocurrencyName.js';
const ADD_LIQUIDITY_SLIPPAGE = 0.05;
@@ -66,7 +66,7 @@ export default class FarmingManager {
const assetBDecimals = assetToDecimals[assetB];
if (assetBDecimals === undefined) throw new Error(`Decimals for asset '${assetB}' not found`);
const nativeCryptocurrency = getNativeCryptocurrency(assetToAddress);
const nativeCryptocurrency = getNativeCryptocurrencyName(assetToAddress);
const balances = await getBalances(
{
[assetA]: assetAAddress,
@@ -248,7 +248,7 @@ export default class FarmingManager {
const exchangeContract = Exchange__factory
.connect(exchangeContractAddress, this.orionUnit.provider);
const nativeCryptocurrency = getNativeCryptocurrency(assetToAddress);
const nativeCryptocurrency = getNativeCryptocurrencyName(assetToAddress);
const balances = await getBalances(
{
[assetA]: assetAAddress,

View File

@@ -1,6 +1,6 @@
import { ERC20__factory, type Exchange } from '@orionprotocol/contracts';
import type { BigNumber } from 'bignumber.js';
import type { BigNumber } from 'bignumber.js';
import { ethers } from 'ethers';
import { INTERNAL_ORION_PRECISION, NATIVE_CURRENCY_PRECISION } from '../constants/index.js';
import type { OrionAggregator } from '../services/OrionAggregator/index.js';
@@ -8,7 +8,7 @@ import denormalizeNumber from './denormalizeNumber.js';
export default async function getBalance(
orionAggregator: OrionAggregator,
asset: string,
assetName: string,
assetAddress: string,
walletAddress: string,
exchangeContract: Exchange,
@@ -32,13 +32,13 @@ export default async function getBalance(
}
const assetContractBalance = await exchangeContract.getBalance(assetAddress, walletAddress);
const denormalizedAssetInContractBalance = denormalizeNumber(assetContractBalance, INTERNAL_ORION_PRECISION);
const denormalizedAssetLockedBalanceResult = await orionAggregator.getLockedBalance(walletAddress, asset);
const denormalizedAssetLockedBalanceResult = await orionAggregator.getLockedBalance(walletAddress, assetName);
if (denormalizedAssetLockedBalanceResult.isErr()) {
throw new Error(denormalizedAssetLockedBalanceResult.error.message);
}
return {
exchange: denormalizedAssetInContractBalance.minus(denormalizedAssetLockedBalanceResult.value[asset] ?? 0),
exchange: denormalizedAssetInContractBalance.minus(denormalizedAssetLockedBalanceResult.value[assetName] ?? 0),
wallet: denormalizedAssetInWalletBalance,
};
}

View File

@@ -13,18 +13,18 @@ export default async (
) => {
const balances = await Promise.all(
Object.entries(balancesRequired)
.map(async ([asset, assetAddress]) => {
if (assetAddress === undefined) throw new Error(`Asset address of ${asset} not found`);
.map(async ([assetName, assetAddress]) => {
if (assetAddress === undefined) throw new Error(`Asset address of ${assetName} not found`);
const balance = await getBalance(
orionAggregator,
asset,
assetName,
assetAddress,
walletAddress,
exchangeContract,
provider,
);
return {
asset,
assetName,
amount: balance,
};
}),
@@ -35,6 +35,6 @@ export default async (
wallet: BigNumber
}>>>((prev, curr) => ({
...prev,
[curr.asset]: curr.amount,
[curr.assetName]: curr.amount,
}), {});
};

View File

@@ -1,19 +0,0 @@
import { ethers } from 'ethers';
const getNativeCryptocurrency = (assetToAddress: Partial<Record<string, string>>) => {
const addressToAsset = Object
.entries(assetToAddress)
.reduce<Partial<Record<string, string>>>((prev, [asset, address]) => {
if (address === undefined) return prev;
return {
...prev,
[address]: asset,
};
}, {});
const nativeCryptocurrency = addressToAsset[ethers.constants.AddressZero];
if (nativeCryptocurrency === undefined) throw new Error('Native cryptocurrency asset is not found');
return nativeCryptocurrency;
};
export default getNativeCryptocurrency;

View File

@@ -0,0 +1,21 @@
import { ethers } from 'ethers';
const getNativeCryptocurrencyName = (assetToAddress: Partial<Record<string, string>>) => {
const addressToAssetName = Object
.entries(assetToAddress)
.reduce<Partial<Record<string, string>>>((prev, [assetName, address]) => {
if (address === undefined) return prev;
return {
...prev,
[address]: assetName,
};
}, {});
const nativeCryptocurrencyName = addressToAssetName[ethers.constants.AddressZero];
if (nativeCryptocurrencyName === undefined) {
throw new Error('Native cryptocurrency asset name is not found');
}
return nativeCryptocurrencyName;
};
export default getNativeCryptocurrencyName;

View File

@@ -11,7 +11,7 @@ export { default as parseExchangeTradeTransaction } from './parseExchangeTradeTr
export { default as toUpperCase } from './toUpperCase.js';
export { default as toLowerCase } from './toLowerCase.js';
export { default as isUppercasedNetworkCode } from './isUppercasedNetworkCode.js';
export { default as getNativeCryptocurrency } from './getNativeCryptocurrency.js';
export { default as getNativeCryptocurrencyName } from './getNativeCryptocurrencyName.js';
export { default as isValidChainId } from './isValidChainId.js';
export { default as isKnownEnv } from './isKnownEnv.js';