-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #101 from White-Whale-Defi-Platform/feat/orderbook
Feat/orderbook
- Loading branch information
Showing
28 changed files
with
1,029 additions
and
288 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
export { getFlashArbMessages } from "./messages/getFlashArbMessages"; | ||
export { messageFactory } from "./messages/messageFactory"; | ||
export { getPoolStates } from "./queries/getPoolState"; | ||
export { initPools } from "./queries/getPoolState"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { toUtf8 } from "@cosmjs/encoding"; | ||
import { EncodeObject } from "@cosmjs/proto-signing"; | ||
import { MsgExecuteContract } from "cosmjs-types/cosmwasm/wasm/v1/tx"; | ||
|
||
import { Asset, isNativeAsset } from "../../../core/types/base/asset"; | ||
import { Pool } from "../../../core/types/base/pool"; | ||
|
||
/** | ||
* | ||
*/ | ||
export function getSwapMessage( | ||
pool: Pool, | ||
offerAsset: Asset, | ||
walletAddress: string, | ||
beliefPrice: string, | ||
maxSpread = 0.005, | ||
) { | ||
const msg = { | ||
swap: { | ||
max_spread: String(maxSpread), | ||
offer_asset: offerAsset, | ||
// belief_price: beliefPrice, | ||
}, | ||
}; | ||
const encodedMsgObject: EncodeObject = { | ||
typeUrl: "/cosmwasm.wasm.v1.MsgExecuteContract", | ||
value: MsgExecuteContract.fromJSON({ | ||
funds: [ | ||
{ | ||
denom: isNativeAsset(offerAsset.info) | ||
? offerAsset.info.native_token.denom | ||
: offerAsset.info.token.contract_addr, | ||
amount: offerAsset.amount, | ||
}, | ||
], | ||
sender: walletAddress, | ||
contract: pool.address, | ||
msg: toUtf8(JSON.stringify(msg)), | ||
}), | ||
}; | ||
return encodedMsgObject; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { EncodeObject } from "@cosmjs/proto-signing"; | ||
|
||
import { OptimalTrade } from "../../../core/arbitrage/arbitrage"; | ||
import { OptimalOrderbookTrade } from "../../../core/arbitrage/optimizers/orderbookOptimizer"; | ||
import { getOrderbookArbMessages } from "../../inj/messages/getOrderbookArbMessage"; | ||
import { getFlashArbMessages } from "./getFlashArbMessages"; | ||
/** | ||
* | ||
*/ | ||
export function messageFactory( | ||
arbTrade: OptimalTrade | OptimalOrderbookTrade, | ||
publicAddress: string, | ||
flashloancontract?: string, | ||
): [Array<EncodeObject>, number] | undefined { | ||
if (arbTrade.path["orderbook" as keyof typeof arbTrade.path] !== undefined) { | ||
return getOrderbookArbMessages(<OptimalOrderbookTrade>arbTrade, publicAddress); | ||
} else if (flashloancontract !== undefined) { | ||
return getFlashArbMessages(<OptimalTrade>arbTrade, publicAddress, flashloancontract); | ||
} else { | ||
return undefined; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
//export all known setups for chains | ||
export * as defaults from "./defaults"; | ||
export * as injective from "./inj"; | ||
export * as juno from "./juno"; | ||
export * as terra from "./terra"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
export * from "../defaults"; | ||
export { getOrderbookState } from "./queries/getOrderbookState"; | ||
export { initOrderbooks } from "./queries/initOrderbook"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { EncodeObject } from "@cosmjs/proto-signing"; | ||
|
||
import { OptimalOrderbookTrade } from "../../../core/arbitrage/optimizers/orderbookOptimizer"; | ||
import { toChainAsset, toChainPrice } from "../../../core/types/base/asset"; | ||
import { OrderSequence } from "../../../core/types/base/path"; | ||
import { outGivenIn } from "../../../core/types/base/pool"; | ||
import { getSwapMessage } from "../../defaults/messages/getSwapMessage"; | ||
import { getMarketSpotOrderMessage } from "./getSpotOrderMessage"; | ||
|
||
/** | ||
* | ||
*/ | ||
export function getOrderbookArbMessages( | ||
arbTrade: OptimalOrderbookTrade, | ||
publicAddress: string, | ||
): [Array<EncodeObject>, number] { | ||
if (arbTrade.path.orderSequence === OrderSequence.AmmFirst) { | ||
//buy on the amm, transfer to trading account, sell the inj there, withdraw the usdt to injective account | ||
const [outGivenIn0, outInfo0] = outGivenIn(arbTrade.path.pool, arbTrade.offerAsset); | ||
|
||
const price = toChainPrice(arbTrade.offerAsset, { amount: String(outGivenIn0), info: outInfo0 }); | ||
const offerAsset = toChainAsset(arbTrade.offerAsset); | ||
const msg0 = getSwapMessage(arbTrade.path.pool, offerAsset, publicAddress, price); | ||
|
||
const offerAsset1 = { | ||
amount: String(outGivenIn0), | ||
info: outInfo0, | ||
}; | ||
|
||
const msg1 = getMarketSpotOrderMessage(arbTrade, publicAddress, offerAsset1, 2); | ||
|
||
return [[msg0, msg1], 2]; | ||
} else { | ||
const offerAsset1 = { | ||
amount: String(arbTrade.outGivenIn), | ||
info: arbTrade.path.orderbook.baseAssetInfo, | ||
}; | ||
const msg0 = getMarketSpotOrderMessage(arbTrade, publicAddress, offerAsset1, 1); | ||
|
||
const [outGivenIn1, outInfo1] = outGivenIn(arbTrade.path.pool, { | ||
amount: String(arbTrade.outGivenIn), | ||
info: offerAsset1.info, | ||
}); | ||
|
||
const belief_price = toChainPrice( | ||
{ | ||
amount: String(arbTrade.outGivenIn), | ||
info: offerAsset1.info, | ||
}, | ||
{ amount: String(outGivenIn1), info: outInfo1 }, | ||
); | ||
const offerAsset = toChainAsset({ | ||
amount: String(arbTrade.outGivenIn), | ||
info: offerAsset1.info, | ||
}); | ||
|
||
const msg1 = getSwapMessage(arbTrade.path.pool, offerAsset, publicAddress, belief_price); | ||
|
||
return [[msg0, msg1], 2]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { MsgCreateSpotMarketOrder, spotPriceToChainPriceToFixed } from "@injectivelabs/sdk-ts"; | ||
import { BigNumberInBase } from "@injectivelabs/utils/dist/cjs/classes"; | ||
|
||
import { OptimalOrderbookTrade } from "../../../core/arbitrage/optimizers/orderbookOptimizer"; | ||
import { Asset, isMatchingAssetInfos } from "../../../core/types/base/asset"; | ||
import { SpotMarketOrderMessage } from "../../../core/types/messages/spotorders"; | ||
|
||
/** | ||
*'/injective.exchange.v1beta1.MsgCreateSpotMarketOrder'. | ||
*/ | ||
export function getMarketSpotOrderMessage( | ||
arbTrade: OptimalOrderbookTrade, | ||
injectiveAddress: string, | ||
offerAsset: Asset, | ||
orderType: 1 | 2, | ||
) { | ||
let decimals = 6; | ||
if (isMatchingAssetInfos(offerAsset.info, arbTrade.path.orderbook.baseAssetInfo)) { | ||
decimals = arbTrade.path.orderbook.baseAssetDecimals - arbTrade.path.orderbook.quoteAssetDecimals; | ||
} else { | ||
decimals = arbTrade.path.orderbook.quoteAssetDecimals - arbTrade.path.orderbook.quoteAssetDecimals; | ||
} | ||
|
||
const beliefPriceOrderbook = spotPriceToChainPriceToFixed({ | ||
value: Math.round(arbTrade.worstPrice * 1000) / 1000, | ||
baseDecimals: arbTrade.path.orderbook.baseAssetDecimals, | ||
quoteDecimals: arbTrade.path.orderbook.quoteAssetDecimals, | ||
}); | ||
|
||
let orderSize = +new BigNumberInBase(offerAsset.amount).toWei(decimals).toFixed(); | ||
orderSize = | ||
Math.floor(+orderSize / arbTrade.path.orderbook.minQuantityIncrement) * | ||
arbTrade.path.orderbook.minQuantityIncrement; | ||
|
||
const marketSpotOrderMsg: SpotMarketOrderMessage = { | ||
marketId: arbTrade.path.orderbook.marketId, | ||
subaccountId: "", | ||
injectiveAddress: injectiveAddress, | ||
orderType: orderType, | ||
feeRecipient: injectiveAddress, | ||
price: beliefPriceOrderbook, | ||
quantity: String(orderSize), | ||
}; | ||
return { | ||
typeUrl: "/injective.exchange.v1beta1.MsgCreateSpotMarketOrder", | ||
value: MsgCreateSpotMarketOrder.fromJSON(marketSpotOrderMsg), | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { BigNumberInBase, BigNumberInWei } from "@injectivelabs/utils"; | ||
|
||
import { ChainOperator } from "../../../core/chainOperator/chainoperator"; | ||
import { isNativeAsset } from "../../../core/types/base/asset"; | ||
import { Order, Orderbook } from "../../../core/types/base/orderbook"; | ||
|
||
/** | ||
* | ||
*/ | ||
export async function getOrderbookState(chainOperator: ChainOperator, orderbooks: Array<Orderbook>) { | ||
await Promise.all( | ||
orderbooks.map(async (orderbook) => { | ||
const ob = await chainOperator.queryOrderbook(orderbook.marketId); | ||
if (!ob) { | ||
console.log("cannot fetch orderbook: ", orderbook.marketId); | ||
return; | ||
} | ||
orderbook.sells = []; | ||
orderbook.buys = []; | ||
let quantityDecimals: number; | ||
let priceDecimals: number; | ||
if (isNativeAsset(orderbook.baseAssetInfo) && orderbook.baseAssetInfo.native_token.denom === "inj") { | ||
quantityDecimals = 12; | ||
priceDecimals = 12; | ||
} else { | ||
quantityDecimals = 0; | ||
priceDecimals = 0; | ||
} | ||
ob.buys.map((buy) => { | ||
const quantity = new BigNumberInWei(buy.quantity).toBase(quantityDecimals); | ||
const price = new BigNumberInBase(buy.price).toWei(priceDecimals); | ||
const buyOrder: Order = { | ||
quantity: +quantity.toFixed(), | ||
price: +price.toFixed(), | ||
type: "buy", | ||
}; | ||
orderbook.buys.push(buyOrder); | ||
ob.sells.map((sell) => { | ||
const quantity = new BigNumberInWei(sell.quantity).toBase(quantityDecimals); | ||
const price = new BigNumberInBase(sell.price).toWei(priceDecimals); | ||
const sellOrder: Order = { | ||
quantity: +quantity.toFixed(), | ||
price: +price.toFixed(), | ||
type: "sell", | ||
}; | ||
orderbook.sells.push(sellOrder); | ||
}); | ||
}); | ||
}), | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { ChainOperator } from "../../../core/chainOperator/chainoperator"; | ||
import { AssetInfo } from "../../../core/types/base/asset"; | ||
import { DexConfig } from "../../../core/types/base/configs"; | ||
import { Orderbook } from "../../../core/types/base/orderbook"; | ||
import { identity } from "../../../core/types/identity"; | ||
import { getOrderbookState } from "./getOrderbookState"; | ||
/** | ||
* | ||
*/ | ||
export async function initOrderbooks( | ||
chainoperator: ChainOperator, | ||
botConfig: DexConfig, | ||
): Promise<Array<Orderbook> | undefined> { | ||
const orderbooks: Array<Orderbook> = []; | ||
for (const orderbookAddress of botConfig.orderbooks) { | ||
const marketInfo = await chainoperator.queryMarket(orderbookAddress); | ||
if (!marketInfo) { | ||
console.log("cannot fetch market: ", orderbookAddress); | ||
return; | ||
} | ||
const baseAssetInfo: AssetInfo = { native_token: { denom: marketInfo.baseDenom } }; | ||
const quoteAssetInfo: AssetInfo = { native_token: { denom: marketInfo.quoteDenom } }; | ||
const ob = identity<Orderbook>({ | ||
baseAssetInfo: baseAssetInfo, | ||
quoteAssetInfo: quoteAssetInfo, | ||
baseAssetDecimals: marketInfo.baseToken?.decimals ?? 6, | ||
quoteAssetDecimals: marketInfo.quoteToken?.decimals ?? 6, | ||
minQuantityIncrement: marketInfo.minQuantityTickSize ?? 10e3, | ||
buys: [], | ||
sells: [], | ||
marketId: orderbookAddress, | ||
}); | ||
orderbooks.push(ob); | ||
} | ||
await getOrderbookState(chainoperator, orderbooks); | ||
return orderbooks; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.