diff --git a/src/connectors/kujira/kujira.convertors.ts b/src/connectors/kujira/kujira.convertors.ts index 5c7355d14a..b14d1f7dbd 100644 --- a/src/connectors/kujira/kujira.convertors.ts +++ b/src/connectors/kujira/kujira.convertors.ts @@ -1,4 +1,5 @@ import { + Amount, Balances, ConvertOrderType, GetKujiraTokenSymbolsToCoinGeckoTokenIdsMapResponse, @@ -12,6 +13,7 @@ import { Order, OrderAmount, OrderBook, + OrderFilling, OrderId, OrderPrice, OrderSide, @@ -328,12 +330,47 @@ export const convertKujiraOrdersToMapOfOrders = (options: { status: convertKujiraOrderToStatus(bundle), type: OrderType.LIMIT, fee: undefined, + filling: { + free: { + token: undefined, + amount: undefined, + }, + filled: { + token: undefined, + amount: undefined, + }, + } as unknown as OrderFilling, fillingTimestamp: undefined, creationTimestamp: Number(bundle['created_at']), hashes: undefined, connectorOrder: bundle, } as Order; + if ( + [OrderStatus.PARTIALLY_FILLED, OrderStatus.FILLED].includes( + getNotNullOrThrowError(order.status) + ) + ) { + const filling = getNotNullOrThrowError(order.filling); + const freeToken = + order.side == OrderSide.BUY + ? order.market.quoteToken + : order.market.baseToken; + filling.free.token = freeToken; + filling.free.amount = BigNumber(order.connectorOrder.offer_amount).div( + BigNumber(10).pow(filling.free.token.decimals) + ); + + const filledToken = + order.side == OrderSide.BUY + ? order.market.baseToken + : order.market.quoteToken; + filling.filled.token = filledToken; + filling.filled.amount = BigNumber( + order.connectorOrder.filled_amount + ).div(BigNumber(10).pow(filling.filled.token.decimals)); + } + output.set(orderId, order); } } else if (ConvertOrderType.CANCELLED_ORDERS == options.type) { @@ -473,7 +510,7 @@ export const convertKujiraBalancesToBalances = async ( for (const balance of balances) { const token = convertKujiraTokenToToken(Denom.from(balance.denom)); - if (!token.symbol.startsWith('x') || token.symbol.startsWith('X')) { + if (!token.symbol.startsWith('x')) { let quotation = BigNumber(0); quotation = getNotNullOrThrowError(quotations.get(token.id)); @@ -507,72 +544,78 @@ export const convertKujiraBalancesToBalances = async ( } for (const order of orders.values()) { - let token: any = undefined; - let amount: any = undefined; - if ( - [OrderStatus.OPEN, OrderStatus.PARTIALLY_FILLED].includes( - getNotNullOrThrowError(order.status) - ) - ) { - token = - order.side == OrderSide.BUY - ? order.market.quoteToken - : order.market.baseToken; - - amount = BigNumber(order.connectorOrder.offer_amount).div( - BigNumber(10).pow(token?.decimals) - ); - } else if (order.status == OrderStatus.FILLED) { - token = - order.side == OrderSide.BUY - ? order.market.baseToken - : order.market.quoteToken; - - amount = BigNumber(order.connectorOrder.filled_amount).div( - BigNumber(10).pow(token?.decimals) - ); - } + const freeToken: Token = + order.side == OrderSide.BUY + ? order.market.quoteToken + : order.market.baseToken; + const filledToken: Token = + order.side == OrderSide.BUY + ? order.market.baseToken + : order.market.quoteToken; + + let freeAmount: Amount = BigNumber(0); + let filledAmount: Amount = BigNumber(0); + + const freeQuotation = getNotNullOrThrowError( + quotations.get(freeToken.id) + ); - const quotation = getNotNullOrThrowError( - quotations.get(token?.id) + const filledQuotation = getNotNullOrThrowError( + quotations.get(filledToken.id) ); - if (!output.tokens.has(token?.id)) { - output.tokens.set(token?.id, { - token: token, - free: BigNumber(0), - lockedInOrders: BigNumber(0), - unsettled: BigNumber(0), - total: BigNumber(0), - inUSD: { - quotation: BigNumber(0), + const filling = getNotNullOrThrowError(order.filling); + if (order.status == OrderStatus.OPEN) { + freeAmount = BigNumber(order.amount); + } else if (order.status == OrderStatus.PARTIALLY_FILLED) { + freeAmount = getNotNullOrThrowError(filling.free.amount); + filledAmount = getNotNullOrThrowError(filling.filled.amount); + } else if (order.status == OrderStatus.FILLED) { + filledAmount = getNotNullOrThrowError(filling.filled.amount); + } else { + throw Error('Unrecognized order status.'); + } + + for (const token of [freeToken, filledToken]) { + if (!output.tokens.has(token.id)) { + output.tokens.set(token.id, { + token: token, free: BigNumber(0), lockedInOrders: BigNumber(0), unsettled: BigNumber(0), total: BigNumber(0), - }, - }); + inUSD: { + quotation: BigNumber(0), + free: BigNumber(0), + lockedInOrders: BigNumber(0), + unsettled: BigNumber(0), + total: BigNumber(0), + }, + }); + } } - const tokenBalance = getNotNullOrThrowError( - output.tokens.get(token?.id) - ); - tokenBalance.inUSD.quotation = quotation; + const freeTokenBalance = getNotNullOrThrowError( + output.tokens.get(freeToken.id) + ); + freeTokenBalance.inUSD.quotation = freeQuotation; + freeTokenBalance.lockedInOrders = + freeTokenBalance.lockedInOrders.plus(freeAmount); + freeTokenBalance.inUSD.lockedInOrders = + freeTokenBalance.inUSD.lockedInOrders.plus( + freeAmount.multipliedBy(freeQuotation) + ); - if ( - [OrderStatus.OPEN, OrderStatus.PARTIALLY_FILLED].includes( - getNotNullOrThrowError(order.status) - ) - ) { - tokenBalance.lockedInOrders = tokenBalance.lockedInOrders.plus(amount); - tokenBalance.inUSD.lockedInOrders = - tokenBalance.inUSD.lockedInOrders.plus(amount?.multipliedBy(quotation)); - } else if (order.status == OrderStatus.FILLED) { - tokenBalance.unsettled = tokenBalance.unsettled.plus(amount); - tokenBalance.inUSD.unsettled = tokenBalance.inUSD.unsettled.plus( - amount?.multipliedBy(quotation) + const filledTokenBalance = getNotNullOrThrowError( + output.tokens.get(filledToken.id) + ); + filledTokenBalance.inUSD.quotation = filledQuotation; + filledTokenBalance.unsettled = + filledTokenBalance.unsettled.plus(filledAmount); + filledTokenBalance.inUSD.unsettled = + filledTokenBalance.inUSD.unsettled.plus( + filledAmount.multipliedBy(filledQuotation) ); - } } let allFreeBalancesSum = BigNumber(0); diff --git a/src/connectors/kujira/kujira.types.ts b/src/connectors/kujira/kujira.types.ts index 4edf82b8fe..fe0e48c216 100644 --- a/src/connectors/kujira/kujira.types.ts +++ b/src/connectors/kujira/kujira.types.ts @@ -51,6 +51,7 @@ export type PayerAddress = Address; export type Price = BigNumber; export type Amount = BigNumber; export type Fee = BigNumber; +export type Percentage = BigNumber; export type Timestamp = number; export type Block = number; export type EncryptedWallet = string; @@ -187,6 +188,16 @@ export interface KujiraTicker { price: Price; } +export interface TokenAmount { + token: Token; + amount: Amount; +} + +export interface OrderFilling { + free: TokenAmount; + filled: TokenAmount; +} + export interface TokenPriceInDolar { token: TokenName; price: Price; @@ -283,6 +294,7 @@ export interface Order { status?: OrderStatus; type?: OrderType; fee?: OrderFee; + filling?: OrderFilling; creationTimestamp?: OrderCreationTimestamp; fillingTimestamp?: OrderFillingTimestamp; hashes?: OrderTransactionHashes;