mirror of
https://github.com/orionprotocol/sdk.git
synced 2026-03-14 06:02:36 +03:00
Price conversion iprovements
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@orionprotocol/sdk",
|
||||
"version": "0.19.47",
|
||||
"version": "0.19.48",
|
||||
"description": "Orion Protocol SDK",
|
||||
"main": "./lib/index.cjs",
|
||||
"module": "./lib/index.js",
|
||||
|
||||
@@ -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) {
|
||||
|
||||
33
src/types.ts
33
src/types.ts
@@ -283,42 +283,9 @@ export type RedeemOrder = {
|
||||
claimReceiver: string
|
||||
}
|
||||
|
||||
export enum AtomicSwapStatus {
|
||||
ROUTING_REQUESTED = 'ROUTING_REQUESTED',
|
||||
ROUTING_PENDING = 'ROUTING_PENDING',
|
||||
ROUTING = 'ROUTING',
|
||||
ROUTING_FAILED = 'ROUTING_FAILED',
|
||||
|
||||
// ACCEPTED = 'ACCEPTED',
|
||||
FAILED = 'FAILED',
|
||||
REJECTED = 'REJECTED',
|
||||
|
||||
CHECK_REDEEM_THROUGH_OB_AVAILABLE = 'CHECK_REDEEM_THROUGH_OB_AVAILABLE',
|
||||
// Redeem
|
||||
// Blockchain redeem
|
||||
READY_TO_REDEEM = 'READY_TO_REDEEM',
|
||||
REDEEM_REQUESTED = 'REDEEM_REQUESTED',
|
||||
REDEEM_PENDING = 'REDEEM_PENDING',
|
||||
REDEEM_FAILED = 'REDEEM_FAILED',
|
||||
|
||||
// Orion blockchain redeem
|
||||
READY_TO_REDEEM_THROUGH_OB = 'READY_TO_REDEEM_THROUGH_OB',
|
||||
REDEEM_PENDING_THROUGH_OB = 'REDEEM_PENDING_THROUGH_OB',
|
||||
REDEEM_FAILED_THROUGH_OB = 'REDEEM_FAILED_THROUGH_OB',
|
||||
|
||||
SETTLED = 'SETTLED',
|
||||
|
||||
// Refund
|
||||
REFUND_REQUESTED = 'REFUND_REQUESTED',
|
||||
REFUND_PENDING = 'REFUND_PENDING',
|
||||
REFUNDED = 'REFUNDED',
|
||||
REFUND_FAILED = 'REFUND_FAILED',
|
||||
}
|
||||
|
||||
export type AtomicSwap = {
|
||||
secret: string
|
||||
secretHash: string
|
||||
status: AtomicSwapStatus
|
||||
|
||||
walletAddress: string
|
||||
env?: string | undefined
|
||||
|
||||
@@ -7,10 +7,10 @@ export default function convertPrice(
|
||||
prices: Partial<Record<string, string>> // quoted in quoteAsset. [name]: priceQuotedInQuoteAsset
|
||||
) {
|
||||
const assetInPrice = prices[assetInName];
|
||||
if (assetInPrice === undefined) throw Error(`Price conversion: AssetIn (${assetInName}) price is undefined`);
|
||||
if (assetInPrice === undefined) throw Error(`Price conversion: AssetIn (${assetInName}) price is undefined. Available prices: ${JSON.stringify(prices)}`);
|
||||
|
||||
const assetOutPrice = prices[assetOutName];
|
||||
if (assetOutPrice === undefined) throw Error(`Price conversion: AssetOut (${assetOutName}) price is undefined`);
|
||||
if (assetOutPrice === undefined) throw Error(`Price conversion: AssetOut (${assetOutName}) price is undefined. Available prices: ${JSON.stringify(prices)}`);
|
||||
|
||||
const assetInPriceBN = new BigNumber(assetInPrice);
|
||||
const assetOutPriceBN = new BigNumber(assetOutPrice);
|
||||
|
||||
@@ -16,6 +16,7 @@ export { default as getNativeCryptocurrencyName } from './getNativeCryptocurrenc
|
||||
export { default as laconicParse } from './laconic-parse.js';
|
||||
export { default as isValidChainId } from './isValidChainId.js';
|
||||
export { default as isKnownEnv } from './isKnownEnv.js';
|
||||
export { default as removeFieldsFromObject } from './removeFieldsFromObject.js';
|
||||
// export { default as HttpError } from './httpError';
|
||||
|
||||
export * from './typeHelpers.js';
|
||||
|
||||
16
src/utils/removeFieldsFromObject.ts
Normal file
16
src/utils/removeFieldsFromObject.ts
Normal file
@@ -0,0 +1,16 @@
|
||||
const removeFieldsFromObject = <
|
||||
T extends Record<string, unknown>,
|
||||
K extends keyof T
|
||||
>(
|
||||
obj: T,
|
||||
fields: K[]
|
||||
): Omit<T, K> => {
|
||||
const result = { ...obj };
|
||||
for (const field of fields) {
|
||||
// eslint-disable-next-line @typescript-eslint/no-dynamic-delete
|
||||
delete result[field];
|
||||
}
|
||||
return result;
|
||||
};
|
||||
|
||||
export default removeFieldsFromObject;
|
||||
Reference in New Issue
Block a user