Strictest / tests / minor improvements

This commit is contained in:
Aleksandr Kraiz
2023-02-17 17:31:35 +04:00
parent 11c8348ee9
commit 2c24f71453
39 changed files with 566 additions and 128 deletions

View File

@@ -116,12 +116,19 @@ export default async function deposit({
unsignedTx.nonce = nonce;
const signedTx = await signer.signTransaction(unsignedTx);
const txResponse = await provider.sendTransaction(signedTx);
console.log(`Deposit tx sent: ${txResponse.hash}. Waiting for confirmation...`);
const txReceipt = await txResponse.wait();
if (txReceipt.status !== undefined) {
console.log('Deposit tx confirmed');
} else {
console.log('Deposit tx failed');
try {
const txResponse = await provider.sendTransaction(signedTx);
console.log(`Deposit tx sent: ${txResponse.hash}. Waiting for confirmation...`);
const txReceipt = await txResponse.wait();
if (txReceipt.status !== undefined) {
console.log('Deposit tx confirmed');
} else {
console.log('Deposit tx failed');
}
} catch (e) {
if (!(e instanceof Error)) throw new Error('e is not an Error');
console.error(`Deposit tx failed: ${e.message}`, {
unsignedTx,
});
}
}

View File

@@ -1,8 +1,8 @@
import BigNumber from 'bignumber.js';
import { ethers } from 'ethers';
import { NATIVE_CURRENCY_PRECISION, SWAP_THROUGH_ORION_POOL_GAS_LIMIT } from '../../constants';
import { type OrionAggregator } from '../../services/OrionAggregator';
import { type OrionBlockchain } from '../../services/OrionBlockchain';
import type { OrionAggregator } from '../../services/OrionAggregator';
import type { OrionBlockchain } from '../../services/OrionBlockchain';
import simpleFetch from '../../simpleFetch';
import { calculateFeeInFeeAsset, denormalizeNumber, getNativeCryptocurrency } from '../../utils';

View File

@@ -10,6 +10,8 @@ import getNativeCryptocurrency from '../../utils/getNativeCryptocurrency';
import simpleFetch from '../../simpleFetch';
import { calculateFeeInFeeAsset, denormalizeNumber, normalizeNumber } from '../../utils';
import { signOrder } from '../../crypt';
import type orderSchema from '../../services/OrionAggregator/schemas/orderSchema';
import type { z } from 'zod';
export type SwapMarketParams = {
type: 'exactSpend' | 'exactReceive'
@@ -34,11 +36,13 @@ export type SwapMarketParams = {
type AggregatorOrder = {
through: 'aggregator'
id: string
wait: () => Promise<z.infer<typeof orderSchema>>
}
type PoolSwap = {
through: 'orion_pool'
txHash: string
wait: (confirmations?: number | undefined) => Promise<ethers.providers.TransactionReceipt>
}
export type Swap = AggregatorOrder | PoolSwap;
@@ -293,6 +297,7 @@ export default async function swapMarket({
const swapThroughOrionPoolTxResponse = await signer.sendTransaction(unsignedSwapThroughOrionPoolTx);
options?.logger?.(`Transaction sent. Tx hash: ${swapThroughOrionPoolTxResponse.hash}`);
return {
wait: swapThroughOrionPoolTxResponse.wait,
through: 'orion_pool',
txHash: swapThroughOrionPoolTxResponse.hash,
};
@@ -408,6 +413,26 @@ export default async function swapMarket({
options?.logger?.(`Order placed. Order id: ${orderId}`);
return {
wait: () => new Promise<z.infer<typeof orderSchema>>((resolve, reject) => {
const timeout = setTimeout(() => {
reject(new Error('Timeout'))
}, 60000);
const interval = setInterval(() => {
simpleFetch(orionAggregator.getOrder)(orderId).then((data) => {
if (data.order.status === 'SETTLED') {
options?.logger?.(`Order ${orderId} settled`);
clearTimeout(timeout);
clearInterval(interval);
resolve(data);
} else {
options?.logger?.(`Order ${orderId} status: ${data.order.status}`);
}
}).catch((e) => {
if (!(e instanceof Error)) throw new Error('Not an error');
options?.logger?.(`Error while getting order status: ${e.message}`);
});
}, 1000);
}),
through: 'aggregator',
id: orderId,
};

View File

@@ -109,10 +109,17 @@ export default async function withdraw({
const signedTx = await signer.signTransaction(unsignedTx);
const txResponse = await provider.sendTransaction(signedTx);
console.log(`Withdraw tx sent: ${txResponse.hash}. Waiting for confirmation...`);
const txReceipt = await txResponse.wait();
if (txReceipt.status !== undefined) {
console.log('Withdraw tx confirmed');
} else {
console.log('Withdraw tx failed');
try {
const txReceipt = await txResponse.wait();
if (txReceipt.status !== undefined) {
console.log('Withdraw tx confirmed');
} else {
console.log('Withdraw tx failed');
}
} catch (e) {
if (!(e instanceof Error)) throw new Error('e is not an Error');
console.error(`Deposit tx failed: ${e.message}`, {
unsignedTx,
});
}
}

View File

@@ -2,16 +2,16 @@ import { ethers } from 'ethers';
import { OrionAggregator } from '../services/OrionAggregator';
import { OrionBlockchain } from '../services/OrionBlockchain';
import { PriceFeed } from '../services/PriceFeed';
import type { SupportedChainId, VerboseOrionUnitConfig } from '../types';
import type { KnownEnv, SupportedChainId, VerboseOrionUnitConfig } from '../types';
import Exchange from './Exchange';
import FarmingManager from './FarmingManager';
import { chains } from '../config';
import { type networkCodes } from '../constants';
import { chains, envs } from '../config';
import type { networkCodes } from '../constants';
// type KnownConfig = {
// env: string;
// chainId: SupportedChainId;
// }
type KnownConfig = {
env: KnownEnv
chainId: SupportedChainId
}
// type OrionUnitConfig = KnownConfig | VerboseOrionUnitConfig;
@@ -39,8 +39,36 @@ export default class OrionUnit {
// constructor(config: KnownConfig);
// constructor(config: VerboseConfig);
constructor(config: VerboseOrionUnitConfig) {
this.config = config;
constructor(config: KnownConfig | VerboseOrionUnitConfig) {
if ('env' in config) {
const staticConfig = envs[config.env];
if (!staticConfig) throw new Error(`Invalid environment: ${config.env}. Available environments: ${Object.keys(envs).join(', ')}`);
const chainConfig = chains[config.chainId];
if (!chainConfig) throw new Error(`Invalid chainId: ${config.chainId}. Available chainIds: ${Object.keys(chains).join(', ')}`);
const networkConfig = staticConfig.networks[config.chainId];
if (!networkConfig) throw new Error(`Invalid chainId: ${config.chainId}. Available chainIds: ${Object.keys(staticConfig.networks).join(', ')}`);
this.config = {
chainId: config.chainId,
nodeJsonRpc: networkConfig.rpc ?? chainConfig.rpc,
services: {
orionBlockchain: {
http: networkConfig.api + networkConfig.services.blockchain.http,
},
orionAggregator: {
http: networkConfig.api + networkConfig.services.aggregator.http,
ws: networkConfig.api + networkConfig.services.aggregator.ws,
},
priceFeed: {
api: networkConfig.api + networkConfig.services.priceFeed.all,
},
},
}
} else {
this.config = config;
}
const chainInfo = chains[config.chainId];
if (!chainInfo) throw new Error('Chain info is required');
@@ -48,14 +76,14 @@ export default class OrionUnit {
// this.env = config.env;
this.chainId = config.chainId;
this.networkCode = chainInfo.code;
this.provider = new ethers.providers.StaticJsonRpcProvider(config.nodeJsonRpc);
this.provider = new ethers.providers.StaticJsonRpcProvider(this.config.nodeJsonRpc);
this.orionBlockchain = new OrionBlockchain(config.services.orionBlockchain.http);
this.orionBlockchain = new OrionBlockchain(this.config.services.orionBlockchain.http);
this.orionAggregator = new OrionAggregator(
config.services.orionAggregator.http,
config.services.orionAggregator.ws,
this.config.services.orionAggregator.http,
this.config.services.orionAggregator.ws,
);
this.priceFeed = new PriceFeed(config.services.priceFeed.api);
this.priceFeed = new PriceFeed(this.config.services.priceFeed.api);
this.exchange = new Exchange(this);
this.farmingManager = new FarmingManager(this);
}