From a623b71829be2bb459ece7ef840dbf4175c96cf5 Mon Sep 17 00:00:00 2001 From: Noah Saso Date: Sat, 6 Jan 2024 15:55:08 -0800 Subject: [PATCH] Added gov proposal version. --- src/core/env.ts | 14 ++++++++++---- src/core/types.ts | 1 + ...-state-event-balance-type-bigint-to-string.ts | 5 +++-- ...40106235549-add-version-to-gov-state-event.ts | 16 ++++++++++++++++ src/db/models/GovStateEvent.ts | 6 +++++- src/scripts/export/handlers/gov.ts | 9 +++++++-- 6 files changed, 42 insertions(+), 9 deletions(-) create mode 100644 src/db/migrations/20240106235549-add-version-to-gov-state-event.ts diff --git a/src/core/env.ts b/src/core/env.ts index 6ec1dbf7..de6e4da3 100644 --- a/src/core/env.ts +++ b/src/core/env.ts @@ -1137,7 +1137,10 @@ export const getEnv = ({ // Call hook. await onFetch?.([event]) - return event.value + return { + ...event.value, + version: event.version, + } } const getProposals: FormulaProposalsGetter = async () => { @@ -1199,11 +1202,14 @@ export const getEnv = ({ // Call hook. await onFetch?.(events) - // Create denom balance map. + // Create proposal map. return events.reduce( - (acc, { proposalId, value }) => ({ + (acc, { proposalId, value, version }) => ({ ...acc, - [proposalId]: value, + [proposalId]: { + ...value, + version, + }, }), {} as Record> ) diff --git a/src/core/types.ts b/src/core/types.ts index e95cceae..34253664 100644 --- a/src/core/types.ts +++ b/src/core/types.ts @@ -482,6 +482,7 @@ export type ParsedGovStateEvent = { blockTimeUnixMs: string blockTimestamp: Date value: Record + version: string } type RequireAtLeastOne = Pick< diff --git a/src/db/migrations/20230930091910-alter-bank-state-event-balance-type-bigint-to-string.ts b/src/db/migrations/20230930091910-alter-bank-state-event-balance-type-bigint-to-string.ts index e8e41113..635bf958 100644 --- a/src/db/migrations/20230930091910-alter-bank-state-event-balance-type-bigint-to-string.ts +++ b/src/db/migrations/20230930091910-alter-bank-state-event-balance-type-bigint-to-string.ts @@ -1,16 +1,17 @@ import { QueryInterface } from 'sequelize' +import { DataType } from 'sequelize-typescript' module.exports = { async up(queryInterface: QueryInterface) { await queryInterface.changeColumn('BankStateEvents', 'balance', { - type: 'text', + type: DataType.TEXT, allowNull: false, }) }, async down(queryInterface: QueryInterface) { await queryInterface.changeColumn('BankStateEvents', 'balance', { - type: 'bigint', + type: DataType.BIGINT, allowNull: false, }) }, diff --git a/src/db/migrations/20240106235549-add-version-to-gov-state-event.ts b/src/db/migrations/20240106235549-add-version-to-gov-state-event.ts new file mode 100644 index 00000000..00c4b9db --- /dev/null +++ b/src/db/migrations/20240106235549-add-version-to-gov-state-event.ts @@ -0,0 +1,16 @@ +import { QueryInterface } from 'sequelize' +import { DataType } from 'sequelize-typescript' + +module.exports = { + async up(queryInterface: QueryInterface) { + await queryInterface.addColumn('GovStateEvents', 'version', { + type: DataType.STRING, + allowNull: false, + defaultValue: 'v1', + }) + }, + + async down(queryInterface: QueryInterface) { + await queryInterface.removeColumn('GovStateEvents', 'version') + }, +} diff --git a/src/db/models/GovStateEvent.ts b/src/db/models/GovStateEvent.ts index 05d9e50d..3ec2a663 100644 --- a/src/db/models/GovStateEvent.ts +++ b/src/db/models/GovStateEvent.ts @@ -47,10 +47,14 @@ export class GovStateEvent extends DependendableEventModel { @Column(DataType.DATE) blockTimestamp!: Date - @AllowNull + @AllowNull(false) @Column(DataType.JSONB) value!: any + @AllowNull(false) + @Column(DataType.STRING) + version!: string + get block(): Block { return { height: BigInt(this.blockHeight), diff --git a/src/scripts/export/handlers/gov.ts b/src/scripts/export/handlers/gov.ts index 427ebfb0..872f575a 100644 --- a/src/scripts/export/handlers/gov.ts +++ b/src/scripts/export/handlers/gov.ts @@ -56,11 +56,14 @@ export const gov: HandlerMaker = async () => { // Attempt v1 decoding, falling back to v1beta1. If both fail, ignore. let value: any + let version try { value = ProposalV1.toJSON(ProposalV1.decode(valueData)) + version = 'v1' } catch { try { value = ProposalV1Beta1.toJSON(ProposalV1Beta1.decode(valueData)) + version = 'v1beta1' } catch { return } @@ -73,6 +76,7 @@ export const gov: HandlerMaker = async () => { blockTimeUnixMs, blockTimestamp, value, + version, } } @@ -80,10 +84,11 @@ export const gov: HandlerMaker = async () => { const exportEvents = async () => // Unique index on [blockHeight, proposalId] ensures that we don't insert // duplicate events. If we encounter a duplicate, we update the `value` - // field in case event processing for a block was batched separately. + // and `version` fields in case event processing for a block was batched + // separately. events.length > 0 ? await GovStateEvent.bulkCreate(events, { - updateOnDuplicate: ['value'], + updateOnDuplicate: ['value', 'version'], }) : []