Skip to content

Commit

Permalink
fix: return user specified change when getLiveCells
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangyouxin committed Apr 3, 2023
1 parent 45d4787 commit c244cba
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -111,14 +111,30 @@ describe('FullOwnership', () => {
});
});

it('should get live cells', async () => {
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

0 comments on commit c244cba

Please sign in to comment.