Skip to content

Commit

Permalink
feat: create tx id column in smart contract event (main) (#651) TG-16…
Browse files Browse the repository at this point in the history
…6 #waiting

* feat: create tx_id column in smart_contract_event

* fix: update query smart_contract_event join event only; remove knex.transaction

* feat: add tx_id to smart_contract_event in hasura metadata
  • Loading branch information
fibonacci998 authored Feb 16, 2024
1 parent 3257cac commit 557a79f
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,13 @@ select_permissions:
- role: internal_service
permission:
columns:
- event_id
- action
- created_at
- event_id
- id
- index
- smart_contract_id
- created_at
- tx_id
- updated_at
filter: {}
limit: 100
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { Knex } from 'knex';

export async function up(knex: Knex): Promise<void> {
console.log('create tx_id column in smart_contract_event');
const chunkSizeQuery = 10000;
let startId = 0;

await knex.schema.alterTable('smart_contract_event', (table) => {
table.integer('tx_id').index();
});
let done = false;
while (!done) {
console.log(`update tx_id column in smart_contract_event at id ${startId}`);
const smartContractEvents = await knex.raw(
`select smart_contract_event.id smart_contract_event_id,
event.tx_id transaction_id from smart_contract_event
join event on event.id = smart_contract_event.event_id
where smart_contract_event.id > ${startId}
order by smart_contract_event.id asc
limit ${chunkSizeQuery};`
);
if (smartContractEvents.rows.length === 0) {
done = true;
break;
}
const stringListUpdates = smartContractEvents.rows
.map(
(update: any) =>
`(${update.smart_contract_event_id}, ${update.transaction_id})`
)
.join(',');
await knex.raw(
`UPDATE smart_contract_event SET tx_id = temp.tx_id from (VALUES ${stringListUpdates}) as temp(id, tx_id) where temp.id = smart_contract_event.id`
);
startId =
smartContractEvents.rows[smartContractEvents.rows.length - 1]
.smart_contract_event_id;
}
}

export async function down(knex: Knex): Promise<void> {
await knex.schema.alterTable('smart_contract_event', (table) => {
table.dropColumn('tx_id');
});
}
2 changes: 1 addition & 1 deletion src/common/utils/smart_contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export async function getContractActivities(
const contractActivities: IContractMsgInfo[] = [];
const wasmEvents = await Event.query()
.alias('event')
.withGraphFetched('[attributes(selectAttribute)]')
.withGraphFetched('[attributes(selectAttribute), transaction]')
.modifiers({
selectAttribute(builder) {
builder.select('event_id', 'index', 'key', 'value').orderBy([
Expand Down
3 changes: 3 additions & 0 deletions src/models/smart_contract_event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ export class SmartContractEvent extends BaseModel {

index!: number;

tx_id!: number;

static get tableName() {
return 'smart_contract_event';
}
Expand All @@ -33,6 +35,7 @@ export class SmartContractEvent extends BaseModel {
smart_contract_id: { type: 'number' },
event_id: { type: 'number' },
index: { type: 'number' },
tx_id: { type: 'number' },
},
};
}
Expand Down
2 changes: 2 additions & 0 deletions src/services/crawl-cosmwasm/crawl_contract_event.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ export default class CrawlContractEventService extends BullableService {
action: contractEvent.action,
event_id: contractEvent.event_id,
index: contractEvent.index,
tx_id: contractEvent.tx.id,
});
const smartContractId =
contractByAddress[contractEvent.contractAddress].id;
Expand All @@ -88,6 +89,7 @@ export default class CrawlContractEventService extends BullableService {
action: contractEvent.action,
event_id: contractEvent.event_id,
index: contractEvent.index,
tx_id: contractEvent.tx.id,
}),
attributes: contractEvent.attributes,
})
Expand Down
2 changes: 1 addition & 1 deletion test/unit/services/cw20/cw20.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ export default class Cw20 {
this.cw20UpdateByContractService.getQueueManager().stopAll();
await this.broker.start();
await knex.raw(
'TRUNCATE TABLE code, cw20_contract, block, transaction, smart_contract_event, block_checkpoint RESTART IDENTITY CASCADE'
'TRUNCATE TABLE code, cw20_contract, block, transaction, event, event_attribute, smart_contract_event, block_checkpoint RESTART IDENTITY CASCADE'
);
await Block.query().insert(this.block);
await Transaction.query().insertGraph(this.txInsert);
Expand Down

0 comments on commit 557a79f

Please sign in to comment.