Skip to content

Commit

Permalink
[Validator] Connect additional providers to two chains
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelKim20 committed Apr 25, 2024
1 parent bdbd918 commit f08c17b
Show file tree
Hide file tree
Showing 12 changed files with 222 additions and 63 deletions.
52 changes: 52 additions & 0 deletions packages/validator/config/config-local.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
################################################################################
## Network interface ##
################################################################################
server:
# Address to which we bind (default: 127.0.0.1)
address: "${HOST_IP}"
# Port on which we bind (default: 7070)
port: "${HOST_PORT}"

################################################################################
## Database options ##
################################################################################
database:
host: "${DATABASE_HOST}"
user: "${DATABASE_USER}"
password: "${DATABASE_PASSWORD}"
database: "${DATABASE_NAME}"
scheme: "${DATABASE_SCHEME}"
port: "${DATABASE_PORT}"
connectionTimeoutMillis: 5000
max: 100

################################################################################
## Logging options ##
################################################################################
logging:
# Values: error, warn, info, http, verbose, debug, silly
level: verbose

################################################################################
## Scheduler options ##
################################################################################
scheduler:
enable: true
items:
- name: bridge
enable: true
expression: "*/10 * * * * *"

bridge:
networkAName: "production_main"
networkABridgeAddress: "0x4Ca91738C7cD24895467c6d550D96BE8dC4b33AA"
networkATokenAddress: "0xB1A90a5C6e30d64Ab6f64C30eD392F46eDBcb022"
networkBName: "production_side"
networkBBridgeAddress: "0xd73e6a2f2e47236F1Ff737E72497f598652122F9"
networkBTokenAddress: "0xB1A90a5C6e30d64Ab6f64C30eD392F46eDBcb022"
validators:
- "${BRIDGE_VALIDATOR1}"
- "${BRIDGE_VALIDATOR2}"
- "${BRIDGE_VALIDATOR3}"
- "${BRIDGE_VALIDATOR4}"
- "${BRIDGE_VALIDATOR5}"
52 changes: 52 additions & 0 deletions packages/validator/config/config-testnet.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
################################################################################
## Network interface ##
################################################################################
server:
# Address to which we bind (default: 127.0.0.1)
address: "${HOST_IP}"
# Port on which we bind (default: 7070)
port: "${HOST_PORT}"

################################################################################
## Database options ##
################################################################################
database:
host: "${DATABASE_HOST}"
user: "${DATABASE_USER}"
password: "${DATABASE_PASSWORD}"
database: "${DATABASE_NAME}"
scheme: "${DATABASE_SCHEME}"
port: "${DATABASE_PORT}"
connectionTimeoutMillis: 5000
max: 100

################################################################################
## Logging options ##
################################################################################
logging:
# Values: error, warn, info, http, verbose, debug, silly
level: verbose

################################################################################
## Scheduler options ##
################################################################################
scheduler:
enable: true
items:
- name: bridge
enable: true
expression: "*/10 * * * * *"

bridge:
networkAName: "production_main"
networkABridgeAddress: "0x7265F4D550AEdc3Ed97a6ae6dDAf4289eDFaC0C5"
networkATokenAddress: "0x3A90432BBfd5Bd87788f397F46c24685492fb30E"
networkBName: "production_side"
networkBBridgeAddress: "0x6DA3F9D26C5964ab6E3902fFD7907B6bc3465493"
networkBTokenAddress: "0x3A90432BBfd5Bd87788f397F46c24685492fb30E"
validators:
- "${BRIDGE_VALIDATOR1}"
- "${BRIDGE_VALIDATOR2}"
- "${BRIDGE_VALIDATOR3}"
- "${BRIDGE_VALIDATOR4}"
- "${BRIDGE_VALIDATOR5}"
8 changes: 4 additions & 4 deletions packages/validator/config/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ scheduler:

