Skip to content

Commit

Permalink
Merge branch 'kjr-development' into fix/withdraw-fee-sum
Browse files Browse the repository at this point in the history
  • Loading branch information
danilo-silva-funttastic committed Oct 18, 2023
2 parents 1e0edc2 + d8290ad commit 6e32c9b
Show file tree
Hide file tree
Showing 6 changed files with 439 additions and 246 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.30.1",
"@cosmjs/stargate": "^0.30.1",
"@cosmjs/proto-signing": "^0.31.1",
"@cosmjs/stargate": "^0.31.1",
"@crocswap/sdk": "^2.4.5",
"@ethersproject/abstract-provider": "5.7.0",
"@ethersproject/address": "5.7.0",
Expand Down Expand Up @@ -79,7 +79,7 @@
"http-status-codes": "2.2.0",
"immutable": "^4.2.4",
"js-yaml": "^4.1.0",
"kujira.js": "https://github.com/Team-Kujira/kujira.js",
"kujira.js": "0.9.6",
"level": "^8.0.0",
"libsodium-wrappers": "^0.7.11",
"libsodium-wrappers-sumo": "^0.7.11",
Expand Down
130 changes: 97 additions & 33 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,51 +544,78 @@ export const convertKujiraBalancesToBalances = async (
}

for (const order of orders.values()) {
const token =
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;

const quotation = getNotNullOrThrowError<BigNumber>(
quotations.get(token.id)
let freeAmount: Amount = BigNumber(0);
let filledAmount: Amount = BigNumber(0);

const freeQuotation = getNotNullOrThrowError<BigNumber>(
quotations.get(freeToken.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 filledQuotation = getNotNullOrThrowError<BigNumber>(
quotations.get(filledToken.id)
);

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)
);

const amount = order.amount;

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 = 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
16 changes: 8 additions & 8 deletions src/connectors/kujira/kujira.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ import {
DirectSecp256k1HdWallet,
EncodeObject,
} from '@cosmjs/proto-signing';
import { HttpBatchClient, Tendermint34Client } from '@cosmjs/tendermint-rpc';
import { HttpBatchClient, Tendermint37Client } from '@cosmjs/tendermint-rpc';
import { StdFee } from '@cosmjs/amino';
import { IndexedTx } from '@cosmjs/stargate/build/stargateclient';
import { BigNumber } from 'bignumber.js';
Expand Down Expand Up @@ -226,7 +226,7 @@ export class Kujira {
*/
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
private tendermint34Client: Tendermint34Client;
private tendermint37Client: Tendermint37Client;

/**
*
Expand Down Expand Up @@ -345,7 +345,7 @@ export class Kujira {

this.kujiraGetHttpBatchClient(rpcEndpoint);

await this.kujiraGetTendermint34Client();
await this.kujiraGetTendermint37Client();

this.kujiraGetKujiraQueryClient();

Expand All @@ -364,12 +364,12 @@ export class Kujira {

private kujiraGetKujiraQueryClient() {
this.kujiraQueryClient = kujiraQueryClient({
client: this.tendermint34Client,
client: this.tendermint37Client,
});
}

private async kujiraGetTendermint34Client() {
this.tendermint34Client = await Tendermint34Client.create(
private async kujiraGetTendermint37Client() {
this.tendermint37Client = await Tendermint37Client.create(
this.httpBatchClient
);
}
Expand Down Expand Up @@ -2106,8 +2106,8 @@ export class Kujira {
return JSON.parse(decryptedString);
}

async toClient(endpoint: string): Promise<[Tendermint34Client, string]> {
const client = await Tendermint34Client.create(
async toClient(endpoint: string): Promise<[Tendermint37Client, string]> {
const client = await Tendermint37Client.create(
new HttpBatchClient(endpoint, {
dispatchInterval: 100,
batchSizeLimit: 200,
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
2 changes: 1 addition & 1 deletion src/templates/kujira.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ markets:
tickers:
sources:
coinGecko:
url: "https://api.coingecko.com/api/v3/simple/price?ids={targets}&vs_currencies=usd"
url: "https://api.coingecko.com/api/v3/simple/price?vs_currencies=usd&x_cg_demo_api_key=<API_KEY>&ids={targets}"
# orderBookSimpleAveragePrice:
# orderBookWeightedAveragePrice:
# orderBookVolumeWeightedAveragePrice:
Expand Down
Loading

0 comments on commit 6e32c9b

Please sign in to comment.