From f678b5e3c77fd30faa517093c2468f865be2ce7d Mon Sep 17 00:00:00 2001 From: Michael Kim Date: Tue, 2 Apr 2024 12:39:29 +0900 Subject: [PATCH] Improve the ability to check the liquidity balance of the bridge --- packages/validator/config/config.yaml | 4 +- packages/validator/config/config_test.yaml | 4 +- packages/validator/package.json | 2 +- packages/validator/src/DefaultServer.ts | 4 +- packages/validator/src/common/Config.ts | 20 +++--- .../src/scheduler/BridgeScheduler.ts | 69 ++++++++++++------- packages/validator/src/scheduler/Validator.ts | 8 +-- .../test-local/chain1/docker-compose.yml | 7 -- packages/validator/test-local/localtest.ts | 6 +- packages/validator/test/02-Bridge.test.ts | 4 +- 10 files changed, 71 insertions(+), 57 deletions(-) diff --git a/packages/validator/config/config.yaml b/packages/validator/config/config.yaml index 2f4c6a2..8241b73 100644 --- a/packages/validator/config/config.yaml +++ b/packages/validator/config/config.yaml @@ -39,10 +39,10 @@ scheduler: bridge: networkAName: "production_main" - networkAContractAddress: "0x10Da82287982A7e3E718F9adE892a566F92C1Be2" + networkABridgeAddress: "0x10Da82287982A7e3E718F9adE892a566F92C1Be2" networkATokenAddress: "0x10Da82287982A7e3E718F9adE892a566F92C1Be2" networkBName: "production_side" - networkBContractAddress: "0x10Da82287982A7e3E718F9adE892a566F92C1Be2" + networkBBridgeAddress: "0x10Da82287982A7e3E718F9adE892a566F92C1Be2" networkBTokenAddress: "0x10Da82287982A7e3E718F9adE892a566F92C1Be2" validators: - "${BRIDGE_VALIDATOR1}" diff --git a/packages/validator/config/config_test.yaml b/packages/validator/config/config_test.yaml index 2ef1777..e2e7202 100644 --- a/packages/validator/config/config_test.yaml +++ b/packages/validator/config/config_test.yaml @@ -38,9 +38,9 @@ scheduler: expression: "*/10 * * * * *" bridge: - networkAContractAddress: "0x10Da82287982A7e3E718F9adE892a566F92C1Be2" + networkABridgeAddress: "0x10Da82287982A7e3E718F9adE892a566F92C1Be2" networkAName: "hardhat" - networkBContractAddress: "0x10Da82287982A7e3E718F9adE892a566F92C1Be2" + networkBBridgeAddress: "0x10Da82287982A7e3E718F9adE892a566F92C1Be2" networkBName: "hardhat" validators: - "${BRIDGE_VALIDATOR1}" diff --git a/packages/validator/package.json b/packages/validator/package.json index c3e930a..660dd9a 100644 --- a/packages/validator/package.json +++ b/packages/validator/package.json @@ -69,7 +69,7 @@ "chai": "^4.3.7", "chai-http": "^4.3.7", "cors": "^2.8.5", - "dms-bridge-contracts": "^1.2.1", + "dms-bridge-contracts": "^1.2.3", "dom-parser": "^1.1.5", "dotenv": "^10.0.0", "ethereum-waffle": "^4.0.10", diff --git a/packages/validator/src/DefaultServer.ts b/packages/validator/src/DefaultServer.ts index 5de9dea..dff9a34 100644 --- a/packages/validator/src/DefaultServer.ts +++ b/packages/validator/src/DefaultServer.ts @@ -34,8 +34,8 @@ export class DefaultServer extends WebService { register.clear(); this.metrics = new Metrics(); this.metrics.create("gauge", "status", "serve status"); - this.metrics.createGauge("native_tokens", "native token balance", ["chain"]); - this.metrics.createGauge("main_tokens", "main token balance", ["chain"]); + this.metrics.createGauge("native_token", "native token balance", ["chain"]); + this.metrics.createGauge("main_token", "main token balance", ["chain"]); this.config = config; this.storage = storage; diff --git a/packages/validator/src/common/Config.ts b/packages/validator/src/common/Config.ts index ccb0488..8b9dc15 100644 --- a/packages/validator/src/common/Config.ts +++ b/packages/validator/src/common/Config.ts @@ -224,30 +224,30 @@ export class SchedulerConfig implements ISchedulerConfig { export class BridgeConfig implements IBridgeConfig { public networkAName: string; - public networkAContractAddress: string; + public networkABridgeAddress: string; public networkATokenAddress: string; public networkBName: string; - public networkBContractAddress: string; + public networkBBridgeAddress: string; public networkBTokenAddress: string; public validators: string[]; constructor() { const defaults = BridgeConfig.defaultValue(); this.networkAName = defaults.networkAName; - this.networkAContractAddress = defaults.networkAContractAddress; + this.networkABridgeAddress = defaults.networkABridgeAddress; this.networkATokenAddress = defaults.networkATokenAddress; this.networkBName = defaults.networkBName; - this.networkBContractAddress = defaults.networkBContractAddress; + this.networkBBridgeAddress = defaults.networkBBridgeAddress; this.networkBTokenAddress = defaults.networkBTokenAddress; this.validators = defaults.validators; } public readFromObject(config: IBridgeConfig) { if (config.networkAName !== undefined) this.networkAName = config.networkAName; - if (config.networkAContractAddress !== undefined) this.networkAContractAddress = config.networkAContractAddress; + if (config.networkABridgeAddress !== undefined) this.networkABridgeAddress = config.networkABridgeAddress; if (config.networkATokenAddress !== undefined) this.networkATokenAddress = config.networkATokenAddress; if (config.networkBName !== undefined) this.networkBName = config.networkBName; - if (config.networkBContractAddress !== undefined) this.networkBContractAddress = config.networkBContractAddress; + if (config.networkBBridgeAddress !== undefined) this.networkBBridgeAddress = config.networkBBridgeAddress; if (config.networkBTokenAddress !== undefined) this.networkBTokenAddress = config.networkBTokenAddress; if (config.validators !== undefined) this.validators = config.validators; } @@ -258,10 +258,10 @@ export class BridgeConfig implements IBridgeConfig { public static defaultValue(): IBridgeConfig { return { networkAName: "", - networkAContractAddress: "", + networkABridgeAddress: "", networkATokenAddress: "", networkBName: "", - networkBContractAddress: "", + networkBBridgeAddress: "", networkBTokenAddress: "", validators: [], } as unknown as IBridgeConfig; @@ -302,10 +302,10 @@ export interface ISchedulerConfig { export interface IBridgeConfig { networkAName: string; - networkAContractAddress: string; + networkABridgeAddress: string; networkATokenAddress: string; networkBName: string; - networkBContractAddress: string; + networkBBridgeAddress: string; networkBTokenAddress: string; validators: string[]; } diff --git a/packages/validator/src/scheduler/BridgeScheduler.ts b/packages/validator/src/scheduler/BridgeScheduler.ts index f89d58e..6567f93 100644 --- a/packages/validator/src/scheduler/BridgeScheduler.ts +++ b/packages/validator/src/scheduler/BridgeScheduler.ts @@ -1,5 +1,5 @@ import "@nomiclabs/hardhat-ethers"; -import { BIP20DelegatedTransfer } from "../../typechain-types"; +import { BIP20DelegatedTransfer, IBridge, IBridge__factory } from "../../typechain-types"; import { Config } from "../common/Config"; import { logger } from "../common/Logger"; import { ValidatorStorage } from "../storage/ValidatorStorage"; @@ -22,6 +22,12 @@ export class BridgeScheduler extends Scheduler { private _tokenA: BIP20DelegatedTransfer | undefined; private _tokenB: BIP20DelegatedTransfer | undefined; + private _bridgeA: IBridge | undefined; + private _bridgeB: IBridge | undefined; + + private _tokenIdA: string | undefined; + private _tokenIdB: string | undefined; + private old_time_stamp: number; private new_time_stamp: number; @@ -74,50 +80,63 @@ export class BridgeScheduler extends Scheduler { public async onStart() { await hre.changeNetwork(this.config.bridge.networkAName); + + this._bridgeA = new hre.ethers.Contract( + this.config.bridge.networkABridgeAddress, + IBridge__factory.createInterface(), + hre.ethers.provider + ) as IBridge; const factoryA = await hre.ethers.getContractFactory("BIP20DelegatedTransfer"); this._tokenA = factoryA.attach(this.config.bridge.networkATokenAddress); + this._tokenIdA = ContractUtils.getTokenId(await this._tokenA.name(), await this._tokenA.symbol()); console.log("Chain A: ", this.config.bridge.networkAName); - console.log(" : ", this.config.bridge.networkAContractAddress); + console.log(" : ", this.config.bridge.networkABridgeAddress); const balanceA1 = new BOACoin( - await this._tokenA.provider.getBalance(this.config.bridge.networkAContractAddress) + await this._bridgeA.provider.getBalance(this.config.bridge.networkABridgeAddress) ); console.log("BOA : ", balanceA1.toDisplayString(true, 2)); - const balanceA2 = new BOACoin(await this._tokenA.balanceOf(this.config.bridge.networkAContractAddress)); + + const balanceA2 = new BOACoin(await this._bridgeA.getTotalLiquidity(this._tokenIdA)); console.log("Token : ", balanceA2.toDisplayString(true, 2)); await hre.changeNetwork(this.config.bridge.networkBName); + this._bridgeB = new hre.ethers.Contract( + this.config.bridge.networkBBridgeAddress, + IBridge__factory.createInterface(), + hre.ethers.provider + ) as IBridge; const factoryB = await hre.ethers.getContractFactory("BIP20DelegatedTransfer"); this._tokenB = factoryB.attach(this.config.bridge.networkBTokenAddress); + this._tokenIdB = ContractUtils.getTokenId(await this._tokenB.name(), await this._tokenB.symbol()); console.log("Chain B: ", this.config.bridge.networkBName); - console.log(" : ", this.config.bridge.networkBContractAddress); - const balanceB1 = new BOACoin( - await this._tokenB.provider.getBalance(this.config.bridge.networkBContractAddress) - ); + console.log(" : ", this.config.bridge.networkBBridgeAddress); + const balanceB1 = new BOACoin(await this._tokenB.provider.getBalance(this.config.bridge.networkBBridgeAddress)); console.log("BOA : ", balanceB1.toDisplayString(true, 2)); - const balanceB2 = new BOACoin(await this._tokenB.balanceOf(this.config.bridge.networkBContractAddress)); + const balanceB2 = new BOACoin(await this._bridgeB.getTotalLiquidity(this._tokenIdB)); console.log("Token : ", balanceB2.toDisplayString(true, 2)); } protected async work() { try { this.new_time_stamp = ContractUtils.getTimeStamp(); - const old_source_period = Math.floor(this.old_time_stamp / 2); - const new_source_period = Math.floor(this.new_time_stamp / 2); + const old_source_period = Math.floor(this.old_time_stamp / 60); + const new_source_period = Math.floor(this.new_time_stamp / 60); if (old_source_period !== new_source_period) { - if (this._tokenA !== undefined && this._tokenB !== undefined) { - const balanceA1 = await this._tokenA.provider.getBalance( - this.config.bridge.networkAContractAddress - ); - this.metrics.gaugeLabels("native_tokens", { name: "A" }, balanceA1.div(1_000_000_000).toNumber()); - const balanceA2 = await this._tokenA.balanceOf(this.config.bridge.networkAContractAddress); - this.metrics.gaugeLabels("main_tokens", { name: "A" }, balanceA2.div(1_000_000_000).toNumber()); - - const balanceB1 = await this._tokenB.provider.getBalance( - this.config.bridge.networkBContractAddress - ); - this.metrics.gaugeLabels("native_tokens", { name: "B" }, balanceB1.div(1_000_000_000).toNumber()); - const balanceB2 = await this._tokenB.balanceOf(this.config.bridge.networkBContractAddress); - this.metrics.gaugeLabels("main_tokens", { name: "B" }, balanceB2.div(1_000_000_000).toNumber()); + if ( + this._bridgeA !== undefined && + this._bridgeB !== undefined && + this._tokenIdA !== undefined && + this._tokenIdB !== undefined + ) { + const balanceA1 = await this._bridgeA.provider.getBalance(this.config.bridge.networkABridgeAddress); + this.metrics.gaugeLabels("native_token", { chain: "A" }, balanceA1.div(1_000_000_000).toNumber()); + const balanceA2 = await this._bridgeA.getTotalLiquidity(this._tokenIdA); + this.metrics.gaugeLabels("main_token", { chain: "A" }, balanceA2.div(1_000_000_000).toNumber()); + + const balanceB1 = await this._bridgeB.provider.getBalance(this.config.bridge.networkBBridgeAddress); + this.metrics.gaugeLabels("native_token", { chain: "B" }, balanceB1.div(1_000_000_000).toNumber()); + const balanceB2 = await this._bridgeB.getTotalLiquidity(this._tokenIdB); + this.metrics.gaugeLabels("main_token", { chain: "B" }, balanceB2.div(1_000_000_000).toNumber()); } this.old_time_stamp = this.new_time_stamp; } diff --git a/packages/validator/src/scheduler/Validator.ts b/packages/validator/src/scheduler/Validator.ts index d766750..570db3b 100644 --- a/packages/validator/src/scheduler/Validator.ts +++ b/packages/validator/src/scheduler/Validator.ts @@ -25,7 +25,7 @@ export class Validator { storage, ValidatorType.A, config.bridge.networkAName, - config.bridge.networkAContractAddress, + config.bridge.networkABridgeAddress, 1n, this.wallet ); @@ -34,7 +34,7 @@ export class Validator { storage, ValidatorType.B, config.bridge.networkBName, - config.bridge.networkBContractAddress, + config.bridge.networkBBridgeAddress, 1n, this.wallet ); @@ -45,7 +45,7 @@ export class Validator { config.bridge.networkAName, ValidatorType.B, config.bridge.networkBName, - config.bridge.networkBContractAddress, + config.bridge.networkBBridgeAddress, this.wallet ); @@ -55,7 +55,7 @@ export class Validator { config.bridge.networkBName, ValidatorType.A, config.bridge.networkAName, - config.bridge.networkAContractAddress, + config.bridge.networkABridgeAddress, this.wallet ); } diff --git a/packages/validator/test-local/chain1/docker-compose.yml b/packages/validator/test-local/chain1/docker-compose.yml index 3ce0e20..87afde0 100644 --- a/packages/validator/test-local/chain1/docker-compose.yml +++ b/packages/validator/test-local/chain1/docker-compose.yml @@ -20,11 +20,4 @@ services: --mine=true --miner.etherbase=0xF85B2dbA83A08DEe6E24E4461E0334e7384896F9 --http.corsdomain="*" - networks: - - bosagora_network -networks: - bosagora_network: - driver: bridge - external: true - internal: true diff --git a/packages/validator/test-local/localtest.ts b/packages/validator/test-local/localtest.ts index 434452d..65b8c8d 100644 --- a/packages/validator/test-local/localtest.ts +++ b/packages/validator/test-local/localtest.ts @@ -54,14 +54,16 @@ describe("Test for Bridge", function () { await deploymentsA.doDeployAll(); tokenAContract = deploymentsA.getContract("TestKIOS") as BIP20DelegatedTransfer; bridgeAContract = deploymentsA.getContract("Bridge") as Bridge; - config.bridge.networkAContractAddress = bridgeAContract.address; + config.bridge.networkABridgeAddress = bridgeAContract.address; + config.bridge.networkATokenAddress = tokenAContract.address; await hre.changeNetwork(config.bridge.networkBName); deploymentsB = new Deployments(config, config.bridge.networkBName); await deploymentsB.doDeployAll(); tokenBContract = deploymentsB.getContract("TestKIOS") as BIP20DelegatedTransfer; bridgeBContract = deploymentsB.getContract("Bridge") as Bridge; - config.bridge.networkBContractAddress = bridgeBContract.address; + config.bridge.networkBBridgeAddress = bridgeBContract.address; + config.bridge.networkBTokenAddress = tokenBContract.address; }); it("Create TestServer", async () => { diff --git a/packages/validator/test/02-Bridge.test.ts b/packages/validator/test/02-Bridge.test.ts index 46fbeeb..9f04c52 100644 --- a/packages/validator/test/02-Bridge.test.ts +++ b/packages/validator/test/02-Bridge.test.ts @@ -67,9 +67,9 @@ describe("Test for Bridge", () => { }); before("Create Config", async () => { - config.bridge.networkAContractAddress = bridgeAContract.address; + config.bridge.networkABridgeAddress = bridgeAContract.address; config.bridge.networkATokenAddress = tokenAContract.address; - config.bridge.networkBContractAddress = bridgeBContract.address; + config.bridge.networkBBridgeAddress = bridgeBContract.address; config.bridge.networkBTokenAddress = tokenBContract.address; });