Skip to content

Commit fe01e9e

Browse files
authored
Merge pull request #129 from hack-a-chain-software/streaming-fix
fix: streaming type issues; added balanceId in guards table
2 parents 071c3f3 + a74dd03 commit fe01e9e

File tree

6 files changed

+39
-54
lines changed

6 files changed

+39
-54
lines changed

indexer/migrations/20241105001040-add-indexes-to-guards.js

Lines changed: 0 additions & 26 deletions
This file was deleted.

indexer/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
"devDependencies": {
4444
"@types/chai": "^4.3.17",
4545
"@types/cors": "^2.8.17",
46+
"@types/eventsource": "^1.1.15",
4647
"@types/express": "^4.17.21",
4748
"@types/jest": "^29.5.12",
4849
"@types/mocha": "^10.0.7",
@@ -74,4 +75,4 @@
7475
"migrate:up": "dotenv -e .env npx sequelize-cli db:migrate",
7576
"migrate:down": "dotenv -e .env npx sequelize-cli db:migrate:undo"
7677
}
77-
}
78+
}

indexer/src/models/guard.ts

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { Model, DataTypes, Optional } from "sequelize";
22
import { sequelize } from "../config/database";
3+
import Balance from "./balance";
34

45
export interface GuardAttributes {
56
id: number;
67
publicKey: string;
7-
chainId: string;
8-
account: string;
98
predicate: string;
9+
balanceId: number;
1010
}
1111

1212
interface GuardCreationAttributes extends Optional<GuardAttributes, "id"> {}
@@ -17,9 +17,8 @@ class Guard
1717
{
1818
public id!: number;
1919
public publicKey!: string;
20-
public chainId!: string;
21-
public account!: string;
2220
public predicate!: string;
21+
public balanceId!: number;
2322
}
2423

2524
Guard.init(
@@ -35,29 +34,34 @@ Guard.init(
3534
allowNull: false,
3635
comment: "The public key associated with the account",
3736
},
38-
chainId: {
39-
type: DataTypes.INTEGER,
40-
allowNull: false,
41-
comment: "The chain ID associated with the account",
42-
},
43-
account: {
44-
type: DataTypes.STRING,
45-
allowNull: false,
46-
comment: "The account associated with the public key",
47-
},
4837
predicate: {
4938
type: DataTypes.STRING,
5039
allowNull: false,
5140
comment:
5241
"The predicate associated with the account, public key and chain",
5342
},
43+
balanceId: {
44+
type: DataTypes.INTEGER,
45+
allowNull: true,
46+
comment: "The ID of the associated balance (e.g., 204).",
47+
},
5448
},
5549
{
5650
sequelize,
5751
modelName: "Guard",
58-
tableName: "Guards", // optional, specifies the table name
59-
timestamps: true, // automatically adds `createdAt` and `updatedAt` fields
52+
tableName: "Guards",
53+
indexes: [
54+
{
55+
name: "guards_publickey_idx",
56+
fields: ["publicKey"],
57+
},
58+
],
6059
},
6160
);
6261

62+
Guard.belongsTo(Balance, {
63+
foreignKey: "balanceId",
64+
as: "balance",
65+
});
66+
6367
export default Guard;

