diff --git a/src/handlers/OwnershipTransferred.ts b/src/handlers/OwnershipTransferred.ts index ffad492..38c5543 100644 --- a/src/handlers/OwnershipTransferred.ts +++ b/src/handlers/OwnershipTransferred.ts @@ -9,11 +9,10 @@ export default class OwnershipTransferred implements Handler { async handle(log: Log, _block: BlockData, ctx: DataHandlerContext) { if (log.topics[0] === events.OwnershipTransferred.topic) { - const appId = log.transaction?.to?.toLowerCase(); - const application = appId - ? this.applicationStorage.get(appId) ?? - (await ctx.store.get(Application, appId)) - : undefined; + const appId = log.transaction?.to?.toLowerCase() ?? ''; + const application = + this.applicationStorage.get(appId) ?? + (await ctx.store.get(Application, appId)); if (application) { // decode event diff --git a/tests/handlers/OwnershipTransferred.test.ts b/tests/handlers/OwnershipTransferred.test.ts index dd978c6..f528999 100644 --- a/tests/handlers/OwnershipTransferred.test.ts +++ b/tests/handlers/OwnershipTransferred.test.ts @@ -1,6 +1,7 @@ import { beforeEach, describe, expect, test, vi } from 'vitest'; import OwnerShipTransferred from '../../src/handlers/OwnershipTransferred'; +import { Application } from '../../src/model'; import { block, ctx, logs } from '../stubs/params'; vi.mock('../../src/model/', async (importOriginal) => { @@ -14,7 +15,7 @@ vi.mock('../../src/model/', async (importOriginal) => { describe('ApplicationCreated', () => { let ownershipTransferred: OwnerShipTransferred; - const mockApplicationStorage = new Map(); + const mockApplicationStorage = new Map(); beforeEach(() => { ownershipTransferred = new OwnerShipTransferred(mockApplicationStorage); mockApplicationStorage.clear(); @@ -34,6 +35,7 @@ describe('ApplicationCreated', () => { await ownershipTransferred.handle(logs[0], block, ctx); expect(mockApplicationStorage.size).toBe(0); }); + test('Ownership Transferred', async () => { const mockApplicationStorage2 = new Map(); const appId = logs[2].transaction.to; @@ -50,5 +52,27 @@ describe('ApplicationCreated', () => { '0xf05d57a5bed2d1b529c56001fc5810cc9afc0335', ); }); + + test('should find application in the database and make the ownership transfer', async () => { + vi.spyOn(ctx.store, 'get').mockResolvedValueOnce({ + id: logs[2].transaction.to, + owner: '0xf05d57a5bed2d1b529c56001fc5810cc9afc0335', + factory: { + id: '0x7122cd1221c20892234186facfe8615e6743ab02', + }, + } as Application); + + await ownershipTransferred.handle(logs[2], block, ctx); + + expect(mockApplicationStorage.size).toBe(1); + const app = mockApplicationStorage.get(logs[2].transaction.to); + + expect(app?.id).toEqual( + '0x7122cd1221c20892234186facfe8615e6743ab02', + ); + expect(app?.owner).toEqual( + '0x96ae2ecbfde74b1ec55e9cf626ee80e4f64c8a63', + ); + }); }); });