The Sepolia testnet is technologically equivalent to the mainnet, however the Eth used has no value. I have no idea why not, but that's the agreement. It is provided for free via a faucet, and the network is maintained on a volunteer basis, with the storage and computation costs paid for in Sepolia Eth which are worth zero in any other currency.
The HelloWorld contract was compiled,
npx hardhat compileA local blockchain was started,
npx hardhat nodeand the contract deployed to the localnet,
npx hardhat run --network localhost ./scripts/deploy.jswhere we deployed a simple deploy script deploy.js.
We received the address of the contract. We used this address to interact with the contract using the console,
npx hardhat console --network localhostBy the way, you can also interact with the localnet using curl requests,
The Ethereum blockchain is a collection of communicating nodes. To run your contracts on the Ethereum blockchain, you must contact a node in the blockchain network. This can be done through a number of ways, such as JSON-RPC or HTTPS PUT.
In the case of HTTPS PUT, a node provides an open webserver-like port, and the client, your code, speaks to that port with http. The request is framed as an http PUT message.
In the case of JSON-RPC, an RPC (Remote Procedure Call) protocol carries your request to the node and the node carries it into the network. JSON is a data format standard based on JavaScript, and is a textual representation of a javascript object.
Computation on the Ethereum blockchain is counted in a unit called gas. An entity required computation from the blockchain must pay for the gas in a currency called Ether. Ether is denominated in ETH, or WEI, with 10^18 WEI to one ETH. The price of gas in ETH is negotiable. Along with the request is sent an amount of ETH. The actual computation cost will be deducted from that amount, and the rest refunded. If the computation attempts to exceed the provided ETH amount, the computation is terminated and there might be no refund.
Real networks cost real money. Sepolia is a test network that uses Sepolia-ETH that is free. The next step is to get a supply of Sepolia-ETH, connect to the Sepolia network, and deploy and interact with our contract.
To accomplish this next step, we will need Infura and Metamask.
When you create an account with Infura (which you will have to do), and create an API key, this API key is used to allow an Sepolia node to accept your requests. In this case, the request looks like an HTTPS request, so of the form,
curl https://sepolia.infura.io/v3/__INFURA_API_KEY__ \ -X POST \ -H "Content-Type: application/json" \ -d '{ "jsonrpc": "2.0", "method": "eth_blockNumber", "params": [], "id": 1 }'Once you are setup with an API Key, using curl to send that HTTPS request will return the current Sepolia testnet blocknumber.
The Ethereum block chain as two types of accounts,
A wallet manages a Public-key pair, the secret key is kept in the wallet and is not part of Ethereum; the public part is hashed to an address, and stored in the ethereum chain at that address, with the wallet balance.
After installing the MetaMask browser extension, you can create a wallet, copy out the secret key for use with Hardhat; and refer to the address of the wallet to send and receive Sepolia-ETH was well as mainnet ETH (and some other cryptocurrencies).
Cavaet Emptor: was the above about external accounts correct?
The way you get ETH is you go to a crpyo-exchange that has ETH and you agree with it on an exchange of dollars (say) for ETH. The exchange takes your dollars and transfers over to you ETH. The exchanges compete in a market place for the exact exchange rate.
However Sepolia ETH is worthless, and therefore should be free. The network is a voluntary loss-leader to help developers develop for the main net. Everything technical should be identical between the testnet and the mainnet, except on a testnet, its currency has an exchange rate value of zero.
And ironically, since it is worthless, it is somewhat hard to obtain.
The testnet is a "loss-leader" to encourage main-net activity. It is run voluntarily, including the mining of its ether, which is distributed freely by a faucet that will provide you with daily drips of Sepolia. Here is one:
https://cloud.google.com/application/web3/faucet/ethereum/sepoliaThis one requires an account with the same address with some true ETH in it. If you want to try this out, purchase ETH somehow, and transfer it to this address. Note: the same address will have two types of currencies.
npm install --save-dev @nomicfoundation/hardhat-toolbox npm install --save-dev @nomicfoundation/hardhat-ignition-ethersand set the configuration variables using Hardhat's configuration management,
npx hardhat vars set INFURA_API_KEY ----type the Infura API key--- npx hardhat vars set SEPOLIA_PRIVATE_KEY ----type the private key for your wallet--- npx hardhat vars list // to list
NB: the toolbox install is a rollup that might neglect to enter the rolled-up tools in the config file; It might be sufficient to omit the toolbox and install ethers and ignition. For the moment this is what we need.
Then update hardhat.config.js to require the new modules, and to declare a sepolia network.
/** @type import('hardhat/config').HardhatUserConfig */ require("@nomicfoundation/hardhat-ethers"); require("@nomicfoundation/hardhat-ignition-ethers"); require("@nomicfoundation/hardhat-toolbox"); // this is from the tutorial https://hardhat.org/tutorial/deploying-to-a-live-network // see that URL for explanations. const { vars } = require("hardhat/config"); const INFURA_API_KEY = vars.get("INFURA_API_KEY"); const SEPOLIA_PRIVATE_KEY = vars.get("SEPOLIA_PRIVATE_KEY"); module.exports = { solidity: "0.8.24", networks: { sepolia: { url: `https://sepolia.infura.io/v3/${INFURA_API_KEY}`, accounts: [SEPOLIA_PRIVATE_KEY], }, }, };
Create the path ignition/modules/ and add this script,
// ignition/modules/hello_world.js const { buildModule } = require("@nomicfoundation/hardhat-ignition/modules"); const HelloWorldModule = buildModule("HelloWorldModule", (m) => { const hello_world = m.contract("HelloWorld"); return { hello_world }; }); module.exports = HelloWorldModule ;
then deploy to sepolia,
npx hardhat ignition deploy ./ignition/modules/hello_world.js --network sepolia --resetand test as before, but using the console open,
npx hardhat console --network sepoliaYou can check the connection by asking for the current block number of the head block of the Sepolia chain, and for the balance on an account given the account address,
> await ethers.provider.getBlockNumber() 6441556 > (await ethers.provider.getBalance("0x2B0799d4F4A0252C5cB8110674610073983076a6"))I have found that deploy costs 0.0005 ETH. Setting the greeting costs about 0.0001 ETH.
author: burton rosenberg
created: 1 aug 2024
update: 3 oct 2024