Skip to content

Commit

Permalink
fix: second review
Browse files Browse the repository at this point in the history
  • Loading branch information
zengzengzenghuy committed Oct 24, 2023
1 parent f802b1c commit bf7dd35
Show file tree
Hide file tree
Showing 12 changed files with 57 additions and 181 deletions.
6 changes: 2 additions & 4 deletions packages/reporter/.env.example
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
PRIVATE_KEY=

REPORTERS_ENABLED=amb,sygma

GOERLI_RPC_URL=
GNOSIS_RPC_URL=
SOURCE_CHAIN=goerli
DEST_CHAIN=gnosis


AMB_CONTROLLER=false
GAS=30000

SYGMA_CONTROLLER=false
SYGMA_FEE_DATA=0x

TELEPATHY_CONTROLLER=false
TELEPATHY_PROOF_API_URL=https://api.telepathy.xyz/api/hashi/

76 changes: 0 additions & 76 deletions packages/reporter/src/ABIs/WormholeReporterContractABI.json

This file was deleted.

2 changes: 1 addition & 1 deletion packages/reporter/src/BlockListener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Chain, publicActions } from "viem"
import winston from "winston"

import Multiclient from "./MultiClient"
import { BlockListenerConfig } from "./utils/type"
import { BlockListenerConfig } from "./types/index"

class BlocksListener {
controllers: any[]
Expand Down
4 changes: 2 additions & 2 deletions packages/reporter/src/MultiClient.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createWalletClient, http, Chain, publicActions } from "viem"
import { createWalletClient, http, Chain, publicActions, WalletClient, PublicClient } from "viem"
import { privateKeyToAccount } from "viem/accounts"

type ContructorConfigs = {
Expand All @@ -21,7 +21,7 @@ const getClient = ({ chain, privateKey, rpcUrl }: GetClientsConfigs) =>
}).extend(publicActions)

