Skip to content

Commit

Permalink
Merge pull request #269 from shkangr/feature/bridge/added-another-pro…
Browse files Browse the repository at this point in the history
…vider

Applied Multi-provider
  • Loading branch information
shkangr authored Dec 5, 2023
2 parents 7fd7e11 + af2ab4e commit 5da489c
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 32 deletions.
22 changes: 19 additions & 3 deletions bridge/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ process.on("uncaughtException", console.error);
);
const NCG_MINTER: string = Configuration.get("NCG_MINTER");
const KMS_PROVIDER_URL: string = Configuration.get("KMS_PROVIDER_URL");
const KMS_PROVIDER_SUB_URL: string = Configuration.get(
"KMS_PROVIDER_SUB_URL"
);
const KMS_PROVIDER_KEY_ID: string = Configuration.get(
"KMS_PROVIDER_KEY_ID"
);
Expand Down Expand Up @@ -343,6 +346,7 @@ process.on("uncaughtException", console.error);
},
});
const web3 = new Web3(kmsProvider);

const wNCGToken: ContractDescription = {
abi: wNCGTokenAbi,
address: WNCG_CONTRACT_ADDRESS,
Expand Down Expand Up @@ -371,6 +375,19 @@ process.on("uncaughtException", console.error);
gasPriceLimitPolicy,
]);

const providerMain = new ethers.providers.JsonRpcProvider(KMS_PROVIDER_URL);
const providerSub = new ethers.providers.JsonRpcProvider(
KMS_PROVIDER_SUB_URL
);

const provider = new ethers.providers.FallbackProvider(
[
{ provider: providerMain, priority: 0 },
{ provider: providerSub, priority: 1 },
],
1
);

async function makeSafeWrappedNCGMinter(): Promise<SafeWrappedNCGMinter> {
if (
!USE_SAFE_WRAPPED_NCG_MINTER ||
Expand All @@ -381,8 +398,6 @@ process.on("uncaughtException", console.error);
throw new Error("Unsufficient environment variables were given.");
}

const provider = new ethers.providers.JsonRpcProvider(KMS_PROVIDER_URL);

const [owner1Signer, owner2Signer, owner3Signer] =
SAFE_OWNER_CREDENTIALS.map(
(credentials) => new AwsKmsSigner(credentials, provider)
Expand All @@ -400,6 +415,7 @@ process.on("uncaughtException", console.error);
);
}

