Skip to content

Commit e2ba3b7

Browse files
authored
Merge pull request #315 from rsksmart/feature/GBI2403-createRskForkForTesting
feat: added hardhat fork and tested the change ownership on a forked …
2 parents 7a63b63 + b112c09 commit e2ba3b7

File tree

7 files changed

+122
-21
lines changed

7 files changed

+122
-21
lines changed

e2e/multisig-migration.test.ts

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import { ethers } from "hardhat";
2+
import * as helpers from "@nomicfoundation/hardhat-toolbox/network-helpers";
3+
import { changeMultisigOwner } from "../scripts/deployment-utils/change-multisig-owner";
4+
import { expect } from "chai";
5+
import { DeploymentConfig, read } from "../scripts/deployment-utils/deploy";
6+
import multsigInfoJson from "../multisig-owners.json";
7+
8+
type MultisigInfo = Record<
9+
string,
10+
{
11+
address: string;
12+
owners?: string[];
13+
}
14+
>;
15+
16+
const { FORK_NETWORK_NAME } = process.env;
17+
18+
const multsigInfo: MultisigInfo = multsigInfoJson;
19+
20+
describe("Should change LBC owner to the multisig", function () {
21+
it("Should change the owner", async () => {
22+
await checkForkedNetwork();
23+
24+
const networkName = FORK_NETWORK_NAME ?? "rskTestnet";
25+
26+
const addresses: Partial<DeploymentConfig> = read();
27+
const networkDeployments: Partial<DeploymentConfig[string]> | undefined =
28+
addresses[networkName];
29+
30+
const lbcAddress = networkDeployments?.LiquidityBridgeContract?.address;
31+
32+
if (!lbcAddress) {
33+
throw new Error(
34+
"LiquidityBridgeContract proxy deployment info not found"
35+
);
36+
}
37+
38+
const lbc = await ethers.getContractAt(
39+
"LiquidityBridgeContractV2",
40+
lbcAddress
41+
);
42+
43+
const safeAddress = multsigInfo[networkName].address;
44+
45+
const lbcOwner = await lbc.owner();
46+
console.info("LBC owner:", lbcOwner);
47+
await helpers.impersonateAccount(lbcOwner);
48+
const impersonatedSigner = await ethers.getSigner(lbcOwner);
49+
50+
await expect(
51+
changeMultisigOwner(safeAddress, "rskTestnet", impersonatedSigner)
52+
).to.not.be.reverted;
53+
const newLbcOwner = await lbc.owner();
54+
console.info("New LBC owner:", newLbcOwner);
55+
});
56+
});
57+
58+
async function checkForkedNetwork() {
59+
try {
60+
await ethers.provider.send("evm_snapshot", []);
61+
} catch (error) {
62+
console.error("Not a forked network:", error);
63+
}
64+
}

example.env

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,6 @@ DEV_SIGNER_PRIVATE_KEY=<private_key>
77
MAINNET_MNEMONIC=<mnemonic>
88
TESTNET_MNEMONIC=<mnemonic>
99
DEV_MNEMONIC=<mnemonic>
10+
FORK_NETWORK_NAME=<network to be forked should have same name as in hardhat.config>
11+
FORK_NETWORK_URL=https://public-node.testnet.rsk.co
12+
FORK_NETWORK_BLOCK=<Block to be forked for testnet at least 6020639 to have all information>

hardhat.config.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ const {
2323
DEV_MNEMONIC,
2424
} = process.env;
2525

26+
const forkEnabled = shouldEnableFork(process.argv);
27+
2628
const rskMainnetDerivationPath = "m/44'/137'/0'/0/0";
2729
const rskTestnetDerivationPath = "m/44'/37310'/0'/0/0";
2830

@@ -52,6 +54,26 @@ const config: HardhatUserConfig = {
5254
chainId: 30,
5355
accounts: getAccounts("mainnet"),
5456
},
57+
hardhat: {
58+
...(forkEnabled
59+
? {
60+
forking: {
61+
url:
62+
process.env.FORK_NETWORK_URL ??
63+
"https://public-node.testnet.rsk.co",
64+
blockNumber: Number(process.env.FORK_NETWORK_BLOCK ?? 6100000),
65+
},
66+
chains: {
67+
31: {
68+
hardforkHistory: {
69+
london: 4000000,
70+
berlin: 60000000,
71+
},
72+
},
73+
},
74+
}
75+
: {}),
76+
},
5577
},
5678
solidity: {
5779
version: "0.8.18",
@@ -117,3 +139,10 @@ function getDevAccounts() {
117139
return undefined;
118140
}
119141
}
142+
143+
function shouldEnableFork(argv: string[]) {
144+
if (argv.includes("e2e/multisig-migration.test.ts")) {
145+
return true;
146+
}
147+
return false;
148+
}

