Skip to content

Commit

Permalink
Merge pull request #1 from matejdrazic/mdrazic/pause-effect
Browse files Browse the repository at this point in the history
early draft of pause functionality
  • Loading branch information
nmlinaric authored Jul 13, 2023
2 parents 6368c41 + 0c57136 commit 7969a2c
Show file tree
Hide file tree
Showing 22 changed files with 150 additions and 26 deletions.
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
GOERLI_RPC_URL=
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,12 @@
# maintenance-utils
# maintenance-utils
NodeCLI for various purposes

## Commands

```pause``` - Pause all transfers across all bridges on selected enviroment (devnet, testnet or mainnet)

Run with:

```
node pause -pk "private-key" -m "mnemonic words" -e "environment"
```
54 changes: 44 additions & 10 deletions src/bridgePausing.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,56 @@
import 'dotenv/config';
import axios from 'axios';
import {Command, Option} from 'commander';
import {ConfigUrl} from "./constants";
import {RawConfig} from "@buildwithsygma/sygma-sdk-core";
import { ethers } from 'ethers';
import { Command, Option } from 'commander';
import { SharedConfig } from "./constants";
import { getWalletsForDifferentProviders, deriveWalletsFromMnemonic, sendPauseTransactions } from "./utils";
import { RawConfig, Domain } from '@buildwithsygma/sygma-sdk-core';

const program = new Command();
console.log("pero");

program
.name("pause-bridge")
.description("Pauses all bridge instances across all networks")
.argument("<string>", "mnemonic or private key of the wallet")
.version("0.0.1")

program
.command("pause")
.addOption(
new Option('--environment, -e', 'Environment on which to pause bridge instances')
new Option('-e, --environment <environment>', 'Environment on which to pause bridge instances')
.choices(['devnet', 'testnet', 'mainnet'])
)
.action(async (mnemonic: string, environment: keyof typeof ConfigUrl) => {
.addOption(
new Option('-pk, --private-key <privateKey>', 'Private key to use for signing transactions')
)
.addOption(
new Option('-m, --mnemonic <mnemonic>', 'Mnemonic to use for signing transactions').conflicts('private-key')
)
.action(async (configs: any) => {
try {
console.log("pero");
const response = await axios.get(ConfigUrl[environment]) as unknown as RawConfig;
console.log(response)
const network: keyof typeof SharedConfig = configs.environment;
const {
privateKey,
mnemonic
} = configs;

const {
data
} = await axios.get(SharedConfig[network]) as unknown as { data: RawConfig };

const networks: Array<Domain> = data.domains.filter((network: Domain) => network.name === "ethereum"); // just evms for now

let wallets: Array<ethers.Wallet | ethers.HDNodeWallet> = [];

if (mnemonic) {
wallets = await deriveWalletsFromMnemonic(mnemonic, networks);
} else if (privateKey) {
wallets = await getWalletsForDifferentProviders(privateKey, networks);
} else {
throw new Error('Either mnemonic or private key must be provided');
}

await sendPauseTransactions(networks, wallets);

} catch (err) {
if (err instanceof Error) {
throw new Error(`Failed to fetch shared config because of: ${err.message}`);
Expand All @@ -27,3 +59,5 @@ program
}
}
})

program.parse(process.argv);
5 changes: 0 additions & 5 deletions src/constants.ts

This file was deleted.

2 changes: 2 additions & 0 deletions src/constants/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { chainIdToRpc } from "./rpc";
export { SharedConfig } from "./sharedConfig";
5 changes: 5 additions & 0 deletions src/constants/rpc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// chain id -> provider url
export const chainIdToRpc = {
5: process.env.GOERLI_RPC_URL,
}

5 changes: 5 additions & 0 deletions src/constants/sharedConfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const SharedConfig = {
devnet: "https://chainbridge-assets-stage.s3.us-east-2.amazonaws.com/shared-config-dev.json",
testnet: "https://chainbridge-assets-stage.s3.us-east-2.amazonaws.com/shared-config-test.json",
mainnet: "https://sygma-assets-mainnet.s3.us-east-2.amazonaws.com/shared-config-mainnet.json"
};
47 changes: 47 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { ethers } from 'ethers';
import { chainIdToRpc } from "./constants";
import { Bridge__factory } from "@buildwithsygma/sygma-contracts";
import { Domain } from "@buildwithsygma/sygma-sdk-core";

export async function getWalletsForDifferentProviders(privateKey: string, networks: Array<Domain>) {
const wallets = [];
for (let i = 0; i < networks.length; i++) {
const network = networks[i];
const chainId = network.chainId;
const rpc = chainIdToRpc[chainId as keyof typeof chainIdToRpc];
if (rpc) {
const provider = new ethers.JsonRpcProvider(rpc);
const wallet = new ethers.Wallet(privateKey, provider); // add error handling for invalid private key
wallets.push(wallet);
}
}
return wallets;
}

export async function deriveWalletsFromMnemonic(mnemonic: string, networks: Array<Domain>) {
const wallets = [];
for (let i = 0; i < networks.length; i++) {
const network = networks[i];
const chainId = network.chainId;
const rpc = chainIdToRpc[chainId as keyof typeof chainIdToRpc];
if (rpc) {
const provider = new ethers.JsonRpcProvider(rpc);
const wallet = ethers.Wallet.fromPhrase(mnemonic, provider);
wallets.push(wallet);
}
}
return wallets;
}

export async function sendPauseTransactions(networks: Array<any>, wallets: Array<ethers.Wallet | ethers.HDNodeWallet>) {
const receipts = [];
for (let i = 0; i < networks.length; i++) {
const network = networks[i];
const wallet = wallets[i];
const bridge = Bridge__factory.connect(network.bridge, wallet);
const tx = await bridge.adminPauseTransfers();
console.log(`Transaction no. ${i + 1} completed, bridge on ${network.name} paused`);
receipts.push(tx);
}
return receipts;
}
2 changes: 1 addition & 1 deletion tsconfig.cjs.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@
"declaration": true,
"esModuleInterop": true,
"module": "commonjs",
"outDir": "./dis-cjs"
"outDir": "./dist-cjs"
}
}
2 changes: 1 addition & 1 deletion types/bridgePausing.d.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export {};
import 'dotenv/config';
//# sourceMappingURL=bridgePausing.d.ts.map
2 changes: 1 addition & 1 deletion types/bridgePausing.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 0 additions & 6 deletions types/constants.d.ts

