Skip to content

Commit

Permalink
Fixing problem regarding quotations not being found. Updating Kujira …
Browse files Browse the repository at this point in the history
…and CosmJS libraries.
  • Loading branch information
danilo-silva-funttastic committed Apr 13, 2024
1 parent a5b28d7 commit deb077a
Show file tree
Hide file tree
Showing 4 changed files with 318 additions and 369 deletions.
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
"test:scripts": "jest -i --verbose ./test-scripts/*.test.ts"
},
"dependencies": {
"@cosmjs/proto-signing": "^0.31.1",
"@cosmjs/stargate": "^0.31.1",
"@cosmjs/proto-signing": "^0.32.3",
"@cosmjs/stargate": "^0.32.3",
"@crocswap/sdk": "^2.4.5",
"@ethersproject/abstract-provider": "5.7.0",
"@ethersproject/address": "5.7.0",
Expand Down Expand Up @@ -84,7 +84,7 @@
"http-status-codes": "2.2.0",
"immutable": "^4.2.4",
"js-yaml": "^4.1.0",
"kujira.js": "0.9.142",
"kujira.js": "1.0.22",
"level": "^8.0.0",
"lodash": "^4.17.21",
"lru-cache": "^7.14.1",
Expand Down
27 changes: 21 additions & 6 deletions src/connectors/kujira/kujira.convertors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,21 @@ export const convertCoinGeckoQuotationsToQuotations = (
return output;
};

export const extractQuotation = (quotations: IMap<TokenId, Price>, token: Token) => {
let quotation = BigNumber(0);

try {
quotation = getNotNullOrThrowError<BigNumber>(quotations.get(token.id));
} catch (exception) {
// TODO investigate better!!!
if (token.id.includes('usk')) {
quotation = BigNumber(1);
}
}

return quotation;
}

export const convertKujiraBalancesToBalances = async (
balances: readonly Coin[],
orders: IMap<OrderId, Order>,
Expand All @@ -514,7 +529,7 @@ export const convertKujiraBalancesToBalances = async (
if (!token.symbol.startsWith('x')) {
let quotation = BigNumber(0);

quotation = getNotNullOrThrowError<BigNumber>(quotations.get(token.id));
quotation = getNotNullOrThrowError<BigNumber>(extractQuotation(quotations, token));

const freeAmount = BigNumber(balance.amount).div(
BigNumber(10).pow(token.decimals)
Expand Down Expand Up @@ -558,11 +573,11 @@ export const convertKujiraBalancesToBalances = async (
let filledAmount: Amount = BigNumber(0);

const freeQuotation = getNotNullOrThrowError<BigNumber>(
quotations.get(freeToken.id)
extractQuotation(quotations, freeToken)
);

const filledQuotation = getNotNullOrThrowError<BigNumber>(
quotations.get(filledToken.id)
extractQuotation(quotations, filledToken)
);

const filling = getNotNullOrThrowError<OrderFilling>(order.filling);
Expand Down Expand Up @@ -662,8 +677,8 @@ export const convertKujiraTransactionToTransaction = (
return {
hash: input.hash,
blockNumber: input.height,
gasUsed: input.gasUsed,
gasWanted: input.gasWanted,
gasUsed: Number(input.gasUsed),
gasWanted: Number(input.gasWanted),
code: input.code,
data: new TextDecoder('utf-8').decode(input.tx),
};
Expand Down Expand Up @@ -746,7 +761,7 @@ export const convertKujiraSettlementToSettlement = (

if (amount.gt(BigNumber(0))) {
const quotation = getNotNullOrThrowError<BigNumber>(
quotations.get(token.id)
extractQuotation(quotations, token)
);

const amountInUSD = amount.multipliedBy(quotation);
Expand Down
3 changes: 2 additions & 1 deletion src/connectors/kujira/kujira.helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ export const getNotNullOrThrowError = <R>(
value?: any,
errorMessage: string = 'Value is null or undefined'
): R => {
if (value === undefined || value === null) throw new Error(errorMessage);
if (value === undefined || value === null)
throw new Error(errorMessage);

return value as R;
};
Expand Down
Loading

0 comments on commit deb077a

Please sign in to comment.