bridge:
networkAName: "production_main"
networkABridgeAddress: "0x4Ca91738C7cD24895467c6d550D96BE8dC4b33AA"
networkATokenAddress: "0xB1A90a5C6e30d64Ab6f64C30eD392F46eDBcb022"
networkABridgeAddress: "0x7265F4D550AEdc3Ed97a6ae6dDAf4289eDFaC0C5"
networkATokenAddress: "0x3A90432BBfd5Bd87788f397F46c24685492fb30E"
networkBName: "production_side"
networkBBridgeAddress: "0xd73e6a2f2e47236F1Ff737E72497f598652122F9"
networkBTokenAddress: "0xB1A90a5C6e30d64Ab6f64C30eD392F46eDBcb022"
networkBBridgeAddress: "0x6DA3F9D26C5964ab6E3902fFD7907B6bc3465493"
networkBTokenAddress: "0x3A90432BBfd5Bd87788f397F46c24685492fb30E"
validators:
- "${BRIDGE_VALIDATOR1}"
- "${BRIDGE_VALIDATOR2}"
Expand Down
34 changes: 34 additions & 0 deletions packages/validator/env/.env.testnet
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# HOST SERVER
HOST_IP=0.0.0.0
HOST_PORT=7500

# CHAIN URL
PRODUCTION_MAIN_URL=https://testnet.bosagora.org
PRODUCTION_MAIN_CHAIN_ID=2019
PRODUCTION_SIDE_URL=https://rpc.test.kios.bosagora.org
PRODUCTION_SIDE_CHAIN_ID=215115

# DATABASE INFO
DATABASE_HOST=127.0.0.1
DATABASE_USER=agora
DATABASE_NAME=bridge-loyalty
DATABASE_SCHEME=
DATABASE_PASSWORD=12345678
DATABASE_PORT=5432

# 0x6F8CF905906dDe9E440F0DF5B26146cf1f195F12
DEPLOYER=0x0d451ab5bd459f59fda0cf8018cfbfa4df5c454c5c25a844b70a5afcaf246c98
# 0x6629E54deC62B88b34d6fF7439B290cC0f1bfaEC
FEE=0xcfefe53d3c74cda9aa7b0af55d0b615cdcf7d4ed3b8653fb87fb2259f083f007
# 0x035DEa1B3427B7118B0750aA3e2341dBf3eB521d
BRIDGE_VALIDATOR1=0xdc09a76275fd2e8cfc8e5bf3cd2d04d4ff1be591d5b852f9cf939adeef1a09f2
# 0xF8d80AB87DFd751d9B045FE68ACB47eBaa6fa361
BRIDGE_VALIDATOR2=0xe77a456994aa357ed3a7fa20974445ba55a752593499901182ec24892042861f
# 0xf762Eea24A9B751D49159478214d37c414FE9037
BRIDGE_VALIDATOR3=0x26d91992f8f68e10e398d0e752713d48bbe279e3f33f99930b7c570f4d1044ea
# 0x060c3D1ec7EE75FB45DC229B3ed58167AD826740
BRIDGE_VALIDATOR4=0xdc4ecf707e85aa04b1c107f211af1294ce235192dd06e048cc527b0adc07955a
# 0x443bc4b6D445459047423Eae577d6E01eE490B78
BRIDGE_VALIDATOR5=0x5b620b1429056d3429647d06d460236adbde78b12bb1226a29a1896a1e6d35cd

