Skip to content

Commit

Permalink
Improving and fixing convertors to calculate the balances and parse t…
Browse files Browse the repository at this point in the history
…he orders.
  • Loading branch information
danilo-silva-funttastic committed Oct 5, 2023
1 parent 9e6aff5 commit 204e45b
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 56 deletions.
155 changes: 99 additions & 56 deletions src/connectors/kujira/kujira.convertors.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
Amount,
Balances,
ConvertOrderType,
GetKujiraTokenSymbolsToCoinGeckoTokenIdsMapResponse,
Expand All @@ -12,6 +13,7 @@ import {
Order,
OrderAmount,
OrderBook,
OrderFilling,
OrderId,
OrderPrice,
OrderSide,
Expand Down Expand Up @@ -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<OrderStatus>(order.status)
)
) {
const filling = getNotNullOrThrowError<OrderFilling>(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) {
Expand Down Expand Up @@ -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<BigNumber>(quotations.get(token.id));
Expand Down Expand Up @@ -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<OrderStatus>(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<BigNumber>(
quotations.get(freeToken.id)
);

const quotation = getNotNullOrThrowError<BigNumber>(
quotations.get(token?.id)
const filledQuotation = getNotNullOrThrowError<BigNumber>(
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<OrderFilling>(order.filling);
if (order.status == OrderStatus.OPEN) {
freeAmount = BigNumber(order.amount);
} else if (order.status == OrderStatus.PARTIALLY_FILLED) {
freeAmount = getNotNullOrThrowError<BigNumber>(filling.free.amount);
filledAmount = getNotNullOrThrowError<BigNumber>(filling.filled.amount);
} else if (order.status == OrderStatus.FILLED) {
filledAmount = getNotNullOrThrowError<BigNumber>(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<TokenBalance>(
output.tokens.get(token?.id)
);

tokenBalance.inUSD.quotation = quotation;
const freeTokenBalance = getNotNullOrThrowError<TokenBalance>(
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<OrderStatus>(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<TokenBalance>(
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);
Expand Down
12 changes: 12 additions & 0 deletions src/connectors/kujira/kujira.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -283,6 +294,7 @@ export interface Order {
status?: OrderStatus;
type?: OrderType;
fee?: OrderFee;
filling?: OrderFilling;
creationTimestamp?: OrderCreationTimestamp;
fillingTimestamp?: OrderFillingTimestamp;
hashes?: OrderTransactionHashes;
Expand Down

0 comments on commit 204e45b

Please sign in to comment.