From 4c0f74b0bc08d96513d119b5af001f13d0a2b8cf Mon Sep 17 00:00:00 2001 From: Michael Kim Date: Thu, 4 Apr 2024 18:00:59 +0900 Subject: [PATCH] [Relay] Add rpc url in config --- packages/relay/config/config.yaml | 2 + packages/relay/config/config_test.yaml | 2 + packages/relay/src/common/Config.ts | 10 ++++ .../relay/src/contract/ContractManager.ts | 48 ++++++++++++++++--- packages/relay/src/routers/TokenRouter.ts | 12 +++++ packages/relay/test/Approval.test.ts | 4 +- 6 files changed, 70 insertions(+), 8 deletions(-) diff --git a/packages/relay/config/config.yaml b/packages/relay/config/config.yaml index 84d2345a..685e3b51 100644 --- a/packages/relay/config/config.yaml +++ b/packages/relay/config/config.yaml @@ -87,6 +87,7 @@ relay: contracts: sideChain: network: "production_side" + url: "" ledgerAddress : "${LEDGER_CONTRACT_ADDRESS}" phoneLinkerAddress : "${PHONE_LINKER_CONTRACT_ADDRESS}" shopAddress : "${SHOP_CONTRACT_ADDRESS}" @@ -99,6 +100,7 @@ contracts: loyaltyBridgeAddress: "${SIDE_CHAIN_LOYALTY_BRIDGE_CONTRACT_ADDRESS}" chainBridgeAddress: "${SIDE_CHAIN_BRIDGE_CONTRACT_ADDRESS}" mainChain: + url: "" network: "production_main" tokenAddress : "${MAIN_CHAIN_TOKEN_CONTRACT_ADDRESS}" loyaltyBridgeAddress: "${MAIN_CHAIN_LOYALTY_BRIDGE_CONTRACT_ADDRESS}" diff --git a/packages/relay/config/config_test.yaml b/packages/relay/config/config_test.yaml index 7456cc38..aeb0dee2 100644 --- a/packages/relay/config/config_test.yaml +++ b/packages/relay/config/config_test.yaml @@ -87,6 +87,7 @@ relay: contracts: sideChain: network: "hardhat" + url: "" ledgerAddress : "${LEDGER_CONTRACT_ADDRESS}" phoneLinkerAddress : "${PHONE_LINKER_CONTRACT_ADDRESS}" shopAddress : "${SHOP_CONTRACT_ADDRESS}" @@ -100,6 +101,7 @@ contracts: chainBridgeAddress: "${SIDE_CHAIN_BRIDGE_CONTRACT_ADDRESS}" mainChain: network: "hardhat" + url: "" tokenAddress : "${MAIN_CHAIN_TOKEN_CONTRACT_ADDRESS}" loyaltyBridgeAddress: "${MAIN_CHAIN_LOYALTY_BRIDGE_CONTRACT_ADDRESS}" chainBridgeAddress: "${MAIN_CHAIN_BRIDGE_CONTRACT_ADDRESS}" diff --git a/packages/relay/src/common/Config.ts b/packages/relay/src/common/Config.ts index ef826efd..4ce7f8a6 100644 --- a/packages/relay/src/common/Config.ts +++ b/packages/relay/src/common/Config.ts @@ -249,6 +249,7 @@ export class RelayConfig implements IRelayConfig { export class ContractsConfig implements IContractsConfig { public sideChain: { network: string; + url: string; tokenAddress: string; ledgerAddress: string; phoneLinkerAddress: string; @@ -263,6 +264,7 @@ export class ContractsConfig implements IContractsConfig { }; public mainChain: { network: string; + url: string; tokenAddress: string; loyaltyBridgeAddress: string; chainBridgeAddress: string; @@ -273,6 +275,7 @@ export class ContractsConfig implements IContractsConfig { this.sideChain = { network: defaults.sideChain.network, + url: defaults.sideChain.url, tokenAddress: defaults.sideChain.tokenAddress, ledgerAddress: defaults.sideChain.ledgerAddress, phoneLinkerAddress: defaults.sideChain.phoneLinkerAddress, @@ -287,6 +290,7 @@ export class ContractsConfig implements IContractsConfig { }; this.mainChain = { network: defaults.mainChain.network, + url: defaults.mainChain.url, tokenAddress: defaults.mainChain.tokenAddress, loyaltyBridgeAddress: defaults.mainChain.loyaltyBridgeAddress, chainBridgeAddress: defaults.mainChain.chainBridgeAddress, @@ -297,6 +301,7 @@ export class ContractsConfig implements IContractsConfig { return { sideChain: { network: "production_side", + url: "", tokenAddress: process.env.TOKEN_CONTRACT_ADDRESS || "", ledgerAddress: process.env.LEDGER_CONTRACT_ADDRESS || "", phoneLinkerAddress: process.env.PHONE_LINKER_CONTRACT_ADDRESS || "", @@ -311,6 +316,7 @@ export class ContractsConfig implements IContractsConfig { }, mainChain: { network: "production_main", + url: "", tokenAddress: process.env.MAIN_CHAIN_TOKEN_CONTRACT_ADDRESS || "", loyaltyBridgeAddress: process.env.MAIN_CHAIN_LOYALTY_BRIDGE_CONTRACT_ADDRESS || "", chainBridgeAddress: process.env.MAIN_CHAIN_BRIDGE_CONTRACT_ADDRESS || "", @@ -320,6 +326,7 @@ export class ContractsConfig implements IContractsConfig { public readFromObject(config: IContractsConfig) { if (config.sideChain.network !== undefined) this.sideChain.network = config.sideChain.network; + if (config.sideChain.url !== undefined) this.sideChain.url = config.sideChain.url; if (config.sideChain.tokenAddress !== undefined) this.sideChain.tokenAddress = config.sideChain.tokenAddress; if (config.sideChain.ledgerAddress !== undefined) this.sideChain.ledgerAddress = config.sideChain.ledgerAddress; if (config.sideChain.phoneLinkerAddress !== undefined) @@ -341,6 +348,7 @@ export class ContractsConfig implements IContractsConfig { this.sideChain.chainBridgeAddress = config.sideChain.chainBridgeAddress; if (config.mainChain.network !== undefined) this.mainChain.network = config.mainChain.network; + if (config.mainChain.url !== undefined) this.mainChain.url = config.mainChain.url; if (config.mainChain.tokenAddress !== undefined) this.mainChain.tokenAddress = config.mainChain.tokenAddress; if (config.mainChain.loyaltyBridgeAddress !== undefined) this.mainChain.loyaltyBridgeAddress = config.mainChain.loyaltyBridgeAddress; @@ -461,6 +469,7 @@ export interface IRelayConfig { export interface IContractsConfig { sideChain: { network: string; + url: string; tokenAddress: string; ledgerAddress: string; loyaltyProviderAddress: string; @@ -475,6 +484,7 @@ export interface IContractsConfig { }; mainChain: { network: string; + url: string; tokenAddress: string; loyaltyBridgeAddress: string; chainBridgeAddress: string; diff --git a/packages/relay/src/contract/ContractManager.ts b/packages/relay/src/contract/ContractManager.ts index b251f746..09ac6ee6 100644 --- a/packages/relay/src/contract/ContractManager.ts +++ b/packages/relay/src/contract/ContractManager.ts @@ -17,6 +17,7 @@ import { logger } from "../common/Logger"; import { ethers } from "ethers"; import * as hre from "hardhat"; import { HttpNetworkConfig } from "hardhat/src/types/config"; +import { ContractUtils } from "../utils/ContractUtils"; export class ContractManager { private readonly config: Config; @@ -45,6 +46,9 @@ export class ContractManager { private _sideChainURL: string | undefined; private _mainChainURL: string | undefined; + private _sideTokenId: string | undefined; + private _mainTokenId: string | undefined; + constructor(config: Config) { this.config = config; } @@ -54,13 +58,21 @@ 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 = ""; + this._sideChainURL = this.config.contracts.sideChain.url; + if (this._sideChainURL === "") { + 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); + this._sideTokenId = ContractUtils.getTokenId( + await this._sideTokenContract.name(), + await this._sideTokenContract.symbol() + ); + const factory2 = await hre.ethers.getContractFactory("Ledger"); this._sideLedgerContract = factory2.attach(this.config.contracts.sideChain.ledgerAddress); @@ -95,13 +107,21 @@ export class ContractManager { 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 = ""; + this._mainChainURL = this.config.contracts.mainChain.url; + if (this._mainChainURL === "") { + 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); + this._mainTokenId = ContractUtils.getTokenId( + await this._mainTokenContract.name(), + await this._mainTokenContract.symbol() + ); + const factory12 = await hre.ethers.getContractFactory("Bridge"); this._mainLoyaltyBridgeContract = factory12.attach(this.config.contracts.mainChain.loyaltyBridgeAddress); @@ -133,6 +153,22 @@ export class ContractManager { } } + public get mainTokenId(): string { + if (this._mainTokenId !== undefined) return this._mainTokenId; + else { + logger.error("mainTokenId is not ready yet."); + process.exit(1); + } + } + + public get sideTokenId(): string { + if (this._sideTokenId !== undefined) return this._sideTokenId; + else { + logger.error("sideTokenId is not ready yet."); + process.exit(1); + } + } + public get sideChainProvider(): ethers.providers.Provider { if (this._sideChainProvider !== undefined) return this._sideChainProvider; else { diff --git a/packages/relay/src/routers/TokenRouter.ts b/packages/relay/src/routers/TokenRouter.ts index 8a9c41b7..ceadede0 100644 --- a/packages/relay/src/routers/TokenRouter.ts +++ b/packages/relay/src/routers/TokenRouter.ts @@ -332,6 +332,12 @@ export class TokenRouter { name: "main-chain", chainId: this.contractManager.mainChainId, ensAddress: AddressZero, + transferFee: "0", + bridgeFee: ( + await this.contractManager.mainLoyaltyBridgeContract.getFee( + this.contractManager.mainTokenId + ) + ).toString(), }, contract: { token: this.contractManager.mainTokenContract.address, @@ -363,6 +369,12 @@ export class TokenRouter { name: "side-chain", chainId: this.contractManager.sideChainId, ensAddress: AddressZero, + transferFee: (await this.contractManager.sideLoyaltyTransferContract.getFee()).toString(), + bridgeFee: ( + await this.contractManager.sideLoyaltyBridgeContract.getFee( + this.contractManager.sideTokenId + ) + ).toString(), }, contract: { token: this.contractManager.sideTokenContract.address, diff --git a/packages/relay/test/Approval.test.ts b/packages/relay/test/Approval.test.ts index 9166a70e..dea38d66 100644 --- a/packages/relay/test/Approval.test.ts +++ b/packages/relay/test/Approval.test.ts @@ -152,12 +152,12 @@ describe("Test of Server", function () { deployments.getContractAddress("LoyaltyExchanger") || ""; config.contracts.sideChain.loyaltyTransferAddress = deployments.getContractAddress("LoyaltyTransfer") || ""; config.contracts.sideChain.loyaltyBridgeAddress = deployments.getContractAddress("LoyaltyBridge") || ""; - config.contracts.sideChain.bridgeAddress = deployments.getContractAddress("SideChainBridge") || ""; + config.contracts.sideChain.chainBridgeAddress = deployments.getContractAddress("SideChainBridge") || ""; config.contracts.mainChain.tokenAddress = deployments.getContractAddress("MainChainKIOS") || ""; config.contracts.mainChain.loyaltyBridgeAddress = deployments.getContractAddress("MainChainLoyaltyBridge") || ""; - config.contracts.mainChain.bridgeAddress = deployments.getContractAddress("MainChainBridge") || ""; + config.contracts.mainChain.chainBridgeAddress = deployments.getContractAddress("MainChainBridge") || ""; config.relay.managerKeys = deployments.accounts.certifiers.map((m) => m.privateKey); config.relay.approvalSecond = 2;