Skip to content

Commit

Permalink
Define custom errors for the checks
Browse files Browse the repository at this point in the history
The ERC4626 already uses custom errors, so it would be nice to have them
here for consistency.
  • Loading branch information
r-czajkowski committed Dec 12, 2023
1 parent 7516de4 commit 331c1d3
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 26 deletions.
37 changes: 20 additions & 17 deletions core/contracts/Acre.sol
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ contract Acre is ERC4626, Ownable {
uint256 maximumTotalAssets
);

error StakingAmountLessThanMin(uint256 amount, uint256 min);
error InvalidStakingParameter();

constructor(
IERC20 tbtc
) ERC4626(tbtc) ERC20("Acre Staked Bitcoin", "stBTC") Ownable(msg.sender) {
Expand All @@ -52,15 +55,9 @@ contract Acre is ERC4626, Ownable {
uint256 maximumTotalAssets
) external onlyOwner {
// TODO: Introduce a parameters update process.
require(
minimumDepositAmount > 0,
"Minimum deposit amount must be greater than zero"
);

require(
maximumTotalAssets > 0,
"Maximum total assets amount must be greater than zero"
);
if (minimumDepositAmount <= 0 || maximumTotalAssets <= 0) {
revert InvalidStakingParameter();
}

stakingParameters.minimumDepositAmount = minimumDepositAmount;
stakingParameters.maximumTotalAssets = maximumTotalAssets;
Expand All @@ -72,10 +69,12 @@ contract Acre is ERC4626, Ownable {
uint256 assets,
address receiver
) public override returns (uint256) {
require(
assets >= stakingParameters.minimumDepositAmount,
"Amount is less than minimum"
);
if (assets < stakingParameters.minimumDepositAmount) {
revert StakingAmountLessThanMin(
assets,
stakingParameters.minimumDepositAmount
);
}

return super.deposit(assets, receiver);
}
Expand All @@ -84,10 +83,14 @@ contract Acre is ERC4626, Ownable {
uint256 shares,
address receiver
) public override returns (uint256) {
require(
previewMint(shares) >= stakingParameters.minimumDepositAmount,
"Amount is less than minimum"
);
uint256 assets = previewMint(shares);

if (assets < stakingParameters.minimumDepositAmount) {
revert StakingAmountLessThanMin(
assets,
stakingParameters.minimumDepositAmount
);
}

return super.mint(shares, receiver);
}
Expand Down
14 changes: 5 additions & 9 deletions core/test/Acre.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ describe("Acre", () => {
acre
.connect(staker1)
.stake(amountToStake, staker1.address, referral),
).to.revertedWith("Amount is less than minimum")
).to.revertedWithCustomError(acre, "StakingAmountLessThanMin")
})
})

Expand All @@ -188,7 +188,7 @@ describe("Acre", () => {
acre
.connect(staker1)
.stake(amountToStake, staker1.address, referral),
).to.revertedWith("Amount is less than minimum")
).to.revertedWithCustomError(acre, "StakingAmountLessThanMin")
})
})

Expand Down Expand Up @@ -534,7 +534,7 @@ describe("Acre", () => {
it("should take into account the min deposit amount parameter and revert", async () => {
await expect(
acre.connect(staker1).mint(sharesToMint, staker1.address),
).to.be.revertedWith("Amount is less than minimum")
).to.be.revertedWithCustomError(acre, "StakingAmountLessThanMin")
})
},
)
Expand Down Expand Up @@ -596,9 +596,7 @@ describe("Acre", () => {
minimumDepositAmount,
validMaximumTotalAssetsAmount,
),
).to.be.revertedWith(
"Minimum deposit amount must be greater than zero",
)
).to.be.revertedWithCustomError(acre, "InvalidStakingParameter")
})
})
})
Expand All @@ -615,9 +613,7 @@ describe("Acre", () => {
validMinimumDepositAmount,
maximumTotalAssets,
),
).to.be.revertedWith(
"Maximum total assets amount must be greater than zero",
)
).to.be.revertedWithCustomError(acre, "InvalidStakingParameter")
})
})
})
Expand Down

0 comments on commit 331c1d3

Please sign in to comment.