REPORT_GAS=true
2 changes: 1 addition & 1 deletion packages/validator/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
"dotenv": "^10.0.0",
"ethereum-waffle": "^4.0.10",
"ethereumjs-util": "^7.1.5",
"ethers": "npm:boa-ethers2@^5.7.9",
"ethers": "^5.7.0",
"expo-server-sdk": "^3.7.0",
"express": "^4.17.1",
"express-validator": "^6.14.0",
Expand Down
3 changes: 0 additions & 3 deletions packages/validator/src/DefaultServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ export class DefaultServer extends WebService {

public readonly defaultRouter: DefaultRouter;
public readonly storage: ValidatorStorage;
public readonly validators: Validator[];

/**
* Constructor
Expand All @@ -40,7 +39,6 @@ export class DefaultServer extends WebService {
this.config = config;
this.storage = storage;
this.defaultRouter = new DefaultRouter(this, this.metrics);
this.validators = this.config.bridge.validators.map((m) => new Validator(this.config, this.storage, m));

if (!schedules) schedules = [];
schedules.forEach((m) => this.schedules.push(m));
Expand All @@ -49,7 +47,6 @@ export class DefaultServer extends WebService {
config: this.config,
storage: this.storage,
metrics: this.metrics,
validators: this.validators,
})
);
}
Expand Down
57 changes: 38 additions & 19 deletions packages/validator/src/scheduler/BridgeScheduler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ export class BridgeScheduler extends Scheduler {
private _tokenIdA: string | undefined;
private _tokenIdB: string | undefined;

private _providerA: ethers.providers.Provider | undefined;
private _providerB: ethers.providers.Provider | undefined;

private old_time_stamp: number;
private new_time_stamp: number;

Expand Down Expand Up @@ -73,47 +76,63 @@ export class BridgeScheduler extends Scheduler {
if (options) {
if (options.config && options.config instanceof Config) this._config = options.config;
if (options.storage && options.storage instanceof ValidatorStorage) this._storage = options.storage;
if (options.validators) this._validators = options.validators;
if (options.metrics && options.metrics instanceof Metrics) this._metrics = options.metrics;
}
}

public async onStart() {
console.log("Chain A : ", this.config.bridge.networkAName);
console.log(" Bridge : ", this.config.bridge.networkABridgeAddress);
console.log(" Token : ", this.config.bridge.networkATokenAddress);
console.log("Chain B : ", this.config.bridge.networkBName);
console.log(" Bridge : ", this.config.bridge.networkBBridgeAddress);
console.log(" Token : ", this.config.bridge.networkBTokenAddress);
console.log("");

await hre.changeNetwork(this.config.bridge.networkAName);
this._providerA = hre.ethers.provider;

this._bridgeA = new hre.ethers.Contract(
this.config.bridge.networkABridgeAddress,
IBridge__factory.createInterface(),
hre.ethers.provider
) as IBridge;
this._providerA
).connect(this._providerA) as IBridge;
const factoryA = await hre.ethers.getContractFactory("BIP20DelegatedTransfer");
this._tokenA = factoryA.attach(this.config.bridge.networkATokenAddress);
this._tokenA = factoryA.attach(this.config.bridge.networkATokenAddress).connect(this._providerA);
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.networkABridgeAddress);
const balanceA1 = new BOACoin(
await this._bridgeA.provider.getBalance(this.config.bridge.networkABridgeAddress)
);
console.log("BOA : ", balanceA1.toDisplayString(true, 2));

const balanceA1 = new BOACoin(await this._tokenA.provider.getBalance(this.config.bridge.networkABridgeAddress));
console.log("Chain A Balance : ");
console.log(" BOA : ", balanceA1.toDisplayString(true, 2));
const balanceA2 = new BOACoin(await this._bridgeA.getTotalLiquidity(this._tokenIdA));
console.log("Token : ", balanceA2.toDisplayString(true, 2));
console.log(" Token : ", balanceA2.toDisplayString(true, 2));

await hre.changeNetwork(this.config.bridge.networkBName);
this._providerB = hre.ethers.provider;
this._bridgeB = new hre.ethers.Contract(
this.config.bridge.networkBBridgeAddress,
IBridge__factory.createInterface(),
hre.ethers.provider
) as IBridge;
this._providerB
).connect(this._providerB) as IBridge;
const factoryB = await hre.ethers.getContractFactory("BIP20DelegatedTransfer");
this._tokenB = factoryB.attach(this.config.bridge.networkBTokenAddress);
this._tokenB = factoryB.attach(this.config.bridge.networkBTokenAddress).connect(this._providerB);
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.networkBBridgeAddress);
const balanceB1 = new BOACoin(await this._tokenB.provider.getBalance(this.config.bridge.networkBBridgeAddress));
console.log("BOA : ", balanceB1.toDisplayString(true, 2));
console.log("Chain B Balance : ");
console.log(" BOA : ", balanceB1.toDisplayString(true, 2));
const balanceB2 = new BOACoin(await this._bridgeB.getTotalLiquidity(this._tokenIdB));
console.log("Token : ", balanceB2.toDisplayString(true, 2));
console.log(" Token : ", balanceB2.toDisplayString(true, 2));

const info = {
providerA: this._providerA,
providerB: this._providerB,
tokenA: this._tokenA,
tokenB: this._tokenB,
bridgeA: this._bridgeA,
bridgeB: this._bridgeB,
tokenIdA: this._tokenIdA,
tokenIdB: this._tokenIdB,
};
this._validators = this.config.bridge.validators.map((m) => new Validator(this.config, this.storage, m, info));
}

protected async work() {
Expand Down
11 changes: 4 additions & 7 deletions packages/validator/src/scheduler/EventCollector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export class EventCollector {
private readonly contractAddress: string;
private readonly startNumber: bigint;
private storage: ValidatorStorage;
private provider: Provider | undefined;
private provider: Provider;
private interfaceOfBridge: IBridgeInterface | undefined;

constructor(
Expand All @@ -25,22 +25,19 @@ export class EventCollector {
network: string,
contractAddress: string,
startBlockNumber: bigint,
wallet: Wallet
wallet: Wallet,
provider: Provider
) {
this.storage = storage;
this.type = type;
this.network = network;
this.contractAddress = contractAddress;
this.startNumber = startBlockNumber;
this.wallet = wallet;
this.provider = provider;
}

public async work() {
if (this.provider === undefined) {
await hre.changeNetwork(this.network);
this.provider = hre.ethers.provider;
}

if (this.interfaceOfBridge === undefined) {
this.interfaceOfBridge = IBridge__factory.createInterface();
}
Expand Down
29 changes: 10 additions & 19 deletions packages/validator/src/scheduler/Executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,33 +20,30 @@ export class Executor {
private readonly targetType: ValidatorType;
private readonly sourceNetwork: string;
private readonly targetNetwork: string;
private readonly targetContractAddress: string;
private targetProvider: Provider | undefined;
private targetProvider: Provider;
private targetBridge: IBridge;

constructor(
storage: ValidatorStorage,
sourceType: ValidatorType,
sourceNetwork: string,
targetType: ValidatorType,
targetNetwork: string,
targetContractAddress: string,
wallet: Wallet
wallet: Wallet,
targetProvider: Provider,
targetBridge: IBridge
) {
this.storage = storage;
this.sourceType = sourceType;
this.targetType = targetType;
this.sourceNetwork = sourceNetwork;
this.targetNetwork = targetNetwork;
this.targetContractAddress = targetContractAddress;
this.wallet = new Wallet(wallet.privateKey);
this.targetProvider = targetProvider;
this.targetBridge = targetBridge;
}

public async work() {
if (this.targetProvider === undefined) {
await hre.changeNetwork(this.targetNetwork);
this.targetProvider = hre.ethers.provider;
}

const events = await this.storage.getNotExecutedEvents(
this.wallet.address,
this.sourceType,
Expand All @@ -56,16 +53,10 @@ export class Executor {

const signer = new NonceManager(new GasPriceManager(this.wallet.connect(this.targetProvider)));

const contract = new hre.ethers.Contract(
this.targetContractAddress,
IBridge__factory.createInterface(),
this.targetProvider
) as IBridge;

for (const event of events) {
const status = await contract.getWithdrawInfo(event.depositId);
const status = await this.targetBridge.getWithdrawInfo(event.depositId);
if (!status.executed) {
const confirmed = await contract.isConfirmedOf(event.depositId, this.wallet.address);
const confirmed = await this.targetBridge.isConfirmedOf(event.depositId, this.wallet.address);
if (!confirmed) {
if (
event.withdrawStatus < WithdrawStatus.Sent ||
Expand All @@ -76,7 +67,7 @@ export class Executor {
logger.info(
`[${this.wallet.address}]-[${this.targetNetwork}]: Starting Withdraw [${event.depositId}]`
);
const tx = await contract
const tx = await this.targetBridge
.connect(signer)
.withdrawFromBridge(event.tokenId, event.depositId, event.account, event.amount);

Expand Down
Loading

0 comments on commit f08c17b

Please sign in to comment.