Skip to content

Commit

Permalink
FIX: queryPrice hided server errors
Browse files Browse the repository at this point in the history
FIX: `queryOrders` does not return `traderId` and `finishTime`
(SDK-4260)
  • Loading branch information
melsomino committed Dec 29, 2022
1 parent a179d12 commit 91c2930
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 35 deletions.
33 changes: 31 additions & 2 deletions dist/flex/trader/query.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

48 changes: 22 additions & 26 deletions flex/market.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,54 +3,54 @@ import { Token, TokenInfo } from "./token";

export type MarketInfo = {
/// Flex Pair account address
address: string
address: string;

/// Abbreviation used to identify pair.
/// Derived from major and minor root tickers, i.e. 'EVER/SOL'
ticker: string
ticker: string;

/// Major token
major: TokenInfo
major: TokenInfo;

/// Minor token
minor: TokenInfo
minor: TokenInfo;

/// Minimum amount of major token required for an order creation
/// in token units.
minAmount: number
minAmount: number;

/// Price tick size numerator
minMove: string
minMove: string;

/// Token price denominator
priceScale: string
priceScale: string;

/// Code hash of price contracts for the pair
priceCodeHash: string
priceCodeHash: string;

/// Price contracts code BOC encoded with base64
priceCode: string
priceCode: string;

/// Notification contract address
notifyAddress: String
}
notifyAddress: String;
};

export type OrderBookInfo = {
/// Buys
bids: OrderBookItem[],
bids: OrderBookItem[];

/// Sells
asks: OrderBookItem[],
}
asks: OrderBookItem[];
};

/// Market price summary for time range
export type OrderBookItem = {
/// Major token price
price: number
price: number;

/// Amount of major tokens
amount: number
}
amount: number;
};

export class Market {
static async queryOrderBook(flex: Flex, market: string): Promise<OrderBookInfo> {
Expand All @@ -72,16 +72,12 @@ export class Market {
}

static async queryPrice(flex: Flex, market: string): Promise<number | null> {
try {
const result = await flex.query(`
market(pairAddress: "${market}") {
price
}
`);
return result.market.price;
} catch {
return null;
const result = await flex.query(`
market(pairAddress: "${market}") {
price
}
`);
return result.market.price;
}

static async queryMarkets(flex: Flex): Promise<MarketInfo[]> {
Expand Down
44 changes: 37 additions & 7 deletions flex/trader/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,21 @@ import { Token } from "../token";
import { WalletInfo, walletInfoFromApi } from "../client";
import { OrderInfo, TradeInfo } from "./types";

function orderInfoInfoFromApi(result: any): OrderInfo {
return {
orderId: result.orderId,
traderId: result.userId,
price: result.price,
priceNum: result.priceNum,
priceScale: result.priceScale,
amountProcessed: result.amountProcessed,
amountLeft: result.amountLeft,
side: result.side,
finishTime: result.finishTime,
pair: result.pair,
};
}

/** @internal */
export async function queryOrders(flex: Flex, trader: string): Promise<OrderInfo[]> {
const result = await flex.query(`
Expand All @@ -17,9 +32,25 @@ export async function queryOrders(flex: Flex, trader: string): Promise<OrderInfo
userId
amountProcessed
amountLeft
finishTime
}
`);
return result.userOrders;
return result.userOrders.map(orderInfoInfoFromApi);
}

function tradeFromApi(result: any): TradeInfo {
return {
pair: result.pair,
price: result.price,
amount: result.amount,
time: result.time,
side: result.side,
liquidity: result.liquidity,
fees: result.fees,
feesToken: result.feesToken,
userOrderId: result.userOrderId,
cursor: result.cursor,
};
}

/** @internal */
Expand All @@ -38,21 +69,20 @@ export async function queryTrades(flex: Flex, trader: string): Promise<TradeInfo
cursor
}
`);
return result.userTrades;
return result.userTrades.map(tradeFromApi);
}

export type QueryWalletsOptions = {

clientAddress: string,
clientAddress: string;
/**
* Trader ID as uint256 hex string
*/
traderId?: string,
traderId?: string;
/**
* Token DEX Wrapper address
*/
token?: string,
}
token?: string;
};

/** @internal */
export async function queryWallets(
Expand Down

0 comments on commit 91c2930

Please sign in to comment.