Skip to content

Commit

Permalink
chore: iterate through all owner objects to find target
Browse files Browse the repository at this point in the history
`getOwnedObjects` returns a limited number of objects per page (50 by
default), and our target might not be found in the first one. That's why
we should iterate through all pages of the result until no next page is
found, or we track our target object.
  • Loading branch information
sdaveas committed Aug 14, 2024
1 parent 46cff96 commit 7175992
Showing 1 changed file with 17 additions and 8 deletions.
25 changes: 17 additions & 8 deletions sui/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,14 +146,23 @@ const getSigners = async (keypair, config, chain, options) => {
};

const findOwnedObjectId = async (client, ownerAddress, objectType) => {
const ownedObjects = await client.getOwnedObjects({
owner: ownerAddress,
options: {
showContent: true,
},
});

const targetObject = ownedObjects.data.find(({ data }) => data.content.type === objectType);
var hasNextPage = true;
var nextCursor = null;
var targetObject = null;

while (hasNextPage && targetObject == null) {
var ownedObjects = await client.getOwnedObjects({
owner: ownerAddress,
options: {
showContent: true,
},
cursor: nextCursor,
});

targetObject = ownedObjects.data.find(({ data }) => data.content.type === objectType);
hasNextPage = ownedObjects.hasNextPage;
nextCursor = ownedObjects.nextCursor;
}

if (!targetObject) {
throw new Error(`No object found for type: ${objectType}`);
Expand Down

0 comments on commit 7175992

Please sign in to comment.