LAMBO Token contract Everyman walkthrough
Welcome to the Everyman introduction into the LAMBO contract! This is designed so that someone inexperienced with code can try to read and understand the LAMBO contract easier. This is a great supplement to looking at the actual contract, and if there’s something you don’t understand (for example: What is mapping in Solidity?) we recommend checking out the Solidity documentation.
Note: Line numbers are estimates. Contract numbers might not align perfectly as comments are removed, etc.
The first 1,080 lines are standard dependencies that are included (Context, Ownable, IERC20, SafeMath, Address, SafeERC20, Pausable, ERC0, IUniswapV2Pair, FixedPoint, UniswapV2OracleLibrary, and UniswapV2Library).
This is where we store bonus amounts and the block when they’re released. You can see that the struct TimelockedBonus stores the pair of bonusAmount and releaseBlock. These pairs are then mapped to an address in _timelockedBonuses.
_maxSellRemoval is the maximum % of tokens permitted to be removed from a sell order.
_maxBuyBonus is the maximum % bonus tokens permitted to be rewarded to a buy order.
This is then followed by the functions corresponding to those internal variables that allow anyone to view the variables (Lines 1100 and 1104), and the bonus amount (Line 1118) / bonus unlock time (Line 1111) for any given address.
These functions allow the contract itself to change the max payout (Line 1134) and max penalty (Line 1127).
Lastly, these two functions are in charge of managing user bonus amounts.
_addToTimelockedBonus allows the contract to increase the bonus balance for any given bonusAddress, by combining the input tokens_to_add with the currently existing balance. The bonus unlock time is updated to be 48 hours from this event. That is, if you already have a bonus amount (for example, with 20 hours until it’s unlocked), and you buy again from Uniswap, your new bonus amount will be the old + the new one, and it will unlock in 48 hours as a single amount.
_removeTimelockedBonus allows the contract to effortlessly remove the bonus balance of any given bonusAddress — this is only used when someone is claiming their tokens.
Main LamboToken Contract
There are a lot of internal and public variables in the LAMBO contract. A lot of these are self-explanatory, and come with thorough comments. Ask us if any of them aren’t clear! Here’s the list of emittable events:
What does this mean for you? Let’s look at the BonusBalanceUpdated event. When you buy from Uniswap, you get a transaction that looks like this:
The Ethereum comes out of your wallet, passes through the Uniswap router (labeled Uniswap above), and reaches the trading pair.
The LAMBO comes out of the trading pair and directly into your wallet. How do you know that you were awarded bonus LAMBO? Let’s investigate the Logs tab (7 events are indicated as having occurred).
We can see a list of every event emitted during this transaction, but most importantly there’s an event called BonusBalanceUpdated. Great! We can directly view the creation of our bonus balance being recorded on the blockchain.
Here’s an example sell order:
We can see that the Nitro protocol functions as it should: Some of the tokens go to the nitro address and the staking address, and the remaining go to the trading pair.
Let’s continue looking through the contract.
This is the initial constructor for the token contract. It performs a series of administrative functions, and pauses the contract (Line 1353) to prevent tokens from being transferred before everything is ready.
Here’s where the initial distribution of LAMBO occurs. Line 1373 defines how much LAMBO goes to the contract owner (to commit liquidity to Uniswap, pay the moderators, and distribute giveaway funds). Line 1377 defines how much LAMBO starts with the contract (Nitro protocol, presale contract). On line 1379 we check to make sure that we added these two numbers correctly to 2,049 LAMBO.
Lines 1381–1382 mint to the contract owner and whitelist it from participating in the nitro protocol. The same thing is done for the token contract itself on lines 1384–1385.
Lastly, the Uniswap router we’re expecting to interact with is whitelisted from participating in the nitro protocol too.
The contract, when created, doesn’t allow anyone but the token contract, presale contract, and initial distribution address to move LAMBO around. This is to prevent users from maliciously creating the Uniswap pair with the wrong ratio. Once everything is set and ready to go, the owner can unpause the contract to fully launch the project. How do we know the owner can’t pause the contract again? Let’s look at the function the constructor called, _pause().
We can see that the _pause() function has the modifiers internal and whenNotPaused. This means two things:
1. We can’t program the contract to re-pause itself at any time, because it can only be paused once.
2. Only the contract itself can call this function.
So, the contract cannot be paused again.
Continuing: Lines 1410–1482 modify internal variables such as the Max Buy Bonus, Max Sell Removal, etc.
This function is an internal function (Only the contract itself can call this function) that is used for updating a user’s bonus amount of tokens owed. On line 1489 we define the release time to be the current block number + the defined bonusReleaseTime (48 hours worth of blocks). Then on line 1492 we tell the nitro protocol to add to the user’s bonus amount with this new data.
Lines 1495–1534 modify contract variables such as the delta time required to set the new TWAP, set addresses that are excluded from participating in the nitro protocol, and to set addresses that are exchange addresses.
Time for the big boy!
First, we check if the sender or recipient is an exchange address. If the exchange address is sending lambo, we know that someone is buying. We check to make sure that the twap is updated. We get the current nitro rate, and calculate the amount of bonus tokens to be awarded. We save those bonus tokens in the nitro protocol and then emit the BonusBalanceUpdated event.
If the exchange address is receiving lambo, we know that someone is selling lambo. We check to make sure that the twap is updated. We get the current nitro rate, and calculate the amount of tokens to be removed. From there, we split those between the stakers and the nitro protocol, and fire off the transactions.
All of these functions relating to the TWAP are standard for utilizing, implementing, and calculating the Time-Weighted average price. The “real-time” price is not actually real-time; in an Automated Market Maker such as Uniswap, calculating the real-time price as the ratio between the two pools of assets is vulnerable to certain kinds of attacks. As such, the real-time price is actually a TWAP with a very short delta-time requirement (5–10 minutes), as compared to the TWAP used for nitro calculations (48 hours).
The claimBonusTokens() function is how users interact with the Nitro Protocol to claim their bonus tokens. On line 1706, we get the current bonus tokens owed to the user. Lines 1709–1713 validate that there are tokens that can be paid to this user at this moment.
Line 1718 removes the bonus stored in the Nitro Protocol, and then emits the bonus update event on Line 1721.
Line 1724 transfers the bonusTokens removed from the Nitro Protocol from the contract address(this) to the user msg.sender.
Line 1727 emits that a buyer was successfully paid their due bonus.
Lastly, we have the function that calculates the current Nitro rate. The Nitro rate is currently the same for buyers and sellers, but just in case, we have permitted the input variable isBuy to let the Nitro Protocol calculate a different rate for buyers and sellers if it becomes necessary.
Thank you for reading this Everyman introduction into the Lambo platform! The github with this code will be live today and this post will be updated soon after.
Our presale is happening tomorrow! Details:
Whitelisters (Price: 0.28 ETH/ LAMBO Max: 1.5 eth) and Devs (0.18 eth/ LAMBO Max: 2.8 ETH) have 24 hours to invest
Then it goes to a public presale for 1 hour or until it sells out (Price: 0.42 ETH/ LAMBO, Max: 0.5 eth). The maximum includes if you’ve already invested.
Once that presale is over, we list on Uniswap at 0.42 ETH/LAMBO.