Skip to content

Commit

Permalink
Added gov proposal version.
Browse files Browse the repository at this point in the history
  • Loading branch information
NoahSaso committed Jan 6, 2024
1 parent 2e8013b commit a623b71
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 9 deletions.
14 changes: 10 additions & 4 deletions src/core/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down Expand Up @@ -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<string, Record<string, any>>
)
Expand Down
1 change: 1 addition & 0 deletions src/core/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,7 @@ export type ParsedGovStateEvent = {
blockTimeUnixMs: string
blockTimestamp: Date
value: Record<string, any>
version: string
}

type RequireAtLeastOne<T, Keys extends keyof T = keyof T> = Pick<
Expand Down
Original file line number Diff line number Diff line change
@@ -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,
})
},
Expand Down
16 changes: 16 additions & 0 deletions src/db/migrations/20240106235549-add-version-to-gov-state-event.ts
Original file line number Diff line number Diff line change
@@ -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')
},
}
6 changes: 5 additions & 1 deletion src/db/models/GovStateEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
9 changes: 7 additions & 2 deletions src/scripts/export/handlers/gov.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,14 @@ export const gov: HandlerMaker<ParsedGovStateEvent> = 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
}
Expand All @@ -73,17 +76,19 @@ export const gov: HandlerMaker<ParsedGovStateEvent> = async () => {
blockTimeUnixMs,
blockTimestamp,
value,
version,
}
}

const process: Handler<ParsedGovStateEvent>['process'] = async (events) => {
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'],
})
: []

Expand Down

0 comments on commit a623b71

Please sign in to comment.