-
Notifications
You must be signed in to change notification settings - Fork 115
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[TRA-572] Add handler for upsert vault events to ender. (#2274)
- Loading branch information
1 parent
7faee36
commit f2fb2b9
Showing
16 changed files
with
387 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
150 changes: 150 additions & 0 deletions
150
indexer/services/ender/__tests__/handlers/upsert-vault-handler.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
import { | ||
IndexerTendermintBlock, | ||
UpsertVaultEventV1, | ||
VaultStatus, | ||
} from '@dydxprotocol-indexer/v4-protos'; | ||
import { | ||
dbHelpers, | ||
testMocks, | ||
testConstants, | ||
VaultFromDatabase, | ||
VaultTable, | ||
VaultStatus as IndexerVaultStatus, | ||
} from '@dydxprotocol-indexer/postgres'; | ||
import { KafkaMessage } from 'kafkajs'; | ||
import { createKafkaMessage } from '@dydxprotocol-indexer/kafka'; | ||
import { onMessage } from '../../src/lib/on-message'; | ||
import { DydxIndexerSubtypes } from '../../src/lib/types'; | ||
import { createIndexerTendermintBlock, createIndexerTendermintEvent } from '../helpers/indexer-proto-helpers'; | ||
import { | ||
defaultHeight, | ||
defaultPreviousHeight, | ||
defaultTime, | ||
defaultTxHash, | ||
} from '../helpers/constants'; | ||
import { updateBlockCache } from '../../src/caches/block-cache'; | ||
import { createPostgresFunctions } from '../../src/helpers/postgres/postgres-functions'; | ||
|
||
describe('upsertVaultHandler', () => { | ||
beforeAll(async () => { | ||
await dbHelpers.migrate(); | ||
await dbHelpers.clearData(); | ||
await createPostgresFunctions(); | ||
}); | ||
|
||
beforeEach(async () => { | ||
await testMocks.seedData(); | ||
updateBlockCache(defaultPreviousHeight); | ||
}); | ||
|
||
afterEach(async () => { | ||
await dbHelpers.clearData(); | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
afterAll(async () => { | ||
await dbHelpers.teardown(); | ||
jest.resetAllMocks(); | ||
}); | ||
|
||
it('should upsert new vaults in single block', async () => { | ||
const events: UpsertVaultEventV1[] = [ | ||
{ | ||
address: testConstants.defaultVaultAddress, | ||
clobPairId: 0, | ||
status: VaultStatus.VAULT_STATUS_QUOTING, | ||
}, { | ||
address: testConstants.defaultAddress, | ||
clobPairId: 1, | ||
status: VaultStatus.VAULT_STATUS_STAND_BY, | ||
}, | ||
]; | ||
const block: IndexerTendermintBlock = createBlockFromEvents( | ||
defaultHeight, | ||
...events, | ||
); | ||
const binaryBlock: Uint8Array = IndexerTendermintBlock.encode(block).finish(); | ||
const kafkaMessage: KafkaMessage = createKafkaMessage(Buffer.from(binaryBlock)); | ||
|
||
await onMessage(kafkaMessage); | ||
|
||
const vaults: VaultFromDatabase[] = await VaultTable.findAll( | ||
{}, | ||
[], | ||
{}, | ||
); | ||
expect(vaults).toHaveLength(2); | ||
expect(vaults[0]).toEqual({ | ||
address: testConstants.defaultVaultAddress, | ||
clobPairId: '0', | ||
status: IndexerVaultStatus.QUOTING, | ||
createdAt: block.time?.toISOString(), | ||
updatedAt: block.time?.toISOString(), | ||
}); | ||
expect(vaults[1]).toEqual({ | ||
address: testConstants.defaultAddress, | ||
clobPairId: '1', | ||
status: IndexerVaultStatus.STAND_BY, | ||
createdAt: block.time?.toISOString(), | ||
updatedAt: block.time?.toISOString(), | ||
}); | ||
}); | ||
|
||
it('should upsert an existing vault', async () => { | ||
await VaultTable.create(testConstants.defaultVault); | ||
|
||
const events: UpsertVaultEventV1[] = [ | ||
{ | ||
address: testConstants.defaultVaultAddress, | ||
clobPairId: 0, | ||
status: VaultStatus.VAULT_STATUS_CLOSE_ONLY, | ||
}, | ||
]; | ||
const block: IndexerTendermintBlock = createBlockFromEvents( | ||
defaultHeight, | ||
...events, | ||
); | ||
const binaryBlock: Uint8Array = IndexerTendermintBlock.encode(block).finish(); | ||
const kafkaMessage: KafkaMessage = createKafkaMessage(Buffer.from(binaryBlock)); | ||
|
||
await onMessage(kafkaMessage); | ||
|
||
const vaults: VaultFromDatabase[] = await VaultTable.findAll( | ||
{}, | ||
[], | ||
{}, | ||
); | ||
expect(vaults).toHaveLength(1); | ||
expect(vaults[0]).toEqual({ | ||
address: testConstants.defaultVault.address, | ||
clobPairId: testConstants.defaultVault.clobPairId, | ||
status: IndexerVaultStatus.CLOSE_ONLY, | ||
createdAt: testConstants.defaultVault.createdAt, | ||
updatedAt: block.time?.toISOString(), | ||
}); | ||
}); | ||
}); | ||
|
||
function createBlockFromEvents( | ||
height: number, | ||
...events: UpsertVaultEventV1[] | ||
): IndexerTendermintBlock { | ||
const transactionIndex = 0; | ||
let eventIndex = 0; | ||
|
||
return createIndexerTendermintBlock( | ||
height, | ||
defaultTime, | ||
events.map((event) => { | ||
const indexerEvent = createIndexerTendermintEvent( | ||
DydxIndexerSubtypes.UPSERT_VAULT, | ||
UpsertVaultEventV1.encode(event).finish(), | ||
transactionIndex, | ||
eventIndex, | ||
); | ||
eventIndex += 1; | ||
return indexerEvent; | ||
}), | ||
[defaultTxHash], | ||
); | ||
} |
92 changes: 92 additions & 0 deletions
92
indexer/services/ender/__tests__/validators/upsert-vault-validator.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import { ParseMessageError, logger } from '@dydxprotocol-indexer/base'; | ||
import { | ||
IndexerTendermintBlock, | ||
IndexerTendermintEvent, | ||
UpsertVaultEventV1, | ||
VaultStatus, | ||
} from '@dydxprotocol-indexer/v4-protos'; | ||
import { dbHelpers, testConstants, testMocks } from '@dydxprotocol-indexer/postgres'; | ||
import { DydxIndexerSubtypes } from '../../src/lib/types'; | ||
import { defaultHeight, defaultTime, defaultTxHash } from '../helpers/constants'; | ||
import { | ||
createIndexerTendermintBlock, | ||
createIndexerTendermintEvent, | ||
} from '../helpers/indexer-proto-helpers'; | ||
import { expectDidntLogError } from '../helpers/validator-helpers'; | ||
import { createPostgresFunctions } from '../../src/helpers/postgres/postgres-functions'; | ||
import { UpsertVaultValidator } from '../../src/validators/upsert-vault-validator'; | ||
|
||
describe('upsert-vault-validator', () => { | ||
beforeAll(async () => { | ||
await dbHelpers.migrate(); | ||
await createPostgresFunctions(); | ||
}); | ||
|
||
beforeEach(async () => { | ||
await testMocks.seedData(); | ||
jest.spyOn(logger, 'error'); | ||
}); | ||
|
||
afterEach(async () => { | ||
await dbHelpers.clearData(); | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
afterAll(async () => { | ||
await dbHelpers.teardown(); | ||
jest.resetAllMocks(); | ||
}); | ||
|
||
describe('validate', () => { | ||
it('does not throw error on valid uspert vault event', () => { | ||
const event: UpsertVaultEventV1 = { | ||
address: testConstants.defaultVaultAddress, | ||
clobPairId: 0, | ||
status: VaultStatus.VAULT_STATUS_QUOTING, | ||
}; | ||
const validator: UpsertVaultValidator = new UpsertVaultValidator( | ||
event, | ||
createBlock(event), | ||
0, | ||
); | ||
|
||
validator.validate(); | ||
expectDidntLogError(); | ||
}); | ||
|
||
it('throws error if address in event is empty', () => { | ||
const event: UpsertVaultEventV1 = { | ||
address: '', | ||
clobPairId: 0, | ||
status: VaultStatus.VAULT_STATUS_QUOTING, | ||
}; | ||
const validator: UpsertVaultValidator = new UpsertVaultValidator( | ||
event, | ||
createBlock(event), | ||
0, | ||
); | ||
|
||
expect(() => validator.validate()).toThrow(new ParseMessageError( | ||
'UpsertVaultEvent address is not populated', | ||
)); | ||
}); | ||
}); | ||
}); | ||
|
||
function createBlock( | ||
upsertVaultEvent: UpsertVaultEventV1, | ||
): IndexerTendermintBlock { | ||
const event: IndexerTendermintEvent = createIndexerTendermintEvent( | ||
DydxIndexerSubtypes.UPSERT_VAULT, | ||
UpsertVaultEventV1.encode(upsertVaultEvent).finish(), | ||
0, | ||
0, | ||
); | ||
|
||
return createIndexerTendermintBlock( | ||
defaultHeight, | ||
defaultTime, | ||
[event], | ||
[defaultTxHash], | ||
); | ||
} |
18 changes: 18 additions & 0 deletions
18
indexer/services/ender/src/handlers/upsert-vault-handler.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { UpsertVaultEventV1 } from '@dydxprotocol-indexer/v4-protos'; | ||
import * as pg from 'pg'; | ||
|
||
import { ConsolidatedKafkaEvent } from '../lib/types'; | ||
import { Handler } from './handler'; | ||
|
||
export class UpsertVaultHandler extends Handler<UpsertVaultEventV1> { | ||
eventType: string = 'UpsertVaultEventV1'; | ||
|
||
public getParallelizationIds(): string[] { | ||
return []; | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/require-await | ||
public async internalHandle(_: pg.QueryResultRow): Promise<ConsolidatedKafkaEvent[]> { | ||
return []; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.