Skip to content

Commit

Permalink
add getOraclePrice & generateMatchOrders func
Browse files Browse the repository at this point in the history
  • Loading branch information
lehieuhust committed Aug 21, 2023
1 parent 1b60e3a commit c0d9b46
Show file tree
Hide file tree
Showing 3 changed files with 221 additions and 95 deletions.
16 changes: 15 additions & 1 deletion packages/market-maker/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
deployOrderbook,
deployToken,
getCoingeckoPrice,
getOraclePrice,
makeOrders,
setupWallet,
toDecimals
Expand All @@ -24,6 +25,12 @@ const [spreadMin, spreadMax] = process.env.SPREAD_RANGE
? process.env.SPREAD_RANGE.split(",").map(Number)
: [0.001, 0.004];

const oraiThreshold = Number(process.env.ORAI_THRESHOLD);
const usdtThreshold = Number(process.env.USDT_THRESHOLD);

const spreadMatch = Number(process.env.SPREAD_MATCH);
const spreadCancel = Number(process.env.SPREAD_CANCEL);

const maxRepeat = 5;
const totalOrders = 5;

Expand All @@ -34,6 +41,10 @@ const orderConfig: MakeOrderConfig = {
spreadMin,
sellDepth,
buyDepth,
oraiThreshold,
usdtThreshold,
spreadMatch,
spreadCancel,
totalOrders
};
const [orderIntervalMin, orderIntervalMax] = process.env.ORDER_INTERVAL_RANGE
Expand Down Expand Up @@ -73,7 +84,10 @@ const [orderIntervalMin, orderIntervalMax] = process.env.ORDER_INTERVAL_RANGE
console.log("buyer address: ", buyer.address, "seller address: ", seller.address);

// get price from coingecko
const oraiPrice = await getCoingeckoPrice("oraichain-token");
const oraiCoinGeckoPrice = await getCoingeckoPrice("oraichain-token");
const oraiOraclePrice = await getOraclePrice("orai");

const oraiPrice = (oraiCoinGeckoPrice + oraiOraclePrice)/2;

let processInd = 0;
while (processInd < maxRepeat) {
Expand Down
76 changes: 43 additions & 33 deletions packages/market-maker/src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,13 @@ export const getCoingeckoPrice = async (token: "oraichain-token" | "airight"): P
return res[token].usd;
};

export const getOraclePrice = async (token: string): Promise<number> => {
const res = await fetch(`https://api.orchai.io/lending/mainnet/token/${token}`).then((res) =>
res.json()
);
return res.current_price;
};

const truncDecimals = 6;
export const atomic = 10 ** truncDecimals;

Expand Down Expand Up @@ -158,43 +165,46 @@ export const cancelOutofSpreadOrder = async (
sender: UserWallet,
assetInfos: AssetInfo[],
direction: string,
spread_percentage: number,
oraiPrice: number,
limit: number,
spread_percentage: number,
) => {
const upperPriceLimit = oraiPrice * (1 + spread_percentage);
const lowerPriceLimit = oraiPrice * (1 - spread_percentage);
console.log({upperPriceLimit});
console.log({lowerPriceLimit});

let queryTicks = await sender.client.queryContractSmart(orderbookAddress, {
ticks: {
asset_infos: assetInfos,
order_by: direction === "buy" ? 2 : 1,
direction,
limit,
limit: 100,
}
} as OraiswapLimitOrderTypes.QueryMsg);

for (const ticks of Object.values(queryTicks)) {
for (const tick of ticks as any[]) {
let tick_price = Number(tick.price);
if (tick_price >= upperPriceLimit || tick_price <= lowerPriceLimit) {
console.log("cancel all orders with price", tick_price);
console.log({sender_addr: sender.address});
const queryorderswithPrice = await sender.client.queryContractSmart(orderbookAddress, {
orders: {
asset_infos: assetInfos,
order_by: 1,
limit,
filter: {
price: tick_price.toString()
}
interface tick {
price: string
total_orders: number
}

const multipleCancelMsg: ExecuteInstruction[] = [];

queryTicks.ticks.forEach(async (tick: tick) => {
let tick_price = parseFloat(tick.price);
console.log({tick_price});
if (tick_price > upperPriceLimit || tick_price < lowerPriceLimit) {
console.log("cancel all orders with price", tick_price);
const ordersbyPrice = await sender.client.queryContractSmart(orderbookAddress, {
orders: {
asset_infos: assetInfos,
order_by: 1,
limit: tick.total_orders,
filter: {
price: tick.price
}
} as OraiswapLimitOrderTypes.QueryMsg);
console.log({queryorderswithPrice: queryorderswithPrice});
}
} as OraiswapLimitOrderTypes.QueryMsg);

const multipleCancelMsg: ExecuteInstruction[] = [];
for (const order of queryorderswithPrice.orders) {
for (const order of ordersbyPrice.orders) {
if (order.bidder_addr === sender.address) {
const cancelMsg: ExecuteInstruction = {
contractAddress: orderbookAddress,
msg: {
Expand All @@ -204,19 +214,19 @@ export const cancelOutofSpreadOrder = async (
}
}
};
console.log({bidder_addr: order.bidder_addr});
// console.log({sender_addr: sender.address});
if (order.bidder_addr === sender.address) {
multipleCancelMsg.push(cancelMsg);
}
}
if (multipleCancelMsg.length > 0) {
const cancelResult = await sender.client.executeMultiple(sender.address, multipleCancelMsg, "auto");
console.log("cancel orders - txHash:", cancelResult.transactionHash);
multipleCancelMsg.push(cancelMsg);
}
}
}
}
if (multipleCancelMsg.length > 0) {
try {
const cancelResult = await sender.client.executeMultiple(sender.address, multipleCancelMsg, "auto");
console.log("spread cancel orders - txHash:", cancelResult.transactionHash);
} catch (error) {
console.log({error});
}
}
})
};

export const cancelOrder = async (
Expand Down
Loading

0 comments on commit c0d9b46

Please sign in to comment.