Vidma is pleased to present this audit report outlining our assessment of code, smart contracts, and other important audit insights and suggestions for management, developers, and users.
The audited scope of work includes the GGStaking smart contract. The contract is a staking contract that allows users to stake and unstake their NFTs. Additionally, users can get EG tokens, which are calculated with a formula and depend on the claim fee set. These tokens are immutable and set only in constructor. Moreover, they inherited the ERC20 standard and the NFT is ERC721 accordingly. Both standards are from OpenZeppelin library, which makes their transactions safe. The contract has an access control with Ownable contract from @openZeppelin library, which makes functions such as depositReward and setTokensInfo, unreachable for other users. In addition, user should stake 8 different types of tokens to get more eg tokens.
During the audit process, the Vidma. A detailed summary and the current state are displayed in the table below.
After evaluating the findings in this report and the final state after fixes, the Vidma auditors can state that the contracts are fully operational and secure. Under the given circumstances, we set the following risk level:
To set the codebase quality mark, our auditors are evaluating the initial commit given for the scope of the audit and the last commit with the fixes. This approach helps us adequately and sequentially evaluate the quality of the code. Code style, optimization of the contracts, the number of issues, and risk level of the issues are all taken into consideration. The Vidma team has developed a transparent evaluation codebase quality system presented below.
Evaluating the initial commit and the last commit with the fixes, Vidma audit team set the following codebase quality mark.
Score
Based on the overall result of the audit and the state of the final reviewed commit, the Vidma audit team grants the following score:
In addition to manual check and static analysis, the auditing team has conducted a number of integrated autotests to ensure the given codebase has an adequate performance and security level. The test results and coverage can be found in the accompanying section of this audit report.
Please be aware that this audit does not certify the definitive reliability and security level of the contract. This document describes all vulnerabilities, typos, performance issues, and security issues found by the Vidma audit team.
If the code is still under development, we highly recommend running one more audit once the code is finalized.
EG’s mission is to leverage community action and blockchain technologies to grow a global movement that defies the status quo and makes profitability intrinsically linked to positive social impact.
Within the scope of this audit, two independent auditors thoroughly investigated the given codebase and analyzed the overall security and performance of the smart contracts.
The audit was conducted from March 23, 2023 to March 30, 2023. The outcome is disclosed in this document. The review of the fixes was finished on April 7, 2023.
The scope of work for the given audit consists of the following contract:
The source code was taken from the following source:
https://github.com/EG-Ecosystem/GG-NFT-Staking
Initial commit submitted for the audit:
6532930875a995e3a4c357fdb3b5444967179cf5
Last commit reviewed by the auditing team:
Vidma audit team uses the most sophisticated and contemporary methods and well-developed techniques to ensure contracts are free of vulnerabilities and security risks. The overall workflow consists of the following phases:
After the Audit kick-off, our security team conducts research on the contract’s logic and expected behavior of the audited contract.
Vidma auditors do a deep dive into your tech documentation with the aim of discovering all the behavior patterns of your codebase and analyzing the potential audit and testing scenarios.
At this point, the Vidma auditors are ready to kick off the process. We set the auditing strategies and methods and are prepared to conduct the first audit part.
During the manual phase of the audit, the Vidma team manually looks through the code in order to find any security issues, typos, or discrepancies with the logic of the contract. The initial commit as stated in the agreement is taken into consideration.
Static analysis tools are used to find any other vulnerabilities in smart contracts that were missed after a manual check.
An interim report with the list of issues.
Within the testing part, Vidma auditors run integration tests using the Truffle or Hardhat testing framework. The test coverage and the test results are inserted in the accompanying section of this audit report.
Second interim report with the list of new issues found during the testing part of the audit process.
For simplicity in reviewing the findings in this report, Vidma auditors classify the findings in accordance with the severity level of the issues. (from most critical to least critical).
All issues are marked as “Resolved” or “Unresolved”, depending on if they have been fixed by project team or not. The issues with “Not Relevant” status are left on the list of findings but are not eligible for the score points deduction.
The latest commit with the fixes reviewed by the auditors is indicated in the “Scope of Work” section of the report.
The Vidma team always provides a detailed description of the issues and recommendations on how to fix them.
Classification of found issues is graded according to 6 levels of severity described below:
High MH – 01 | Invalid
In GGStaking contract the depositReward() function uses the accumulated reward per share, which is stored as an integer with 12 decimal places. This could lead to precision loss if the reward amount or the number of staked tokens is too large.
Add precision for calculation.
Medium MM – 01 | Resolved
In GGStaking contract the global variable claimFeeWallet allowed to be address 0, when it is set in function setClaimFeeWallet(). At the same a require claimFeeWallet != 0 in function setClaimFee() does not allow to set claimFee variable, if the fee wallet address is zero-address. Hence, in function claim() fee amount can be sent to zero-address claim fee wallet and lost for always.
Add require condition for setClaimWallet() function and delete one from setClaimFee().
Medium MM – 02 | Resolved
In GGStaking contract the function depositReward() uses the function transfers tokens to the contract before updating the accumulated reward per share. This could allow an attacker to re-enter the function and claim rewards multiple times before the accumulated reward per share is updated.
Use checks-effects-interactions pattern; call the transfer function after all rewards updating.
Medium MM – 03 | Invalid
In GGStaking contract unstake() function is not checked if the user has any pending rewards before unstaking. If the user has pending rewards, they should first claim them before unstaking their NFTs. If the user unsatake their NFTs before claiming their rewards, they could lose their pending rewards.
Add checking for pending rewards before unstake them.
Medium MM – 04 | Invalid
In GGStaking contract unstake() function is not checked if the user’s depositNumber is less than or equel to the totalDepositCount before calculating the pending rewards. This could cause the function to throw an error if the user’s depositNumber is greater that totalDepositCount.
Add checking for depositNumber and totalDepositCount.
Medium TM – 01 | Resolved
The WithdrawUnusedRewardPot event always takes a zero value due to a typo in the code.
Store the value of unusedRewardPot in a temporary variable before nulling it and use the stored value in the event.
Low ML – 01 | Resolved
In GGStaking contract the constructor parameters are set without checking their addresses. For example, if the address will be zero-address all funds will be lost.
Add require condition for all variables in constructor.
Low ML – 02 | Invalid
In GGStaking smart contract the setTokenInfo function is only called by the contract owner, it is safe to remove the external visibility modifier and replace it with public. This can result in some gas savings.
Low ML – 03 | Resolved
In GGStaking contract events are emitted without indexed parameter, which helps to log parameters in a transaction. The reason it is important to add indexed parameter to events is because it allows for more efficient and selective event filtering. By making certain parameters as indexed, Solidity can create an index of these values, making it easier and faster to search for events with the specific parameter value.
For example, event Staked, Unstaked, Claim, SetClaimWallet need indexed parameter for address staker.
Add indexed parameter in events.
Low TL – 01 | Resolved
In lines 405, 406, the variable is set to 0. This is a useless step that increases the use of gas.
Remove initialization of variables.
Please follow the same recommendations as stated in the following issue: TL - 01.
Low TL – 02 | Resolved
The following line of code can be shortened
Write something like this
Informational MI – 01 | Resolved
In GGStaking smart contract has the order of functions and declared variables not the same as in solidity documentation.
Layouts, functions, and modifier order should be grouped according to Solidity documentation:
Layouts:
Functions:
After reordering variables and functions, in new comment lines 77 and 79 should be swapped to have one style.
Informational MI – 02 | Resolved
GGStaking smart contract has no explanation for functions and variables, that makes it harder to understand the contract’s logic.
Add Natspeck even for internal and private functions in all contracts that do not have it.
Informational MI – 03 | Resolved
Naming conventions in Solidity are important for creating clear, understandable, and maintainable code.
Informational MI – 04 | Resolved
In GGStaking contract the ‘for’ loop is executed twice for setting the token info. This can be combined into a single loop to optimize gas usage.
In the for loop for checking the _squadIds array, the condition (_squadIds[i] < squadTokenFeatures.length) is used to check if the squid id is valid. However, squadTokenFeatures.length is hardcoded to 8 and does not match the actual length of the squadTokenFeatures array. This can cause the function to fail for valid squad ids. A better way to check the valid squad ids would be to use squadTokenFeatures.contains(_squadIds[i]) instead of the hardcoded length.
Informational MI – 05 | Resolved
In GGStaking contract the depositReward() function uses a lot 1e12 data, without any explanation what this variable is mean.
Create and declare the global constant for 1e12.
Informational MI – 06 | Resolved
Since the function userStakedNFTs() in GGStaking contract is read-only, it is not necessary to mark the user variable as storage. This variable can be marked as memory to reduce gas costs.
Use
In function getPending(), which is also view, is used UserInfo storage user = userInfos[_user];, but user data is not rewrited into struct. Hence, it woulb be better to use memory instead of storage to reguce gast cost.
Informational MI – 07 | Resolved
In GGStaking contract function getPending() is possible an optimization - remove the conditional statement if (user.depositNumber < totalDepositCount) because it is always true. Since totalDepositCount is incremented every time a deposit is made, it will always be greater than or equal to user.depositNumber.
Informational MI – 08 | Invalid
In GGStaking contract function unstake() has the lastTokenIndex variable, which is also unnecessary and can be removed by using the length of the ownedTokens mapping.
The function iterates twice over the tokens array. The first iteration is to validate the tokens, and the second iteration is to unstake them. Instead, the function could combine both interactions into a single loop to reduce gas cost.
Implement gas optimization advice.
Informational MI – 09 | Invalid
GGStaking contract function stake() has the use of nested for loops to check duplicates and ownership of each NFT in the input array is a costly operation, especially if the input array contains many elements.
It would be more gas efficient to use a mapping to keep track of which NFTs have already been checked.
Informational MI – 10 | Invalid
The usage of the transferFrom function inside the for loop is not recommended (function stake), as it is a costly operation, and the cost increases with the number of NFTs being transferred.
It would be more gas efficient to use a single sefeBatchTransferFrom function from the ERC721 contract to transfer all the NFTs at once.
Informational MI – 11 | Invalid
In GGStaking contract the function stake has the if statement checking if requireStakeLegendaryCount > 0 is unnecessary since requireStakeLegendaryCount will always be greater than or equal to zero.
Remove this if statement.
Informational MI – 12 | Resolved
In GGStaking contract the function claim has egToken.balalnceOf(address(this)), that call is made twice, which can be replaced with a single call and stored in a variable for efficiency.
Create a variable contractBalance = egToken.balalnceOf(address(this)) and use the variable in function.
Informational MI – 13 | Resolved
GGStaking contract function _getPending() has some room for gas optimization. The function performs several divisions by the constant 1e12. These divisions can be avoided by multiplying the final result by 1e12. Additionally, there is no need to declare the pending variable before assigning it a value, as it is only used to store intermediate values. Instead, the function can directly return the final result.
Informational MI – 14 | Resolved
The function can be optimized by breaking the loop early if the user has already staked NFTs from all eight squads. This can be done by adding a break statement after the return true statement.
The userSquadTokenFeaturesSum variable can be incremented inside the loop using the += operator instead of the + operator.
Please follow the recommendations when it is better to use storage data location and when memory.
Memory, is a temporary data location used for storing variables that are only needed during the execution of a particular function. Memory is cheaper to access than storage, but its contents are lost once the function call is completed. Memory is typically used for variables that are only needed within the scope of a single function.
Another recommendation is to save gas-cost. As this contract has pragma version 0.8.17, which has internal SafeMath implementation, math operation can be used +=. Please check all contracts for that.
To verify the security of contracts and their performance, a number of integration tests were carried out using the Hardhat testing framework.
In this section, we provide both tests written by EG and tests written by Vidma auditors.
Vidma Coverage – 95.8%
Industry Standard – 95%
It is important to note that Vidma auditors do not modify, edit or add tests to the existing tests provided in the EG repository. We write totally separate tests with code coverage of a minimum of 95% to meet the industry standards.
We are delighted to have a chance to work with the EG team and contribute to your company's success by reviewing and certifying the security of your smart contracts.
The statements made in this document should be interpreted neither as investment or legal advice, nor should its authors be held accountable for decisions made based on this document.