Skip to content

Commit 366fd30

Browse files
committed
feat: add script for xrp timestamp unix conversion
1 parent 53b8550 commit 366fd30

File tree

6 files changed

+32
-5
lines changed

6 files changed

+32
-5
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"run-native-indexer-debug": "ts-node packages/fasset-indexer-core/src/run/run-indexer.ts",
2121
"run-native-watchdog": "yarn build-base && node packages/fasset-indexer-core/dist/src/run/run-watchdog.js",
2222
"run-xrp-indexer": "yarn build-base && node packages/fasset-indexer-xrp/dist/src/run/run-indexer.js",
23+
"run-xrp-indexer-debug": "ts-node packages/fasset-indexer-xrp/src/run/run-indexer.ts",
2324
"run-btc-indexer": "yarn build-base && node packages/fasset-indexer-btc/dist/src/run/run-indexer.js",
2425
"run-doge-indexer": "yarn build-base && node packages/fasset-indexer-doge/dist/src/run/run-indexer.js",
2526
"run-api": "yarn build && node packages/fasset-indexer-api/dist/src/main",

packages/fasset-indexer-core/src/orm/entities/underlying/address.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Entity, ManyToOne, PrimaryKey, Property } from "@mikro-orm/core"
1+
import { Entity, OneToOne, PrimaryKey, Property } from "@mikro-orm/core"
22
import { uint256 } from "../../custom/uint"
33

44

@@ -15,7 +15,7 @@ export class UnderlyingAddress {
1515
@Entity()
1616
export class UnderlyingBalance {
1717

18-
@ManyToOne({ entity: () => UnderlyingAddress, primary: true })
18+
@OneToOne({ entity: () => UnderlyingAddress, owner: true, primary: true })
1919
address!: UnderlyingAddress
2020

2121
@Property({ type: new uint256() })
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
export const FIRST_UNHANDLED_DOGE_BLOCK_DB_KEY = 'firstUnhandledXrpBlock'
22
export const MIN_DOGE_BLOCK_NUMBER_DB_KEY = 'minXrpBlockNumber'
33

4-
export const BLOCK_HEIGHT_OFFSET = 15
4+
export const XRP_BLOCK_HEIGHT_OFFSET = 15
5+
6+
export const XRP_TIMESTAMP_UNIX_OFFSET = 946684800

packages/fasset-indexer-xrp/src/indexer/xrp-indexer.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { findOrCreateEntity, getVar, setVar, type EntityManager } from "fasset-i
44
import { PaymentReference } from "fasset-indexer-core/utils"
55
import { logger } from "fasset-indexer-core/logger"
66
import { IXrpMemo, IXrpBlock, IXrpTransaction } from "../client/interface"
7+
import { XRP_TIMESTAMP_UNIX_OFFSET } from "../constants"
78
import type { XrpContext } from "../context"
89

910

@@ -137,7 +138,8 @@ export class XrpIndexer {
137138

138139
private async storeXrpBlock(em: EntityManager, xrpBlock: IXrpBlock): Promise<Entities.UnderlyingBlock> {
139140
return em.create(Entities.UnderlyingBlock, {
140-
hash: xrpBlock.ledger_hash, height: xrpBlock.ledger_index, timestamp: xrpBlock.close_time
141+
hash: xrpBlock.ledger_hash, height: xrpBlock.ledger_index,
142+
timestamp: xrpBlock.close_time + XRP_TIMESTAMP_UNIX_OFFSET
141143
})
142144
}
143145

packages/fasset-indexer-xrp/src/run/run-indexer.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { XrpIndexer } from "../indexer/xrp-indexer"
33
import { XrpConfigLoader } from "../config/config"
44
import { XrpContext } from "../context"
55
import { monitor } from "./run-monitor"
6+
import { fixRippleBlockTimestamp } from "../scripts/fix-ripple-block-timestamps"
67
import {
78
FIRST_UNHANDLED_XRP_BLOCK_DB_KEY,
89
MIN_XRP_BLOCK_NUMBER_DB_KEY
@@ -19,7 +20,11 @@ async function runIndexer() {
1920
config.xrpMonitoredAddresses
2021
)
2122
const runner = new IndexerRunner(indexer, 'xrp')
22-
await Promise.all([monitor(config, context), runner.run(config.xrpMinBlockNumber)])
23+
await Promise.all([
24+
fixRippleBlockTimestamp(context),
25+
monitor(config, context),
26+
runner.run(config.xrpMinBlockNumber)
27+
])
2328
}
2429

2530
runIndexer()
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { UnderlyingBlock } from "fasset-indexer-core/entities"
2+
import { logger } from "fasset-indexer-core/logger"
3+
import { XRP_TIMESTAMP_UNIX_OFFSET } from "../constants"
4+
import type { XrpContext } from "../context"
5+
6+
7+
const MAX_XRP_TIMESTAMP = 30 * 365 * 24 * 60 * 60 // 2030
8+
9+
export async function fixRippleBlockTimestamp(context: XrpContext) {
10+
const em = context.orm.em.fork()
11+
const blocks = await em.find(UnderlyingBlock, { timestamp: { $lt: MAX_XRP_TIMESTAMP } })
12+
for (const block of blocks) {
13+
block.timestamp = block.timestamp + XRP_TIMESTAMP_UNIX_OFFSET
14+
await em.persistAndFlush(block)
15+
}
16+
logger.info('finished fixing ripple block timestamps')
17+
}

0 commit comments

Comments
 (0)