I wanted to publish the third part of this series after I finished my first Solidity project alongside the source code of that project.
Sadly, I got a few issues connecting the frontend to the Hardhat local network.
Anyway, I figured I could still publish it unfinished; at least all the tests pass, haha.
Here is the link to the repo. The examples from the book Hands-On Smart Contract Development with Solidity and Ethereum but rewritten for Hardhat and TypeScript. Again, the frontend doesn’t work; I started one with Preact but had some issues with the local network and didn’t find the time to solve this.
Maybe, some of you might figure out how to get the frontend connected to the local network?
The EVM calls two special functions when someone sends transactions to your contract that no other functions could handle. They don’t need the
function keyword and must be
contract MyContract fallback() external payable // called when none of the contract's functions // match the called function signature receive() external payable // called when the call data is empty
You can use the
fallback function to delegate calls to different contracts. Since contracts deployed on the Ethereum blockchain are immutable, you need some indirection if you want to upgrade them over time. This can be done with a contract that only implements the
fallback function that will relay calls to any function to a contract address. You can change this address and, in turn, indirectly change the implementation of the contract.
You can use the
receive function to handle calls only concerned with Ether and nothing else. For example, when you want to store a token amount into the smart contract’s balance.
Because solidity uses static typing, the language knows what type every variable at compile time. Each of these types has an initial value the EVM will use when it executes.
- array (dynamic):
- array (fixed): fixed-size array with initial values
- enum: first enum element
- function (external): a function that always throws an error
- function (internal): a function that returns initial values if a return is defined
- mapping: empty mapping
- struct: a struct where all members are set to initial values
Browser extension wallets usually block access to the accounts they manage from websites. The user has to allow the website to access it manually.
Somehow Ethers.js doesn’t ask for permissions automatically when you want to use a wallet account. You have to manually send a request to the wallet before you can use an account/signer.
const provider = new ethers.providers.Web3Provider( window.ethereum, chainId ) await provider.send("eth_requestAccounts", ) const account = provider.getSigner()
The call to the asynchronous
send method will block until the user accepted the request.
With this article I finished my first steps into Solidity, smart contract development, and web3 in general.
I hope, I could clear things up for you and my project helps you to get started!