class Multiclient {
private _clients: { [chainName: string]: any }
private _clients: { [chainName: string]: PublicClient & WalletClient }

constructor({ chains, privateKey, rpcUrls }: ContructorConfigs) {
this._clients = chains.reduce((_acc: { [chainName: string]: any }, _chain: Chain) => {
Expand Down
8 changes: 4 additions & 4 deletions packages/reporter/src/controllers/AMBReporterController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,19 @@ import "dotenv/config"

import contractABI from "../ABIs/AMBReporterContractABI.json"
import Multiclient from "../MultiClient"
import { ControllerConfig } from "../utils/type"
import { ControllerConfig } from "../types/index"

class AMBReporterController {
sourceChain: Chain
destinationChains: Chain[]
isEnabled: boolean = false
name: string = "amb"
logger: winston.Logger
multiClient: Multiclient
reporterAddr: string
adapterAddr: { [chainName: string]: string }
constructor(props: ControllerConfig) {
this.sourceChain = props.sourceChain
this.destinationChains = props.destinationChains
this.isEnabled = props.isEnabled
this.logger = props.logger
this.multiClient = props.multiClient
this.reporterAddr = props.reporterAddress
Expand All @@ -34,12 +33,13 @@ class AMBReporterController {
const account = privateKeyToAccount(process.env.PRIVATE_KEY as `0x${string}`)

for (const chain of this.destinationChains) {
let chainName = chain.name.toLocaleLowerCase()
const { result, request } = await client.simulateContract({
account, // calling from account
address: this.reporterAddr as `0x${string}`,
abi: contractABI,
functionName: "reportHeaders",
args: [blockNumbers, this.adapterAddr[chain.name.toLocaleLowerCase()], process.env.GAS],
args: [blockNumbers, this.adapterAddr[chainName], process.env.GAS],
})

const txhash = await client.writeContract(request)
Expand Down
35 changes: 15 additions & 20 deletions packages/reporter/src/controllers/SygmaReporterController.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,28 @@
import { parseEther, Chain } from "viem"
import { gnosis } from "viem/chains"
import { privateKeyToAccount } from "viem/accounts"
import winston from "winston"
import "dotenv/config"

import contractABI from "../ABIs/SygmaReporterContractABI.json"
import Multiclient from "../MultiClient"
import { ControllerConfig } from "../utils/type"
import { ControllerConfig } from "../types/index"
import { settings } from "../settings"

class SygmaReporterController {
sourceChain: Chain
destinationChains: Chain[]
isEnabled: boolean = false
name: string = "sygma"
logger: winston.Logger
multiClient: Multiclient
reporterAddr: string
adapterAddr: { [chainName: string]: string }
reporterAddress: string
adapterAddress: { [chainName: string]: string }
constructor(props: ControllerConfig) {
this.sourceChain = props.sourceChain
this.destinationChains = props.destinationChains
this.isEnabled = props.isEnabled
this.logger = props.logger
this.multiClient = props.multiClient
this.reporterAddr = props.reporterAddress
this.adapterAddr = props.adapterAddress
this.reporterAddress = props.reporterAddress
this.adapterAddress = props.adapterAddress
}

async onBlocks(blockNumbers: string[]) {
Expand All @@ -34,13 +33,18 @@ class SygmaReporterController {
const account = privateKeyToAccount(process.env.PRIVATE_KEY as `0x${string}`)

for (const chain of this.destinationChains) {
const destDomainId = this.getDomainID(chain)
const chainName = chain.name.toLocaleLowerCase()
const { result, request } = await client.simulateContract({
account, // calling from account
address: this.reporterAddr as `0x${string}`,
address: this.reporterAddress as `0x${string}`,
abi: contractABI,
functionName: "reportHeadersToDomain",
args: [blockNumbers, this.adapterAddr[chain.name.toLocaleLowerCase()], destDomainId, "0x"],
args: [
blockNumbers,
this.adapterAddress[chainName],
settings.sygmaDomainID[chainName as keyof typeof settings.sygmaDomainID],
"0x",
],
value: parseEther("0.0001"),
})
const txhash = await client.writeContract(request)
Expand All @@ -50,15 +54,6 @@ class SygmaReporterController {
this.logger.error(`Sygma: Error from Sygma Controller: ${error}`)
}
}

getDomainID(destinationChain: Chain) {
switch (destinationChain) {
case gnosis:
return 101
default:
return 101
}
}
}

export default SygmaReporterController
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,20 @@ import "dotenv/config"
import lightClientContractABI from "../ABIs/TelepathyContractABI.json"
import adapterContractABI from "../ABIs/TelepathyAdapterABI.json"
import Multiclient from "../MultiClient"
import { ControllerConfig } from "../utils/type"
import settings from "../utils/settings.json"
import { ControllerConfig } from "../types/index"
import { settings } from "../settings"

class TelepathyReporterController {
sourceChain: Chain
destinationChains: Chain[]
isEnabled: boolean = false
name: string = "telepathy"
logger: winston.Logger
multiClient: Multiclient
reporterAddr: string
adapterAddr: { [chainName: string]: string }
constructor(props: ControllerConfig) {
this.sourceChain = props.sourceChain
this.destinationChains = props.destinationChains
this.isEnabled = props.isEnabled
this.logger = props.logger
this.multiClient = props.multiClient
this.reporterAddr = props.reporterAddress
Expand All @@ -46,7 +45,9 @@ class TelepathyReporterController {
const blockBuffer = 10n // put 10 blocks before the current block in case the node provider don't sync up at the head
const startBlock = currentBlockNumber - queryBlockLength
const endBlock = currentBlockNumber - blockBuffer

this.logger.info(`Telepathy: Getting Contract Event from block ${startBlock} to block ${currentBlockNumber}`)

const logs = await client.getContractEvents({
address: lightClientAddr as `0x${string}`,
abi: lightClientContractABI,
Expand Down
23 changes: 9 additions & 14 deletions packages/reporter/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
import { gnosis, goerli, mainnet } from "viem/chains"
import winston from "winston"
import "dotenv/config"

import Multiclient from "./MultiClient"
import AMBReporterController from "./controllers/AMBReporterController"
import SygmaReporterController from "./controllers/SygmaReporterController"
import TelepathyReporterController from "./controllers/TelepathyReporterController"
import BlocksListener from "./BlockListener"
import "dotenv/config"
import winston from "winston"
import settings from "./utils/settings.json"
import { settings } from "./settings/index"

function main() {
const goerliRPC = process.env.GOERLI_RPC_URL as string
const gnosisRPC = process.env.GNOSIS_RPC_URL as string
const sourceChain = process.env.SOURCE_CHAIN
const destChain = process.env.DEST_CHAIN
const privKey = process.env.PRIVATE_KEY as `0x${string}`
const isAMBEnabled = process.env.AMB_CONTROLLER === "true"
const isSygmaEnabled = process.env.SYGMA_CONTROLLER === "true"
const isTelepathyEnabled = process.env.TELEPATHY_CONTROLLER === "true"
const timeFetchBlocksMs = 10 * 1000
const timeFetchBlocksMs = 5 * 60 * 1000 // modify the frequency here

const logger = winston.createLogger({
level: "info",
Expand All @@ -35,7 +32,6 @@ function main() {
const ambReporterController = new AMBReporterController({
sourceChain: goerli,
destinationChains: [gnosis],
isEnabled: isAMBEnabled,
logger: logger,
multiClient: multiClient,
reporterAddress: settings.contractAddresses.goerli.AMBReporter,
Expand All @@ -44,7 +40,6 @@ function main() {
const sygmaReporterController = new SygmaReporterController({
sourceChain: goerli,
destinationChains: [gnosis],
isEnabled: isSygmaEnabled,
logger: logger,
multiClient: multiClient,
reporterAddress: settings.contractAddresses.goerli.SygmaReporter,
Expand All @@ -53,22 +48,22 @@ function main() {
const telepathyReporterController = new TelepathyReporterController({
sourceChain: goerli,
destinationChains: [gnosis],
isEnabled: isTelepathyEnabled,
logger: logger,
multiClient: multiClient,
reporterAddress: "",
adapterAddress: { gnosis: settings.contractAddresses.gnosis.SygmaAdapter },
})

const controllersEnabled = process.env.REPORTERS_ENABLED?.split(",")
const blocksListener = new BlocksListener({
controllers: [ambReporterController, sygmaReporterController, telepathyReporterController].filter(
(controller) => controller.isEnabled == true,
(controller) => controllersEnabled?.includes(controller.name),
),
timeFetchBlocksMs: timeFetchBlocksMs,
logger: logger,
multiclient: multiClient,
sourceChain: goerli,
queryBlockLength: 100,
queryBlockLength: 100, // modify the query block length here, <256
lastProcessedBlock: 0n,
})
blocksListener.start()
Expand Down
19 changes: 19 additions & 0 deletions packages/reporter/src/settings/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
export const settings = {
contractAddresses: {
goerli: {
AMBReporter: "0xedc0b1d3de4496e0d917af42f29cb71eb2982319",
SygmaReporter: "0x2f96d347c932ac73b56e9352ecc0707e25173d88",
},
mainnet: {},
gnosis: {
AMBAdapter: "0x01268DB05965CeAc2a89566c42CD550ED7eE5ECD",
SygmaAdapter: "0x9AD7a6f4FDA8247cC0bF5932B68c5b619937dB15",
TelepathyLightClient: "0x34b5378DE786389a477b40dD710812c250185f83",
TelapathyAdapter: "0x2f1E51a2763FB67fe09971Fd8d849716137A3357",
},
},
sygmaDomainID: {
gnosis: "101",
goerli: "1",
},
}
16 changes: 0 additions & 16 deletions packages/reporter/src/utils/address.json

This file was deleted.

15 changes: 0 additions & 15 deletions packages/reporter/src/utils/settings.json

This file was deleted.

Loading

0 comments on commit bf7dd35

Please sign in to comment.