Skip to content

Commit

Permalink
Add revert into burn method
Browse files Browse the repository at this point in the history
  • Loading branch information
Mike-CZ committed Dec 16, 2024
1 parent 4e77902 commit de08380
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
6 changes: 5 additions & 1 deletion contracts/sfc/SFC.sol
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ contract SFC is OwnableUpgradeable, UUPSUpgradeable, Version {
// values
error ZeroAmount();
error ZeroRewards();
error ValueTooLarge();

// pubkeys
error PubkeyUsedByOtherValidator();
Expand Down Expand Up @@ -823,7 +824,10 @@ contract SFC is OwnableUpgradeable, UUPSUpgradeable, Version {
/// Burn native tokens.
/// The tokens are sent to the zero address.
function _burnNativeTokens(uint256 amount) internal {
if (amount != 0 && totalSupply >= amount) {
if (amount != 0) {
if (amount > totalSupply) {
revert ValueTooLarge();
}
totalSupply -= amount;
payable(address(0)).transfer(amount);
emit BurntNativeTokens(amount);
Expand Down
12 changes: 11 additions & 1 deletion test/SFC.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { BlockchainNode, ValidatorMetrics } from './helpers/BlockchainNode';
describe('SFC', () => {
const fixture = async () => {
const [owner, user] = await ethers.getSigners();
const totalSupply = ethers.parseEther('100');
const sfc = await upgrades.deployProxy(await ethers.getContractFactory('UnitTestSFC'), {
kind: 'uups',
initializer: false,
Expand All @@ -24,7 +25,7 @@ describe('SFC', () => {
const evmWriter: IEVMWriter = await ethers.deployContract('StubEvmWriter');
const initializer: UnitTestNetworkInitializer = await ethers.deployContract('UnitTestNetworkInitializer');

await initializer.initializeAll(0, ethers.parseEther('100000'), sfc, nodeDriverAuth, nodeDriver, evmWriter, owner);
await initializer.initializeAll(0, totalSupply, sfc, nodeDriverAuth, nodeDriver, evmWriter, owner);
const constants: UnitTestConstantsManager = await ethers.getContractAt(
'UnitTestConstantsManager',
await sfc.constsAddress(),
Expand All @@ -39,6 +40,7 @@ describe('SFC', () => {
nodeDriver,
nodeDriverAuth,
constants,
totalSupply
};
};

Expand All @@ -63,6 +65,14 @@ describe('SFC', () => {
);
});

it('Should revert when amount greater than total supply', async function () {
await expect(this.sfc.connect(this.user).burnNativeTokens({ value: this.totalSupply + 1n }))
.to.be.revertedWithCustomError(
this.sfc,
'ValueTooLarge',
);
});

it('Should succeed and burn native tokens', async function () {
const amount = ethers.parseEther('1.5');
const totalSupply = await this.sfc.totalSupply();
Expand Down

0 comments on commit de08380

Please sign in to comment.