implement market order for perps

Signed-off-by: microwavedcola1 <microwavedcola@gmail.com>
This commit is contained in:
microwavedcola1 2021-12-06 20:54:14 +01:00
parent 230416ee77
commit 62e7da44cf
3 changed files with 22 additions and 11 deletions

View File

@ -374,18 +374,12 @@ class MangoSimpleClient {
public async placeOrder(
market: string,
type: "market" | "limit",
side: "buy" | "sell",
quantity: number,
price?: number,
orderType: "ioc" | "postOnly" | "limit" = "limit",
orderType: "ioc" | "postOnly" | "market" | "limit" = "limit",
clientOrderId?: number
): Promise<void> {
if (type === "market") {
// todo
throw new Error("Not implemented!");
}
if (market.includes("PERP")) {
const perpMarketConfig = getMarketByBaseSymbolAndKind(
this.mangoGroupConfig,
@ -398,6 +392,9 @@ class MangoSimpleClient {
perpMarketConfig.baseDecimals,
perpMarketConfig.quoteDecimals
);
// TODO: this is a workaround, mango-v3 has a assertion for price>0 for all order types
// this will be removed soon hopefully
price = orderType !== "market" ? price : 1;
await this.client.placePerpOrder(
this.mangoGroup,
this.mangoAccount,
@ -411,6 +408,11 @@ class MangoSimpleClient {
clientOrderId
);
} else {
if (orderType === "market") {
// todo
throw new Error("Not implemented!");
}
const spotMarketConfig = getMarketByBaseSymbolAndKind(
this.mangoGroupConfig,
market.split("/")[0],

View File

@ -35,7 +35,6 @@ class OrdersController implements Controller {
body("market").not().isEmpty().custom(isValidMarket),
body("side").not().isEmpty().isIn(["sell", "buy"]),
body("type").not().isEmpty().isIn(["limit", "market"]),
body("price").isNumeric(),
body("size").not().isEmpty().isNumeric(),
body("reduceOnly").isBoolean(),
body("ioc").isBoolean(),
@ -101,16 +100,26 @@ class OrdersController implements Controller {
}
const placeOrderDto = request.body as PlaceOrderDto;
if (placeOrderDto.type !== "market" && placeOrderDto.price === undefined) {
logger.error("here");
return response.status(400).send({
errors: [{ msg: "missing price" } as RequestErrorCustom],
});
}
logger.info(`placing order`);
this.mangoSimpleClient
.placeOrder(
patchExternalMarketName(placeOrderDto.market),
placeOrderDto.type,
placeOrderDto.side,
placeOrderDto.size,
placeOrderDto.price,
placeOrderDto.ioc
// preference - market, then ioc, then postOnly, otherwise default i.e. limit
placeOrderDto.type == "market"
? "market"
: placeOrderDto.ioc
? "ioc"
: placeOrderDto.postOnly
? "postOnly"

View File

@ -24,7 +24,7 @@ export async function transactionSize(
export const i80f48ToPercent = (value: I80F48) =>
value.mul(I80F48.fromNumber(100));
const groupName = process.env.GROUP || "devnet.1";
const groupName = process.env.GROUP || "mainnet.1";
const mangoGroupConfig: GroupConfig = Config.ids().groups.filter(
(group) => group.name === groupName
)[0];