// Todo: Apply Multi-provider at WrappedNCGMinter
const minter: IWrappedNCGMinter = USE_SAFE_WRAPPED_NCG_MINTER
? await makeSafeWrappedNCGMinter()
: new WrappedNCGMinter(
Expand Down Expand Up @@ -480,7 +496,7 @@ process.on("uncaughtException", console.error);
FAILURE_SUBSCRIBERS
);
const ethereumBurnEventMonitor = new EthereumBurnEventMonitor(
web3,
provider,
wNCGToken,
await monitorStateStore.load("ethereum"),
CONFIRMATIONS
Expand Down
63 changes: 39 additions & 24 deletions bridge/src/monitors/ethereum-burn-event-monitor.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,28 @@
import Web3 from "web3";
import { Contract, EventData } from "web3-eth-contract";
import { EventData } from "web3-eth-contract";
import { TriggerableMonitor } from "./triggerable-monitor";
import { ContractDescription } from "../types/contract-description";
import { TransactionLocation } from "../types/transaction-location";

const BURN_EVENT_NAME = "Burn";
import { ethers } from "ethers";

export class EthereumBurnEventMonitor extends TriggerableMonitor<EventData> {
private readonly _web3: Web3;
private readonly _contract: Contract;
private readonly _provider: ethers.providers.FallbackProvider;
private readonly _contract: ethers.Contract;
private readonly _contractDescription: ContractDescription;
private readonly _confirmations: number;

constructor(
web3: Web3,
provider: ethers.providers.FallbackProvider,
contractDescription: ContractDescription,
latestTransactionLocation: TransactionLocation | null,
confirmations: number
) {
super(latestTransactionLocation);

this._web3 = web3;
this._contract = new this._web3.eth.Contract(
this._provider = provider;
this._contract = new ethers.Contract(
contractDescription.address,
contractDescription.abi,
contractDescription.address
this._provider
);
this._contractDescription = contractDescription;
this._confirmations = confirmations;
Expand All @@ -34,7 +33,7 @@ export class EthereumBurnEventMonitor extends TriggerableMonitor<EventData> {
);
const events = await this.getEvents(blockIndex);
const returnEvents = [];
let skip: boolean = true;
let skip = true;
for (const event of events) {
if (skip) {
if (event.txId === transactionLocation.txId) {
Expand Down Expand Up @@ -67,34 +66,50 @@ export class EthereumBurnEventMonitor extends TriggerableMonitor<EventData> {
}

protected async getBlockIndex(blockHash: string) {
const block = await this._web3.eth.getBlock(blockHash);
const block = await this._provider.getBlock(blockHash);
return block.number;
}

protected getTipIndex(): Promise<number> {
return this._web3.eth.getBlockNumber();
return this._provider.getBlockNumber();
}

protected async getBlockHash(blockIndex: number): Promise<string> {
const block = await this._web3.eth.getBlock(blockIndex);
const block = await this._provider.getBlock(blockIndex);
return block.hash;
}

protected async getEvents(blockIndex: number) {
// 0xc3599666213715dfabdf658c56a97b9adfad2cd9689690c70c79b20bc61940c9
const BURN_EVENT_HASH = Web3.utils.sha3(
"Burn(address,bytes32,uint256)"
);
const pastEvents = await this._contract.getPastEvents(BURN_EVENT_NAME, {
const BURN_EVENT_SIG = "Burn(address,bytes32,uint256)";

const filter = {
address: this._contractDescription.address,
topics: [BURN_EVENT_HASH],
topics: [ethers.utils.id(BURN_EVENT_SIG)], // This is equal with Web3.utils.sha3
fromBlock: blockIndex,
toBlock: blockIndex,
});
return pastEvents.map((x) => {
};

const pastEvents = await this._provider.getLogs(filter);
const parsedEvents = pastEvents.map((log) =>
this._contract.interface.parseLog(log)
);

return parsedEvents.map((parsedEvent, idx) => {
return {
txId: x.transactionHash,
...x,
...pastEvents[idx],
...parsedEvent,
txId: pastEvents[idx].transactionHash,
returnValues: {
...parsedEvent.args,
amount: ethers.BigNumber.from(
parsedEvent.args.amount
).toString(),
},
raw: {
data: pastEvents[idx].data,
topics: pastEvents[idx].topics,
},
event: parsedEvent.name,
};
});
}
Expand Down
4 changes: 2 additions & 2 deletions bridge/src/types/contract-description.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { AbiItem } from "web3-utils";
import { ContractInterface } from "ethers";

export interface ContractDescription {
abi: AbiItem | AbiItem[];
abi: ContractInterface;
address: string;
}
3 changes: 2 additions & 1 deletion bridge/src/wrapped-ncg-minter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { PromiEvent } from "web3-core";
import { ContractDescription } from "./types/contract-description";
import { IWrappedNCGMinter } from "./interfaces/wrapped-ncg-minter";
import { IGasPricePolicy } from "./policies/gas-price";
import { AbiItem } from "web3-utils";

export class WrappedNCGMinter implements IWrappedNCGMinter {
private readonly _web3: Web3;
Expand All @@ -33,7 +34,7 @@ export class WrappedNCGMinter implements IWrappedNCGMinter {
this._web3 = web3;
this._contractDescription = contractDescription;
this._contract = new this._web3.eth.Contract(
this._contractDescription.abi,
this._contractDescription.abi as AbiItem[],
this._contractDescription.address
);
this._minterAddress = minterAddress;
Expand Down
4 changes: 2 additions & 2 deletions bridge/src/wrapped-ncg-token.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { AbiItem } from "web3-utils";
import { ContractInterface } from "ethers";

export const wNCGTokenAbi: AbiItem | AbiItem[] = [
export const wNCGTokenAbi: ContractInterface = [
{
inputs: [],
stateMutability: "nonpayable",
Expand Down

0 comments on commit 5da489c

Please sign in to comment.