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,
});
}
}