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?
Anyway, here are the last three Solidity tips for you, my fellow JavaScript users!
Contracts have a receive
/fallback
Function
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 external payable
.
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
}
}
The Fallback Function
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.
The Receive Function
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.
Solidity Variables are Initialized by Default
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.
Initial Values
- address:
0x0
- array (dynamic):
[]
- array (fixed): fixed-size array with initial values
- boolean:
false
- 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
- int:
0
- mapping: empty mapping
- string:
""
- struct: a struct where all members are set to initial values
- uint:
0
Requesting a Wallet Account Requires a Manual Call of eth_requestAccounts
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.
Conclusion
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!
I saw thousands of views on this series, so I pursuing more ideas in that direction this year. So if you're a web2 JavaScript developer who wants to learn more about web3, stay tuned!
Top comments (0)