Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PP-669: Migrate the NativeHolderSmartWallet contract tests to ethers #123

Merged
merged 8 commits into from
Feb 22, 2023
2 changes: 1 addition & 1 deletion contracts/smartwallet/NativeHolderSmartWallet.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import "./SmartWallet.sol";
/* solhint-disable avoid-low-level-calls */

contract NativeHolderSmartWallet is SmartWallet {
function directExecuteWithValue(
function directExecute(
address to,
uint256 value,
bytes calldata data
Expand Down
77 changes: 11 additions & 66 deletions tasks/allowTokens.ts
Original file line number Diff line number Diff line change
@@ -1,78 +1,21 @@
import { ContractTransaction } from 'ethers';
import { HardhatRuntimeEnvironment } from 'hardhat/types';
import { getExistingConfig } from './deploy';
import { getVerifiers } from './utils';

export const allowTokens = async (
taskArgs: { tokenlist: string },
hre: HardhatRuntimeEnvironment
) => {
const tokenAddresses = taskArgs.tokenlist.split(',');

const { ethers, network } = hre;

if (!network) {
throw new Error('Unknown Network');
}

const { chainId } = network.config;

if (!chainId) {
throw new Error('Unknown Chain Id');
}

const contractAddresses = getExistingConfig();

if (!contractAddresses) {
throw new Error('No contracts deployed');
}

const networkChainKey = `${network.name}.${chainId}`;
const contractAddressesDeployed = contractAddresses[networkChainKey];

if (!contractAddressesDeployed) {
throw new Error(`Contracts not deployed for chain ID ${chainId}`);
}

const deployVerifierAddress = contractAddressesDeployed.DeployVerifier;
const relayVerifierAddress = contractAddressesDeployed.RelayVerifier;
const customDeployVerifierAddress =
contractAddressesDeployed.CustomSmartWalletDeployVerifier;
const customRelayVerifierAddress =
contractAddressesDeployed.CustomSmartWalletRelayVerifier;

if (!deployVerifierAddress) {
throw new Error('Could not obtain deploy verifier address');
}

if (!relayVerifierAddress) {
throw new Error('Could not obtain relay verifier address');
}

if (!customDeployVerifierAddress) {
throw new Error('Could not obtain custom deploy verifier address');
}

if (!customRelayVerifierAddress) {
throw new Error('Could not obtain custom deploy verifier address');
}

const deployVerifier = await ethers.getContractAt(
'DeployVerifier',
deployVerifierAddress
);
const relayVerifier = await ethers.getContractAt(
'RelayVerifier',
relayVerifierAddress
);
const customDeployVerifier = await ethers.getContractAt(
'CustomSmartWalletDeployVerifier',
customDeployVerifierAddress
);

const customRelayVerifier = await ethers.getContractAt(
'RelayVerifier',
customRelayVerifierAddress
);
const {
deployVerifier,
relayVerifier,
customDeployVerifier,
customRelayVerifier,
nativeHolderDeployVerifier,
nativeHolderRelayVerifier,
} = await getVerifiers(hre);

const verifierMap: Map<
string,
Expand All @@ -82,6 +25,8 @@ export const allowTokens = async (
verifierMap.set('relayVerifier', relayVerifier);
verifierMap.set('customDeployVerifier', customDeployVerifier);
verifierMap.set('customRelayVerifier', customRelayVerifier);
verifierMap.set('nativeHolderDeployVerifier', nativeHolderDeployVerifier);
verifierMap.set('nativeHolderRelayVerifier', nativeHolderRelayVerifier);

for (const tokenAddress of tokenAddresses) {
for (const [key, verifier] of verifierMap) {
Expand Down
34 changes: 21 additions & 13 deletions tasks/deploy.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,12 @@
import { HardhatEthersHelpers, HardhatRuntimeEnvironment } from 'hardhat/types';
import fs from 'node:fs';
import { ContractAddresses, NetworkConfig } from '../utils/scripts/types';
import { parseJsonFile } from './utils';
import { getExistingConfig } from './utils';

const ADDRESS_FILE = process.env['ADDRESS_FILE'] || 'contract-addresses.json';

export type AddressesConfig = { [key: string]: ContractAddresses };

export const getExistingConfig = () => {
try {
return parseJsonFile<AddressesConfig>(ADDRESS_FILE);
} catch (error) {
console.warn(error);
}

return undefined;
};

export const writeConfigToDisk = (config: NetworkConfig) => {
fs.writeFileSync(ADDRESS_FILE, JSON.stringify(config));
console.log(`address file available at: ${ADDRESS_FILE}`);
Expand All @@ -43,7 +33,7 @@ export const updateConfig = (
}

return {
...getExistingConfig(),
...getExistingConfig(ADDRESS_FILE),
[`${network}.${chainId}`]: contractAddresses,
};
};
Expand Down Expand Up @@ -71,6 +61,9 @@ export const deployContracts = async (
const customSmartWalletDeployVerifierF = await ethers.getContractFactory(
'CustomSmartWalletDeployVerifier'
);
const nativeHolderSmartWalletF = await ethers.getContractFactory(
'NativeHolderSmartWallet'
);

const versionRegistryFactory = await ethers.getContractFactory(
'VersionRegistry'
Expand Down Expand Up @@ -103,7 +96,18 @@ export const deployContracts = async (
customSmartWalletFactoryAddress
);
const { address: customRelayVerifierAddress } = await relayVerifierF.deploy(
smartWalletFactoryAddress
customSmartWalletFactoryAddress
);

const { address: nativeHolderSmartWalletAddress } =
await nativeHolderSmartWalletF.deploy();
const { address: nativeHolderSmartWalletFactoryAddress } =
await smartWalletFactoryF.deploy(nativeHolderSmartWalletAddress);
const { address: nativeDeployVerifierAddress } = await deployVerifierF.deploy(
nativeHolderSmartWalletFactoryAddress
);
const { address: nativeRelayVerifierAddress } = await relayVerifierF.deploy(
nativeHolderSmartWalletFactoryAddress
);

const { address: versionRegistryAddress } =
Expand All @@ -126,6 +130,10 @@ export const deployContracts = async (
CustomSmartWalletFactory: customSmartWalletFactoryAddress,
CustomSmartWalletDeployVerifier: customDeployVerifierAddress,
CustomSmartWalletRelayVerifier: customRelayVerifierAddress,
NativeHolderSmartWallet: nativeHolderSmartWalletAddress,
NativeHolderSmartWalletFactory: nativeHolderSmartWalletFactoryAddress,
NativeHolderSmartWalletDeployVerifier: nativeDeployVerifierAddress,
NativeHolderSmartWalletRelayVerifier: nativeRelayVerifierAddress,
UtilToken: utilTokenAddress,
VersionRegistry: versionRegistryAddress,
};
Expand Down
76 changes: 11 additions & 65 deletions tasks/getAllowedTokens.ts
Original file line number Diff line number Diff line change
@@ -1,71 +1,15 @@
import { HardhatRuntimeEnvironment } from 'hardhat/types';
import { getExistingConfig } from './deploy';
import { getVerifiers } from './utils';

export const getAllowedTokens = async (hre: HardhatRuntimeEnvironment) => {
const { ethers, network } = hre;

if (!network) {
throw new Error('Unknown Network');
}

const { chainId } = network.config;

if (!chainId) {
throw new Error('Unknown Chain Id');
}

const contractAddresses = getExistingConfig();

if (!contractAddresses) {
throw new Error('No contracts deployed');
}

const networkChainKey = `${network.name}.${chainId}`;
const contractAddressesDeployed = contractAddresses[networkChainKey];
if (!contractAddressesDeployed) {
throw new Error(`Contracts not deployed for chain ID ${chainId}`);
}

const deployVerifierAddress = contractAddressesDeployed.DeployVerifier;
const relayVerifierAddress = contractAddressesDeployed.RelayVerifier;
const customDeployVerifierAddress =
contractAddressesDeployed.CustomSmartWalletDeployVerifier;
const customRelayVerifierAddress =
contractAddressesDeployed.CustomSmartWalletRelayVerifier;

if (!deployVerifierAddress) {
throw new Error('Could not obtain deploy verifier address');
}

if (!relayVerifierAddress) {
throw new Error('Could not obtain relay verifier address');
}

if (!customDeployVerifierAddress) {
throw new Error('Could not obtain custom deploy verifier address');
}

if (!customRelayVerifierAddress) {
throw new Error('Could not obtain custom deploy verifier address');
}

const deployVerifier = await ethers.getContractAt(
'DeployVerifier',
deployVerifierAddress
);
const relayVerifier = await ethers.getContractAt(
'RelayVerifier',
relayVerifierAddress
);
const customDeployVerifier = await ethers.getContractAt(
'CustomSmartWalletDeployVerifier',
customDeployVerifierAddress
);

const customRelayVerifier = await ethers.getContractAt(
'RelayVerifier',
customRelayVerifierAddress
);
const {
deployVerifier,
relayVerifier,
customDeployVerifier,
customRelayVerifier,
nativeHolderDeployVerifier,
nativeHolderRelayVerifier,
} = await getVerifiers(hre);

const verifierMap: Map<
string,
Expand All @@ -75,6 +19,8 @@ export const getAllowedTokens = async (hre: HardhatRuntimeEnvironment) => {
verifierMap.set('relayVerifier', relayVerifier);
verifierMap.set('customDeployVerifier', customDeployVerifier);
verifierMap.set('customRelayVerifier', customRelayVerifier);
verifierMap.set('nativeHolderDeployVerifier', nativeHolderDeployVerifier);
verifierMap.set('nativeHolderRelayVerifier', nativeHolderRelayVerifier);

for (const [key, verifier] of verifierMap) {
try {
Expand Down
76 changes: 11 additions & 65 deletions tasks/removeTokens.ts
Original file line number Diff line number Diff line change
@@ -1,77 +1,21 @@
import { ContractTransaction } from 'ethers';
import { HardhatRuntimeEnvironment } from 'hardhat/types';
import { getExistingConfig } from './deploy';
import { getVerifiers } from './utils';

export const removeTokens = async (
taskArgs: { tokenlist: string },
hre: HardhatRuntimeEnvironment
) => {
const tokenAddresses = taskArgs.tokenlist.split(',');

const { ethers, network } = hre;

if (!network) {
throw new Error('Unknown Network');
}

const { chainId } = network.config;

if (!chainId) {
throw new Error('Unknown Chain Id');
}

const contractAddresses = getExistingConfig();

if (!contractAddresses) {
throw new Error('No contracts deployed');
}

const networkChainKey = `${network.name}.${chainId}`;
const contractAddressesDeployed = contractAddresses[networkChainKey];

if (!contractAddressesDeployed) {
throw new Error(`Contracts not deployed for chain ID ${chainId}`);
}

const deployVerifierAddress = contractAddressesDeployed.DeployVerifier;
const relayVerifierAddress = contractAddressesDeployed.RelayVerifier;
const customDeployVerifierAddress =
contractAddressesDeployed.CustomSmartWalletDeployVerifier;
const customRelayVerifierAddress =
contractAddressesDeployed.CustomSmartWalletRelayVerifier;

if (!deployVerifierAddress) {
throw new Error('Could not obtain deploy verifier address');
}

if (!relayVerifierAddress) {
throw new Error('Could not obtain relay verifier address');
}

if (!customDeployVerifierAddress) {
throw new Error('Could not obtain custom deploy verifier address');
}

if (!customRelayVerifierAddress) {
throw new Error('Could not obtain custom deploy verifier address');
}

const deployVerifier = await ethers.getContractAt(
'DeployVerifier',
deployVerifierAddress
);
const relayVerifier = await ethers.getContractAt(
'RelayVerifier',
relayVerifierAddress
);
const customDeployVerifier = await ethers.getContractAt(
'CustomSmartWalletDeployVerifier',
customDeployVerifierAddress
);
const customRelayVerifier = await ethers.getContractAt(
'RelayVerifier',
customRelayVerifierAddress
);
const {
deployVerifier,
relayVerifier,
customDeployVerifier,
customRelayVerifier,
nativeHolderDeployVerifier,
nativeHolderRelayVerifier,
} = await getVerifiers(hre);

const verifierMap: Map<
string,
Expand All @@ -88,6 +32,8 @@ export const removeTokens = async (
verifierMap.set('relayVerifier', relayVerifier);
verifierMap.set('customDeployVerifier', customDeployVerifier);
verifierMap.set('customRelayVerifier', customRelayVerifier);
verifierMap.set('nativeHolderDeployVerifier', nativeHolderDeployVerifier);
verifierMap.set('nativeHolderRelayVerifier', nativeHolderRelayVerifier);

for (const tokenAddress of tokenAddresses) {
for (const [key, verifier] of verifierMap) {
Expand Down