Skip to content

Commit

Permalink
[Relay] Add endpoints for chain information
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelKim20 committed Apr 3, 2024
1 parent 475dbe7 commit 289ed75
Show file tree
Hide file tree
Showing 6 changed files with 200 additions and 49 deletions.
52 changes: 40 additions & 12 deletions packages/relay/src/contract/ContractManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { logger } from "../common/Logger";

import { ethers } from "ethers";
import * as hre from "hardhat";
import { HttpNetworkConfig } from "hardhat/src/types/config";

export class ContractManager {
private readonly config: Config;
Expand All @@ -29,17 +30,21 @@ export class ContractManager {
private _sideLoyaltyExchangerContract: LoyaltyExchanger | undefined;
private _sideLoyaltyTransferContract: LoyaltyTransfer | undefined;
private _sideLoyaltyBridgeContract: LoyaltyBridge | undefined;
private _sideChainBridge: Bridge | undefined;
private _sideChainBridgeContract: Bridge | undefined;

private _mainTokenContract: BIP20DelegatedTransfer | undefined;
private _mainLoyaltyBridgeContract: Bridge | undefined;
private _mainChainBridge: Bridge | undefined;
private _mainChainBridgeContract: Bridge | undefined;

private _sideChainProvider: ethers.providers.Provider | undefined;
private _mainChainProvider: ethers.providers.Provider | undefined;

private _sideChainId: number | undefined;
private _mainChainId: number | undefined;

private _sideChainURL: string | undefined;
private _mainChainURL: string | undefined;

constructor(config: Config) {
this.config = config;
}
Expand All @@ -49,6 +54,10 @@ export class ContractManager {
this._sideChainProvider = hre.ethers.provider;
this._sideChainId = (await this._sideChainProvider.getNetwork()).chainId;

const hardhatConfig = hre.config.networks[this.config.contracts.sideChain.network] as HttpNetworkConfig;
if (hardhatConfig.url !== undefined) this._sideChainURL = hardhatConfig.url;
else this._sideChainURL = "";

const factory1 = await hre.ethers.getContractFactory("BIP20DelegatedTransfer");
this._sideTokenContract = factory1.attach(this.config.contracts.sideChain.tokenAddress);

Expand Down Expand Up @@ -80,10 +89,15 @@ export class ContractManager {
this._sideLoyaltyBridgeContract = factory9.attach(this.config.contracts.sideChain.loyaltyBridgeAddress);

const factory10 = await hre.ethers.getContractFactory("Bridge");
this._sideChainBridge = factory10.attach(this.config.contracts.sideChain.chainBridgeAddress);
this._sideChainBridgeContract = factory10.attach(this.config.contracts.sideChain.chainBridgeAddress);

await hre.changeNetwork(this.config.contracts.mainChain.network);
this._mainChainProvider = hre.ethers.provider;
this._mainChainId = (await this._mainChainProvider.getNetwork()).chainId;

const hardhatConfig2 = hre.config.networks[this.config.contracts.mainChain.network] as HttpNetworkConfig;
if (hardhatConfig2.url !== undefined) this._mainChainURL = hardhatConfig2.url;
else this._mainChainURL = "";

const factory11 = await hre.ethers.getContractFactory("BIP20DelegatedTransfer");
this._mainTokenContract = factory11.attach(this.config.contracts.mainChain.tokenAddress);
Expand All @@ -92,9 +106,7 @@ export class ContractManager {
this._mainLoyaltyBridgeContract = factory12.attach(this.config.contracts.mainChain.loyaltyBridgeAddress);

const factory13 = await hre.ethers.getContractFactory("Bridge");
this._mainChainBridge = factory13.attach(this.config.contracts.mainChain.chainBridgeAddress);

this._mainChainId = (await this._mainChainProvider.getNetwork()).chainId;
this._mainChainBridgeContract = factory13.attach(this.config.contracts.mainChain.chainBridgeAddress);
}

public get mainChainProvider(): ethers.providers.Provider {
Expand All @@ -113,6 +125,14 @@ export class ContractManager {
}
}

public get mainChainURL(): string {
if (this._mainChainURL !== undefined) return this._mainChainURL;
else {
logger.error("mainChainURL is not ready yet.");
process.exit(1);
}
}

public get sideChainProvider(): ethers.providers.Provider {
if (this._sideChainProvider !== undefined) return this._sideChainProvider;
else {
Expand All @@ -129,6 +149,14 @@ export class ContractManager {
}
}

public get sideChainURL(): string {
if (this._sideChainURL !== undefined) return this._sideChainURL;
else {
logger.error("sideChainURL is not ready yet.");
process.exit(1);
}
}

public get sideTokenContract(): BIP20DelegatedTransfer {
if (this._sideTokenContract !== undefined) return this._sideTokenContract;
else {
Expand Down Expand Up @@ -209,10 +237,10 @@ export class ContractManager {
}
}

public get sideChainBridge(): Bridge {
if (this._sideChainBridge !== undefined) return this._sideChainBridge;
public get sideChainBridgeContract(): Bridge {
if (this._sideChainBridgeContract !== undefined) return this._sideChainBridgeContract;
else {
logger.error("sideChainBridge is not ready yet.");
logger.error("sideChainBridgeContract is not ready yet.");
process.exit(1);
}
}
Expand All @@ -233,10 +261,10 @@ export class ContractManager {
}
}

public get mainChainBridge(): Bridge {
if (this._mainChainBridge !== undefined) return this._mainChainBridge;
public get mainChainBridgeContract(): Bridge {
if (this._mainChainBridgeContract !== undefined) return this._mainChainBridgeContract;
else {
logger.error("mainChainBridge is not ready yet.");
logger.error("mainChainBridgeContract is not ready yet.");
process.exit(1);
}
}
Expand Down
23 changes: 15 additions & 8 deletions packages/relay/src/routers/BridgeRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,10 +159,17 @@ export class BridgeRouter {
}
}

private async getDepositId(account: string): Promise<string> {
private async getDepositIdMainChain(account: string): Promise<string> {
while (true) {
const id = ContractUtils.getRandomId(account);
if (await this.contractManager.sideChainBridge.isAvailableDepositId(id)) return id;
if (await this.contractManager.mainChainBridgeContract.isAvailableDepositId(id)) return id;
}
}

private async getDepositIdSideChain(account: string): Promise<string> {
while (true) {
const id = ContractUtils.getRandomId(account);
if (await this.contractManager.sideChainBridgeContract.isAvailableDepositId(id)) return id;
}
}

Expand All @@ -186,7 +193,7 @@ export class BridgeRouter {
const nonce = await this.contractManager.sideTokenContract.nonceOf(account);
const message = ContractUtils.getTransferMessage(
account,
this.contractManager.sideChainBridge.address,
this.contractManager.sideChainBridgeContract.address,
amount,
nonce,
this.contractManager.sideChainId
Expand All @@ -198,8 +205,8 @@ export class BridgeRouter {
await this.contractManager.sideTokenContract.name(),
await this.contractManager.sideTokenContract.symbol()
);
const depositId = await this.getDepositId(account);
const tx = await this.contractManager.sideChainBridge
const depositId = await this.getDepositIdSideChain(account);
const tx = await this.contractManager.sideChainBridgeContract
.connect(signerItem.signer)
.depositToBridge(tokenId, depositId, account, amount, signature);

Expand Down Expand Up @@ -234,7 +241,7 @@ export class BridgeRouter {
const nonce = await this.contractManager.mainTokenContract.nonceOf(account);
const message = ContractUtils.getTransferMessage(
account,
this.contractManager.mainChainBridge.address,
this.contractManager.mainChainBridgeContract.address,
amount,
nonce,
this.contractManager.mainChainId
Expand All @@ -246,8 +253,8 @@ export class BridgeRouter {
await this.contractManager.mainTokenContract.name(),
await this.contractManager.mainTokenContract.symbol()
);
const depositId = await this.getDepositId(account);
const tx = await this.contractManager.mainChainBridge
const depositId = await this.getDepositIdMainChain(account);
const tx = await this.contractManager.mainChainBridgeContract
.connect(signerItem.signer)
.depositToBridge(tokenId, depositId, account, amount, signature);

Expand Down
33 changes: 20 additions & 13 deletions packages/relay/src/routers/LedgerRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ export class LedgerRouter {
);

this.app.post(
"/v1/ledger/withdraw_by_bridge",
"/v1/ledger/withdraw_via_bridge",
[
body("account").exists().trim().isEthereumAddress(),
body("amount").exists().custom(Validation.isAmount),
Expand All @@ -185,11 +185,11 @@ export class LedgerRouter {
.trim()
.matches(/^(0x)[0-9a-f]{130}$/i),
],
this.ledger_withdraw_by_bridge.bind(this)
this.ledger_withdraw_via_bridge.bind(this)
);

this.app.post(
"/v1/ledger/deposit_by_bridge",
"/v1/ledger/deposit_via_bridge",
[
body("account").exists().trim().isEthereumAddress(),
body("amount").exists().custom(Validation.isAmount),
Expand All @@ -198,7 +198,7 @@ export class LedgerRouter {
.trim()
.matches(/^(0x)[0-9a-f]{130}$/i),
],
this.ledger_deposit_by_bridge.bind(this)
this.ledger_deposit_via_bridge.bind(this)
);
}

Expand Down Expand Up @@ -624,15 +624,22 @@ export class LedgerRouter {
}
}

private async getDepositId(account: string): Promise<string> {
private async getDepositIdMainChain(account: string): Promise<string> {
while (true) {
const id = ContractUtils.getRandomId(account);
if (await this.contractManager.mainLoyaltyBridgeContract.isAvailableDepositId(id)) return id;
}
}

private async getDepositIdSideChain(account: string): Promise<string> {
while (true) {
const id = ContractUtils.getRandomId(account);
if (await this.contractManager.sideLoyaltyBridgeContract.isAvailableDepositId(id)) return id;
}
}

private async ledger_withdraw_by_bridge(req: express.Request, res: express.Response) {
logger.http(`POST /v1/ledger/withdraw_by_bridge ${req.ip}:${JSON.stringify(req.body)}`);
private async ledger_withdraw_via_bridge(req: express.Request, res: express.Response) {
logger.http(`POST /v1/ledger/withdraw_via_bridge ${req.ip}:${JSON.stringify(req.body)}`);

const errors = validationResult(req);
if (!errors.isEmpty()) {
Expand Down Expand Up @@ -663,24 +670,24 @@ export class LedgerRouter {
await this.contractManager.sideTokenContract.name(),
await this.contractManager.sideTokenContract.symbol()
);
const depositId = await this.getDepositId(account);
const depositId = await this.getDepositIdSideChain(account);
const tx = await this.contractManager.sideLoyaltyBridgeContract
.connect(signerItem.signer)
.depositToBridge(tokenId, depositId, account, amount, signature);

return res.status(200).json(this.makeResponseData(0, { tokenId, depositId, amount, txHash: tx.hash }));
} catch (error: any) {
const msg = ResponseMessage.getEVMErrorMessage(error);
logger.error(`POST /v1/ledger/withdraw_by_bridge : ${msg.error.message}`);
logger.error(`POST /v1/ledger/withdraw_via_bridge : ${msg.error.message}`);
this.metrics.add("failure", 1);
return res.status(200).json(this.makeResponseData(msg.code, undefined, msg.error));
} finally {
this.releaseRelaySigner(signerItem);
}
}

private async ledger_deposit_by_bridge(req: express.Request, res: express.Response) {
logger.http(`POST /v1/ledger/deposit_by_bridge ${req.ip}:${JSON.stringify(req.body)}`);
private async ledger_deposit_via_bridge(req: express.Request, res: express.Response) {
logger.http(`POST /v1/ledger/deposit_via_bridge ${req.ip}:${JSON.stringify(req.body)}`);

const errors = validationResult(req);
if (!errors.isEmpty()) {
Expand Down Expand Up @@ -711,15 +718,15 @@ export class LedgerRouter {
await this.contractManager.mainTokenContract.name(),
await this.contractManager.mainTokenContract.symbol()
);
const depositId = await this.getDepositId(account);
const depositId = await this.getDepositIdMainChain(account);
const tx = await this.contractManager.mainLoyaltyBridgeContract
.connect(signerItem.signer)
.depositToBridge(tokenId, depositId, account, amount, signature);

return res.status(200).json(this.makeResponseData(0, { tokenId, depositId, amount, txHash: tx.hash }));
} catch (error: any) {
const msg = ResponseMessage.getEVMErrorMessage(error);
logger.error(`POST /v1/ledger/deposit_by_bridge : ${msg.error.message}`);
logger.error(`POST /v1/ledger/deposit_via_bridge : ${msg.error.message}`);
this.metrics.add("failure", 1);
return res.status(200).json(this.makeResponseData(msg.code, undefined, msg.error));
} finally {
Expand Down
Loading

0 comments on commit 289ed75

Please sign in to comment.