Reward for gas fees optimizations implemented for AIP21 staking contract.
aip, treasury, voting
- Ape coin staking has been one of the most anticipated events in the crypto space. Ensuring the security and efficiency is a high priority.
- Multiple gas optimizations were proposed to the community and Horizen Labs team and received positive feedback, the team have implemented them and they are live on the mainnet staking contract.
- For the input on optimizing the gas fees consumption that were disclosed and discussed previously on an AIP idea, a reward is proposed for the timely contribution.
- Note that gas optimizations are completely out of AIP-134 bug bounty scope, as it is only focused on security vulnerabilities.
- Due to the large-scale impact on saving Apecoin ecosystem stakers on gas fees for a prolonged period of time on every action they take in the process of using the staking service; includes deposit, claim and withdraw, a reward is proposed for the genuine contribution in the best interest of the ecosystem at the right time which has been implemented, deployed on the testnet, and already live on the mainnet!
- Gas fees saving currently might be insignificant that can be amounted from few cents to few dollars, but considering the magnitude of the apecoin users and the times when the Ethereum network has higher demand for block space (higher gas prices) similar to the ones of 2021 craze, then it would definitely make a significant difference.
- Staking contract would be serving the ApeCoin family for the next 3 years. In this period of time, the amount of gas fees would be consumed is immense. In this regard, there are 3 collections eligible for staking with 25,000 total unique NFTs owners that owns at least 1 NFT and 103,000 unique $APE owners. Thus, any optimization would save the Apecoin family holders a lot of ETH in gas fees upfront! instead of being blatantly burnt.
- Since any user would at least deposit, claim and withdraw once in their lifetime usage. Which is 3 unique separate transactions. Let’s try to calculate an estimate for the gas cost that would be saved on average based on the past 3 years data, considering the following parameters:
- we use an average gas price of the past 3 years up to December 1st 2022 (credit: Etherscan) which is 71.2 GWEI.
- we use an average ETH price of the past 3 years up to December 1st 2022 (credit: Etherscan) which is 1658$.
- The following is a table illustrating an overall gas stat when interacting with the contract functions using only 1 NFT token for NFT related functions using the previously mentioned parameters and 1000 iterations to average out gas consumption:
- We have total unique owners as of (Dec 1st):
- APECOIN_TotalOwners = 103000
- BAYC_TotalOwners = 6420
- MAYC_TotalOwners = 12936
- BAKC_TotalOwners = 5586
- We sum up all functions saved cost:
- APECOIN_TotalCost = 3.07 + 0.584 + 3.48 = 7.13$
- BAYC_TotalCost = 3.073 + 0.764 + 3.426 = 7.26 $
- MAYC_TotalCost = 3.086 + 0.759 + 3.425 = 7.27$
- BAKC_TotalCost = 3.548 + 3.603 + 3.562 = 10.71$
- By calculating the total cost, we find:
TotalCost = ∑〖TotalOwners * TotalCost〗≅ 934,870$
- At least a 934,000$ is saved upfront in gas fees potentially for all users in the next 3 years!
- gas consumption: total gas unit which is consumed by a transaction, it constitutes any transaction in the following formula:
Gas * Gas Price (in GWEI) = Gas Fees (in ETH)
- GWEI: a denomination of ether and a computation unit commonly used in gas prices:
1 GWEI = 10^9 Gas
Steps to Implement
Once the proposal is approved, the Ape Foundation may use the tokens equivalent to a floor BAYC from the treasury to buy it on the open market and reward it to the contributor.
Since the optimizations are already implemented, reviewed, incorporated and live on the mainnet, there is no needed timeline to be implemented.
Proposed reward is a floor BAYC, equivalent to 10% of the funds saved upfront. It would be HODLed and staked to earn $APE rewards.
Hi, code. These are the kinds of proposals I love to see.
First of all, I’m 100% against not setting a price. PLEASE don’t “leave it up to the community.” Your work is the most valuable work in this DAO, and in crypto at-large. Set a price and make sure that price includes a profit margin that is worth your skill set.
Second, I wonder if those contracts can be changed at this late date? And does updating them mean another round of audits?
Hi Mantis, Thanks for your valuable feedback.
in terms of the changes that were made, they are targeting some of the best practices in terms of using memory and storage efficiently. as an audit have been taken place should have covered most aspects and possible vulnerabilities, so since HALBORN have made an audit:
, they can run the same unit tests, and use Slither and MythX for automated testing as they did in that report. so the only thing that might take sometime is manual assessment if needed by a security engineer. i am fine with sharing my findings with Horizon Labs to assess the changes and see if they are worth the integration and if they are worth going for another round of audit.
in terms of the payment i ll make sure to set a price if it this proposal made it out there and thank you for your input Mantis, much appreciated <3
Reducing gas by 19.3% sounds like a no brainer to me. I understand that people want to stake their ApeCoin asap but at this point, since we have already passed all previous deadlines and hit roadblocks, let just implement this.
Cool. The next thing I’d ask for is your repo or resume, just because I’ve just met you. But the ideas you present sound good.
You will have the hurdle of Apecoin holder emotions. Everybody wants staking now, and many people are willing to take shortcuts to achieve that.
may i know if you have a github account?, so i can share the repo access with you.
emotions are pretty hard to control, especially with the past due deadlines
i maxed out on replies, so i ll reply here for now lol,
replying to death comment:
actually gas is the main constant component in calculating the final gas fees, the other 2 variables that keeps on fluctuating are ETH price and Gas Price in Gwei
so the main gas amount cost will be there at all time.
consider all the variables that makes up the denominated ETH price and then make your judgement. so for now let’s try to calculate the gas fees difference:
Tx Fee = (gas amount) * Gas Price = (fees in ETH)
referring to Ethereum Average Gas Price
we can take as an average gas price for now is 20 GWEI which is very conservative.
we will be using 3 test cases when we deposit / claim / withdraw :
1 NFT and do iterate through the same calls 5 times (to get better gas consumption averaged out between cold/warm storage slot access.)
2 NFTs and do iterate through the same calls 5 times
5 NFTs and do iterate through the same calls 5 times
so to make it easier to calculate for any ETH price or Gas Price, you can use the following formula:
- ‘optimized’ and ‘unoptimized’ refers to the columns in previous tables which are the amount of gas that certain function consumes
- Tx fee = (unoptimized - optimized) * ( (GWEI PRICE e.g: 20) * 10**9 ) * (ETH PRICE e.g: 1260) = $ value
- something to consider is that this is in a single transaction, a single user would engage in at least 3 of those functions, which adds up, so i would say that it’s very important to keep it in mind. maybe you would need to wait a bit longer, we are in a bear anyway, and rip off the benefits later on.
Yea sure, I just DM’d u about it
Can you put down some estimate fees in ETH pre and post optimization instead of percentages.
The reason is, if this will delay staking by 2 more weeks, and will cost the DAO xK USD, these funds might be more than what will be saved from Y users staking/unstaking Z times. Also if the fees are ~1-10$ a 15%-35% saving would be 3.5$ at most which is not worth another delay imo.
You can’t assume today’s fees will be tomorrow’s fees. Fees are not constant, so this calculation is impossible.
And that $3.5 might be worth it for some people. Have you been around for the many staking debacles of 2020-2021?
You are awesome, thanks for taking the time to put this together
Hi mate, I sent your proposal to Dean from Horizen on Twitter. Maybe they could do the optimization during the bug bounty process - without the classic DAO proposal structure/process.
@RedVulkan Don’t you have direct access to their team? It would be nice to be effective in the case of staking timeline and deliver it in time after the last postponement. But with effectiveness on the contract side too.
Hey @codeislight ,
I would suggest following up with @llama or @austin regarding the bug bounty, asap. They may be able to consider your contribution here in a way that doesn’t require a new AIP or cause any further delays. Along with setting up appropriate compensation.
Thank you for taking the time to do this work for the community here.
Amazing work @codeislight, love the way you took action without waiting, and yes you should be compensated. Hopefully we can have this integrated before staking goes live.
I am trying to reach out to Horizon Labs through twitter, github and email. but so far without any response, i would love to know if i can have any contact to their dev team, to share my findings and assess the optimizations that are safe to integrate, as it’s just improvement without any breaking changes.
Can we get some help here to a worthy cause, perhaps someone with some pull on Twitter who can organize a tweet storm or something? LFG!!!
Code, link the tweet here. I’ll like and RT it so maybe they’ll pay more attention. Others should do the same
I’ll retweet as well. I believe we need a Special Council member that is involved more in the forum… like I’ve only seen a reply from 1 of them so far, BoredApeG and that’s it.
I have spent this day to do more optimization, I only included the functions relevant to the end-users, I managed to beat the previous records
when depositing / claim / withdraw 1 NFT:
when depositing / claim / withdraw 2 NFT:
when depositing / claim / withdraw 5 NFT:
After almost a week of trying to reach out to Horizen Labs, no response from their product manager on twitter and no response on Github nor through email so i have decided to open source the optimizations so at least i would have played my role in the DAO:
I believe it can be even more optimized if there would be more effort put into it. but for now, i would just go hands off, let the community to decide how they want to proceed with it.
- forming a proposal to review and integrate those changes, and reduce the costs upfront.
- ignore it and kick the can down the road, and eventually pay the price for the next 3 years.
- let’s get it going
- no need, we want to roll out ASAP
I was glad to meet all the amazing people in the community that were trying their best to help, and i am grateful for their help. @Lost @austin @Mantis @llama
Do your best; that’s all you can do. Props from me!