Skip to content

Commit

Permalink
Refactor burn native tokens method
Browse files Browse the repository at this point in the history
  • Loading branch information
Mike-CZ committed Dec 16, 2024
1 parent 7c57e0e commit 4e77902
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 10 deletions.
22 changes: 13 additions & 9 deletions contracts/sfc/SFC.sol
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ contract SFC is OwnableUpgradeable, UUPSUpgradeable, Version {
);
event ClaimedRewards(address indexed delegator, uint256 indexed toValidatorID, uint256 rewards);
event RestakedRewards(address indexed delegator, uint256 indexed toValidatorID, uint256 rewards);
event BurntFTM(uint256 amount);
event BurntNativeTokens(uint256 amount);
event UpdatedSlashingRefundRatio(uint256 indexed validatorID, uint256 refundRatio);
event RefundedSlashedLegacyDelegation(address indexed delegator, uint256 indexed validatorID, uint256 amount);
event AnnouncedRedirection(address indexed from, address indexed to);
Expand Down Expand Up @@ -459,9 +459,12 @@ contract SFC is OwnableUpgradeable, UUPSUpgradeable, Version {
emit TreasuryFeesResolved(fees);
}

/// burnFTM allows SFC to burn an arbitrary amount of FTM tokens.
function burnFTM(uint256 amount) external onlyOwner {
_burnFTM(amount);
/// Burn native tokens by sending them to the SFC contract.
function burnNativeTokens() external payable {
if (msg.value == 0) {
revert ZeroAmount();
}
_burnNativeTokens(msg.value);
}

/// Issue tokens to the issued tokens recipient as a counterparty to the burnt FTM tokens.
Expand Down Expand Up @@ -753,7 +756,7 @@ contract SFC is OwnableUpgradeable, UUPSUpgradeable, Version {
if (!sent) {
revert TransferFailed();
}
_burnFTM(penalty);
_burnNativeTokens(penalty);

emit Withdrawn(delegator, toValidatorID, wrID, amount - penalty, penalty);
}
Expand Down Expand Up @@ -817,12 +820,13 @@ contract SFC is OwnableUpgradeable, UUPSUpgradeable, Version {
return rewards;
}

/// Burn FTM tokens.
/// Burn native tokens.
/// The tokens are sent to the zero address.
function _burnFTM(uint256 amount) internal {
if (amount != 0) {
function _burnNativeTokens(uint256 amount) internal {
if (amount != 0 && totalSupply >= amount) {
totalSupply -= amount;
payable(address(0)).transfer(amount);
emit BurntFTM(amount);
emit BurntNativeTokens(amount);
}
}

Expand Down
22 changes: 21 additions & 1 deletion test/SFC.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ describe('SFC', () => {
const evmWriter: IEVMWriter = await ethers.deployContract('StubEvmWriter');
const initializer: UnitTestNetworkInitializer = await ethers.deployContract('UnitTestNetworkInitializer');

await initializer.initializeAll(0, 0, sfc, nodeDriverAuth, nodeDriver, evmWriter, owner);
await initializer.initializeAll(0, ethers.parseEther('100000'), sfc, nodeDriverAuth, nodeDriver, evmWriter, owner);
const constants: UnitTestConstantsManager = await ethers.getContractAt(
'UnitTestConstantsManager',
await sfc.constsAddress(),
Expand Down Expand Up @@ -55,6 +55,26 @@ describe('SFC', () => {
).to.revertedWithCustomError(this.sfc, 'TransfersNotAllowed');
});

describe('Burn native tokens', () => {
it('Should revert when no amount sent', async function () {
await expect(this.sfc.connect(this.user).burnNativeTokens()).to.be.revertedWithCustomError(
this.sfc,
'ZeroAmount',
);
});

it('Should succeed and burn native tokens', async function () {
const amount = ethers.parseEther('1.5');
const totalSupply = await this.sfc.totalSupply();
const tx = await this.sfc.connect(this.user).burnNativeTokens({ value: amount });
await expect(tx).to.emit(this.sfc, 'BurntNativeTokens').withArgs(amount);
expect(await this.sfc.totalSupply()).to.equal(totalSupply - amount);
await expect(tx).to.changeEtherBalance(this.sfc, 0);
await expect(tx).to.changeEtherBalance(this.user, -amount);
await expect(tx).to.changeEtherBalance(ethers.ZeroAddress, amount);
});
});

describe('Genesis validator', () => {
beforeEach(async function () {
const validator = ethers.Wallet.createRandom();
Expand Down

0 comments on commit 4e77902

Please sign in to comment.