Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: getLiveCells returns specified change type cells #183

Merged
merged 2 commits into from
Apr 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -111,14 +111,38 @@ describe('FullOwnership', () => {
});
});

it('should get live cells', async () => {
it('should get live cells with cursor', async () => {
await ownershipService.getLiveCells({ cursor: '3:0x1234' });
expect(backend.getLiveCellsByLocks).toBeCalledWith({
cursor: '0x1234',
locks: [scriptInfos[3].lock],
});
});

it('should get live cells with default change', async () => {
await ownershipService.getLiveCells({});
expect(backend.getLiveCellsByLocks).toBeCalledWith({
cursor: '',
locks: [scriptInfos[1].lock, scriptInfos[3].lock],
});
});

it('should get external live cells with change set to external', async () => {
await ownershipService.getLiveCells({ change: 'external' });
expect(backend.getLiveCellsByLocks).toBeCalledWith({
cursor: '',
locks: [scriptInfos[1].lock],
});
});

it('should get internal live cells with change set to internal', async () => {
await ownershipService.getLiveCells({ change: 'internal' });
expect(backend.getLiveCellsByLocks).toBeCalledWith({
cursor: '',
locks: [scriptInfos[3].lock],
});
});

it('should signData by keystore service with proper params', async () => {
await ownershipService.signData({ data: '0x1234', lock: scriptInfos[0].lock, url: MOCK_PLATFORM_URL });
expect(keystoreService.signMessage).toBeCalledWith({
Expand Down
23 changes: 14 additions & 9 deletions packages/extension-chrome/src/services/ownership/fullOwnership.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,22 +42,27 @@ export function createFullOwnershipService({
}

return {
getLiveCells: async ({ cursor: encodedCursor } = {}) => {
getLiveCells: async ({ cursor: encodedCursor, change } = {}) => {
const db = await getDb();
const backend = await backendProvider.resolve();

const infos = await db.getAll();

const queryCursor: CellCursor = encodedCursor ? decodeCursor(encodedCursor) : { indexerCursor: '', localId: 0 };

const onChainLocks = infos
.filter(
(info) =>
info.status === 'OnChain' &&
// only fetch cells after or containing this lock
info.id >= queryCursor.localId,
)
.map((info) => info.lock);
let onChainInfos = infos.filter(
(info) =>
info.status === 'OnChain' &&
// only fetch cells after or containing this lock
info.id >= queryCursor.localId,
);

// by default, will return both `external` and `internal` cells. If `change` is specified, will only return cells of the specified type.
if (change) {
onChainInfos = onChainInfos.filter(filterByChange(change));
}

const onChainLocks = onChainInfos.map((info) => info.lock);

const { objects, cursor, lastLock } = await backend.getLiveCellsByLocks({
locks: onChainLocks,
Expand Down