Welcome to my post about Smart Contract development! This post is mostly just my notes on how to get a super simple smart contract (self executing program on a blockchain) written, deployed and called. For me it is easier to learn something new by doing. When dealing with blockchains and smart contracts however, that "doing" becomes expensive when you are trying to do something many times for educational purposes. Since every write to a blockchain costs something (on Ethereum Virtual Machine, or EVM, based networks, this cost is referred to as a Gas Fee). To avoid the cost of writing to a real blockchain, we'll use a tool called .
In this post we'll create simple contract with the server-side Javascript runtime already installed. Note that my notes below are based on my experience on a Linux server, but it shouldn't be too difficult to adapt to some other OS.
Create a directory and node project
mkdir counter-test
cd counter-test/
npm init --yesAdd Hardhat to project
npm install --save-dev hardhat
Create new Hardhat project
npx hardhat init
Now that you have a local blockchain to test with, let's go back to the other terminal and deploy our smart contract with the deploy.js script that we created earlier.
npx hardhat run scripts/deploy.js --network localhost
If we monitor the Hardhat node we will see our attempt to deploy the smart contract. Note that the From address matches the public address of the first test account. Also make note that we now have a contract address (0x5fbdb2315678afecb367f032d93f642f64180aa3), we'll need this to interact with the contract.
Interact With Your Contract From Hardhat Console
Now that our contract is written and deployed on the blockchain, let's try to call some of it's functions.
Start the Hardhat Javascript console
npx hardhat console --network localhost
We'll store our contract connection in the myContract variable
const myContract = await ethers.getContractAt("CounterTest","0x5fbdb2315678afecb367f032d93f642f64180aa3")
Next, let's call our getCount() function and see what the current count is:
await myContract.getCount()
You should get "0n" if this is our first run. Ignore the n, this is just indicates the type. You can cast this to an integer or whatever type you need in code if you were using this contract in real-life.Now, let's increment the counter with our incrementCount() function:
await myContract.incrementCount()
You'll see a lot more output from this call since we are actually adding information to the blockchain. Most notably you'll see the blockHash of the transaction and the gasPrice.Call getCount() again to confirm that the counter incremented as expected. The entire interaction should look something like this:
library to interact with your contract.↗ Original-Artikel auf dev.to lesenVollständiger Original-BerichtAusführliche Details, Code-Beispiele & Hersteller-Stellungnahme auf dev.to.
SOCIAL SHARE CARD GENERATOR