Skip to content

Commit

Permalink
feat: add getting spools and spool in query
Browse files Browse the repository at this point in the history
  • Loading branch information
mr-donor committed Oct 2, 2023
1 parent 5716107 commit ba92d7b
Show file tree
Hide file tree
Showing 6 changed files with 514 additions and 42 deletions.
50 changes: 49 additions & 1 deletion src/models/scallopQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,14 @@ import {
getMarketPool,
getMarketCollaterals,
getMarketCollateral,
getSpools,
getSpool,
getCoinAmounts,
getCoinAmount,
getMarketCoinAmounts,
getMarketCoinAmount,
getLendings,
getLending,
} from '../queries';
import {
ScallopQueryParams,
Expand Down Expand Up @@ -239,7 +242,7 @@ export class ScallopQuery {
}

/**
* Get market collateral
* Get market collateral
*
* @param coinName - Specific support coin name.
* @return Market collateral data.
Expand All @@ -248,6 +251,26 @@ export class ScallopQuery {
return await getMarketCollateral(this, coinName);
}

/**
* Get spools data.
*
* @param marketCoinNames - Specific an array of support stake market coin name.
* @return Spools data.
*/
public async getSpools(marketCoinNames?: SupportStakeMarketCoins[]) {
return await getSpools(this, marketCoinNames);
}

/**
* Get spool data.
*
* @param marketCoinName - Specific support stake market coin name.
* @return Spool data.
*/
public async getSpool(marketCoinName: SupportStakeMarketCoins) {
return await getSpool(this, marketCoinName);
}

/**
* Get all coins amount
*
Expand Down Expand Up @@ -304,6 +327,31 @@ export class ScallopQuery {
return await getMarketCoinAmount(this, marketCoinName, ownerAddress);
}

/**
* Get all lending pool dta for the user
*
* @param coinNames - Specific an array of support coin name.
* @param ownerAddress - The owner address.
* @return Lending pools data.
*/
public async getLendings(
coinNames?: SupportPoolCoins[],
ownerAddress?: string
) {
return await getLendings(this, coinNames, ownerAddress);
}

/**
* Get specific lending pool information for the user
*
* @param coinName - Specific support coin name.
* @param ownerAddress - The owner address.
* @return Lending pool data.
*/
public async getLending(coinName: SupportPoolCoins, ownerAddress?: string) {
return await getLending(this, coinName, ownerAddress);
}

public async getCollaterals() {
return await getLendings(this);
}
Expand Down
184 changes: 165 additions & 19 deletions src/queries/spoolQuery.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,160 @@
import { normalizeStructTag } from '@mysten/sui.js/utils';
import {
parseOriginStakePoolData,
calculateStakePoolData,
parseOriginRewardPoolData,
calculateRewardPoolData,
isMarketCoin,
} from '../utils';
import type { ScallopQuery } from '../models';
import type { SuiObjectResponse } from '@mysten/sui.js/client';
import type {
MarketPool,
Spools,
Spool,
StakePool,
RewardPool,
StakeAccount,
StakeAccounts,
SupportStakeMarketCoins,
SupportCoins,
SupportStakeCoins,
} from '../types';
import { SUPPORT_SPOOLS } from 'src/constants';

/**
* Get spools data.
*
* @param query - The Scallop query instance.
* @param marketCoinNames - Specific an array of support stake market coins.
* @return Spools data.
*/
export const getSpools = async (
query: ScallopQuery,
marketCoinNames?: SupportStakeMarketCoins[]
) => {
marketCoinNames = marketCoinNames || [...SUPPORT_SPOOLS];
const coinNames = marketCoinNames.map(
(marketCoinName) => marketCoinName.slice(1) as SupportStakeCoins
);
const marketPool = await query.getMarketPools(coinNames);
const spools: Spools = {};
for (const marketCoinName of marketCoinNames) {
const coinName = marketCoinName.slice(1) as SupportStakeCoins;
const spool = await getSpool(query, marketCoinName, marketPool[coinName]);

if (spool) {
spools[marketCoinName] = spool;
}
}

return spools;
};

/**
* Get spool data.
*
* @param query - The Scallop query instance.
* @param marketCoinName - Support stake market coins.
* @param marketPool - The market pool data.
* @return Spool data.
*/
export const getSpool = async (
query: ScallopQuery,
marketCoinName: SupportStakeMarketCoins,
marketPool?: MarketPool
) => {
const coinName = marketCoinName.slice(1) as SupportStakeCoins;
marketPool = marketPool || (await query.getMarketPool(coinName));
const poolId = query.address.get(`spool.pools.${marketCoinName}.id`);
const rewardPoolId = query.address.get(
`spool.pools.${marketCoinName}.rewardPoolId`
);
let sPool: Spool | undefined = undefined;
const stakePoolObjectResponse = await query.suiKit.client().multiGetObjects({
ids: [poolId, rewardPoolId],
options: {
showContent: true,
},
});

if (
marketPool &&
stakePoolObjectResponse[0].data &&
stakePoolObjectResponse[1].data
) {
const coinName = marketCoinName.slice(1) as SupportStakeCoins;
const rewardCoin = query.utils.getRewardCoinName(marketCoinName);
const coinPrices = await query.utils.getCoinPrices([coinName, rewardCoin]);

const stakePoolObject = stakePoolObjectResponse[0].data;
const rewardPoolObject = stakePoolObjectResponse[1].data;
if (stakePoolObject.content && 'fields' in stakePoolObject.content) {
const fields = stakePoolObject.content.fields as any;
const parsedStakePoolData = parseOriginStakePoolData({
stakeType: fields.stake_type,
maxDistributedPoint: fields.max_distributed_point,
distributedPoint: fields.distributed_point,
distributedPointPerPeriod: fields.distributed_point_per_period,
pointDistributionTime: fields.point_distribution_time,
maxStake: fields.max_stakes,
stakes: fields.stakes,
index: fields.index,
createdAt: fields.created_at,
lastUpdate: fields.last_update,
});

const stakeMarketCoinPrice =
(coinPrices?.[coinName] ?? 0) * marketPool.conversionRate;
const stakeMarketCoinDecimal = query.utils.getCoinDecimal(coinName);
const calculatedStakePoolData = calculateStakePoolData(
parsedStakePoolData,
stakeMarketCoinPrice,
stakeMarketCoinDecimal
);

if (rewardPoolObject.content && 'fields' in rewardPoolObject.content) {
const fields = rewardPoolObject.content.fields as any;
const parsedRewardPoolData = parseOriginRewardPoolData({
claimed_rewards: fields.claimed_rewards,
exchange_rate_numerator: fields.exchange_rate_numerator,
exchange_rate_denominator: fields.exchange_rate_denominator,
rewards: fields.rewards,
spool_id: fields.spool_id,
});

const rewardCoinPrice = coinPrices?.[rewardCoin] ?? 0;
const rewardCoinDecimal = query.utils.getCoinDecimal(rewardCoin);

const calculatedRewardPoolData = calculateRewardPoolData(
parsedStakePoolData,
parsedRewardPoolData,
calculatedStakePoolData,
rewardCoinPrice,
rewardCoinDecimal
);

sPool = {
marketCoin: marketCoinName,
symbol: query.utils.parseSymbol(coinName),
coinType: query.utils.parseCoinType(coinName),
marketCoinType: query.utils.parseMarketCoinType(coinName),
rewardCoinType: isMarketCoin(rewardCoin)
? query.utils.parseMarketCoinType(rewardCoin)
: query.utils.parseCoinType(rewardCoin),
coinDecimal: query.utils.getCoinDecimal(coinName),
rewardCoinDecimal: query.utils.getCoinDecimal(rewardCoin),
coinPrice: coinPrices?.[coinName] ?? 0,
marketCoinPrice: stakeMarketCoinPrice,
rewardCoinPrice: rewardCoinPrice,
...calculatedStakePoolData,
...calculatedRewardPoolData,
};
}
}
}

return sPool;
};

/**
* Get all stake accounts of the owner.
Expand Down Expand Up @@ -48,26 +195,25 @@ export const getStakeAccounts = async (
}
} while (hasNextPage);

const stakeAccounts: Record<SupportStakeMarketCoins, StakeAccount[]> = {
const stakeAccounts: StakeAccounts = {
ssui: [],
susdc: [],
susdt: [],
};

const stakeCointTypes: Record<SupportStakeMarketCoins, string> = Object.keys(
stakeAccounts
).reduce(
(types, marketCoinName) => {
const coinName = marketCoinName.slice(1) as SupportCoins;
const marketCoinType = query.utils.parseMarketCoinType(coinName);

types[
marketCoinName as SupportStakeMarketCoins
] = `${spoolPkgId}::spool_account::SpoolAccount<${marketCoinType}>`;
return types;
},
{} as Record<SupportStakeMarketCoins, string>
);
const stakeMarketCoinTypes: Record<SupportStakeMarketCoins, string> =
Object.keys(stakeAccounts).reduce(
(types, marketCoinName) => {
const coinName = marketCoinName.slice(1) as SupportCoins;
const marketCoinType = query.utils.parseMarketCoinType(coinName);

types[
marketCoinName as SupportStakeMarketCoins
] = `${spoolPkgId}::spool_account::SpoolAccount<${marketCoinType}>`;
return types;
},
{} as Record<SupportStakeMarketCoins, string>
);

const stakeObjectIds: string[] = stakeObjectsResponse
.map((ref: any) => ref?.data?.objectId)
Expand All @@ -84,7 +230,7 @@ export const getStakeAccounts = async (
const index = Number(fields.index);
const points = Number(fields.points);
const totalPoints = Number(fields.total_points);
if (normalizeStructTag(type) === stakeCointTypes.ssui) {
if (normalizeStructTag(type) === stakeMarketCoinTypes.ssui) {
stakeAccounts.ssui.push({
id,
type,
Expand All @@ -95,7 +241,7 @@ export const getStakeAccounts = async (
points,
totalPoints,
});
} else if (normalizeStructTag(type) === stakeCointTypes.susdc) {
} else if (normalizeStructTag(type) === stakeMarketCoinTypes.susdc) {
stakeAccounts.susdc.push({
id,
type,
Expand All @@ -106,7 +252,7 @@ export const getStakeAccounts = async (
points,
totalPoints,
});
} else if (normalizeStructTag(type) === stakeCointTypes.susdt) {
} else if (normalizeStructTag(type) === stakeMarketCoinTypes.susdt) {
stakeAccounts.susdt.push({
id,
type,
Expand Down
Loading

0 comments on commit ba92d7b

Please sign in to comment.