Skip to content

Commit

Permalink
[Relay] Add rpc url in config
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelKim20 committed Apr 4, 2024
1 parent 2890b46 commit 4c0f74b
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 8 deletions.
2 changes: 2 additions & 0 deletions packages/relay/config/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ relay:
contracts:
sideChain:
network: "production_side"
url: ""
ledgerAddress : "${LEDGER_CONTRACT_ADDRESS}"
phoneLinkerAddress : "${PHONE_LINKER_CONTRACT_ADDRESS}"
shopAddress : "${SHOP_CONTRACT_ADDRESS}"
Expand All @@ -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}"
Expand Down
2 changes: 2 additions & 0 deletions packages/relay/config/config_test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ relay:
contracts:
sideChain:
network: "hardhat"
url: ""
ledgerAddress : "${LEDGER_CONTRACT_ADDRESS}"
phoneLinkerAddress : "${PHONE_LINKER_CONTRACT_ADDRESS}"
shopAddress : "${SHOP_CONTRACT_ADDRESS}"
Expand All @@ -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}"
Expand Down
10 changes: 10 additions & 0 deletions packages/relay/src/common/Config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -263,6 +264,7 @@ export class ContractsConfig implements IContractsConfig {
};
public mainChain: {
network: string;
url: string;
tokenAddress: string;
loyaltyBridgeAddress: string;
chainBridgeAddress: string;
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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 || "",
Expand All @@ -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 || "",
Expand All @@ -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)
Expand All @@ -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;
Expand Down Expand Up @@ -461,6 +469,7 @@ export interface IRelayConfig {
export interface IContractsConfig {
sideChain: {
network: string;
url: string;
tokenAddress: string;
ledgerAddress: string;
loyaltyProviderAddress: string;
Expand All @@ -475,6 +484,7 @@ export interface IContractsConfig {
};
mainChain: {
network: string;
url: string;
tokenAddress: string;
loyaltyBridgeAddress: string;
chainBridgeAddress: string;
Expand Down
48 changes: 42 additions & 6 deletions packages/relay/src/contract/ContractManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand All @@ -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);

Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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 {
Expand Down
12 changes: 12 additions & 0 deletions packages/relay/src/routers/TokenRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions packages/relay/test/Approval.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 4c0f74b

Please sign in to comment.