indexer/src/services/sync/payload.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,10 @@ import TransactionModel, {
55
import Event, { EventAttributes } from "../../models/event";
66
import Transfer, { TransferAttributes } from "../../models/transfer";
77
import { getNftTransfers, getCoinTransfers } from "./transfers";
8-
import { Op, QueryTypes, Transaction } from "sequelize";
8+
import { QueryTypes, Transaction } from "sequelize";
99
import Signer from "../../models/signer";
1010
import Guard, { GuardAttributes } from "../../models/guard";
1111
import { handleSingleQuery } from "../../kadena-server/utils/raw-query";
12-
import Balance from "../../models/balance";
1312
import { sequelize } from "../../config/database";
1413

1514
const TRANSACTION_INDEX = 0;
@@ -199,7 +198,7 @@ export async function processTransaction(
199198
}
200199

201200
async function saveGuards(balances: BalanceInsertResult[], tx?: Transaction) {
202-
const guardPromises: Array<Promise<GuardAttributes | null>> = balances.map(
201+
const guardPromises: Array<Promise<any | null>> = balances.map(
203202
async (balance) => {
204203
const res = await handleSingleQuery({
205204
chainId: balance.chainId.toString(),
@@ -210,9 +209,8 @@ async function saveGuards(balances: BalanceInsertResult[], tx?: Transaction) {
210209

211210
const result = JSON.parse(res.result ?? "{}");
212211
const withKeys = (result.guard.keys ?? []).map((key: any) => ({
212+
balanceId: balance.id,
213213
account: balance.account,
214-
chainId: balance.chainId,
215-
fungible: balance.module,
216214
publicKey: key,
217215
predicate: result.guard.pred,
218216
}));
@@ -224,9 +222,12 @@ async function saveGuards(balances: BalanceInsertResult[], tx?: Transaction) {
224222
const guards = await Promise.all(guardPromises);
225223
const filteredGuards = guards
226224
.flat()
227-
.filter(
228-
(g) => g !== null && `k:${g.publicKey}` !== g.account,
229-
) as GuardAttributes[];
225+
.filter((g) => g !== null && `k:${g.publicKey}` !== g.account)
226+
.map((g) => ({
227+
balanceId: g.balanceId,
228+
publicKey: g.publicKey,
229+
predicate: g.predicate,
230+
}));
230231

231232
await Guard.bulkCreate(filteredGuards, {
232233
transaction: tx,

indexer/src/services/sync/streaming.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { processPayloadKey } from "./payload";
22
import { getDecoded, getRequiredEnvString } from "../../utils/helpers";
3-
import { EventSource } from "eventsource";
3+
import EventSource from "eventsource";
44
import { DispatchInfo } from "../../jobs/publisher-job";
55
import { uint64ToInt64 } from "../../utils/int-uint-64";
66
import Block, { BlockAttributes } from "../../models/block";
@@ -19,11 +19,11 @@ export async function startStreaming() {
1919
`${SYNC_BASE_URL}/${SYNC_NETWORK}/block/updates`,
2020
);
2121

22-
eventSource.onerror = (error) => {
22+
eventSource.onerror = (error: any) => {
2323
console.error("Connection error:", error);
2424
};
2525

26-
eventSource.addEventListener("BlockHeader", (event) => {
26+
eventSource.addEventListener("BlockHeader", (event: any) => {
2727
try {
2828
const block = JSON.parse(event.data);
2929
const payload = processPayload(block.payloadWithOutputs);

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4467,6 +4467,11 @@
44674467
resolved "https://registry.yarnpkg.com/@types/estree/-/estree-1.0.5.tgz#a6ce3e556e00fd9895dd872dd172ad0d4bd687f4"
44684468
integrity sha512-/kYRxGDLWzHOB7q+wtSUQlFrtcdUccpfy+X+9iMBpHK8QLLhx2wIPYuS5DYtR9Wa/YlZAbIovy7qVdB1Aq6Lyw==
44694469

4470+
"@types/eventsource@^1.1.15":
4471+
version "1.1.15"
4472+
resolved "https://registry.yarnpkg.com/@types/eventsource/-/eventsource-1.1.15.tgz#949383d3482e20557cbecbf3b038368d94b6be27"
4473+
integrity sha512-XQmGcbnxUNa06HR3VBVkc9+A2Vpi9ZyLJcdS5dwaQQ/4ZMWFO+5c90FnMUpbtMZwB/FChoYHwuVg8TvkECacTA==
4474+
44704475
"@types/express-serve-static-core@^4.17.30":
44714476
version "4.19.6"
44724477
resolved "https://registry.yarnpkg.com/@types/express-serve-static-core/-/express-serve-static-core-4.19.6.tgz#e01324c2a024ff367d92c66f48553ced0ab50267"

0 commit comments

Comments
 (0)