diff --git a/src/processors/reverseZero.ts b/src/processors/reverseZero.ts index 0e353ee..0f4f0ed 100644 --- a/src/processors/reverseZero.ts +++ b/src/processors/reverseZero.ts @@ -1,9 +1,7 @@ import bunyan from "bunyan"; -import { ZeroService } from "../service/zero"; -import { ReverseZeroService } from "../service/reverse_zero"; +import { ZeroService } from "../service/zero/index"; import { config } from "../config"; import { BaseProcessor } from "./base"; -import { GenericZeroService } from "../service/generic_zero"; export const NAME = "REVERSE_ZERO"; const REPEAT_JOB_OPT = { @@ -13,12 +11,12 @@ const REPEAT_JOB_OPT = { export class ReverseZero extends BaseProcessor { private logger: bunyan; - private zeroService: GenericZeroService; + private zeroService: ZeroService; constructor(logger: bunyan) { super(NAME); this.logger = logger; - this.zeroService = new GenericZeroService(logger, "reverse"); + this.zeroService = new ZeroService(logger, "reverse"); } init() { this.logger.info("Initialising Reverse XDC-Zero"); diff --git a/src/processors/zero.ts b/src/processors/zero.ts index 60a4f67..6fd10ca 100644 --- a/src/processors/zero.ts +++ b/src/processors/zero.ts @@ -1,8 +1,7 @@ import bunyan from "bunyan"; -import { ZeroService } from "../service/zero"; +import { ZeroService } from "../service/zero/index"; import { config } from "../config"; import { BaseProcessor } from "./base"; -import { GenericZeroService } from "../service/generic_zero"; export const NAME = "ZERO"; const REPEAT_JOB_OPT = { @@ -12,12 +11,12 @@ const REPEAT_JOB_OPT = { export class Zero extends BaseProcessor { private logger: bunyan; - private zeroService: GenericZeroService; + private zeroService: ZeroService; constructor(logger: bunyan) { super(NAME); this.logger = logger; - this.zeroService = new GenericZeroService(logger, "normal"); + this.zeroService = new ZeroService(logger, "normal"); } init() { this.logger.info("Initialising XDC-Zero"); diff --git a/src/service/generic_zero/index.ts b/src/service/generic_zero/index.ts deleted file mode 100644 index 5e1f153..0000000 --- a/src/service/generic_zero/index.ts +++ /dev/null @@ -1,212 +0,0 @@ -import { - Hex, - PrivateKeyAccount, - createWalletClient, - PublicClient, - WalletClient, - createPublicClient, - decodeAbiParameters, - http, -} from "viem"; -import bunyan from "bunyan"; -import { config } from "../../config"; -import { SubnetService } from "../subnet"; -import endpointABI from "../../abi/endpointABI.json"; -import cscABI from "../../abi/cscABI.json"; -import { MainnetService } from "../mainnet"; -import Logger from "bunyan"; -import { privateKeyToAccount } from "viem/accounts"; - -// This class must be called with init() in order to use it - -//Parentnet = chain where zero contract is deployed -//Childnet = chain where data transfer is initiated -// Zero triggers once in a while to to check if there is zero-requests in childnet then store that info into parentnet zero contract -export class GenericZeroService { - private childnetViemClient: PublicClient; - private parentnetViemClient: PublicClient; - private parentnetWalletClient: WalletClient; - private childnetService: SubnetService; - private parentnetService: MainnetService; - private logger: Logger; - private parentnetWalletAccount: PrivateKeyAccount; - private childnetUrl: string; - private parentnetUrl: string; - private subnetZeroAddress: string; - private parentnetZeroAddress: string; - private parentnetCSCAddress: string; - - constructor(logger: bunyan, mode: "reverse" | string ) { - this.logger = logger; - if (mode == "reverse"){ - this.childnetService = new SubnetService(config.mainnet, logger); - this.parentnetService = new MainnetService(config.subnet, logger); - this.childnetUrl = config.mainnet.url; - this.parentnetUrl = config.subnet.url; - this.parentnetCSCAddress = config.subnet.smartContractAddress; - this.subnetZeroAddress = config.xdcZero.parentChainZeroContractAddress; - this.parentnetZeroAddress = config.xdcZero.subnetZeroContractAddress; - this.parentnetWalletAccount = privateKeyToAccount(config.xdcZero.subnetWalletPk as Hex); - } else { - this.childnetService = new SubnetService(config.subnet, logger); - this.parentnetService = new MainnetService(config.mainnet, logger); - this.childnetUrl = config.subnet.url; - this.parentnetUrl = config.mainnet.url; - this.parentnetCSCAddress = config.mainnet.smartContractAddress; - this.subnetZeroAddress = config.xdcZero.subnetZeroContractAddress; - this.parentnetZeroAddress = config.xdcZero.parentChainZeroContractAddress; - this.parentnetWalletAccount = privateKeyToAccount(config.xdcZero.parentnetWalletPk as Hex); - } - } - - // Initialise the client services - async init() { - const subnetNetworkInformation = - await this.childnetService.getNetworkInformation(); - const subnetInfo = { - id: subnetNetworkInformation.NetworkId, - name: subnetNetworkInformation.NetworkName, - network: subnetNetworkInformation.NetworkName, - nativeCurrency: { - decimals: 18, - name: subnetNetworkInformation.Denom, - symbol: subnetNetworkInformation.Denom, - }, - rpcUrls: { - public: { http: [this.childnetUrl]}, - default: { http: [this.childnetUrl] }, - }, - }; - - this.childnetViemClient = createPublicClient({ - chain: subnetInfo, - transport: http(), - }); - - const mainnetNetworkInformation = - await this.parentnetService.getNetworkInformation(); - const mainnetInfo = { - id: mainnetNetworkInformation.NetworkId, - name: mainnetNetworkInformation.NetworkName, - network: mainnetNetworkInformation.NetworkName, - nativeCurrency: { - decimals: 18, - name: mainnetNetworkInformation.Denom, - symbol: mainnetNetworkInformation.Denom, - }, - rpcUrls: { - public: { http: [this.parentnetUrl] }, - default: { http: [this.parentnetUrl] }, - }, - }; - - this.parentnetViemClient = createPublicClient({ - chain: mainnetInfo, - transport: http(), - }); - - this.parentnetWalletClient = createWalletClient({ - account: this.parentnetWalletAccount, - chain: mainnetInfo, - transport: http(), - }); - } - - async getPayloads() { - const payloads = [] as any; - const subnetEndpointContract = { - // address: config.xdcZero.subnetZeroContractAddress, - address: this.subnetZeroAddress, - abi: endpointABI, - }; - - const logs = await this.childnetViemClient.getContractEvents({ - ...(subnetEndpointContract as any), - fromBlock: BigInt(0), - eventName: "Packet", - }); - - const parentChainId = await this.parentnetViemClient.getChainId(); - - logs?.forEach((log) => { - const values = decodeAbiParameters( - [ - { name: "index", type: "uint" }, - { name: "sid", type: "uint" }, - { name: "sua", type: "address" }, - { name: "rid", type: "uint" }, - { name: "rua", type: "address" }, - { name: "data", type: "bytes" }, - ], - `0x${log.data.substring(130)}` - ); - - if (Number(values[3]) == parentChainId) { - const list = [...values]; - list.push(log.transactionHash); - list.push(log.blockNumber); - payloads.push(list); - } - }); - - return payloads; - } - - async getIndexFromParentnet() { - const subnetChainId = await this.childnetViemClient.getChainId(); - const parentnetEndpointContract = { - // address: config.xdcZero.parentChainZeroContractAddress, - address: this.parentnetZeroAddress, - abi: endpointABI, - }; - const chain = (await this.parentnetViemClient.readContract({ - ...(parentnetEndpointContract as any), - functionName: "getChain", - args: [subnetChainId], - })) as { lastIndex: number }; - - return chain?.lastIndex; - } - - async getLatestBlockNumberFromCsc() { - const parentnetCSCContract = { - // address: config.mainnet.smartContractAddress, - address: this.parentnetCSCAddress, - abi: cscABI, - }; - const blocks = (await this.parentnetViemClient.readContract({ - ...(parentnetCSCContract as any), - functionName: "getLatestBlocks", - args: [], - })) as [any, any]; - - return blocks[1]?.number; - } - - async getProof(txHash: string) { - return this.childnetService.getTransactionAndReceiptProof(txHash); - } - - async validateTransactionProof( - key: string, - receiptProof: string[], - transactionProof: string[], - blockhash: string - ) { - const parentnetEndpointContract = { - // address: config.xdcZero.parentChainZeroContractAddress, - address: this.parentnetZeroAddress, - abi: endpointABI, - }; - const subnetChainId = await this.childnetViemClient.getChainId(); - const { request } = await this.parentnetViemClient.simulateContract({ - ...(parentnetEndpointContract as any), - functionName: "validateTransactionProof", - args: [subnetChainId, key, receiptProof, transactionProof, blockhash], - account: this.parentnetWalletAccount, - }); - - const tx = await this.parentnetWalletClient.writeContract(request as any); - this.logger.info(tx); - } -} diff --git a/src/service/reverse_zero/index.ts b/src/service/reverse_zero/index.ts deleted file mode 100644 index ad98b6c..0000000 --- a/src/service/reverse_zero/index.ts +++ /dev/null @@ -1,203 +0,0 @@ -import { - Hex, - PrivateKeyAccount, - createWalletClient, - PublicClient, - WalletClient, - createPublicClient, - decodeAbiParameters, - http, -} from "viem"; -import bunyan from "bunyan"; -import { config } from "../../config"; -import { SubnetService } from "../subnet"; -import endpointABI from "../../abi/endpointABI.json"; -import cscABI from "../../abi/cscABI.json"; -import { MainnetService } from "../mainnet"; -import Logger from "bunyan"; -import { privateKeyToAccount } from "viem/accounts"; - -// This class must be called with init() in order to use it -export class ReverseZeroService { - private subnetViemClient: PublicClient; - private mainnetViemClient: PublicClient; - private mainnetWalletClient: WalletClient; - private subnetService: SubnetService; - private mainnetService: MainnetService; - private logger: Logger; - - private parentChainWalletAccount: PrivateKeyAccount; - - private zeroWalletPk: PrivateKeyAccount; - private subnetUrl: string; - private parentnetUrl: string; - private subnetZeroAddress: string; - private parentnetZeroAddress: string; - private subnetCSCAddress: string; - private parentnetCSCAddress: string; - - constructor(logger: bunyan) { - this.subnetService = new SubnetService(config.mainnet, logger); //TODO: temp swap - this.mainnetService = new MainnetService(config.subnet, logger); //TODO: fix temp swap - this.subnetUrl = config.mainnet.url; - this.parentnetUrl = config.subnet.url; - this.subnetCSCAddress = config.mainnet.smartContractAddress; - this.parentnetCSCAddress = config.subnet.smartContractAddress; - this.subnetZeroAddress = config.xdcZero.parentChainZeroContractAddress; - this.parentnetZeroAddress = config.xdcZero.subnetZeroContractAddress; - - this.logger = logger; - this.parentChainWalletAccount = privateKeyToAccount(config.xdcZero.subnetWalletPk as Hex); - } - - // Initialise the client services - async init() { - const subnetNetworkInformation = - await this.subnetService.getNetworkInformation(); - const subnetInfo = { - id: subnetNetworkInformation.NetworkId, - name: subnetNetworkInformation.NetworkName, - network: subnetNetworkInformation.NetworkName, - nativeCurrency: { - decimals: 18, - name: subnetNetworkInformation.Denom, - symbol: subnetNetworkInformation.Denom, - }, - rpcUrls: { - public: { http: [this.subnetUrl]}, - default: { http: [this.subnetUrl] }, - }, - }; - - this.subnetViemClient = createPublicClient({ - chain: subnetInfo, - transport: http(), - }); - - const mainnetNetworkInformation = - await this.mainnetService.getNetworkInformation(); - const mainnetInfo = { - id: mainnetNetworkInformation.NetworkId, - name: mainnetNetworkInformation.NetworkName, - network: mainnetNetworkInformation.NetworkName, - nativeCurrency: { - decimals: 18, - name: mainnetNetworkInformation.Denom, - symbol: mainnetNetworkInformation.Denom, - }, - rpcUrls: { - public: { http: [this.parentnetUrl] }, - default: { http: [this.parentnetUrl] }, - }, - }; - - this.mainnetViemClient = createPublicClient({ - chain: mainnetInfo, - transport: http(), - }); - - this.mainnetWalletClient = createWalletClient({ - account: this.parentChainWalletAccount, - chain: mainnetInfo, - transport: http(), - }); - } - - async getPayloads() { - const payloads = [] as any; - const subnetEndpointContract = { - // address: config.xdcZero.subnetZeroContractAddress, - address: this.subnetZeroAddress, - abi: endpointABI, - }; - - const logs = await this.subnetViemClient.getContractEvents({ - ...(subnetEndpointContract as any), - fromBlock: BigInt(0), - eventName: "Packet", - }); - - const parentChainId = await this.mainnetViemClient.getChainId(); - - logs?.forEach((log) => { - const values = decodeAbiParameters( - [ - { name: "index", type: "uint" }, - { name: "sid", type: "uint" }, - { name: "sua", type: "address" }, - { name: "rid", type: "uint" }, - { name: "rua", type: "address" }, - { name: "data", type: "bytes" }, - ], - `0x${log.data.substring(130)}` - ); - - if (Number(values[3]) == parentChainId) { - const list = [...values]; - list.push(log.transactionHash); - list.push(log.blockNumber); - payloads.push(list); - } - }); - - return payloads; - } - - async getIndexFromParentnet() { - const subnetChainId = await this.subnetViemClient.getChainId(); - const parentnetEndpointContract = { - // address: config.xdcZero.parentChainZeroContractAddress, - address: this.parentnetZeroAddress, - abi: endpointABI, - }; - const chain = (await this.mainnetViemClient.readContract({ - ...(parentnetEndpointContract as any), - functionName: "getChain", - args: [subnetChainId], - })) as { lastIndex: number }; - - return chain?.lastIndex; - } - - async getLatestBlockNumberFromCsc() { - const parentnetCSCContract = { - // address: config.mainnet.smartContractAddress, - address: this.parentnetCSCAddress, - abi: cscABI, - }; - const blocks = (await this.mainnetViemClient.readContract({ - ...(parentnetCSCContract as any), - functionName: "getLatestBlocks", - args: [], - })) as [any, any]; - - return blocks[1]?.number; - } - - async getProof(txHash: string) { - return this.subnetService.getTransactionAndReceiptProof(txHash); - } - - async validateTransactionProof( - key: string, - receiptProof: string[], - transactionProof: string[], - blockhash: string - ) { - const parentnetEndpointContract = { - // address: config.xdcZero.parentChainZeroContractAddress, - address: this.parentnetZeroAddress, - abi: endpointABI, - }; - const subnetChainId = await this.subnetViemClient.getChainId(); - const { request } = await this.mainnetViemClient.simulateContract({ - ...(parentnetEndpointContract as any), - functionName: "validateTransactionProof", - args: [subnetChainId, key, receiptProof, transactionProof, blockhash], - account: this.parentChainWalletAccount, - }); - - const tx = await this.mainnetWalletClient.writeContract(request as any); - this.logger.info(tx); - } -} diff --git a/src/service/zero/index.ts b/src/service/zero/index.ts index a0b5cb1..772fdcb 100644 --- a/src/service/zero/index.ts +++ b/src/service/zero/index.ts @@ -18,32 +18,51 @@ import Logger from "bunyan"; import { privateKeyToAccount } from "viem/accounts"; // This class must be called with init() in order to use it + +//Parentnet = chain where zero contract is deployed +//Childnet = chain where data transfer is initiated +// Zero triggers once in a while to to check if there is zero-requests in childnet then store that info into parentnet zero contract export class ZeroService { - private subnetViemClient: PublicClient; - private mainnetViemClient: PublicClient; - private mainnetWalletClient: WalletClient; - private subnetService: SubnetService; - private mainnetService: MainnetService; + private childnetViemClient: PublicClient; + private parentnetViemClient: PublicClient; + private parentnetWalletClient: WalletClient; + private childnetService: SubnetService; + private parentnetService: MainnetService; private logger: Logger; - - private parentChainWalletAccount: PrivateKeyAccount; - - constructor(logger: bunyan) { - this.subnetService = new SubnetService(config.subnet, logger); - this.mainnetService = new MainnetService(config.mainnet, logger); + private parentnetWalletAccount: PrivateKeyAccount; + private childnetUrl: string; + private parentnetUrl: string; + private subnetZeroAddress: string; + private parentnetZeroAddress: string; + private parentnetCSCAddress: string; + + constructor(logger: bunyan, mode: "reverse" | string ) { this.logger = logger; + if (mode == "reverse"){ + this.childnetService = new SubnetService(config.mainnet, logger); + this.parentnetService = new MainnetService(config.subnet, logger); + this.childnetUrl = config.mainnet.url; + this.parentnetUrl = config.subnet.url; + this.parentnetCSCAddress = config.subnet.smartContractAddress; + this.subnetZeroAddress = config.xdcZero.parentChainZeroContractAddress; + this.parentnetZeroAddress = config.xdcZero.subnetZeroContractAddress; + this.parentnetWalletAccount = privateKeyToAccount(config.xdcZero.subnetWalletPk as Hex); + } else { + this.childnetService = new SubnetService(config.subnet, logger); + this.parentnetService = new MainnetService(config.mainnet, logger); + this.childnetUrl = config.subnet.url; + this.parentnetUrl = config.mainnet.url; + this.parentnetCSCAddress = config.mainnet.smartContractAddress; + this.subnetZeroAddress = config.xdcZero.subnetZeroContractAddress; + this.parentnetZeroAddress = config.xdcZero.parentChainZeroContractAddress; + this.parentnetWalletAccount = privateKeyToAccount(config.xdcZero.parentnetWalletPk as Hex); + } } // Initialise the client services async init() { - if (config.xdcZero.parentnetWalletPk) { - this.parentChainWalletAccount = privateKeyToAccount( - config.xdcZero.parentnetWalletPk as Hex - ); - } - const subnetNetworkInformation = - await this.subnetService.getNetworkInformation(); + await this.childnetService.getNetworkInformation(); const subnetInfo = { id: subnetNetworkInformation.NetworkId, name: subnetNetworkInformation.NetworkName, @@ -54,18 +73,18 @@ export class ZeroService { symbol: subnetNetworkInformation.Denom, }, rpcUrls: { - public: { http: [config.subnet.url] }, - default: { http: [config.subnet.url] }, + public: { http: [this.childnetUrl]}, + default: { http: [this.childnetUrl] }, }, }; - this.subnetViemClient = createPublicClient({ + this.childnetViemClient = createPublicClient({ chain: subnetInfo, transport: http(), }); const mainnetNetworkInformation = - await this.mainnetService.getNetworkInformation(); + await this.parentnetService.getNetworkInformation(); const mainnetInfo = { id: mainnetNetworkInformation.NetworkId, name: mainnetNetworkInformation.NetworkName, @@ -76,18 +95,18 @@ export class ZeroService { symbol: mainnetNetworkInformation.Denom, }, rpcUrls: { - public: { http: [config.mainnet.url] }, - default: { http: [config.mainnet.url] }, + public: { http: [this.parentnetUrl] }, + default: { http: [this.parentnetUrl] }, }, }; - this.mainnetViemClient = createPublicClient({ + this.parentnetViemClient = createPublicClient({ chain: mainnetInfo, transport: http(), }); - this.mainnetWalletClient = createWalletClient({ - account: this.parentChainWalletAccount, + this.parentnetWalletClient = createWalletClient({ + account: this.parentnetWalletAccount, chain: mainnetInfo, transport: http(), }); @@ -96,17 +115,18 @@ export class ZeroService { async getPayloads() { const payloads = [] as any; const subnetEndpointContract = { - address: config.xdcZero.subnetZeroContractAddress, + // address: config.xdcZero.subnetZeroContractAddress, + address: this.subnetZeroAddress, abi: endpointABI, }; - const logs = await this.subnetViemClient.getContractEvents({ + const logs = await this.childnetViemClient.getContractEvents({ ...(subnetEndpointContract as any), fromBlock: BigInt(0), eventName: "Packet", }); - const parentChainId = await this.mainnetViemClient.getChainId(); + const parentChainId = await this.parentnetViemClient.getChainId(); logs?.forEach((log) => { const values = decodeAbiParameters( @@ -133,12 +153,13 @@ export class ZeroService { } async getIndexFromParentnet() { - const subnetChainId = await this.subnetViemClient.getChainId(); + const subnetChainId = await this.childnetViemClient.getChainId(); const parentnetEndpointContract = { - address: config.xdcZero.parentChainZeroContractAddress, + // address: config.xdcZero.parentChainZeroContractAddress, + address: this.parentnetZeroAddress, abi: endpointABI, }; - const chain = (await this.mainnetViemClient.readContract({ + const chain = (await this.parentnetViemClient.readContract({ ...(parentnetEndpointContract as any), functionName: "getChain", args: [subnetChainId], @@ -149,10 +170,11 @@ export class ZeroService { async getLatestBlockNumberFromCsc() { const parentnetCSCContract = { - address: config.mainnet.smartContractAddress, + // address: config.mainnet.smartContractAddress, + address: this.parentnetCSCAddress, abi: cscABI, }; - const blocks = (await this.mainnetViemClient.readContract({ + const blocks = (await this.parentnetViemClient.readContract({ ...(parentnetCSCContract as any), functionName: "getLatestBlocks", args: [], @@ -162,7 +184,7 @@ export class ZeroService { } async getProof(txHash: string) { - return this.subnetService.getTransactionAndReceiptProof(txHash); + return this.childnetService.getTransactionAndReceiptProof(txHash); } async validateTransactionProof( @@ -172,18 +194,19 @@ export class ZeroService { blockhash: string ) { const parentnetEndpointContract = { - address: config.xdcZero.parentChainZeroContractAddress, + // address: config.xdcZero.parentChainZeroContractAddress, + address: this.parentnetZeroAddress, abi: endpointABI, }; - const subnetChainId = await this.subnetViemClient.getChainId(); - const { request } = await this.mainnetViemClient.simulateContract({ + const subnetChainId = await this.childnetViemClient.getChainId(); + const { request } = await this.parentnetViemClient.simulateContract({ ...(parentnetEndpointContract as any), functionName: "validateTransactionProof", args: [subnetChainId, key, receiptProof, transactionProof, blockhash], - account: this.parentChainWalletAccount, + account: this.parentnetWalletAccount, }); - const tx = await this.mainnetWalletClient.writeContract(request as any); + const tx = await this.parentnetWalletClient.writeContract(request as any); this.logger.info(tx); } }