Skip to content

Commit

Permalink
fixing saturation check
Browse files Browse the repository at this point in the history
  • Loading branch information
vsubhuman committed Jan 15, 2024
1 parent 4178df0 commit eaa2c73
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 32 deletions.
16 changes: 12 additions & 4 deletions src/API/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ export type World = {|
+pools: string,
+price: number,
+delegators: string,
+saturation: number,
|};

export const Sorting = Object.freeze({
Expand Down Expand Up @@ -170,15 +171,22 @@ const tail = (input: string): string => {
return input?.slice(-10) ?? '';
};

export type ListBiasedPoolsResponse = {|
pools: Pool[],
saturationLimit: number,
|};

export async function listBiasedPools(
externalSeed: string,
searchParams: SearchParams,
): Promise<Pool[]> {
): Promise<ListBiasedPoolsResponse> {
const unbiasedPoolsResponse = await getPools(searchParams);
const unbiasedPools = toPoolArray(unbiasedPoolsResponse.pools);

const saturationLimit = unbiasedPoolsResponse.world.saturation;

if (searchParams.search || searchParams.sort === Sorting.TICKER) {
return unbiasedPools;
return { pools: unbiasedPools, saturationLimit };
}

const [p1, p2, p3] = unbiasedPools;
Expand Down Expand Up @@ -220,9 +228,9 @@ export async function listBiasedPools(
allPools.splice(targetIndex, 0, biasedPool);
}

return allPools;
return { pools: allPools, saturationLimit };
} catch (err) {
return unbiasedPools;
return { pools: unbiasedPools, saturationLimit };
}
}

Expand Down
32 changes: 18 additions & 14 deletions src/containers/Home.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { YoroiCallback } from '../API/yoroi';

import { DesktopOnly, MobileOnly } from '../components/layout/Breakpoints';
import { listBiasedPools } from '../API/api';
import type { Pool, SearchParams } from '../API/api';
import type { ListBiasedPoolsResponse, Pool, SearchParams } from '../API/api';
import DesktopTable from '../components/DesktopTable';
import MobileTable from '../components/MobileTable';
import SortSelect from '../components/SortSelect';
Expand All @@ -21,9 +21,6 @@ import SaturatedPoolAlert from '../components/SaturatedPoolAlert';
import cexplorerIconMini from '../assets/cexplorer-logo-mini.svg';
import cexplorerIcon from '../assets/cexplorer-logo-extend.svg';

// k = 500
const SATURATION_LIMIT = 63600000000000;

const Header = styled.div`
display: flex;
align-items: flex-end;
Expand Down Expand Up @@ -97,6 +94,7 @@ export type DelegationProps = {|
|};

function Home(props: HomeProps): Node {
const [saturationLimit, setSaturationLimit] = React.useState<?number>(null);
const [rowData, setRowData] = React.useState<?Array<Pool>>(null);
const [status, setStatus] = React.useState<QueryState>('idle');
const [filterOptions, setFilterOptions] = React.useState<SearchParams>({
Expand All @@ -113,9 +111,10 @@ function Home(props: HomeProps): Node {
useEffect(() => {
setStatus('pending');
listBiasedPools(seed, {})
.then((pools: Pool[]) => {
.then((resp: ListBiasedPoolsResponse) => {
setStatus('resolved');
setRowData(pools);
setRowData(resp.pools);
setSaturationLimit(resp.saturationLimit);
})
.catch((err) => {
setStatus('rejected');
Expand All @@ -131,9 +130,10 @@ function Home(props: HomeProps): Node {
setFilterOptions(newSearch);
setStatus('pending');
listBiasedPools(seed, newSearch)
.then((pools: Pool[]) => {
.then((resp: ListBiasedPoolsResponse) => {
setStatus('resolved');
setRowData(pools);
setRowData(resp.pools);
setSaturationLimit(resp.saturationLimit);
})
.catch((err) => {
setStatus('rejected');
Expand All @@ -148,9 +148,10 @@ function Home(props: HomeProps): Node {
setFilterOptions(newSearch);
setStatus('pending');
listBiasedPools(seed, newSearch)
.then((pools: Pool[]) => {
.then((resp: ListBiasedPoolsResponse) => {
setStatus('resolved');
setRowData(pools);
setRowData(resp.pools);
setSaturationLimit(resp.saturationLimit);
})
.catch((err) => {
setStatus('rejected');
Expand All @@ -170,7 +171,8 @@ function Home(props: HomeProps): Node {
if (delegation == null) return;
const lovelaceDelegation = totalAda == null ? 0 : totalAda * 1000000;

if (Number(delegation.stakepoolTotalStake) + lovelaceDelegation >= SATURATION_LIMIT) {
const limit: ?number = saturationLimit;
if (limit != null && Number(delegation.stakepoolTotalStake) + lovelaceDelegation >= limit) {
setDelegationModalData({ ...delegation, totalAda });
setConfirmDelegationModal(true);
setOpenModal(true);
Expand All @@ -184,17 +186,19 @@ function Home(props: HomeProps): Node {
function filterPools(pools: ?Array<Pool>, totalAda: ?number): ?Array<Pool> {
if (pools == null) return pools;

const limit: ?number = saturationLimit;

// don't filter out saturated pools if the user explicitly searches
if (filterOptions.search != null && filterOptions.search !== '') {
if (limit == null || (filterOptions.search != null && filterOptions.search !== '')) {
return pools;
}

const lovelaceDelegation = totalAda == null ? 0 : totalAda * 1000000;

if (lovelaceDelegation > SATURATION_LIMIT) return pools;
if (lovelaceDelegation > limit) return pools;

return pools.filter((item) => {
return item != null && Number(item.total_stake) + lovelaceDelegation < SATURATION_LIMIT;
return item != null && Number(item.total_stake) + lovelaceDelegation < limit;
});
}

Expand Down
31 changes: 17 additions & 14 deletions src/containers/HomeRevamp.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { SendFirstAdapool, YoroiCallback } from '../API/yoroi';

import { DesktopOnly, MobileOnly } from '../components/layout/Breakpoints';
import { listBiasedPools, Sorting, SortingDirections } from '../API/api';
import type { Pool, SearchParams } from '../API/api';
import type { ListBiasedPoolsResponse, Pool, SearchParams } from '../API/api';
import SortSelect from '../components/SortSelect';
import type { QueryState } from '../utils/types';

Expand All @@ -22,9 +22,6 @@ import SearchRevamp from '../components/SearchRevamp';
import MobileTableRevamp from '../components/MobileTableRevamp';
import { formatCostLabel } from '../utils/utils';

// k = 500
const SATURATION_LIMIT = 63600000000000;

const Header = styled.div`
display: flex;
align-items: flex-end;
Expand Down Expand Up @@ -148,6 +145,7 @@ const SORTING_FUNCTIONS = {
const defaultActiveSort = { sort: Sorting.TICKER, sortDirection: '' };

function Home(props: HomeProps): Node {
const [saturationLimit, setSaturationLimit] = React.useState<?number>(null);
const [rowData, setRowData] = React.useState<?Array<Pool>>(null);
const [rowDataSorted, setRowDataSorted] = React.useState<?Array<Pool>>(null);
const [status, setStatus] = React.useState<QueryState>('idle');
Expand All @@ -167,11 +165,12 @@ function Home(props: HomeProps): Node {
useEffect(() => {
setStatus('pending');
listBiasedPools(seed, {})
.then((pools: Pool[]) => {
.then((resp: ListBiasedPoolsResponse) => {
setStatus('resolved');
setRowData(pools);
setRowData(resp.pools);
setSaturationLimit(resp.saturationLimit);
// used to show the first pool in revamp banner
SendFirstAdapool(pools[0]);
SendFirstAdapool(resp.pools[0]);
})
.catch((err) => {
setStatus('rejected');
Expand All @@ -187,11 +186,12 @@ function Home(props: HomeProps): Node {
setFilterOptions(newSearch);
setStatus('pending');
listBiasedPools(seed, newSearch)
.then((pools: Pool[]) => {
.then((resp: ListBiasedPoolsResponse) => {
setStatus('resolved');
setRowData(pools);
setRowDataSorted(pools);
setRowData(resp.pools);
setRowDataSorted(resp.pools);
setActiveSort(defaultActiveSort);
setSaturationLimit(resp.saturationLimit);
})
.catch((err) => {
setStatus('rejected');
Expand All @@ -211,7 +211,8 @@ function Home(props: HomeProps): Node {
if (delegation == null) return;
const lovelaceDelegation = totalAda == null ? 0 : totalAda * 1000000;

if (Number(delegation.stakepoolTotalStake) + lovelaceDelegation >= SATURATION_LIMIT) {
const limit: ?number = saturationLimit;
if (limit != null && Number(delegation.stakepoolTotalStake) + lovelaceDelegation >= limit) {
setDelegationModalData({ ...delegation, totalAda });
setConfirmDelegationModal(true);
setOpenModal(true);
Expand All @@ -225,17 +226,19 @@ function Home(props: HomeProps): Node {
function filterPools(pools: ?Array<Pool>, totalAda: ?number): ?Array<Pool> {
if (pools == null) return pools;

const limit: ?number = saturationLimit;

// don't filter out saturated pools if the user explicitly searches
if (filterOptions.search != null && filterOptions.search !== '') {
if (limit == null || (filterOptions.search != null && filterOptions.search !== '')) {
return pools;
}

const lovelaceDelegation = totalAda == null ? 0 : totalAda * 1000000;

if (lovelaceDelegation > SATURATION_LIMIT) return pools;
if (lovelaceDelegation > limit) return pools;

return pools.filter((item) => {
return item != null && Number(item.total_stake) + lovelaceDelegation < SATURATION_LIMIT;
return item != null && Number(item.total_stake) + lovelaceDelegation < limit;
});
}

Expand Down

0 comments on commit eaa2c73

Please sign in to comment.