Liquid Staking

Stakelink
DeFiHardhatOracle
50,000 USDC
View results
Submission Details
Severity: medium
Invalid

test/core/staking-allowance.test.ts

The provided code is a test suite for the StakingAllowance smart contract using the Hardhat framework and Chai for assertions. It covers various functionalities, including minting and burning tokens. Here are some suggested improvements and potential vulnerabilities to consider:

Suggested Improvements

  1. More Descriptive Test Names:

    • While the test names are clear, consider adding more context. For example, rename "should be able to burn tokens" to "should correctly reduce the balance when tokens are burned" for clarity.

  2. Use assert for Consistency:

    • You are using assert.equal in some places and expect(...).to.be.revertedWith in others. To maintain consistency, consider sticking to one assertion style throughout your tests.

  3. Add More Edge Cases:

    • Testing edge cases will improve coverage. For example, test:

      • Burning 0 tokens.

      • Burning tokens when the account has a zero balance.

      • Minting with a negative amount (if applicable).

  4. Gas Usage Checks:

    • Add gas usage assertions to measure the efficiency of the contract methods, which can be helpful for optimization later.

  5. Using beforeEach Hook:

    • If your fixtures are repeated across multiple tests, consider using the beforeEach hook to improve code reuse and reduce redundancy.

  6. Add Comments:

    • Adding comments to explain the purpose of each test case can improve code readability for others (or yourself in the future).

  7. Organize Imports:

    • Group imports logically (e.g., third-party libraries, then your local utilities and types) for better readability.

Potential Vulnerabilities

  1. Approval Race Condition:

    • The burnFrom function could be vulnerable to the classic ERC20 approval race condition. If a user first calls approve and then a malicious actor calls transferFrom or burnFrom before the first transaction completes, it can lead to unexpected behaviors. Ensure you’re using the "increase allowance" pattern if possible.

  2. Overflow and Underflow:

    • Although Solidity versions from 0.8.x and above automatically check for overflow and underflow, ensure you’re using appropriate checks in your smart contract. However, if you’re using earlier versions, consider using SafeMath libraries.

  3. Gas Limit:

    • If the mint or burn functions involve complex calculations or interactions, consider how they may behave under different gas limits or when called by multiple addresses simultaneously.

  4. Access Control:

    • Ensure that the access control on minting and burning operations is rigorously checked. The test for non-owners is good, but check other functions that could be misused if access control is improperly implemented.

  5. Token Reentrancy:

    • If your burn or mint methods call external contracts (like callbacks), they might be vulnerable to reentrancy attacks. Ensure that all state changes are made before any external calls.

Example of Improved Test

Here's an example of an improved test structure with some of the suggestions applied:

describe('StakingAllowance', () => {
let signers, accounts, adrs, token;
beforeEach(async () => {
({ signers, accounts, adrs, token } = await loadFixture(deployFixture));
});
it('should correctly reduce the balance when tokens are burned', async () => {
await token.burn(toEther(1000));
const balance = await token.balanceOf(accounts[0]);
assert.equal(fromEther(balance), 9000, 'balance does not match');
});
it('should not allow burning more than allowed', async () => {
await token.approve(accounts[1], toEther(1000));
await expect(token.connect(signers[1]).burnFrom(accounts[0], toEther(1001)))
.to.be.revertedWith('ERC20: insufficient allowance');
});
it('should not allow minting from non-owner', async () => {
await expect(token.connect(signers[1]).mint(accounts[0], toEther(10000)))
.to.be.revertedWith('Ownable: caller is not the owner');
});
// Additional edge case tests here...
});

By making these improvements and considering potential vulnerabilities, you'll strengthen your testing strategy and contribute to more robust contract development.

Updates

Lead Judging Commences

inallhonesty Lead Judge about 1 year ago
Submission Judgement Published
Invalidated
Reason: Lack of quality

Support

FAQs

Can't find an answer? Chat with us on Discord, Twitter or Linkedin.