An optimization proposal for Staking Contract overall 19.3% gas reduction on Deposit / Claim / Withdraw

Hi Apecoin community, i was thrilled to go through the staking contracts proposed in AIP21 & AIP22, I noticed that the contracts are lacking some gas optimization, and i thought to instead of trying to overhead myself with trying to penetrate the contract, instead to spend sometime to optimize it to save users on transactions fees, as this is a very much underlooked aspect, as they can cost users a lot in the long-run, especially for projects that is of such large scale and user base. so I managed to reach to the following average gas reduction (referring to the amount of gas that has been saved, which you can think of it as a direct correlation to ETH saved on gas fees for users) :
on depositing: 39.36%
on rewards claiming: 15.56%
on withdraws:3%

these stats are an average of running 8 test cases on all the functions:

  • 4 tests using 1 NFT for deposit/claim/withdraw and on each test iterating respectively 1, 2, 3, and 5 times.
  • 4 tests using 2 NFTs for deposit/claim/withdraw and on each test iterating respectively 1, 2, 3, and 5 times.

i have compiled all the results in the following 2 graphs:

first graph for the first 4 test cases:

second graph for the other 4 test cases:

would love to take your feedback on how to proceed with it, should i open a draft proposal? as this is my first participation in a DAO, just would love to let it out there, i am curious to know how i should proceed with it before the staking contract goes live, the code is already written and test cases ready to be shared.

in the construction of the optimized version, i’ve made sure to:

  • not break any logic and keep the workflow as it is.
  • rely on the requirements to optimize variables sizing within the storage slots.
  • optimize the structure for higher efficiency.
  • respect the requirements as they are.
  • make sure to not miss around with anything that might result in security vulnerability.

on regards to payment, I am fine with anything that the community/team sees worth for the work.

looking forward to hear your feedback <3

Best Regards.

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?

7 Likes

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 :slight_smile: and thank you for your input Mantis, much appreciated <3

5 Likes

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.

4 Likes

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.

3 Likes

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.
4 Likes

Yea sure, I just DM’d u about it

2 Likes

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.

1 Like

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?

1 Like

You are awesome, thanks for taking the time to put this together

2 Likes

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.

8 Likes

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.

-Lost
:gorilla::orange_heart:

7 Likes

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.

2 Likes

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.

6 Likes

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

5 Likes

much appreciated :gorilla: :white_heart:

6 Likes

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.

4 Likes

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 :fire:

when depositing / claim / withdraw 1 NFT:

when depositing / claim / withdraw 2 NFT:

when depositing / claim / withdraw 5 NFT:

6 Likes

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.

  1. forming a proposal to review and integrate those changes, and reduce the costs upfront.
  2. ignore it and kick the can down the road, and eventually pay the price for the next 3 years.
  • let’s get it going :white_check_mark:
  • no need, we want to roll out ASAP :x:

0 voters

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 :white_heart: :gorilla:

Best regards,

Code

7 Likes

Do your best; that’s all you can do. Props from me!

5 Likes