-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: add EVM event-watcher support with mongodb * refactor: DB, Watcher, SNS Classes
- Loading branch information
Showing
37 changed files
with
1,205 additions
and
1,223 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,28 @@ | ||
LOG_DIR=. | ||
#Log | ||
LOG_DIR= | ||
LOG_LEVEL=info | ||
|
||
ETH_RPC= | ||
#RPC | ||
ETH_RPC=https://eth.llamarpc.com | ||
|
||
#Database source | ||
DB_SOURCE= | ||
JSON_DB_FILE=db.json | ||
JSON_LAST_BLOCK_FILE=lastBlockByChain.json | ||
|
||
#Server | ||
PORT=3005 | ||
|
||
#MongoDB | ||
MONGODB_URI=mongodb://localhost:27017 | ||
MONGODB_DATABASE=wormhole | ||
|
||
#SNS source | ||
SNS_SOURCE=aws | ||
|
||
#AWS | ||
AWS_SNS_REGION= | ||
AWS_TOPIC_ARN= | ||
AWS_SNS_TOPIC_ARN= | ||
AWS_SNS_SUBJECT=EventWatcher | ||
AWS_ACCESS_KEY_ID= | ||
AWS_SECRET_ACCESS_KEY= | ||
AWS_SECRET_ACCESS_KEY= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { ChainName, EVMChainName } from '@certusone/wormhole-sdk/lib/cjs/utils/consts'; | ||
|
||
export const env = { | ||
NODE_ENV: process.env.NODE_ENV, | ||
|
||
LOG_DIR: process.env.LOG_DIR, | ||
LOG_LEVEL: process.env.LOG_LEVEL || 'info', | ||
|
||
ETH_RPC: process.env.ETH_RPC, | ||
|
||
DB_SOURCE: process.env.NODE_ENV === 'test' ? 'local' : process.env.DB_SOURCE || 'local', | ||
JSON_DB_FILE: process.env.JSON_DB_FILE || './db.json', | ||
JSON_LAST_BLOCK_FILE: process.env.JSON_LAST_BLOCK_FILE || './lastBlockByChain.json', | ||
|
||
PORT: process.env.PORT, | ||
|
||
MONGODB_URI: process.env.MONGODB_URI, | ||
MONGODB_DATABASE: process.env.MONGODB_DATABASE, | ||
|
||
SNS_SOURCE: process.env.SNS_SOURCE, | ||
AWS_SNS_REGION: process.env.AWS_SNS_REGION, | ||
AWS_SNS_TOPIC_ARN: process.env.AWS_SNS_TOPIC_ARN, | ||
AWS_SNS_SUBJECT: process.env.AWS_SNS_SUBJECT, | ||
AWS_ACCESS_KEY_ID: process.env.AWS_ACCESS_KEY_ID, | ||
AWS_SECRET_ACCESS_KEY: process.env.AWS_SECRET_ACCESS_KEY, | ||
} as const; | ||
|
||
// EVM Chains not supported | ||
// aurora, gnosis, neon, sepolia | ||
|
||
export const evmChains: EVMChainName[] = [ | ||
'acala', | ||
'arbitrum', | ||
'avalanche', | ||
'base', | ||
'bsc', | ||
'celo', | ||
'ethereum', | ||
'fantom', | ||
'karura', | ||
'klaytn', | ||
'moonbeam', | ||
'oasis', | ||
'optimism', | ||
'polygon', | ||
]; | ||
|
||
export const supportedChains: ChainName[] = [ | ||
...evmChains, | ||
'algorand', | ||
'aptos', | ||
'injective', | ||
'near', | ||
'solana', | ||
'sui', | ||
'terra', | ||
'terra2', | ||
'xpla', | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { ChainName, coalesceChainId } from '@certusone/wormhole-sdk/lib/cjs/utils/consts'; | ||
import { INITIAL_DEPLOYMENT_BLOCK_BY_CHAIN } from '../common/consts'; | ||
import { getLogger, WormholeLogger } from '../utils/logger'; | ||
import { DBImplementation, LastBlockByChain, VaaLog } from './types'; | ||
abstract class BaseDB implements DBImplementation { | ||
public logger: WormholeLogger; | ||
public lastBlockByChain: LastBlockByChain = {}; | ||
|
||
constructor() { | ||
this.logger = getLogger('db'); | ||
this.lastBlockByChain = {}; | ||
} | ||
|
||
public async start(): Promise<void> { | ||
await this.connect(); | ||
await this.getLastBlocksProcessed(); | ||
console.log('----------DB CONFIGURED------------'); | ||
} | ||
|
||
public async getResumeBlockByChain(chain: ChainName): Promise<number | null> { | ||
const lastBlock = this.getLastBlockByChain(chain); | ||
const initialBlock = INITIAL_DEPLOYMENT_BLOCK_BY_CHAIN[chain]; | ||
|
||
if (lastBlock) return Number(lastBlock) + 1; | ||
if (initialBlock) return Number(initialBlock); | ||
return null; | ||
} | ||
|
||
public getLastBlockByChain(chain: ChainName): string | null { | ||
const chainId = coalesceChainId(chain); | ||
const blockInfo = this.lastBlockByChain?.[chainId]; | ||
|
||
if (blockInfo) { | ||
const tokens = String(blockInfo)?.split('/'); | ||
return chain === 'aptos' ? tokens.at(-1)! : tokens[0]; | ||
} | ||
|
||
return null; | ||
} | ||
|
||
abstract connect(): Promise<void>; | ||
abstract getLastBlocksProcessed(): Promise<void>; | ||
abstract storeVaaLogs(chain: ChainName, vaaLogs: VaaLog[]): Promise<void>; | ||
abstract storeLatestProcessBlock(chain: ChainName, lastBlock: number): Promise<void>; | ||
} | ||
|
||
export default BaseDB; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { ChainName, coalesceChainId } from '@certusone/wormhole-sdk/lib/cjs/utils/consts'; | ||
import { readFileSync, writeFileSync } from 'fs'; | ||
import { env } from '../config'; | ||
import BaseDB from './BaseDB'; | ||
import { VaaLog } from './types'; | ||
|
||
const ENCODING = 'utf8'; | ||
|
||
export default class JsonDB extends BaseDB { | ||
db: {} | null = null; | ||
dbFile: string; | ||
dbLastBlockFile: string; | ||
|
||
constructor() { | ||
super(); | ||
this.db = {}; | ||
this.lastBlockByChain = {}; | ||
this.dbFile = env.JSON_DB_FILE; | ||
this.dbLastBlockFile = env.JSON_LAST_BLOCK_FILE; | ||
} | ||
|
||
async connect(): Promise<void> { | ||
try { | ||
const rawDb = readFileSync(this.dbFile, ENCODING); | ||
this.db = rawDb ? JSON.parse(rawDb) : {}; | ||
console.log('---CONNECTED TO JsonDB---'); | ||
} catch (e) { | ||
this.logger.warn(`${this.dbFile} does not exists, creating new file`); | ||
this.db = {}; | ||
} | ||
} | ||
|
||
async getLastBlocksProcessed(): Promise<void> { | ||
try { | ||
const rawLastBlockByChain = readFileSync(this.dbLastBlockFile, ENCODING); | ||
this.lastBlockByChain = rawLastBlockByChain ? JSON.parse(rawLastBlockByChain) : {}; | ||
} catch (e) { | ||
this.logger.warn(`${this.dbLastBlockFile} does not exists, creating new file`); | ||
this.lastBlockByChain = {}; | ||
} | ||
} | ||
|
||
override async storeVaaLogs(chain: ChainName, vaaLogs: VaaLog[]): Promise<void> { | ||
this.db = [{ ...this.db, ...vaaLogs }]; | ||
writeFileSync(this.dbFile, JSON.stringify(this.db, null, 2), ENCODING); | ||
} | ||
|
||
override async storeLatestProcessBlock(chain: ChainName, lastBlock: number): Promise<void> { | ||
const chainId = coalesceChainId(chain); | ||
this.lastBlockByChain[chainId] = String(lastBlock); | ||
|
||
writeFileSync(this.dbLastBlockFile, JSON.stringify(this.lastBlockByChain, null, 2), ENCODING); | ||
} | ||
} |
Oops, something went wrong.