Skip to content

Commit

Permalink
fix: sort position issue
Browse files Browse the repository at this point in the history
  • Loading branch information
prodesert22 authored and SarjuHansaliya committed May 12, 2023
1 parent a9d4d49 commit 4a9bda2
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
17 changes: 10 additions & 7 deletions src/state/psarstake/hooks/evm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -390,9 +390,11 @@ export function useSarNftsIds() {
balance.sub(1).toHexString(),
);

return indexes?.map((index) => {
return [index.toHexString()];
});
return indexes
?.map((index) => {
return [index.toHexString()];
})
.sort((a, b) => Number(a) - Number(b));
},
{
refetchInterval: 1000 * 60 * 1, // 1 minute
Expand Down Expand Up @@ -531,7 +533,6 @@ export function useSarPositionsViaSubgraph() {
const { nftsIndexes, isLoading: isLoadingIndexes, isRefetching: isRefetchingIndexes } = useSarNftsIds();

const positionsIds = (nftsIndexes || []).map((nftIndex) => nftIndex[0]);

const {
data: subgraphPositions,
isLoading: isLoadingSubgraphPositions,
Expand Down Expand Up @@ -584,10 +585,12 @@ export function useSarPositionsViaSubgraph() {
}

// if is loading or exist error or not exist account return empty array
if (isLoading || !isValid || !nftsIndexes) {
if (isLoading || !isValid || !nftsIndexes || !subgraphPositions) {
return { positions: [] as Position[], isLoading: true };
}

subgraphPositions.sort((a, b) => Number(a.id) - Number(b.id));

// we need to decode the base64 uri to get the real uri
const nftsURIs = nftsURIsState.map((value) => {
if (value.result) {
Expand All @@ -599,12 +602,12 @@ export function useSarPositionsViaSubgraph() {
return undefined;
});

const valuesVariables = (subgraphPositions || [])?.map((position) => ({
const valuesVariables = subgraphPositions?.map((position) => ({
balance: BigNumber.from(position.balance),
sumOfEntryTimes: BigNumber.from(position.sumOfEntryTimes),
}));

const rewardRates = (subgraphPositions || [])?.map((position) => {
const rewardRates = subgraphPositions?.map((position) => {
const userValuesVariables = {
balance: BigNumber.from(position.balance),
sumOfEntryTimes: BigNumber.from(position.sumOfEntryTimes),
Expand Down
10 changes: 7 additions & 3 deletions src/state/psarstake/hooks/hedera.ts
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,8 @@ export function useHederaSarNftsIds() {
const _nftsIndexes: string[][] = [];
const _nftURIs: (string | undefined)[] = [];

nfts.sort((a, b) => a.serial_number - b.serial_number);

nfts.forEach((nft) => {
_nftsIndexes.push([`0x${nft.serial_number.toString(16)}`]);
// the metadata from this endpoint is returned in encoded in base64
Expand Down Expand Up @@ -664,7 +666,7 @@ export function useHederaSarPositionsViaSubgraph() {
}

// if is loading or exist error or not exist account return empty array
if (isLoading || !isValid || !nftsIndexes || !nftsURIs) {
if (isLoading || !isValid || !nftsIndexes || !nftsURIs || !subgraphPositions) {
return { positions: [] as Position[], isLoading: true };
}

Expand All @@ -678,12 +680,14 @@ export function useHederaSarPositionsViaSubgraph() {
return JSON.parse(nftUri) as URI;
});

const valuesVariables = (subgraphPositions || [])?.map((position) => ({
subgraphPositions.sort((a, b) => Number(a.id) - Number(b.id));

const valuesVariables = subgraphPositions.map((position) => ({
balance: BigNumber.from(position.balance),
sumOfEntryTimes: BigNumber.from(position.sumOfEntryTimes),
}));

const rewardRates = (subgraphPositions || [])?.map((position) => {
const rewardRates = subgraphPositions.map((position) => {
const userValuesVariables = {
balance: BigNumber.from(position.balance),
sumOfEntryTimes: BigNumber.from(position.sumOfEntryTimes),
Expand Down

1 comment on commit 4a9bda2

@vercel
Copy link

@vercel vercel bot commented on 4a9bda2 May 12, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.