Skip to content

Commit

Permalink
feat: add pagination to getAllRippleVaults
Browse files Browse the repository at this point in the history
  • Loading branch information
Polybius93 committed Oct 21, 2024
1 parent 9730076 commit 857107e
Show file tree
Hide file tree
Showing 3 changed files with 178 additions and 204 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"type": "module",
"name": "dlc-btc-lib",
"version": "2.4.5",
"version": "2.4.6",
"description": "This library provides a comprehensive set of interfaces and functions for minting dlcBTC tokens on supported blockchains.",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
54 changes: 30 additions & 24 deletions src/functions/ripple/ripple.functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,30 +199,25 @@ export async function getRippleVault(
try {
await connectRippleClient(rippleClient);

let formattedUUID = vaultUUID.substring(0, 2) === '0x' ? vaultUUID.slice(2) : vaultUUID;
formattedUUID = formattedUUID.toUpperCase();
const getAccountNFTsRequest: AccountNFTsRequest = {
command: 'account_nfts',
account: issuerAddress,
};

const {
result: { account_nfts: rippleNFTs },
} = await rippleClient.request(getAccountNFTsRequest);
// let formattedUUID = vaultUUID.substring(0, 2) === '0x' ? vaultUUID.slice(2) : vaultUUID;
// formattedUUID = formattedUUID.toUpperCase();
const formattedUUID = vaultUUID.substring(0, 2) === '0x' ? vaultUUID : `0x${vaultUUID}`;

const nftID = findNFTByUUID(rippleNFTs, formattedUUID).NFTokenID;
const allVaults = await getAllRippleVaults(rippleClient, issuerAddress);

const matchingNFTs = rippleNFTs.filter(nft => nft.NFTokenID === nftID);
const matchingVaults = allVaults.filter(
vault => vault.uuid.toLowerCase() === formattedUUID.toLowerCase()
);

if (matchingNFTs.length === 0) {
if (matchingVaults.length === 0) {
throw new RippleError(`Vault with UUID: ${formattedUUID} not found`);
}

if (matchingNFTs.length > 1) {
if (matchingVaults.length > 1) {
throw new RippleError(`Multiple Vaults found with UUID: ${formattedUUID}`);
}

return hexFieldsToLowercase(decodeURI(matchingNFTs[0].URI!));
return matchingVaults[0];
} catch (error) {
throw new RippleError(`Error getting Vault ${vaultUUID}: ${error}`);
}
Expand All @@ -236,17 +231,28 @@ export async function getAllRippleVaults(
try {
await connectRippleClient(rippleClient);

const getAccountNFTsRequest: AccountNFTsRequest = {
command: 'account_nfts',
account: issuerAddress,
limit: 400,
};
let marker: any = undefined;
const limit = 100;
let allRippleNFTs: any[] = [];

const {
result: { account_nfts: rippleNFTs },
} = await rippleClient.request(getAccountNFTsRequest);
do {
const getAccountNFTsRequest: AccountNFTsRequest = {
command: 'account_nfts',
account: issuerAddress,
limit,
marker,
};

const {
result: { account_nfts: rippleNFTs, marker: newMarker },
} = await rippleClient.request(getAccountNFTsRequest);

allRippleNFTs = allRippleNFTs.concat(rippleNFTs);

marker = newMarker;
} while (marker);

const rippleVaults = rippleNFTs.map(nft => hexFieldsToLowercase(decodeURI(nft.URI!)));
const rippleVaults = allRippleNFTs.map(nft => hexFieldsToLowercase(decodeURI(nft.URI!)));

if (ownerAddress) {
return rippleVaults.filter(vault => vault.creator === ownerAddress);
Expand Down
Loading

0 comments on commit 857107e

Please sign in to comment.