Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// scripts/box-console.ts
import { network } from 'hardhat';

const CONTRACT_ADDRESS = 'INSERT_CONTRACT_ADDRESS';
const VALUE_TO_STORE = 5n;

async function main() {
if (CONTRACT_ADDRESS === 'INSERT_CONTRACT_ADDRESS') {
throw new Error(
'Update CONTRACT_ADDRESS with the Box deployment address (0x...).'
);
}

if (!CONTRACT_ADDRESS.startsWith('0x')) {
throw new Error('CONTRACT_ADDRESS must start with 0x.');
}

const { ethers } = await network.connect('moonbase');
const [signer] = await ethers.getSigners();

console.log(`Using signer ${signer.address}`);
console.log(`Attaching to Box at ${CONTRACT_ADDRESS}`);

const Box = await ethers.getContractFactory('Box');
const box = await Box.attach(CONTRACT_ADDRESS);

const current = (await box.retrieve()).toString();
console.log(`Current stored value: ${current}`);

console.log(`Calling store(${VALUE_TO_STORE})...`);
const tx = await box.store(VALUE_TO_STORE);
console.log(`Submitted tx ${tx.hash}, waiting for confirmation...`);
await tx.wait();

const updated = (await box.retrieve()).toString();
console.log(`Updated stored value: ${updated}`);
}

main().catch((err) => {
console.error(err);
process.exitCode = 1;
});
Original file line number Diff line number Diff line change
@@ -1,36 +1,13 @@
<div id="termynal" data-termynal>
<span data-ty="input">npx hardhat console --network moonbase</span>
<br>
<span data-ty>Welcome to Node.js v20.9.0.</span>
<span data-ty>Type ".help" for more information.</span>
<span data-ty="input" data-ty-prompt=">"> const Box = await ethers.getContractFactory('Box');</span>
<span data-ty>undefined</span>
<br>
<span data-ty="input" data-ty-prompt=">"> const box = await Box.attach('0xfBD78CE8C9E1169851119754C4Ea2f70AB159289');</span>
<span data-ty>undefined</span>
<br>
<span data-ty="input" data-ty-prompt=">"> await box.store(5);</span>
<span data-ty>ContractTransactionResponse {<br>
provider: HardhatEthersProvider { ... },<br>
blockNumber: null,<br>
blockHash: null,<br>
index: undefined,<br>
hash: '0x1c49a64a601fc5dd184f0a368a91130cb49203ec0f533c6fcf20445c68e20264',<br>
type: 2,<br>
to: '0xa84caB60db6541573a091e5C622fB79e175E17be',<br>
from: '0x3B939FeaD1557C741Ff06492FD0127bd287A421e',<br>
nonce: 87,<br>
gasLimit: 45881n,<br>
gasPrice: 1107421875n,<br>
maxPriorityFeePerGas: 1n,<br>
maxFeePerGas: 1107421875n,<br>
data: '0x6057361d0000000000000000000000000000000000000000000000000000000000000005',<br>
value: 0n,<br>
chainId: 5678n,<br>
signature: Signature { r: "0x9233b9cc4ae6879b7e08b9f1a4bfb175c8216eee0099966eca4a305c7f369ecc", s: "0x7663688633006b5a449d02cb08311569fadf2f9696bd7fe65417860a3b5fc57d", yParity: 0, networkV: null },<br>
accessList: [],<br>
blobVersionedHashes: null<br>}</span>
<span data-ty="input" data-ty-prompt=">"> await box.retrieve();</span>
<span data-ty>5n</span>
<br>
<span data-ty="input"><span class="file-path"></span>npx hardhat run --network moonbase scripts/box-console.ts</span>
<br>
<span data-ty></span>
<span data-ty>[hardhat-keystore] Enter the password: **********</span>
<span data-ty>Using signer 0x3B939FeaD1557C741Ff06492FD0127bd287A421e</span>
<span data-ty>Attaching to Box at INSERT_CONTRACT_ADDRESS</span>
<span data-ty>Current stored value: 4</span>
<span data-ty>Calling store(5)...</span>
<span data-ty>Submitted tx 0xdb0a7bfe9caeae5fb590cde27ad156f9b2788434aa4a94b95d1c3a8ce3d58b30, waiting for confirmation...</span>
<span data-ty>Updated stored value: 5</span>
<span data-ty="input"><span class="file-path"></span>%</span>
</div>

This file was deleted.

