Skip to content

Commit

Permalink
Merge pull request #5922 from NomicFoundation/chore/add-mainnet-op-sc…
Browse files Browse the repository at this point in the history
…ript-to-templates

chore: add optimism example script to viem template
  • Loading branch information
alcuadrado authored Nov 1, 2024
2 parents 3499712 + 7ff69b1 commit 53e6898
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 11 deletions.
32 changes: 30 additions & 2 deletions v-next/hardhat/templates/node-test-runner-viem/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,36 @@ npx hardhat3 test

### Multi-chain support

To deploy a contract to an in-memory Hardhat node simulating Base (an Optimism l2 chain), run:
Hardhat network now supports simulating Optimism L2 chains. You can see an example of a local Hardhat network configured to run as an Optimism L2 in the `./hardhat.config.ts` file for the `edrOp` network:

```js
edrOp: {
type: "edr",
chainId: 10,
chainType: "optimism",
forkConfig: {
jsonRpcUrl: "https://mainnet.optimism.io",
},
},
```

A script demonstrating the sending of an L2 transaction is included as `./scripts/send-op-tx.ts`, to run it against the local `edrOp` Optimism network, run:

```shell
npx hardhat3 run scripts/send-op-tx.ts --network edrOp
```

To run the same script against the Optimism mainnet, first set the `OPTIMISM_PRIVATE_KEY` config variable, with the private key of the account you want to use to send the transaction:

> WARNING: the private key is stored unencrypted to file. Full encryption will be included in a future release.
```shell
npx hardhat3 keystore set OPTIMISM_PRIVATE_KEY
# Enter secret to store: ****************************************************************
```

Run the script with the Optimism mainnet network:

```shell
npx hardhat3 run scripts/deploy-counter-contract.ts
npx hardhat3 run scripts/send-op-tx.ts --network op
```
11 changes: 10 additions & 1 deletion v-next/hardhat/templates/node-test-runner-viem/hardhat.config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import type { HardhatUserConfig } from "@ignored/hardhat-vnext/config";
import {
configVariable,
type HardhatUserConfig,
} from "@ignored/hardhat-vnext/config";

import HardhatNodeTestRunner from "@ignored/hardhat-vnext-node-test-runner";
import HardhatViem from "@ignored/hardhat-vnext-viem";
Expand All @@ -21,6 +24,12 @@ const config: HardhatUserConfig = {
],
},
networks: {
op: {
type: "http",
chainType: "optimism",
url: "https://mainnet.optimism.io/",
accounts: [configVariable("OPTIMISM_PRIVATE_KEY")],
},
edrOp: {
type: "edr",
chainId: 10,
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { network } from "@ignored/hardhat-vnext";

const { viem, networkConfig, networkName } = await network.connect(
undefined,
"optimism",
);

console.log("Sending transaction using network", networkName);

if (networkConfig.type === "edr") {
console.log("Using an EDR network simulating Optimism, forking it");
} else {
console.log("Using an HTTP connection to Optimism");
}

const publicClient = await viem.getPublicClient();
const [senderClient] = await viem.getWalletClients();

console.log("Sender:", await senderClient.account.address);

console.log(
"Sender balance:",
await publicClient.getBalance(senderClient.account),
);

console.log("Sending 1 wei from", senderClient.account.address, "to itself");

console.log("Estimating L1 gas first");
const l1Gas = await publicClient.estimateL1Gas({
account: senderClient.account.address,
to: senderClient.account.address,
value: 1n,
});

console.log("Estimated L1 gas:", l1Gas);

console.log("Sending L2 transaction");
const tx = await senderClient.sendTransaction({
to: senderClient.account.address,
value: 1n,
});

const receipt = await publicClient.waitForTransactionReceipt({ hash: tx });

console.log("Transaction receipt:", receipt);

0 comments on commit 53e6898

Please sign in to comment.