Skip to content

Commit

Permalink
Improve the ability to check the liquidity balance of the bridge
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelKim20 committed Apr 2, 2024
1 parent c0dcf28 commit f678b5e
Show file tree
Hide file tree
Showing 10 changed files with 71 additions and 57 deletions.
4 changes: 2 additions & 2 deletions packages/validator/config/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand Down
4 changes: 2 additions & 2 deletions packages/validator/config/config_test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ scheduler:
expression: "*/10 * * * * *"

bridge:
networkAContractAddress: "0x10Da82287982A7e3E718F9adE892a566F92C1Be2"
networkABridgeAddress: "0x10Da82287982A7e3E718F9adE892a566F92C1Be2"
networkAName: "hardhat"
networkBContractAddress: "0x10Da82287982A7e3E718F9adE892a566F92C1Be2"
networkBBridgeAddress: "0x10Da82287982A7e3E718F9adE892a566F92C1Be2"
networkBName: "hardhat"
validators:
- "${BRIDGE_VALIDATOR1}"
Expand Down
2 changes: 1 addition & 1 deletion packages/validator/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
4 changes: 2 additions & 2 deletions packages/validator/src/DefaultServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
20 changes: 10 additions & 10 deletions packages/validator/src/common/Config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;
Expand Down Expand Up @@ -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[];
}
Expand Down
69 changes: 44 additions & 25 deletions packages/validator/src/scheduler/BridgeScheduler.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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;

Expand Down Expand Up @@ -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;
}
Expand Down
8 changes: 4 additions & 4 deletions packages/validator/src/scheduler/Validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export class Validator {
storage,
ValidatorType.A,
config.bridge.networkAName,
config.bridge.networkAContractAddress,
config.bridge.networkABridgeAddress,
1n,
this.wallet
);
Expand All @@ -34,7 +34,7 @@ export class Validator {
storage,
ValidatorType.B,
config.bridge.networkBName,
config.bridge.networkBContractAddress,
config.bridge.networkBBridgeAddress,
1n,
this.wallet
);
Expand All @@ -45,7 +45,7 @@ export class Validator {
config.bridge.networkAName,
ValidatorType.B,
config.bridge.networkBName,
config.bridge.networkBContractAddress,
config.bridge.networkBBridgeAddress,
this.wallet
);

Expand All @@ -55,7 +55,7 @@ export class Validator {
config.bridge.networkBName,
ValidatorType.A,
config.bridge.networkAName,
config.bridge.networkAContractAddress,
config.bridge.networkABridgeAddress,
this.wallet
);
}
Expand Down
7 changes: 0 additions & 7 deletions packages/validator/test-local/chain1/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,4 @@ services:
--mine=true
--miner.etherbase=0xF85B2dbA83A08DEe6E24E4461E0334e7384896F9
--http.corsdomain="*"
networks:
- bosagora_network

networks:
bosagora_network:
driver: bridge
external: true
internal: true
6 changes: 4 additions & 2 deletions packages/validator/test-local/localtest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down
4 changes: 2 additions & 2 deletions packages/validator/test/02-Bridge.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});

Expand Down

0 comments on commit f678b5e

Please sign in to comment.