This file was deleted.

1 change: 0 additions & 1 deletion types/constants.d.ts.map

This file was deleted.

3 changes: 3 additions & 0 deletions types/constants/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export { chainIdToRpc } from "./rpc";
export { SharedConfig } from "./sharedConfig";
//# sourceMappingURL=index.d.ts.map
1 change: 1 addition & 0 deletions types/constants/index.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions types/constants/rpc.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export declare const chainIdToRpc: {
5: string | undefined;
};
//# sourceMappingURL=rpc.d.ts.map
1 change: 1 addition & 0 deletions types/constants/rpc.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions types/constants/sharedConfig.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export declare const SharedConfig: {
devnet: string;
testnet: string;
mainnet: string;
};
//# sourceMappingURL=sharedConfig.d.ts.map
1 change: 1 addition & 0 deletions types/constants/sharedConfig.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions types/utils.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { ethers } from 'ethers';
import { Domain } from "@buildwithsygma/sygma-sdk-core";
export declare function getWalletsForDifferentProviders(privateKey: string, networks: Array<Domain>): Promise<ethers.Wallet[]>;
export declare function deriveWalletsFromMnemonic(mnemonic: string, networks: Array<Domain>): Promise<ethers.HDNodeWallet[]>;
export declare function sendPauseTransactions(networks: Array<any>, wallets: Array<ethers.Wallet | ethers.HDNodeWallet>): Promise<ethers.ContractTransaction[]>;
//# sourceMappingURL=utils.d.ts.map
1 change: 1 addition & 0 deletions types/utils.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ __metadata:
"@buildwithsygma/sygma-sdk-core": ^2.1.0
axios: ^1.4.0
commander: ^11.0.0
dotenv: ^16.3.1
ethers: ^6.6.2
typescript: ^5.1.6
languageName: unknown
Expand Down Expand Up @@ -3005,6 +3006,13 @@ __metadata:
languageName: node
linkType: hard

"dotenv@npm:^16.3.1":
version: 16.3.1
resolution: "dotenv@npm:16.3.1"
checksum: b95ff1bbe624ead85a3cd70dbd827e8e06d5f05f716f2d0cbc476532d54c7c9469c3bc4dd93ea519f6ad711cb522c00ac9a62b6eb340d5affae8008facc3fbd7
languageName: node
linkType: hard

"eastasianwidth@npm:^0.2.0":
version: 0.2.0
resolution: "eastasianwidth@npm:0.2.0"
Expand Down

0 comments on commit 7969a2c

Please sign in to comment.