The incredible math in Ethereum staking contract (and how to implement it in solidity)
This article is heavily inspired by smart contract programmer. Check out his channel
This story is cross-posted on mirror.xyz, a web3 creator platform that accept supporter donation. Please consider visiting my mirror page for this article below. Thank you.
Defi application has gone way too deep into the rabbit hole that smart contract developers sometimes have no way to catch up with the math behind the function but simply copy it and take it for granted.
A solidity program often starts with a readable but very (gas) inefficient implementation. With a mixture of good math and programming techniques in algorithm and data structure, the program will become more efficient, but at the same time more complicated to interpret. A simple concept like staking token is a really good example for developers to truly understand the principle of efficient solidity code.
This article is structured by first walking through an example of a token staking event, converting it…