Skip to content

Commit

Permalink
chore: reduce rpc call
Browse files Browse the repository at this point in the history
  • Loading branch information
thanhlmm committed Jan 2, 2025
1 parent a177cd7 commit dbcb3e2
Show file tree
Hide file tree
Showing 6 changed files with 7,467 additions and 33 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,5 +75,6 @@
},
"peerDependencies": {
"@mysten/sui": ">=1.8.0"
}
},
"packageManager": "[email protected]+sha1.ac34549e6aa8e7ead463a7407e1c7390f61a6610"
}
23 changes: 20 additions & 3 deletions src/getVaultBalances.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
} from "./utils/userHoldings.js";
import { getMultiLatestPrices } from "./utils/prices.js";
import { poolInfo } from "./common/maps.js";
import { getAllReceipts } from "./sui-sdk/functions/getReceipts.js";

export async function getXTokenVaultBalanceForActiveUsers(
params: GetVaultBalanceForActiveUsersParams,
Expand Down Expand Up @@ -110,9 +111,15 @@ export async function getVaultBalance(
address?: string,
poolName?: PoolName,
multiGet?: MultiGetVaultBalancesParams,
ignoreUsd?: boolean,
): Promise<VaultBalance> {
if (address && poolName && !multiGet) {
const vaultBalance = await fetchUserVaultBalances(address, poolName, false);
const vaultBalance = await fetchUserVaultBalances(
address,
poolName,
false,
ignoreUsd,
);

return vaultBalance;
} else if (!address && !poolName && multiGet) {
Expand Down Expand Up @@ -201,12 +208,22 @@ export async function getDoubleAssetVaultBalance(

export async function getAllVaultBalances(
address: string,
ignoreUsd: boolean = false,
): Promise<Map<PoolName, AlphaFiVaultBalance>> {
await getMultiLatestPrices();
await getAllReceipts(address); // Cache all receipts
if (!ignoreUsd) {
await getMultiLatestPrices();
}

const pools = Object.keys(poolInfo);
const res = new Map<PoolName, AlphaFiVaultBalance>();
const promises = pools.map(async (pool) => {
const result = await getVaultBalance(address, pool as PoolName);
const result = await getVaultBalance(
address,
pool as PoolName,
undefined,
ignoreUsd,
);
res.set(pool as PoolName, result as AlphaFiVaultBalance);
});
await Promise.all(promises);
Expand Down
67 changes: 39 additions & 28 deletions src/sui-sdk/functions/fetchUserVaultBalances.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export async function fetchUserVaultBalances(
address: string,
poolName: PoolName,
ignoreCache: boolean,
ignoreUsd: boolean = false,
): Promise<AlphaFiVaultBalance | undefined> {
const suiClient = getSuiClient();

Expand All @@ -32,11 +33,13 @@ export async function fetchUserVaultBalances(
},
ignoreCache,
);
const lockedPortfolioAmountInUSD = await getAlphaPortfolioAmountInUSD(
"ALPHA",
{ suiClient, address, isLocked: true },
ignoreCache,
);
const lockedPortfolioAmountInUSD = !ignoreUsd
? await getAlphaPortfolioAmountInUSD(
"ALPHA",
{ suiClient, address, isLocked: true },
ignoreCache,
)
: "0";
const unlockedPortfolioAmount = await getAlphaPortfolioAmount(
"ALPHA",
{
Expand All @@ -46,11 +49,13 @@ export async function fetchUserVaultBalances(
},
ignoreCache,
);
const unlockedPortfolioAmountInUSD = await getAlphaPortfolioAmountInUSD(
"ALPHA",
{ suiClient, address, isLocked: false },
ignoreCache,
);
const unlockedPortfolioAmountInUSD = !ignoreUsd
? await getAlphaPortfolioAmountInUSD(
"ALPHA",
{ suiClient, address, isLocked: false },
ignoreCache,
)
: "0";
const portfolioAmount = await getAlphaPortfolioAmount(
"ALPHA",
{
Expand All @@ -59,14 +64,16 @@ export async function fetchUserVaultBalances(
},
ignoreCache,
);
const portfolioAmountInUSD = await getAlphaPortfolioAmountInUSD(
"ALPHA",
{
suiClient,
address,
},
ignoreCache,
);
const portfolioAmountInUSD = !ignoreUsd
? await getAlphaPortfolioAmountInUSD(
"ALPHA",
{
suiClient,
address,
},
ignoreCache,
)
: "0";
if (
lockedPortfolioAmount !== undefined &&
lockedPortfolioAmountInUSD !== undefined &&
Expand All @@ -91,11 +98,13 @@ export async function fetchUserVaultBalances(
address,
ignoreCache,
);
const portfolioAmountInUSD = await getDoubleAssetPortfolioAmountInUSD(
poolName as PoolName,
address,
ignoreCache,
);
const portfolioAmountInUSD = !ignoreUsd
? await getDoubleAssetPortfolioAmountInUSD(
poolName as PoolName,
address,
ignoreCache,
)
: "0";
if (portfolioAmount !== undefined && portfolioAmountInUSD !== undefined) {
const res: AlphaFiVaultBalance = {
coinA: portfolioAmount[0].toString(),
Expand All @@ -110,11 +119,13 @@ export async function fetchUserVaultBalances(
address,
ignoreCache,
);
const portfolioAmountInUSD = await getSingleAssetPortfolioAmountInUSD(
poolName as SingleAssetPoolNames,
address,
ignoreCache,
);
const portfolioAmountInUSD = !ignoreUsd
? await getSingleAssetPortfolioAmountInUSD(
poolName as SingleAssetPoolNames,
address,
ignoreCache,
)
: "0";
if (portfolioAmount !== undefined && portfolioAmountInUSD !== undefined) {
const res: AlphaFiVaultBalance = {
coin: portfolioAmount.toString(),
Expand Down
3 changes: 2 additions & 1 deletion src/sui-sdk/functions/fetchUserVaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import {
doubleAssetPoolCoinMap,
poolInfo,
} from "../../common/maps.js";
import { getReceipts } from "./getReceipts.js";
import { getReceipts, getAllReceipts } from "./getReceipts.js";

export async function fetchUserVaults(
address: string,
): Promise<AlphaFiVault[]> {
await getAllReceipts(address); // Cache all receipts
const vaultsArr: AlphaFiVault[] = [];
await Promise.all(
Object.keys(poolInfo).map(async (pool) => {
Expand Down
66 changes: 66 additions & 0 deletions src/sui-sdk/functions/getReceipts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,72 @@ export async function getReceipts(
return cachedPromise;
}

export async function getAllReceipts(address: string): Promise<Receipt[]> {
const suiClient = getSuiClient();
const nfts: Receipt[] = [];
const allObjects: Receipt[] = [];
let currentCursor: string | null | undefined = null;

// Collect all objects first
while (true) {
const paginatedObjects: PaginatedObjectsResponse =
await suiClient.getOwnedObjects({
owner: address,
cursor: currentCursor,
filter: {
MatchAny: Object.keys(poolInfo).map((pool) => {
return {
StructType: poolInfo[pool].receiptType,
};
}),
},
options: {
showContent: true,
},
});

paginatedObjects.data.forEach((obj) => {
allObjects.push(obj.data as Receipt);
});

if (paginatedObjects.hasNextPage && paginatedObjects.nextCursor) {
currentCursor = paginatedObjects.nextCursor;
} else {
break;
}
}
// Group objects by content type
const groupedObjects: { [key: string]: Receipt[] } = {};

allObjects.forEach((o) => {
const contentType = o.content.type;
const poolName = Object.keys(poolInfo).find(
(pool) => poolInfo[pool].receiptType === contentType,
);

if (!poolName) {
return;
}

if (!groupedObjects[poolName]) {
groupedObjects[poolInfo[poolName].receiptName] = [];
}

if (poolName) {
groupedObjects[poolInfo[poolName].receiptName].push(o);
nfts.push(o);
}
});

// Store grouped objects in cache
Object.values(poolInfo).forEach((pool) => {
const receiptsCacheKey = `getReceipts-${pool.receiptName}-${address}`;
receiptsCache.set(receiptsCacheKey, groupedObjects[pool.receiptName] || []);
});

return nfts;
}

const poolCache = new SimpleCache<PoolType | AlphaPoolType>();
const poolPromiseCache = new SimpleCache<Promise<PoolType | AlphaPoolType>>();

Expand Down
Loading

0 comments on commit dbcb3e2

Please sign in to comment.