Skip to content

Commit

Permalink
feat: changed unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
gsoares85 committed Jan 23, 2025
1 parent 1714b8e commit a2786b1
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 127 deletions.
22 changes: 2 additions & 20 deletions networkData.json
Original file line number Diff line number Diff line change
@@ -1,29 +1,11 @@
{
"rskTestnet": {
"owners": [
"0x8E925445BdA88C9F980976dB098Cb1c450BFc719",
"0x452A22889229b87472Cd77d4f2A8aA33b223D6B5"
],
"lbcProxyAddress": "0x4B6E7420F0e22472BFF01309A9BE96eab3a7f004",
"lbcProxyAdminAddress": "0x4B6E7420F0e22472BFF01309A9BE96eab3a7f004",
"providerRpc": "asdasdasd",
"mnemonic": "asdasdasd"
"owners": ["0x452a22889229b87472CD77d4F2A8Aa33B223D6B5"]
},
"rskMainnet": {
"owners": [
"0x8E925445BdA88C9F980976dB098Cb1c450BFc719",
"0x452A22889229b87472Cd77d4f2A8aA33b223D6B5"
],
"lbcProxyAddress": "0xAA9cAf1e3967600578727F975F283446A3Da6612",
"lbcProxyAdminAddress": "0x9dB9edEC34280D4DF6A80dDE6Cb3e80455657d3E",
"providerRpc": "sadasd",
"mnemonic": "asdasdasd"
},
"hardhat": {
"owners": ["s1", "s2"],
"lbcProxyAddress": "0x0000000000000000000000000000000000000000",
"lbcProxyAdminAddress": "0x0000000000000000000000000000000000000000",
"providerRpc": "http://localhost:8545",
"mnemonic": "test test test test test test test test test test test junk"
]
}
}
148 changes: 60 additions & 88 deletions scripts/deployment-utils/changeMultisigOwner.ts
Original file line number Diff line number Diff line change
@@ -1,103 +1,64 @@
import hre from "hardhat";
import { ethers } from "ethers";
import * as dotenv from "dotenv";
import hre, { ethers } from "hardhat";
import { read } from "./deploy";
import networkData from "../../networkData.json";

dotenv.config();

const SafeABI = [
"function VERSION() view returns (string)",
"function getOwners() view returns (address[])",
];

const OwnableABI = [
"function owner() view returns (address)",
"function transferOwnership(address newOwner)",
];
import { HardhatRuntimeEnvironment } from "hardhat/types";

