From b4c0d4eb56b062e513a78994da35629cf36eda99 Mon Sep 17 00:00:00 2001 From: devdanco Date: Mon, 25 Sep 2023 21:23:00 +0200 Subject: [PATCH] feat: update types and rpc --- src/mangataInstance.ts | 4 ++++ src/methods/rpc/calculateBuyPrice.ts | 4 ++-- src/methods/rpc/calculateBuyPriceId.ts | 4 ++-- src/methods/rpc/calculateRewardsAmount.ts | 8 +++---- src/methods/rpc/calculateSellPrice.ts | 4 ++-- src/methods/rpc/calculateSellPriceId.ts | 4 ++-- src/methods/rpc/getBurnAmount.ts | 7 +++++-- .../rpc/getLiquidityTokensForTrading.ts | 16 ++++++++++++++ src/methods/rpc/getTradeableTokens.ts | 21 +++++++++++++++++++ src/types/common.ts | 4 +++- src/types/xyk.ts | 7 +++++++ src/utils/getRatio.ts | 1 + 12 files changed, 68 insertions(+), 16 deletions(-) create mode 100644 src/methods/rpc/getLiquidityTokensForTrading.ts create mode 100644 src/methods/rpc/getTradeableTokens.ts diff --git a/src/mangataInstance.ts b/src/mangataInstance.ts index b2273d55..4922b82c 100644 --- a/src/mangataInstance.ts +++ b/src/mangataInstance.ts @@ -108,6 +108,8 @@ import { isBuyAssetLockFree } from "./methods/rpc/isBuyAssetLockFree"; import { isSellAssetLockFree } from "./methods/rpc/isSellAssetLockFree"; import { withdrawToMoonriver } from "./methods/xTokens/withdrawToMoonriver"; import { getWithdrawFromMoonriverFee } from "./methods/fee/getWithdrawFromMoonriverFee"; +import {getTradeableTokens} from "./methods/rpc/getTradeableTokens"; +import {getLiquidityTokensForTrading} from "./methods/rpc/getLiquidityTokensForTrading"; import { logger } from "./utils/mangataLogger"; /** @@ -163,6 +165,8 @@ export function createMangataInstance(urls: string[]): MangataInstance { multiswapSellAsset(instancePromise, args, false) }, rpc: { + getTradeableTokens: () => getTradeableTokens(instancePromise), + getLiquidityTokensForTrading: () => getLiquidityTokensForTrading(instancePromise), isBuyAssetLockFree: (tokenIds: TokenId[], amount: BN) => isBuyAssetLockFree(instancePromise, tokenIds, amount), isSellAssetLockFree: (tokenIds: TokenId[], amount: BN) => diff --git a/src/methods/rpc/calculateBuyPrice.ts b/src/methods/rpc/calculateBuyPrice.ts index aa1e732b..35ec8d9c 100644 --- a/src/methods/rpc/calculateBuyPrice.ts +++ b/src/methods/rpc/calculateBuyPrice.ts @@ -17,10 +17,10 @@ export const calculateBuyPrice = async ( }); const api = await instancePromise; const { inputReserve, outputReserve, amount } = args; - const result = await (api.rpc as any).xyk.calculate_buy_price( + const price = await (api.rpc as any).xyk.calculate_buy_price( inputReserve, outputReserve, amount ); - return new BN(result.price); + return new BN(price); }; diff --git a/src/methods/rpc/calculateBuyPriceId.ts b/src/methods/rpc/calculateBuyPriceId.ts index d213d6a0..0ed5ae48 100644 --- a/src/methods/rpc/calculateBuyPriceId.ts +++ b/src/methods/rpc/calculateBuyPriceId.ts @@ -18,10 +18,10 @@ export const calculateBuyPriceId = async ( amount: amount.toString() }); const api = await instancePromise; - const result = await (api.rpc as any).xyk.calculate_buy_price_id( + const price = await (api.rpc as any).xyk.calculate_buy_price_id( soldTokenId, boughtTokenId, amount ); - return new BN(result.price); + return new BN(price); }; diff --git a/src/methods/rpc/calculateRewardsAmount.ts b/src/methods/rpc/calculateRewardsAmount.ts index 9d263daa..6e390fc4 100644 --- a/src/methods/rpc/calculateRewardsAmount.ts +++ b/src/methods/rpc/calculateRewardsAmount.ts @@ -21,9 +21,7 @@ export const calculateRewardsAmount = async ( liquidityTokenId ); - const price = isHex(rewards.price.toString()) - ? hexToBn(rewards.price.toString()) - : new BN(rewards.price); - - return price; + return isHex(rewards.toString()) + ? hexToBn(rewards.toString()) + : new BN(rewards); }; diff --git a/src/methods/rpc/calculateSellPrice.ts b/src/methods/rpc/calculateSellPrice.ts index 5219ca95..a49f57f0 100644 --- a/src/methods/rpc/calculateSellPrice.ts +++ b/src/methods/rpc/calculateSellPrice.ts @@ -17,10 +17,10 @@ export const calculateSellPrice = async ( }); const api = await instancePromise; const { inputReserve, outputReserve, amount } = args; - const result = await (api.rpc as any).xyk.calculate_sell_price( + const price = await (api.rpc as any).xyk.calculate_sell_price( inputReserve, outputReserve, amount ); - return new BN(result.price); + return new BN(price); }; diff --git a/src/methods/rpc/calculateSellPriceId.ts b/src/methods/rpc/calculateSellPriceId.ts index 73b6462f..f1963df6 100644 --- a/src/methods/rpc/calculateSellPriceId.ts +++ b/src/methods/rpc/calculateSellPriceId.ts @@ -18,10 +18,10 @@ export const calculateSellPriceId = async ( amount: amount.toString() }); const api = await instancePromise; - const result = await (api.rpc as any).xyk.calculate_sell_price_id( + const price = await (api.rpc as any).xyk.calculate_sell_price_id( soldTokenId, boughtTokenId, amount ); - return new BN(result.price); + return new BN(price); }; diff --git a/src/methods/rpc/getBurnAmount.ts b/src/methods/rpc/getBurnAmount.ts index 9ceae73f..ec15f97c 100644 --- a/src/methods/rpc/getBurnAmount.ts +++ b/src/methods/rpc/getBurnAmount.ts @@ -1,6 +1,7 @@ import { ApiPromise } from "@polkadot/api"; import { BurnAmount, Price } from "../../types/xyk"; import { logger } from "../../utils/mangataLogger"; +import {BN} from "@polkadot/util"; /** * @since 2.0.0 @@ -21,6 +22,8 @@ export const getBurnAmount = async ( secondTokenId, amount ); - const resultAsJson = JSON.parse(result.toString()); - return resultAsJson as BurnAmount; + return { + firstAssetAmount: new BN(result[0]), + secondAssetAmount: new BN(result[1]) + } as BurnAmount }; diff --git a/src/methods/rpc/getLiquidityTokensForTrading.ts b/src/methods/rpc/getLiquidityTokensForTrading.ts new file mode 100644 index 00000000..fc04b538 --- /dev/null +++ b/src/methods/rpc/getLiquidityTokensForTrading.ts @@ -0,0 +1,16 @@ + +import { ApiPromise } from "@polkadot/api"; +import { logger } from "../../utils/mangataLogger"; + +/** + * @since 2.0.0 + */ +export const getLiquidityTokensForTrading = async ( + instancePromise: Promise +) => { + logger.info("getLiquidityTokensForTrading"); + const api = await instancePromise; + const lpTokens = await (api.rpc as any).xyk.get_liq_tokens_for_trading() + const lpTokensForTrading: string[] = lpTokens.map((item: any) => item.toString()); + return lpTokensForTrading +}; \ No newline at end of file diff --git a/src/methods/rpc/getTradeableTokens.ts b/src/methods/rpc/getTradeableTokens.ts new file mode 100644 index 00000000..58cbb7ff --- /dev/null +++ b/src/methods/rpc/getTradeableTokens.ts @@ -0,0 +1,21 @@ +import { ApiPromise } from "@polkadot/api"; +import {TradeAbleTokens} from "../../types/xyk"; +import { logger } from "../../utils/mangataLogger"; + +/** + * @since 2.0.0 + */ +export const getTradeableTokens = async ( + instancePromise: Promise +) => { + logger.info("getTradeableTokens"); + const api = await instancePromise; + const tokens = await (api.rpc as any).xyk.get_tradeable_tokens() + const tradeableTokens: TradeAbleTokens[] = tokens.map((item: any) => ({ + tokenId: item.tokenId.toString(), + decimals: parseInt(item.decimals, 10), + name: item.name.toUtf8(), + symbol: item.symbol.toUtf8(), + })); + return tradeableTokens +}; \ No newline at end of file diff --git a/src/types/common.ts b/src/types/common.ts index 27c7cd1c..8ceaa663 100644 --- a/src/types/common.ts +++ b/src/types/common.ts @@ -44,7 +44,7 @@ import { MultiswapSellAsset, Price, Reserve, - Rewards + Rewards, TradeAbleTokens } from "../types/xyk"; import { Transfer, @@ -232,6 +232,8 @@ export interface MangataInstance { * rpc methods for interacting with various RPC operations. */ rpc: { + getLiquidityTokensForTrading: () => Promise, + getTradeableTokens: () => Promise, isSellAssetLockFree: (tokendIds: TokenId[], amount: BN) => Promise; isBuyAssetLockFree: (tokendIds: TokenId[], amount: BN) => Promise; /** diff --git a/src/types/xyk.ts b/src/types/xyk.ts index b983865d..f9254967 100644 --- a/src/types/xyk.ts +++ b/src/types/xyk.ts @@ -13,6 +13,13 @@ export type BurnAmount = { secondAssetAmount: TokenAmount; }; +export type TradeAbleTokens = { + tokenId: string, + decimals: number, + name: string, + symbol: string +} + export type Rewards = { address: Address; liquidityTokenId: TokenId; diff --git a/src/utils/getRatio.ts b/src/utils/getRatio.ts index 5200725f..5b528b83 100644 --- a/src/utils/getRatio.ts +++ b/src/utils/getRatio.ts @@ -18,6 +18,7 @@ const calculateRatio = (numerator: BN, denominator: BN) => { }; export const getRatio = (left: BN, right: BN) => { + if (left.isZero() && right.isZero()) return BN_ZERO; const ratios = calculateRatio(left, right); const res = ratios[1].mul(BN_DIV_NUMERATOR_MULTIPLIER).div(ratios[0]);