Skip to content

Commit

Permalink
feat: update types and rpc
Browse files Browse the repository at this point in the history
  • Loading branch information
devdanco committed Sep 25, 2023
1 parent 0462c9b commit b4c0d4e
Show file tree
Hide file tree
Showing 12 changed files with 68 additions and 16 deletions.
4 changes: 4 additions & 0 deletions src/mangataInstance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

/**
Expand Down Expand Up @@ -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) =>
Expand Down
4 changes: 2 additions & 2 deletions src/methods/rpc/calculateBuyPrice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
4 changes: 2 additions & 2 deletions src/methods/rpc/calculateBuyPriceId.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
8 changes: 3 additions & 5 deletions src/methods/rpc/calculateRewardsAmount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
4 changes: 2 additions & 2 deletions src/methods/rpc/calculateSellPrice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
4 changes: 2 additions & 2 deletions src/methods/rpc/calculateSellPriceId.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
7 changes: 5 additions & 2 deletions src/methods/rpc/getBurnAmount.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
};
16 changes: 16 additions & 0 deletions src/methods/rpc/getLiquidityTokensForTrading.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

import { ApiPromise } from "@polkadot/api";
import { logger } from "../../utils/mangataLogger";

/**
* @since 2.0.0
*/
export const getLiquidityTokensForTrading = async (
instancePromise: Promise<ApiPromise>
) => {
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
};
21 changes: 21 additions & 0 deletions src/methods/rpc/getTradeableTokens.ts
Original file line number Diff line number Diff line change
@@ -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<ApiPromise>
) => {
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
};
4 changes: 3 additions & 1 deletion src/types/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ import {
MultiswapSellAsset,
Price,
Reserve,
Rewards
Rewards, TradeAbleTokens
} from "../types/xyk";
import {
Transfer,
Expand Down Expand Up @@ -232,6 +232,8 @@ export interface MangataInstance {
* rpc methods for interacting with various RPC operations.
*/
rpc: {
getLiquidityTokensForTrading: () => Promise<string[]>,
getTradeableTokens: () => Promise<TradeAbleTokens[]>,
isSellAssetLockFree: (tokendIds: TokenId[], amount: BN) => Promise<Boolean>;
isBuyAssetLockFree: (tokendIds: TokenId[], amount: BN) => Promise<Boolean>;
/**
Expand Down
7 changes: 7 additions & 0 deletions src/types/xyk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions src/utils/getRatio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand Down

0 comments on commit b4c0d4e

Please sign in to comment.