Skip to content

Commit

Permalink
Improving balances response.
Browse files Browse the repository at this point in the history
  • Loading branch information
darley-silva-funttastic committed Sep 8, 2023
1 parent 1c0809c commit 3455980
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 152 deletions.
5 changes: 5 additions & 0 deletions src/connectors/kujira/kujira.convertors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,7 @@ export const convertKujiraBalancesToBalances = async (
unsettled: BigNumber(0),
total: BigNumber(0),
inUSD: {
quotation: BigNumber(0),
free: BigNumber(0),
lockedInOrders: BigNumber(0),
unsettled: BigNumber(0),
Expand All @@ -500,6 +501,7 @@ export const convertKujiraBalancesToBalances = async (
output.tokens.get(token.id)
);
tokenBalance.free = freeAmount;
tokenBalance.inUSD.quotation = quotation;
tokenBalance.inUSD.free = freeAmount.multipliedBy(quotation);
}
}
Expand All @@ -522,6 +524,7 @@ export const convertKujiraBalancesToBalances = async (
unsettled: BigNumber(0),
total: BigNumber(0),
inUSD: {
quotation: BigNumber(0),
free: BigNumber(0),
lockedInOrders: BigNumber(0),
unsettled: BigNumber(0),
Expand All @@ -535,6 +538,8 @@ export const convertKujiraBalancesToBalances = async (

const amount = order.amount;

tokenBalance.inUSD.quotation = quotation;

if ([OrderStatus.OPEN, OrderStatus.PARTIALLY_FILLED].includes(
getNotNullOrThrowError<OrderStatus>(order.status)
)) {
Expand Down
151 changes: 0 additions & 151 deletions src/connectors/kujira/kujira.helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,6 @@ import { KujiraConfig } from './kujira.config';
import { Kujira } from './kujira';
import { parse as flattedParse, stringify as flattedStringify } from 'flatted';
import { promisify } from 'util';
import {
GetAllTickersResponse,
IMap,
MarketId,
MarketName,
Token,
Ticker,
Price,
TokenId,
TokenName,
TokenPriceInDolar,
} from './kujira.types';
import { BigNumber } from 'bignumber.js';
import { axlUSDC } from 'kujira.js';
import { convertKujiraTokenToToken } from './kujira.convertors';

/**
*
Expand Down Expand Up @@ -218,139 +203,3 @@ export namespace Serializer {
return deserialize(deserializedString) as T;
}
}

export const quoteABaseTokenInDolars = async (
chain: string,
network: string,
targetToken: TokenName,
allTickers?: GetAllTickersResponse
): Promise<TokenPriceInDolar> => {
if (targetToken.toUpperCase() == 'USDC') {
return {
token: targetToken,
price: BigNumber(1),
} as TokenPriceInDolar;
}

if (!allTickers) {
const connector = getNotNullOrThrowError<Kujira>(
Kujira.getInstance(chain, network)
);

allTickers = await connector.getAllTickers({});
}

const usdcAsQuote: IMap<MarketName, Ticker> = getNotNullOrThrowError<
IMap<MarketId, Ticker>
>(
allTickers
?.valueSeq()
.toArray()
.filter(
(ticker) => ticker.market.quoteToken.symbol.toUpperCase() == 'USDC'
)
);

const priceSource = usdcAsQuote.filter(
(ticker) =>
ticker.market.baseToken.symbol.toUpperCase() == targetToken.toUpperCase()
);

const targetTokenAsQuote: IMap<MarketName, Ticker> =
getNotNullOrThrowError<GetAllTickersResponse>(
allTickers
?.valueSeq()
.toArray()
.filter(
(ticker) =>
ticker.market.quoteToken.symbol.toUpperCase() ==
targetToken.toUpperCase()
)
);

if (priceSource.size > 0) {
return {
token: getNotNullOrThrowError<Ticker>(priceSource.valueSeq().first())
.market.name,
price: getNotNullOrThrowError<Ticker>(priceSource.valueSeq().first())
.price,
} as TokenPriceInDolar;
} else {
// const pricesIntersection = usdcAsQuote.filter(
// ticker => ticker.market.baseToken.symbol.toUpperCase() == (
// getNotNullOrThrowError<Ticker>(
// tokenAsQuote.filter(
// value => value.market.baseToken.symbol.toUpperCase() == ticker.market.baseToken.symbol.toUpperCase()
// && value.market.quoteToken.symbol.toUpperCase() == targetToken.toUpperCase()
// )
// )
// )
// ) as IMap<MarketId, Ticker>;

const reference = {} as TokenPriceInDolar;

for (const aTicker of usdcAsQuote.valueSeq().toArray()) {
for (const bTicker of targetTokenAsQuote.valueSeq().toArray()) {
if (
aTicker.market.baseToken.symbol == bTicker.market.baseToken.symbol &&
bTicker.market.quoteToken.symbol.toUpperCase() ==
targetToken.toUpperCase()
) {
reference.token = aTicker.market.baseToken.symbol;
reference.price = aTicker.price;
}
}
}

return reference;
}
};

export const getQuotationInUSD = (
token: Token,
tickers: IMap<TokenId, Ticker>
): { price: Price } => {
const quoteToken = convertKujiraTokenToToken(axlUSDC);

const tickerValues = tickers
.valueSeq()
.filter(
(ticker) =>
ticker.market.baseToken.id == token.id &&
ticker.market.quoteToken.id == quoteToken.id
)
.first();

const ticker =
tickerValues != undefined
? tickerValues
: tickers
.valueSeq()
.filter(
(ticker) =>
ticker.market.quoteToken.id == token.id &&
ticker.market.baseToken.id == quoteToken.id
)
.first();

let buySide = false;

if (
getNotNullOrThrowError(
ticker?.market.quoteToken.symbol != quoteToken.symbol
)
) {
buySide = true;
}
let price =
token.id == quoteToken.id ? BigNumber(1) : ticker?.price || BigNumber(0);

if (buySide) {
const difference = BigNumber(1).minus(price);
price = BigNumber(1).plus(difference);
}

return {
price: price,
};
};
6 changes: 5 additions & 1 deletion src/connectors/kujira/kujira.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,12 +252,16 @@ export interface SimplifiedBalance {
total: Amount;
}

export interface SimplifiedBalanceWithUSD extends SimplifiedBalance {
quotation: Amount;
}

// eslint-disable-next-line @typescript-eslint/no-empty-interface
export interface TotalBalance extends SimplifiedBalance {}

export interface TokenBalance extends SimplifiedBalance {
token: Token;
inUSD: SimplifiedBalance;
inUSD: SimplifiedBalanceWithUSD;
}

export interface Balances {
Expand Down

0 comments on commit 3455980

Please sign in to comment.