multisig-owners.json

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
{
22
"rskTestnet": {
3-
"address": "",
4-
"owners": []
3+
"address": "0x14842613f48485e0cb68ca88fd24363d57f34541",
4+
"owners": [
5+
"0x452a22889229b87472cd77d4f2a8aa33b223d6b5",
6+
"0x8e925445bda88c9f980976db098cb1c450bfc719",
7+
"0x14157c9d0e45dc6d1e12591317c36df45af54695",
8+
"0x9724289766dfbf5039db507128ca967cb84da8ac",
9+
"0xe5c7bfd92f3a26fecf5b6e5c9df9ab2adb51c475"
10+
]
511
},
612
"rskMainnet": {
713
"address": "",

package-lock.json

Lines changed: 0 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
"upgrade-rskMainet": "npm run lint:sol && npm run compile && npx hardhat run scripts/deployment/upgrade-lbc.ts --network rskMainnet",
1515
"contract-size": "npx hardhat size-contracts",
1616
"test:integration": "npx hardhat test integration-test/*.test.ts --bail --network",
17+
"test:e2e": "npx hardhat test e2e/*.test.ts",
1718
"prepare": "pip3 install pre-commit && pre-commit install"
1819
},
1920
"repository": {

scripts/deployment-utils/change-multisig-owner.ts

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import hre, { ethers } from "hardhat";
22
import { read } from "./deploy";
33
import multisigOwners from "../../multisig-owners.json";
4+
import { HardhatEthersSigner } from "@nomicfoundation/hardhat-ethers/signers";
45

56
/**
67
* Changes the multisig owner of the `LiquidityBridgeContract` deployed on the current network to the safe wallet
@@ -12,6 +13,8 @@ import multisigOwners from "../../multisig-owners.json";
1213
*
1314
* @async
1415
* @param {string} newOwner - The address of the new multisig owner (Safe contract).
16+
* @param {string} network - The network where the script will run, by default will be the environment network.
17+
* @param {HardhatEthersSigner} signer - Optional signer for test.
1518
* @throws {Error} If the proxy contract is not deployed on the current network.
1619
* @throws {Error} If the provided `newOwner` address is not a valid multisig Safe contract.
1720
* @throws {Error} If the configuration of owners on the Safe does not match the expected configuration.
@@ -23,8 +26,12 @@ import multisigOwners from "../../multisig-owners.json";
2326
* await changeMultisigOwner(newMultisigAddress);
2427
*/
2528

26-
export const changeMultisigOwner = async (newOwner: string) => {
27-
const network = hre.network.name;
29+
export const changeMultisigOwner = async (
30+
newOwner: string,
31+
network = "",
32+
signer?: HardhatEthersSigner
33+
) => {
34+
network = network === "" ? hre.network.name : network;
2835
console.info(`Changing multisig owner to: ${newOwner} - ${network}`);
2936

3037
const currentNetworkData =
@@ -49,7 +56,7 @@ export const changeMultisigOwner = async (newOwner: string) => {
4956
validateOwners(safeOwners, owners);
5057

5158
console.info("Starting ownership transfer process...");
52-
await transferOwnership(proxyAddress, newOwner);
59+
await transferOwnership(proxyAddress, newOwner, signer);
5360
console.info("Ownership transfer process complete!");
5461
};
5562

@@ -93,7 +100,8 @@ function validateOwners(safeOwners: string[], expectedOwners: string[]): void {
93100

94101
async function transferOwnership(
95102
proxyAddress: string,
96-
newOwnerAddress: string
103+
newOwnerAddress: string,
104+
signer?: HardhatEthersSigner
97105
): Promise<void> {
98106
try {
99107
const contract = await ethers.getContractAt(
@@ -116,15 +124,18 @@ async function transferOwnership(
116124
console.info(
117125
`Transferring ownership of contract at ${proxyAddress} to ${newOwnerAddress}...`
118126
);
119-
await contract.transferOwnership(newOwnerAddress);
127+
const connectedContract = signer ? contract.connect(signer) : contract;
128+
129+
await connectedContract.transferOwnership(newOwnerAddress);
120130

121131
console.info(
122132
`Ownership of contract at ${proxyAddress} successfully transferred to ${newOwnerAddress}!`
123133
);
124134

125135
await hre.upgrades.admin.transferProxyAdminOwnership(
126136
proxyAddress,
127-
newOwnerAddress
137+
newOwnerAddress,
138+
signer
128139
);
129140
} catch (error) {
130141
console.error(

0 commit comments

Comments
 (0)