Skip to content

Commit

Permalink
[Relay] Fix ethers provider
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelKim20 committed Apr 9, 2024
1 parent f942523 commit 7cb795c
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 17 deletions.
87 changes: 71 additions & 16 deletions packages/relay/src/contract/ContractManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
LoyaltyConsumer,
LoyaltyExchanger,
LoyaltyProvider,
LoyaltyToken,
LoyaltyTransfer,
PhoneLinkCollection,
Shop,
Expand Down Expand Up @@ -54,79 +55,133 @@ export class ContractManager {
}

public async attach() {
logger.info("SideChain.Network", this.config.contracts.sideChain.network);
await hre.changeNetwork(this.config.contracts.sideChain.network);
this._sideChainProvider = hre.ethers.provider;
this._sideChainId = (await this._sideChainProvider.getNetwork()).chainId;
logger.info("SideChain.ChainId:", this._sideChainId);

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 = "";
}
logger.info("SideChain.URL:", this._sideChainURL);

const factory1 = await hre.ethers.getContractFactory("BIP20DelegatedTransfer");
this._sideTokenContract = factory1.attach(this.config.contracts.sideChain.tokenAddress);
const factory1 = await hre.ethers.getContractFactory("LoyaltyToken");
this._sideTokenContract = factory1
.attach(this.config.contracts.sideChain.tokenAddress)
.connect(this._sideChainProvider);
logger.info("SideChain.Token:", this._sideTokenContract.address);
logger.info("SideChain.Token.Name:", await this._sideTokenContract.name());
logger.info("SideChain.Token.Symbol:", await this._sideTokenContract.symbol());

this._sideTokenId = ContractUtils.getTokenId(
await this._sideTokenContract.name(),
await this._sideTokenContract.symbol()
);
logger.info("SideChain.TokenId:", this._sideTokenId);

const factory2 = await hre.ethers.getContractFactory("Ledger");
this._sideLedgerContract = factory2.attach(this.config.contracts.sideChain.ledgerAddress);
this._sideLedgerContract = factory2
.attach(this.config.contracts.sideChain.ledgerAddress)
.connect(this._sideChainProvider);
logger.info("SideChain.Ledger:", this._sideLedgerContract.address);

const factory3 = await hre.ethers.getContractFactory("Shop");
this._sideShopContract = factory3.attach(this.config.contracts.sideChain.shopAddress);
this._sideShopContract = factory3
.attach(this.config.contracts.sideChain.shopAddress)
.connect(this._sideChainProvider);
logger.info("SideChain.Shop:", this._sideShopContract.address);

const factory4 = await hre.ethers.getContractFactory("PhoneLinkCollection");
this._sidePhoneLinkerContract = factory4.attach(this.config.contracts.sideChain.phoneLinkerAddress);
this._sidePhoneLinkerContract = factory4
.attach(this.config.contracts.sideChain.phoneLinkerAddress)
.connect(this._sideChainProvider);
logger.info("SideChain.PhoneLinkCollection:", this._sidePhoneLinkerContract.address);

const factory5 = await hre.ethers.getContractFactory("CurrencyRate");
this._sideCurrencyRateContract = factory5.attach(this.config.contracts.sideChain.currencyRateAddress);
this._sideCurrencyRateContract = factory5
.attach(this.config.contracts.sideChain.currencyRateAddress)
.connect(this._sideChainProvider);
logger.info("SideChain.CurrencyRate:", this._sideCurrencyRateContract.address);

const factory51 = await hre.ethers.getContractFactory("LoyaltyProvider");
this._sideLoyaltyProviderContract = factory51.attach(this.config.contracts.sideChain.loyaltyProviderAddress);
this._sideLoyaltyProviderContract = factory51
.attach(this.config.contracts.sideChain.loyaltyProviderAddress)
.connect(this._sideChainProvider);
logger.info("SideChain.LoyaltyProvider:", this._sideLoyaltyProviderContract.address);

const factory6 = await hre.ethers.getContractFactory("LoyaltyConsumer");
this._sideLoyaltyConsumerContract = factory6.attach(this.config.contracts.sideChain.loyaltyConsumerAddress);
this._sideLoyaltyConsumerContract = factory6
.attach(this.config.contracts.sideChain.loyaltyConsumerAddress)
.connect(this._sideChainProvider);
logger.info("SideChain.LoyaltyConsumer:", this._sideLoyaltyConsumerContract.address);

