You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -40501,7 +40501,43 @@ Let's start by setting up Hardhat for your Storage contract project:
40501
40501
6. Configure Hardhat by updating the `hardhat.config.js` file:
40502
40502
40503
40503
```javascript title="hardhat.config.js"
40504
-
Error fetching snippet from https://raw.githubusercontent.com/polkadot-developers/polkavm-hardhat-examples/refs/tags/v0.0.7/storage-hardhat/hardhat.config.js
Ensure that `INSERT_PATH_TO_SUBSTRATE_NODE` and `INSERT_PATH_TO_ETH_RPC_ADAPTER` are replaced with the proper paths to the compiled binaries.
@@ -40531,7 +40567,27 @@ Let's start by setting up Hardhat for your Storage contract project:
40531
40567
1. Create a new folder called `contracts` and create a `Storage.sol` file. Add the contract code from the previous tutorial:
40532
40568
40533
40569
```solidity title="Storage.sol"
40534
-
Error fetching snippet from https://raw.githubusercontent.com/polkadot-developers/polkavm-hardhat-examples/refs/tags/v0.0.7/storage-hardhat/contracts/Storage.sol
40570
+
// SPDX-License-Identifier: MIT
40571
+
pragma solidity ^0.8.28;
40572
+
40573
+
contract Storage {
40574
+
// State variable to store our number
40575
+
uint256 private number;
40576
+
40577
+
// Event to notify when the number changes
40578
+
event NumberChanged(uint256 newNumber);
40579
+
40580
+
// Function to store a new number
40581
+
function store(uint256 newNumber) public {
40582
+
number = newNumber;
40583
+
emit NumberChanged(newNumber);
40584
+
}
40585
+
40586
+
// Function to retrieve the stored number
40587
+
function retrieve() public view returns (uint256) {
40588
+
return number;
40589
+
}
40590
+
}
40535
40591
```
40536
40592
40537
40593
2. Compile the contract:
@@ -40557,9 +40613,28 @@ Testing is a critical part of smart contract development. Hardhat makes it easy
40557
40613
1. Create a folder for testing called `test`. Inside that directory, create a file named `Storage.js` and add the following code:
40558
40614
40559
40615
```javascript title="Storage.js"
40560
-
Error fetching snippet from https://raw.githubusercontent.com/polkadot-developers/polkavm-hardhat-examples/refs/tags/v0.0.7/storage-hardhat/test/Storage.js
Error fetching snippet from https://raw.githubusercontent.com/polkadot-developers/polkavm-hardhat-examples/refs/tags/v0.0.7/storage-hardhat/test/Storage.js
40636
+
});
40637
+
});
40563
40638
```
40564
40639
40565
40640
The `beforeEach` hook ensures stateless contract execution by redeploying a fresh instance of the Storage contract before each test case. This approach guarantees that each test starts with a clean and independent contract state by using `ethers.getSigners()` to obtain test accounts and `ethers.getContractFactory('Storage').deploy()` to create a new contract instance.
@@ -40569,7 +40644,9 @@ Testing is a critical part of smart contract development. Hardhat makes it easy
40569
40644
a. **Initial state verification** - ensures that the contract starts with a default value of zero, which is a fundamental expectation for the `Storage.sol` contract
40570
40645
40571
40646
```javascript title="Storage.js"
40572
-
Error fetching snippet from https://raw.githubusercontent.com/polkadot-developers/polkavm-hardhat-examples/refs/tags/v0.0.7/storage-hardhat/test/Storage.js
40647
+
it('Should return 0 initially', async function () {
40648
+
expect(await storage.retrieve()).to.equal(0);
40649
+
});
40573
40650
```
40574
40651
40575
40652
Explanation:
@@ -40581,7 +40658,13 @@ Testing is a critical part of smart contract development. Hardhat makes it easy
40581
40658
b. **Value storage test** - validate the core functionality of storing and retrieving a value in the contract
40582
40659
40583
40660
```javascript title="Storage.js"
40584
-
Error fetching snippet from https://raw.githubusercontent.com/polkadot-developers/polkavm-hardhat-examples/refs/tags/v0.0.7/storage-hardhat/test/Storage.js
40661
+
it('Should update when store is called', async function () {
@@ -40593,7 +40676,13 @@ Testing is a critical part of smart contract development. Hardhat makes it easy
40593
40676
c. **Event emission verification** - confirm that the contract emits the correct event when storing a value, which is crucial for off-chain tracking
40594
40677
40595
40678
```javascript title="Storage.js"
40596
-
Error fetching snippet from https://raw.githubusercontent.com/polkadot-developers/polkavm-hardhat-examples/refs/tags/v0.0.7/storage-hardhat/test/Storage.js
40679
+
it('Should emit an event when storing a value', async function () {
40680
+
const testValue = 100;
40681
+
// Check if the NumberChanged event is emitted with the correct value
40682
+
await expect(storage.store(testValue))
40683
+
.to.emit(storage, 'NumberChanged')
40684
+
.withArgs(testValue);
40685
+
});
40597
40686
```
40598
40687
40599
40688
Explanation:
@@ -40605,7 +40694,14 @@ Testing is a critical part of smart contract development. Hardhat makes it easy
40605
40694
d. **Sequential value storage test** - check the contract's ability to store multiple values sequentially and maintain the most recent value
40606
40695
40607
40696
```javascript title="Storage.js"
40608
-
Error fetching snippet from https://raw.githubusercontent.com/polkadot-developers/polkavm-hardhat-examples/refs/tags/v0.0.7/storage-hardhat/test/Storage.js
40697
+
it('Should allow storing sequentially increasing values', async function () {
40698
+
const values = [10, 20, 30, 40];
40699
+
40700
+
for (const value of values) {
40701
+
await storage.store(value);
40702
+
expect(await storage.retrieve()).to.equal(value);
40703
+
}
40704
+
});
40609
40705
```
40610
40706
40611
40707
Explanation:
@@ -40618,7 +40714,55 @@ Testing is a critical part of smart contract development. Hardhat makes it easy
40618
40714
40619
40715
???--- code "View complete script"
40620
40716
```javascript title="Storage.js"
40621
-
Error fetching snippet from https://raw.githubusercontent.com/polkadot-developers/polkavm-hardhat-examples/refs/tags/v0.0.7/storage-hardhat/test/Storage.js
it('Should emit an event when storing a value', async function () {
40749
+
const testValue = 100;
40750
+
// Check if the NumberChanged event is emitted with the correct value
40751
+
await expect(storage.store(testValue))
40752
+
.to.emit(storage, 'NumberChanged')
40753
+
.withArgs(testValue);
40754
+
});
40755
+
40756
+
it('Should allow storing sequentially increasing values', async function () {
40757
+
const values = [10, 20, 30, 40];
40758
+
40759
+
for (const value of values) {
40760
+
await storage.store(value);
40761
+
expect(await storage.retrieve()).to.equal(value);
40762
+
}
40763
+
});
40764
+
});
40765
+
});
40622
40766
```
40623
40767
40624
40768
2. Run the tests:
@@ -40647,7 +40791,13 @@ Testing is a critical part of smart contract development. Hardhat makes it easy
40647
40791
1. Create a new folder called`ignition/modules`. Add a new file named `StorageModule.js` with the following logic:
40648
40792
40649
40793
```javascript title="StorageModule.js"
40650
-
Error fetching snippet from https://raw.githubusercontent.com/polkadot-developers/polkavm-hardhat-examples/refs/tags/v0.0.7/storage-hardhat/ignition/modules/StorageModule.js
@@ -40719,7 +40869,40 @@ To interact with your deployed contract:
40719
40869
1. Create a new folder named `scripts` and add the `interact.js` with the following content:
40720
40870
40721
40871
```javascript title="interact.js"
40722
-
Error fetching snippet from https://raw.githubusercontent.com/polkadot-developers/polkavm-hardhat-examples/refs/tags/v0.0.7/storage-hardhat/scripts/interact.js
0 commit comments