87 changes: 16 additions & 71 deletions builders/ethereum/dev-env/hardhat.md
Original file line number Diff line number Diff line change
Expand Up @@ -371,96 +371,41 @@ Congratulations, your contract is live! Save the address, as you will use it to

## Interact with the Contract {: #interacting-with-the-contract }

There are a couple of ways that you can interact with your newly deployed contract using Hardhat: you can use the `console` task, which spins up an interactive JavaScript console, or you can create another script and use the `run` task to execute it.
You can interact with your newly deployed contract using Hardhat in two ways: run console-style commands from a helper script (recommended for Hardhat 3), or reuse that script to automate interactions via the `run` task.

### Using the Hardhat Console {: #hardhat-console }
### Console-Style Interaction {: #hardhat-console }

The [Hardhat console](https://hardhat.org/hardhat-runner/docs/guides/hardhat-console){target=\_blank} uses the same execution environment as the tasks and scripts, so it automatically uses the configurations and plugins defined in the `hardhat.config.js`.
Similarly to the deployment script, you can create a lightweight helper that mirrors the console workflow, save it under `scripts`, and run it with the built-in `run` task. This approach works even when your credentials are stored in the Hardhat keystore because `network.connect()` is executed inside a normal Hardhat task (which can unlock the keystore) instead of the console, which does not have access to the Hardhat keystore.

To launch the Hardhat `console`, you can run:
To get started, create a `box-console.ts` file in the `scripts` directory:

```sh
npx hardhat console --network moonbase
cd scripts && touch box-console.ts
```

Next, you can take the following steps, entering one line at a time:
Update the script with your deployed contract address before running it. The full helper looks like this:

1. Create a local instance of the `Box.sol` contract.
??? code "View the complete script"

```js
const Box = await ethers.getContractFactory('Box');
```

2. Connect the local instance to the deployed contract, using the address of the contract shown in the prior step under **Deployed Addresses**.

```js
const box = await Box.attach('INSERT-CONTRACT-ADDRESS');
```

3. Interact with the attached contract. For this example, you can call the `store` method and store a simple value.

```js
await box.store(5);
```ts
--8<-- 'code/builders/ethereum/dev-env/hardhat/scripts/box-console.ts'
```

The transaction will be signed by your account configured in the `hardhat.config.js` file and broadcasted to the network. The output should look similar to:

--8<-- 'code/builders/ethereum/dev-env/hardhat/terminal/interact.md'

Notice your address labeled `from`, the address of the contract, and the `data` that is being passed. Now, you can retrieve the value by running:

```js
await box.retrieve();
```

You should see `5`, or the value you initially stored.
Replace `INSERT_CONTRACT_ADDRESS` with the address printed by Hardhat Ignition and adjust `VALUE_TO_STORE` if you want to store a different value.

### Using a Script {: #using-a-script }
### Run the Script {: #run-the-script }

Similarly to the deployment script, you can create a script to interact with your deployed contract, store it in the `scripts` directory, and run it using the built-in `run` task.

To get started, create a `set-value.js` file in the `scripts` directory:
Use the `run` task to execute the helper script against your deployed `Box` contract so you can verify the keystore unlock flow works and confirm the contract stores the new value on Moonbase Alpha.

```sh
cd scripts && touch set-value.js
```

Now paste the following contract into the `set-value.js` file:

```js
// scripts/set-value.js
async function main() {
// Create instance of the Box contract
const Box = await ethers.getContractFactory('Box');

// Connect the instance to the deployed contract
const box = await Box.attach('INSERT-CONTRACT-ADDRESS');

// Store a new value
await box.store(2);

// Retrieve the value
const value = await box.retrieve();
console.log(`The new value is: ${value}`);
}

main()
.then(() => process.exit(0))
.catch(error => {
console.error(error);
process.exit(1);
});
npx hardhat run --network moonbase scripts/box-console.ts
```

To run the script, you can use the following command:
You'll be prompted for the Hardhat keystore password (if you're using encrypted secrets), after which the script connects to Moonbase Alpha, attaches to your deployed `Box` contract, and logs the values stored before and after calling `store(5n)`. Upon running it, you should see output similar to:

```sh
npx hardhat run --network moonbase set-value.js
```

The script should return `2` as the value.
--8<-- 'code/builders/ethereum/dev-env/hardhat/terminal/interact.md'

--8<-- 'code/builders/ethereum/dev-env/hardhat/terminal/run.md'
The script prints the signer being used, the value stored before the transaction, the submitted transaction hash, and the updated value after calling `store(5n)`.

## Hardhat Forking {: #hardhat-forking }

Expand Down
Loading