const factory7 = await hre.ethers.getContractFactory("LoyaltyExchanger");
this._sideLoyaltyExchangerContract = factory7.attach(this.config.contracts.sideChain.loyaltyExchangerAddress);
this._sideLoyaltyExchangerContract = factory7
.attach(this.config.contracts.sideChain.loyaltyExchangerAddress)
.connect(this._sideChainProvider);
logger.info("SideChain.LoyaltyExchanger:", this._sideLoyaltyExchangerContract.address);

const factory8 = await hre.ethers.getContractFactory("LoyaltyTransfer");
this._sideLoyaltyTransferContract = factory8.attach(this.config.contracts.sideChain.loyaltyTransferAddress);
this._sideLoyaltyTransferContract = factory8
.attach(this.config.contracts.sideChain.loyaltyTransferAddress)
.connect(this._sideChainProvider);
logger.info("SideChain.LoyaltyTransfer:", this._sideLoyaltyTransferContract.address);

const factory9 = await hre.ethers.getContractFactory("LoyaltyBridge");
this._sideLoyaltyBridgeContract = factory9.attach(this.config.contracts.sideChain.loyaltyBridgeAddress);
this._sideLoyaltyBridgeContract = factory9
.attach(this.config.contracts.sideChain.loyaltyBridgeAddress)
.connect(this._sideChainProvider);
logger.info("SideChain.LoyaltyBridge:", this._sideLoyaltyBridgeContract.address);

const factory10 = await hre.ethers.getContractFactory("Bridge");
this._sideChainBridgeContract = factory10.attach(this.config.contracts.sideChain.chainBridgeAddress);
this._sideChainBridgeContract = factory10
.attach(this.config.contracts.sideChain.chainBridgeAddress)
.connect(this._sideChainProvider);
logger.info("SideChain.ChainBridge:", this._sideChainBridgeContract.address);

logger.info("MainChain.Network:", this.config.contracts.mainChain.network);
await hre.changeNetwork(this.config.contracts.mainChain.network);
this._mainChainProvider = hre.ethers.provider;
this._mainChainId = (await this._mainChainProvider.getNetwork()).chainId;
logger.info("MainChain.ChainId:", this._mainChainId);

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 = "";
}
logger.info("MainChain.URL:", this._mainChainURL);

const factory11 = await hre.ethers.getContractFactory("BIP20DelegatedTransfer");
this._mainTokenContract = factory11.attach(this.config.contracts.mainChain.tokenAddress);
const factory11 = await hre.ethers.getContractFactory("LoyaltyToken");
this._mainTokenContract = factory11
.attach(this.config.contracts.mainChain.tokenAddress)
.connect(this._mainChainProvider);
logger.info("MainChain.Token:", this._mainTokenContract.address);
logger.info("MainChain.Token.Name:", await this._mainTokenContract.name());
logger.info("MainChain.Token.Symbol:", await this._mainTokenContract.symbol());

this._mainTokenId = ContractUtils.getTokenId(
await this._mainTokenContract.name(),
await this._mainTokenContract.symbol()
);
logger.info("MainChain.TokenId:", this._mainTokenId);

const factory12 = await hre.ethers.getContractFactory("Bridge");
this._mainLoyaltyBridgeContract = factory12.attach(this.config.contracts.mainChain.loyaltyBridgeAddress);
this._mainLoyaltyBridgeContract = factory12
.attach(this.config.contracts.mainChain.loyaltyBridgeAddress)
.connect(this._mainChainProvider);
logger.info("MainChain.LoyaltyBridge:", this._mainLoyaltyBridgeContract.address);

const factory13 = await hre.ethers.getContractFactory("Bridge");
this._mainChainBridgeContract = factory13.attach(this.config.contracts.mainChain.chainBridgeAddress);
this._mainChainBridgeContract = factory13
.attach(this.config.contracts.mainChain.chainBridgeAddress)
.connect(this._mainChainProvider);
logger.info("MainChain.ChainBridge:", this._mainChainBridgeContract.address);
}

public get mainChainProvider(): ethers.providers.Provider {
Expand Down
2 changes: 1 addition & 1 deletion packages/relay/src/routers/DefaultRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export class DefaultRouter {
res.set("Content-Type", this.metrics.contentType());
this.metrics.add("status", 1);
for (const elem of this.config.relay.managerKeys) {
const wallet = new Wallet(elem, hre.ethers.provider);
const wallet = new Wallet(elem, this.contractManager.sideChainProvider);
const balance = (await wallet.getBalance()).div(BigNumber.from(1_000_000_000)).toNumber();
this.metrics.gaugeLabels("certifier_balance", { address: wallet.address }, balance);
}
Expand Down

0 comments on commit 7cb795c

Please sign in to comment.