Skip to content

Commit

Permalink
fix(cli): Infer chain id from provider on interact command & get chai…
Browse files Browse the repository at this point in the history
…n id before running anvil (#829)
  • Loading branch information
saeta-eth authored Mar 21, 2024
1 parent 8364021 commit feaa4d9
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 15 deletions.
2 changes: 1 addition & 1 deletion packages/builder/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export async function getExecutionSigner(
const address = ('0x' + hash.slice(0, 40)) as viem.Address;

await provider.impersonateAccount({ address });
await provider.setBalance({ address, value: BigInt(1e22) });
await provider.setBalance({ address, value: viem.parseEther('10000') });

const client = viem.createWalletClient({
account: address,
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/commands/alter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export async function alter(
async getSigner(addr: viem.Address) {
// on test network any user can be conjured
//await provider.impersonateAccount({ address: addr });
//await provider.setBalance({ address: addr, value: BigInt(1e22) });
//await provider.setBalance({ address: addr, value: viem.parseEther('10000') });
return { address: addr, wallet: provider as viem.WalletClient };
},
snapshots: false,
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/commands/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ export async function build({
async function (addr: viem.Address) {
// on test network any user can be conjured
await (provider as unknown as viem.TestClient).impersonateAccount({ address: addr });
await (provider as unknown as viem.TestClient).setBalance({ address: addr, value: BigInt(1e22) });
await (provider as unknown as viem.TestClient).setBalance({ address: addr, value: viem.parseEther('10000') });

return {
address: addr,
Expand Down
8 changes: 5 additions & 3 deletions packages/cli/src/commands/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,13 @@ export async function run(packages: PackageSpecification[], options: RunOptions)

// Start the rpc server
const node = options.node;

const provider = getProvider(node)!;
const nodeLogging = await createLoggingInterface(node);

if (options.fundAddresses && options.fundAddresses.length) {
for (const fundAddress of options.fundAddresses) {
await provider?.setBalance({ address: fundAddress as viem.Address, value: BigInt(1e22) });
await provider?.setBalance({ address: fundAddress as viem.Address, value: viem.parseEther('10000') });
}
}

Expand All @@ -76,19 +77,20 @@ export async function run(packages: PackageSpecification[], options: RunOptions)
// set up signers
for (const addr of (options.impersonate || '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266').split(',')) {
await provider.impersonateAccount({ address: addr as viem.Address });
await provider.setBalance({ address: addr as viem.Address, value: BigInt(1e22) });
await provider.setBalance({ address: addr as viem.Address, value: viem.parseEther('10000') });
signers = [{ address: addr as viem.Address, wallet: provider }];
}

const chainId = await provider.getChainId();

const basicRuntime = new ChainBuilderRuntime(
{
provider: provider,
chainId,
async getSigner(addr: viem.Address) {
// on test network any user can be conjured
await provider.impersonateAccount({ address: addr });
await provider.setBalance({ address: addr, value: BigInt(1e22) });
await provider.setBalance({ address: addr, value: viem.parseEther('10000') });
return { address: addr, wallet: provider };
},
snapshots: chainId === CANNON_CHAIN_ID,
Expand Down
1 change: 0 additions & 1 deletion packages/cli/src/commandsConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -636,7 +636,6 @@ const commandsConfig = {
{
flags: '-c --chain-id <chainId>',
description: 'Chain ID of deployment to interact with ',
required: true,
},
{
flags: '-n --provider-url [url]',
Expand Down
27 changes: 21 additions & 6 deletions packages/cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,10 @@ function configureRun(program: Command) {
forkProvider: provider,
});
} else {
if (options.providerUrl) {
const _provider = viem.createPublicClient({ transport: viem.http(options.providerUrl) });
options.chainId = await _provider.getChainId();
}
node = await runRpc(pickAnvilOptions(options));
}

Expand Down Expand Up @@ -516,17 +520,28 @@ applyCommandsConfig(program.command('interact'), commandsConfig.interact).action
) {
const cliSettings = resolveCliSettings(opts);

let chainId = opts.chainId;
let chainId: number | undefined = opts.chainId ? Number(opts.chainId) : undefined;

// throw an error if the chainId is not consistent with the provider's chainId
await ensureChainIdConsistency(opts.providerUrl, chainId);
const isProviderUrl = cliSettings.providerUrl.startsWith('http');

const { provider, signers } = await resolveWriteProvider(cliSettings, chainId);
// throw an error if both chainId and providerUrl are not provided
if (!chainId && !isProviderUrl) {
throw new Error('Please provide one of the following options: --chain-id or --provider-url');
}

// if chainId is not provided, get it from the provider
if (!chainId) {
chainId = await provider.getChainId();
const _provider = viem.createPublicClient({ transport: viem.http(cliSettings.providerUrl) });
chainId = await _provider.getChainId();
}

// throw an error if the chainId is not consistent with the provider's chainId
if (isProviderUrl) {
await ensureChainIdConsistency(cliSettings.providerUrl, chainId);
}

const { provider, signers } = await resolveWriteProvider(cliSettings, chainId);

const resolver = await createDefaultReadRegistry(cliSettings);

const [name, version] = [packageDefinition.name, packageDefinition.version];
Expand All @@ -553,7 +568,7 @@ applyCommandsConfig(program.command('interact'), commandsConfig.interact).action
async getSigner(address: viem.Address) {
// on test network any user can be conjured
//await p.provider.impersonateAccount({ address: addr });
//await p.provider.setBalance({ address: addr, value: BigInt(1e22) });
//await p.provider.setBalance({ address: addr, value: viem.parseEther('10000') });
return { address: address, wallet: provider };
},
snapshots: false,
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/util/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ async function configureSigners(
getDefaultSigner = async () => {
const addr = signers && signers.length > 0 ? signers[0].address : '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266';
await provider.impersonateAccount({ address: addr });
await provider.setBalance({ address: addr, value: BigInt(1e22) });
await provider.setBalance({ address: addr, value: viem.parseEther('10000') });
return { address: addr, wallet: provider };
};
} else {
Expand Down
2 changes: 1 addition & 1 deletion packages/hardhat-cannon/src/tasks/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ task(TASK_BUILD, 'Assemble a defined chain and save it to to a state which can b
if (hre.network.name !== 'cannon') {
if (impersonate) {
await provider.impersonateAccount({ address: impersonate });
await provider.setBalance({ address: impersonate, value: BigInt(1e22) });
await provider.setBalance({ address: impersonate, value: viem.parseEther('10000') });
defaultSigner = getSigner(impersonate) || null;
// Add the impersonated signer if it is not part of the hardhat config
if (!defaultSigner) {
Expand Down

0 comments on commit feaa4d9

Please sign in to comment.