Price conversion iprovements

This commit is contained in:
Aleksandr Kraiz
2023-08-08 10:21:54 +04:00
parent 72581b0f4b
commit cd0a0128d8
6 changed files with 24 additions and 78 deletions

View File

@@ -1,6 +1,6 @@
import type { ethers } from 'ethers';
import {
AtomicSwapStatus, type AtomicSwap, type SupportedChainId,
type AtomicSwap, type SupportedChainId,
type Unit, INTERNAL_PROTOCOL_PRECISION
} from '../../index.js';
import getHistoryExt from './getHistory.js';
@@ -8,14 +8,6 @@ import swapExt from './swap.js';
import { BigNumber } from 'bignumber.js';
const backendStatusToAtomicSwapStatus = {
LOCKED: AtomicSwapStatus.ROUTING,
CLAIMED: AtomicSwapStatus.SETTLED,
REFUNDED: AtomicSwapStatus.REFUNDED,
REDEEMED: AtomicSwapStatus.SETTLED,
'BEFORE-REDEEM': AtomicSwapStatus.ROUTING,
} as const;
export default class Bridge {
constructor(
private readonly unitsArray: Unit[],
@@ -38,13 +30,12 @@ export default class Bridge {
targetChainId,
asset,
sourceChainId,
status: asStatus,
claimed,
sender,
transactions,
expiration,
creationDate,
} = atomicSwap;
const localSwap = externalStoredAtomicSwaps.find(
(swap) => secretHash === swap.secretHash,
);
@@ -61,53 +52,24 @@ export default class Bridge {
assetName = '—'; // We don't want to display address even if we can't find asset name
}
// Define status
let historyStatus = backendStatusToAtomicSwapStatus[asStatus.source];
if (asStatus.source === 'LOCKED') {
const historySwap = bridgeHistory[secretHash];
if (historySwap?.status.target === 'REDEEMED') {
historyStatus = AtomicSwapStatus.SETTLED;
}
}
if (claimed) historyStatus = AtomicSwapStatus.SETTLED;
let status: AtomicSwapStatus | undefined;
if (
[AtomicSwapStatus.SETTLED, AtomicSwapStatus.REFUNDED].includes(
historyStatus,
)
) {
status = historyStatus;
} else {
status = localSwap !== undefined ? localSwap.status : historyStatus;
}
// Define secret
const secret = localSwap !== undefined ? localSwap.secret : '';
// Define environment
const env = localSwap?.env;
return {
liquidityMigrationTxHash: localSwap?.liquidityMigrationTxHash,
localSwap,
sourceNetwork: sourceChainId,
targetNetwork: targetChainId,
amount: new BigNumber(amount)
.multipliedBy(new BigNumber(10).pow(INTERNAL_PROTOCOL_PRECISION))
.toString(),
walletAddress: sender,
secret,
secretHash,
lockTransactionHash: transactions?.lock,
refundTransactionHash: transactions?.refund,
status,
asset: assetName,
expiration:
expiration?.lock ?? creationDate.getTime() + 60 * 60 * 24 * 4, // Or default 4 days
creationDate: creationDate.getTime(),
env,
redeemOrder: atomicSwap.redeemOrder,
};
}).filter((swap) => swap.env === undefined || swap.env === this.env);
});
}
getHistory(address: string, limit = 1000) {