UNPKG

@oraichain/oraidex-evm-sdk

Version:
115 lines 4.49 kB
import { denomToAssetInfo, isCw20Token, parsePoolKey, toBinary, } from '../utils'; const CONVERTER_CONTRACT = 'orai14wy8xndhnvjmx6zl2866xqvs7fqwv2arhhrqq9'; export class OsorMsgComposer { constructor() { this.parseConverterMsgToPoolId = this.parseConverterMsgToPoolId.bind(this); this.generateMsgFromRouteResponse = this.generateMsgFromRouteResponse.bind(this); this.generateSwapOps = this.generateSwapOps.bind(this); this.generateOraidexV2SwapMsg = this.generateOraidexV2SwapMsg.bind(this); this.generateOraidexV3SwapMsg = this.generateOraidexV3SwapMsg.bind(this); this._generateUniversalSwapMsg = this._generateUniversalSwapMsg.bind(this); } parseConverterMsgToPoolId(tokenIn, tokenOut) { // In Oraichain, conversion from native token to CW20 token always occurs // TODO: Query the converter contract to determine the appropriate conversion method if (isCw20Token(tokenIn)) { // Convert in reverse return toBinary({ contract: CONVERTER_CONTRACT, msg: toBinary({ convert_reverse: { from: { native_token: { denom: tokenOut, }, }, }, }), }); } else { // Convert normally return toBinary({ contract: CONVERTER_CONTRACT, msg: toBinary({ convert: {}, }), }); } } generateMsgFromRouteResponse(routeResponse) { const isUniversalMsg = routeResponse.paths.some((path) => path.actions.some((action) => action.type === 'Bridge')); if (isUniversalMsg) { return this._generateUniversalSwapMsg(routeResponse); } else { const swapActionRouteSample = routeResponse.paths[0].actions; return this.generateSwapOps(swapActionRouteSample); } } generateSwapOps(swapActionRoute) { const swapOps = []; for (const actionRoute of swapActionRoute) { let tokenIn = actionRoute.tokenIn; if (actionRoute.type === 'Bridge') { continue; } for (const path of actionRoute.swapInfo) { switch (actionRoute.type) { case 'Swap': { swapOps.push({ denom_in: tokenIn, denom_out: actionRoute.tokenOut, pool: path.poolId, }); tokenIn = path.tokenOut; break; } case 'Convert': swapOps.push({ denom_in: actionRoute.tokenIn, denom_out: actionRoute.tokenOut, pool: this.parseConverterMsgToPoolId(actionRoute.tokenIn, actionRoute.tokenOut), }); break; } } } return swapOps; } generateOraidexV2SwapMsg(swapActionRoute) { let tokenIn = swapActionRoute.tokenIn; const swapInfo = swapActionRoute.swapInfo; const swapMsgs = []; for (const info of swapInfo) { const swapMsg = { offer_asset_info: denomToAssetInfo(tokenIn), ask_asset_info: denomToAssetInfo(info.tokenOut), }; swapMsgs.push(swapMsg); tokenIn = info.tokenOut; } return swapMsgs; } generateOraidexV3SwapMsg(swapActionRoute) { let tokenIn = swapActionRoute.tokenIn; const swapInfo = swapActionRoute.swapInfo; const swapMsgs = []; for (const info of swapInfo) { const poolKey = parsePoolKey(info.poolId); const isXToY = tokenIn === poolKey.token_x; const swapMsg = { pool_key: poolKey, x_to_y: isXToY, }; swapMsgs.push(swapMsg); tokenIn = isXToY ? poolKey.token_y : poolKey.token_x; } return swapMsgs; } _generateUniversalSwapMsg(_routeResponse) { throw new Error('Universal swap have not supported yet'); } } //# sourceMappingURL=OsorMsgComposer.js.map