async function main() {
const network = hre.network.name;
const address = "";
console.log(`Changing multisig owner to: ${address} - ${network}`);
const address = "0x14842613f48485e0cb68ca88fd24363d57f34541";
console.info(`Changing multisig owner to: ${address} - ${network}`);

const currentNetworkData = networkData[network as keyof typeof networkData];
const signer = (await ethers.getSigners())[0];
console.info(`Signer address: ${signer.address}`);

const { owners, lbcProxyAddress, lbcProxyAdminAddress } = currentNetworkData;
const currentNetworkData = networkData[network as keyof typeof networkData];

const etherProvider = new ethers.JsonRpcProvider(
currentNetworkData.providerRpc
);
const { owners } = currentNetworkData;
const proxyName = "LiquidityBridgeContract";

const isSafe = await isSafeAddress(etherProvider, address);
if (!isSafe) {
console.error(
"Exiting... Provided Safe address is not a valid Safe contract."
);
return;
const proxyAddress = read()[network][proxyName].address;
if (!proxyAddress) {
throw new Error(`Proxy ${proxyName} not deployed on network ${network}`);
}
console.info(`Proxy address: ${proxyAddress}`);

const safeOwners = await getOwners(etherProvider, address);
if (!validateOwners(safeOwners, owners)) {
console.error(
"Exiting... Safe owners do not match expected configuration."
const safeOwners = await isSafeAddress(address);
if (safeOwners.length === 0) {
throw new Error(
"Exiting... Provided Safe address is not a valid Safe contract."
);
return;
}

const signer = ethers.Wallet.fromPhrase(
currentNetworkData.mnemonic,
etherProvider
);

console.log("Starting ownership transfer process...");

await transferOwnership(signer, lbcProxyAddress, address);
await transferOwnership(signer, lbcProxyAdminAddress, address);
validateOwners(safeOwners, owners);

console.info("Starting ownership transfer process...");
await transferOwnership(hre, proxyAddress, address);
console.log("Ownership transfer process complete!");
}

async function isSafeAddress(
provider: ethers.JsonRpcProvider,
address: string
): Promise<boolean> {
async function isSafeAddress(address: string): Promise<string[]> {
try {
const code = await provider.getCode(address);
const code = await ethers.provider.getCode(address);
if (code === "0x") {
console.log(`${address} is not a smart contract`);
return false;
throw new Error(`${address} is not a smart contract`);
}
const contract = new ethers.Contract(address, SafeABI, provider);

const version = (await contract.VERSION()) as string;
console.log(`Address ${address} is a Safe contract! Version: ${version}`);
const contract = await ethers.getContractAt("GnosisSafe", address);

return true;
} catch (error) {
console.error(`Address ${address} Is not a Safe address.`, error);
return false;
}
}
const version = await contract.VERSION();
const owners = await contract.getOwners();
if (owners.length === 0) {
throw new Error("Owners array is empty");
}
console.info(`Address ${address} is a Safe contract! Version: ${version}`);

async function getOwners(
provider: ethers.JsonRpcProvider,
address: string
): Promise<string[]> {
try {
const contract = new ethers.Contract(address, SafeABI, provider);
const owners = (await contract.getOwners()) as string[];
console.log(`Owners of Safe Contract at ${address}:`, owners);
return owners;
} catch (error) {
console.error(`Failed to get owners for address ${address}:`, error);
return [];
console.error(error);
throw new Error(`Address ${address} Is not a Safe address`);
}
}

function validateOwners(
safeOwners: string[],
expectedOwners: string[]
): boolean {
function validateOwners(safeOwners: string[], expectedOwners: string[]): void {
const safeSet = new Set(safeOwners.map((owner) => owner.toLowerCase()));
const expectedSet = new Set(
expectedOwners.map((owner) => owner.toLowerCase())
Expand All @@ -107,45 +68,56 @@ function validateOwners(
safeSet.size === expectedSet.size &&
[...safeSet].every((owner) => expectedSet.has(owner));
if (isValid) {
console.log(`Safe ownership matches expected configuration.`);
console.info(`Safe ownership matches expected configuration.`);
} else {
console.error(`Safe ownership does not match expected configuration.`);
throw new Error(`Safe ownership does not match expected configuration.`);
}

return isValid;
}

async function transferOwnership(
signer: ethers.HDNodeWallet,
contractAddress: string,
hre: HardhatRuntimeEnvironment,
proxyAddress: string,
newOwnerAddress: string
): Promise<void> {
try {
const contract = new ethers.Contract(contractAddress, OwnableABI, signer);
const contract = await ethers.getContractAt(
"LiquidityBridgeContractV2",
proxyAddress
);

const currentOwner = (await contract.owner()) as string;
console.log(
`Current owner of contract at ${contractAddress}: ${currentOwner}`
const currentOwner = await contract.owner();
console.info(
`Current owner of contract at ${proxyAddress}: ${currentOwner}`
);

if (currentOwner.toLowerCase() === newOwnerAddress.toLowerCase()) {
console.log(
`Ownership of contract at ${contractAddress} is already set to ${newOwnerAddress}`
console.info(
`Ownership of contract at ${proxyAddress} is already set to ${newOwnerAddress}`
);
return;
}

console.log(
`Transferring ownership of contract at ${contractAddress} to ${newOwnerAddress}...`
`Transferring ownership of contract at ${proxyAddress} to ${newOwnerAddress}...`
);
await contract.transferOwnership(newOwnerAddress);

console.log(
`Ownership of contract at ${contractAddress} successfully transferred to ${newOwnerAddress}!`
`Ownership of contract at ${proxyAddress} successfully transferred to ${newOwnerAddress}!`
);

const signer = (await ethers.getSigners())[0];
await hre.upgrades.admin.transferProxyAdminOwnership(
proxyAddress,
newOwnerAddress,
signer,
{
silent: true,
}
);
} catch (error) {
console.error(
`Failed to transfer ownership of contract at ${contractAddress}:`,
`Failed to transfer ownership of contract at ${proxyAddress}:`,
error
);
}
Expand Down
22 changes: 3 additions & 19 deletions test/safe.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,6 @@ import { expect } from "chai";
import { GnosisSafe, GnosisSafeProxyFactory } from "../typechain-types";
import { ContractTransactionResponse } from "ethers";
import { deployLbcProxy } from "../scripts/deployment-utils/deploy-proxy";
import { getContractAt } from "@nomicfoundation/hardhat-ethers/internal/helpers";

const TransparentProxyABI = [
"function admin() view returns (address)",
"function implementation() view returns (address)",
"function owner() view returns (address)",
"function transferOwnership(address newOwner)",
];

describe("Safe Wallet Deployment", function async() {
async function safeInitialize() {
Expand Down Expand Up @@ -101,6 +93,7 @@ describe("Safe Wallet Deployment", function async() {
safeSingleton,
[signer1.address, signer2.address]
);

const deployed = await deployLbcProxy(hre.network.name, {
verbose: false,
});
Expand All @@ -109,24 +102,15 @@ describe("Safe Wallet Deployment", function async() {
"LiquidityBridgeContract",
proxyAddress
);
const proxyContract = await getContractAt(
hre,
TransparentProxyABI,
proxyAddress
);

expect(await proxyContract.owner()).to.equal(signer1.address);

const result = await lbc.queryFilter(lbc.getEvent("Initialized"));
expect(deployed.deployed).to.be.eq(true);
expect(result.length).length.equal(1);
expect(await lbc.owner()).to.equal(signer1.address);

const safeAddress = await testSafeWallet!.getAddress();
await lbc.transferOwnership(safeAddress);

await proxyContract.transferOwnership(safeAddress);

expect(await proxyContract.owner()).to.equal(safeAddress);
expect(await lbc.owner()).to.equal(safeAddress);
});
});
});

0 comments on commit a2786b1

Please sign in to comment.