Skip to content

Commit 04c97f3

Browse files
authored
Merge pull request #500 from hack-a-chain-software/develop
Merge 'develop' into 'main'
2 parents 083cf06 + 9424bc8 commit 04c97f3

File tree

185 files changed

+50742
-9346
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

185 files changed

+50742
-9346
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ dist/
33
data/
44
draft/
55
.env
6+
.env.testing
7+
.env.mainnet01
8+
.env.testnet04
69
.terraform
710
.terraform.lock.hcl
811
.terraform.tfstate.lock.info
@@ -12,3 +15,4 @@ draft/
1215
indexer/indexer
1316
.cursor
1417
indexer/tests/logs/*
18+
indexer/tests/integration/api-parity/logs/*

CHANGELOG.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,41 @@
11
# Changelog
22

3+
## Aug 13 - Sep 11, 2025
4+
5+
- fix: remove unnecessary error throwing in dataloaders
6+
- refactor: removed CORS origin handler
7+
- fix: remove token price cache, causing inaccurate usd prices
8+
- feat: added errors monitoring for Kadena report
9+
- fix: liquidity can be object or number
10+
- fix: reverted orphan blocks algorithm behavior
11+
- refactor: changed indexes approach and graphql resolvers to make transfers query faster for all use cases
12+
- fix: tvl history query to better capture daily tvl values
13+
- refactor: improved transactions count when using minHeight and maxHeight
14+
- fix: added hasTokenId to all transfers fields and their counters
15+
- feat: add optional code field to TransactionSummary and related repository updates
16+
- refactor: improved blocks-from-height query
17+
- feat: optional port for migration
18+
- fix: created a missing migration to add an index on the module and chainId columns in the Events table
19+
- feat: added module as a new param in events query and improved the indexes to make the query performance better
20+
- fix: dex metrics tvl history query
21+
- refactor: transactions query improvement
22+
- refactor: separated pairCreation process from indexing
23+
- feat: created a query to get the token price based on its latest swap
24+
- feat: allowing to query holders of certain fungible module
25+
- fix: manually upsert poolstats to not overwrite db transaction
26+
- fix: added badResult field to the transactions-by-pact-code query and changed the creationTime field to DateTime type
27+
- fix: created a migration to add an index on the code column in the TransactionDetails table
28+
- refactor: added token id field in the transfers query for the case where isNft = true
29+
- refactor: started to track RECONCILE events and transform them in NFT transfers
30+
- refactor: changed the totalGasUsed field name to totalGasUsedInKda
31+
- refactor: created the transactionsByPactCode query to avoid performance issues
32+
- feat: balances queries
33+
- feat: added counters to the network info query
34+
- feat: added total count in blocksFromHeight query
35+
- fix: adjusted orphaned transactions field to use the canonical flag
36+
- feat: added total gas used in block type of graphql
37+
- tests: improved api parity tests to use random block hashes in queries like block, transactions, transfers and events
38+
339
## Jul 28 - Aug 13, 2025
440

541
- refactor: removed unnecessary environment variables and made some previously required variables optional
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
package main
2+
3+
import (
4+
"database/sql"
5+
"flag"
6+
"fmt"
7+
"go-backfill/config"
8+
"log"
9+
10+
_ "github.com/lib/pq" // PostgreSQL driver
11+
)
12+
13+
const (
14+
creationTimeBatchSizeEvents = 500
15+
startTransactionIdEvents = 1
16+
endTransactionIdEvents = 1000
17+
)
18+
19+
// This script was created to duplicate the creation time of transaction to the events table.
20+
// The main motivation was to improve the performance of the events query.
21+
22+
func updateCreationTimesForEvents() error {
23+
envFile := flag.String("env", ".env", "Path to the .env file")
24+
flag.Parse()
25+
config.InitEnv(*envFile)
26+
env := config.GetConfig()
27+
connStr := fmt.Sprintf("host=%s port=%s user=%s password=%s dbname=%s sslmode=disable",
28+
env.DbHost, env.DbPort, env.DbUser, env.DbPassword, env.DbName)
29+
30+
db, err := sql.Open("postgres", connStr)
31+
if err != nil {
32+
return fmt.Errorf("failed to connect to database: %v", err)
33+
}
34+
defer db.Close()
35+
36+
log.Println("Connected to database")
37+
38+
// Test database connection
39+
if err := db.Ping(); err != nil {
40+
return fmt.Errorf("failed to ping database: %v", err)
41+
}
42+
43+
// Process transactions in batches
44+
if err := processTransactionsBatchForEvents(db); err != nil {
45+
return fmt.Errorf("failed to process transactions: %v", err)
46+
}
47+
48+
log.Println("Successfully updated all events creation times")
49+
return nil
50+
}
51+
52+
func processTransactionsBatchForEvents(db *sql.DB) error {
53+
currentId := startTransactionIdEvents
54+
totalProcessed := 0
55+
totalTransactions := endTransactionIdEvents - startTransactionIdEvents + 1
56+
lastProgressPrinted := -1.0
57+
58+
log.Printf("Starting to process transactions from ID %d to %d",
59+
startTransactionIdEvents, endTransactionIdEvents)
60+
log.Printf("Total transactions to process: %d", totalTransactions)
61+
62+
for currentId <= endTransactionIdEvents {
63+
// Calculate batch end
64+
batchEnd := currentId + creationTimeBatchSizeEvents - 1
65+
if batchEnd > endTransactionIdEvents {
66+
batchEnd = endTransactionIdEvents
67+
}
68+
69+
// Process this batch
70+
processed, err := processBatchForEvents(db, currentId, batchEnd)
71+
if err != nil {
72+
return fmt.Errorf("failed to process batch %d-%d: %v", currentId, batchEnd, err)
73+
}
74+
75+
totalProcessed += processed
76+
77+
// Calculate progress percentage
78+
transactionsProcessed := batchEnd - startTransactionIdEvents + 1
79+
progressPercent := (float64(transactionsProcessed) / float64(totalTransactions)) * 100.0
80+
81+
// Only print progress if it has increased by at least 0.1%
82+
if progressPercent-lastProgressPrinted >= 0.1 {
83+
log.Printf("Progress: %.1f%%", progressPercent)
84+
lastProgressPrinted = progressPercent
85+
}
86+
87+
// Move to next batch
88+
currentId = batchEnd + 1
89+
}
90+
91+
log.Printf("Completed processing. Total events updated: %d (100.0%%)", totalProcessed)
92+
return nil
93+
}
94+
95+
func processBatchForEvents(db *sql.DB, startId, endId int) (int, error) {
96+
// Begin transaction for atomic operation
97+
tx, err := db.Begin()
98+
if err != nil {
99+
return 0, fmt.Errorf("failed to begin transaction: %v", err)
100+
}
101+
defer tx.Rollback() // Will be ignored if tx.Commit() succeeds
102+
103+
// Update events with creation time from transactions in a single query
104+
updateQuery := `
105+
UPDATE "Events"
106+
SET creationtime = t.creationtime, "updatedAt" = CURRENT_TIMESTAMP
107+
FROM "Transactions" t
108+
WHERE "Events"."transactionId" = t.id
109+
AND t.id >= $1 AND t.id <= $2
110+
`
111+
112+
result, err := tx.Exec(updateQuery, startId, endId)
113+
if err != nil {
114+
return 0, fmt.Errorf("failed to update events: %v", err)
115+
}
116+
117+
rowsAffected, err := result.RowsAffected()
118+
if err != nil {
119+
return 0, fmt.Errorf("failed to get rows affected: %v", err)
120+
}
121+
122+
// Commit the transaction
123+
if err := tx.Commit(); err != nil {
124+
return 0, fmt.Errorf("failed to commit transaction: %v", err)
125+
}
126+
127+
return int(rowsAffected), nil
128+
}
129+
130+
func creationTimesEvents() {
131+
if err := updateCreationTimesForEvents(); err != nil {
132+
log.Fatalf("Error: %v", err)
133+
}
134+
}
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
package main
2+
3+
import (
4+
"database/sql"
5+
"flag"
6+
"fmt"
7+
"go-backfill/config"
8+
"log"
9+
10+
_ "github.com/lib/pq" // PostgreSQL driver
11+
)
12+
13+
const (
14+
creationTimeBatchSize = 500
15+
startTransactionId = 1
16+
endTransactionId = 110239835
17+
)
18+
19+
// This script was created to duplicate the creation time of transaction to the events and transfers tables.
20+
// The main motivation was to improve the performance of the events and transfers queries.
21+
22+
func updateCreationTimes() error {
23+
envFile := flag.String("env", ".env", "Path to the .env file")
24+
flag.Parse()
25+
config.InitEnv(*envFile)
26+
env := config.GetConfig()
27+
connStr := fmt.Sprintf("host=%s port=%s user=%s password=%s dbname=%s sslmode=disable",
28+
env.DbHost, env.DbPort, env.DbUser, env.DbPassword, env.DbName)
29+
30+
db, err := sql.Open("postgres", connStr)
31+
if err != nil {
32+
return fmt.Errorf("failed to connect to database: %v", err)
33+
}
34+
defer db.Close()
35+
36+
log.Println("Connected to database")
37+
38+
// Test database connection
39+
if err := db.Ping(); err != nil {
40+
return fmt.Errorf("failed to ping database: %v", err)
41+
}
42+
43+
// Process transactions in batches
44+
if err := processTransactionsBatch(db); err != nil {
45+
return fmt.Errorf("failed to process transactions: %v", err)
46+
}
47+
48+
log.Println("Successfully updated all events and transfers creation times")
49+
return nil
50+
}
51+
52+
func processTransactionsBatch(db *sql.DB) error {
53+
currentId := startTransactionId
54+
totalProcessed := 0
55+
totalTransactions := endTransactionId - startTransactionId + 1
56+
lastProgressPrinted := -1.0
57+
58+
log.Printf("Starting to process transactions from ID %d to %d",
59+
startTransactionId, endTransactionId)
60+
log.Printf("Total transactions to process: %d", totalTransactions)
61+
62+
for currentId <= endTransactionId {
63+
// Calculate batch end
64+
batchEnd := currentId + creationTimeBatchSize - 1
65+
if batchEnd > endTransactionId {
66+
batchEnd = endTransactionId
67+
}
68+
69+
// Process this batch
70+
processed, err := processBatch(db, currentId, batchEnd)
71+
if err != nil {
72+
return fmt.Errorf("failed to process batch %d-%d: %v", currentId, batchEnd, err)
73+
}
74+
75+
totalProcessed += processed
76+
77+
// Calculate progress percentage
78+
transactionsProcessed := batchEnd - startTransactionId + 1
79+
progressPercent := (float64(transactionsProcessed) / float64(totalTransactions)) * 100.0
80+
81+
// Only print progress if it has increased by at least 0.1%
82+
if progressPercent-lastProgressPrinted >= 0.1 {
83+
log.Printf("Progress: %.1f%%", progressPercent)
84+
lastProgressPrinted = progressPercent
85+
}
86+
87+
// Move to next batch
88+
currentId = batchEnd + 1
89+
}
90+
91+
log.Printf("Completed processing. Total records updated: %d (100.0%%)", totalProcessed)
92+
return nil
93+
}
94+
95+
func processBatch(db *sql.DB, startId, endId int) (int, error) {
96+
// Begin transaction for atomic operation
97+
tx, err := db.Begin()
98+
if err != nil {
99+
return 0, fmt.Errorf("failed to begin transaction: %v", err)
100+
}
101+
defer tx.Rollback() // Will be ignored if tx.Commit() succeeds
102+
103+
// Update events with creation time from transactions
104+
eventsUpdateQuery := `
105+
UPDATE "Events"
106+
SET creationtime = t.creationtime, "updatedAt" = CURRENT_TIMESTAMP
107+
FROM "Transactions" t
108+
WHERE "Events"."transactionId" = t.id
109+
AND t.id >= $1 AND t.id <= $2
110+
`
111+
112+
eventsResult, err := tx.Exec(eventsUpdateQuery, startId, endId)
113+
if err != nil {
114+
return 0, fmt.Errorf("failed to update events: %v", err)
115+
}
116+
117+
eventsRowsAffected, err := eventsResult.RowsAffected()
118+
if err != nil {
119+
return 0, fmt.Errorf("failed to get events rows affected: %v", err)
120+
}
121+
122+
// Update transfers with creation time from transactions
123+
transfersUpdateQuery := `
124+
UPDATE "Transfers"
125+
SET creationtime = t.creationtime, "updatedAt" = CURRENT_TIMESTAMP
126+
FROM "Transactions" t
127+
WHERE "Transfers"."transactionId" = t.id
128+
AND t.id >= $1 AND t.id <= $2
129+
`
130+
131+
transfersResult, err := tx.Exec(transfersUpdateQuery, startId, endId)
132+
if err != nil {
133+
return 0, fmt.Errorf("failed to update transfers: %v", err)
134+
}
135+
136+
transfersRowsAffected, err := transfersResult.RowsAffected()
137+
if err != nil {
138+
return 0, fmt.Errorf("failed to get transfers rows affected: %v", err)
139+
}
140+
141+
// Commit the transaction
142+
if err := tx.Commit(); err != nil {
143+
return 0, fmt.Errorf("failed to commit transaction: %v", err)
144+
}
145+
146+
totalRowsAffected := int(eventsRowsAffected + transfersRowsAffected)
147+
return totalRowsAffected, nil
148+
}
149+
150+
func creationTimes() {
151+
if err := updateCreationTimes(); err != nil {
152+
log.Fatalf("Error: %v", err)
153+
}
154+
}

0 commit comments

Comments
 (0)