Skip to content

Commit

Permalink
feat: enabled getter and event
Browse files Browse the repository at this point in the history
  • Loading branch information
capedcrusader21 committed Jul 18, 2024
1 parent 1a84cf5 commit a59d6ba
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 32 deletions.
22 changes: 13 additions & 9 deletions packages/oft-sand/contracts/OFTAdapterForSand.sol
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ import {ERC2771Handler} from "./sand/ERC2771Handler.sol";
/// @author The Sandbox
/// @dev contract to be used with non-upgradable SAND contract
contract OFTAdapterForSand is OFTAdapter, WithAdmin, ERC2771Handler {
bool public enabled;
bool internal _enabled;

/// @notice Emitted when the enabled state changes
/// @param _enabled The new enabled state
event StateChanged(bool _enabled);
/// @param enabled The new enabled state
event Enabled(bool enabled);

/// @notice Custom error thrown when the send function is called while disabled
error SendFunctionDisabled();
Expand All @@ -43,25 +43,29 @@ contract OFTAdapterForSand is OFTAdapter, WithAdmin, ERC2771Handler {
_trustedForwarder = trustedForwarder;
}

function enable(bool _enabled) external onlyAdmin {
_enable(_enabled);
function enable(bool enabled) external onlyAdmin {
_enable(enabled);
}

function getEnabled() external view returns (bool) {
return _enabled;
}

function send(
SendParam calldata _sendParam,
MessagingFee calldata _fee,
address _refundAddress
) public payable virtual override returns (MessagingReceipt memory msgReceipt, OFTReceipt memory oftReceipt) {
if (!enabled) {
if (!_enabled) {
revert SendFunctionDisabled();
}

super.send(_sendParam, _fee, _refundAddress);
}

function _enable(bool _enabled) internal {
enabled = _enabled;
emit StateChanged(_enabled);
function _enable(bool enabled) internal {
_enabled = enabled;
emit Enabled(_enabled);
}

function _msgSender() internal view override(ERC2771Handler, Context) returns (address sender) {
Expand Down
22 changes: 13 additions & 9 deletions packages/oft-sand/contracts/OFTSand.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ import {SandBaseToken} from "./sand/SandBaseToken.sol";
/// @dev OFTSand is a contract that combines SandBaseToken, ERC2771Handler, and OFTCore functionalities.
/// @dev It provides a token contract implementation of Sand token with LayerZero compatibility.
contract OFTSand is SandBaseToken, ERC2771Handler, OFTCore {
bool public enabled;
bool internal _enabled;

/// @notice Emitted when the enabled state changes
/// @param _enabled The new enabled state
event StateChanged(bool _enabled);
/// @param enabled The new enabled state
event Enabled(bool enabled);

/// @notice Custom error thrown when the send function is called while disabled
error SendFunctionDisabled();
Expand All @@ -39,8 +39,12 @@ contract OFTSand is SandBaseToken, ERC2771Handler, OFTCore {
_trustedForwarder = trustedForwarder;
}

function enable(bool _enabled) external onlyAdmin {
_enable(_enabled);
function enable(bool enabled) external onlyAdmin {
_enable(enabled);
}

function getEnabled() external view returns (bool) {
return _enabled;
}

/// @notice Indicates whether the OFT contract requires approval of the 'token()' to send.
Expand All @@ -62,16 +66,16 @@ contract OFTSand is SandBaseToken, ERC2771Handler, OFTCore {
MessagingFee calldata _fee,
address _refundAddress
) public payable virtual override returns (MessagingReceipt memory msgReceipt, OFTReceipt memory oftReceipt) {
if (!enabled) {
if (!_enabled) {
revert SendFunctionDisabled();
}

super.send(_sendParam, _fee, _refundAddress);
}

function _enable(bool _enabled) internal {
enabled = _enabled;
emit StateChanged(_enabled);
function _enable(bool enabled) internal {
_enabled = enabled;
emit Enabled(_enabled);
}

/// @dev Burns tokens from the sender's specified balance.
Expand Down
28 changes: 14 additions & 14 deletions packages/oft-sand/test/oftSand.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ describe('OFT Contracts', function () {

it('only OFTSand admin can enable or disable the send function', async function () {
const {OFTSand, user1} = await loadFixture(setupOFTSand);
expect(await OFTSand.enabled()).to.be.true;
expect(await OFTSand.getEnabled()).to.be.true;
await expect(
OFTSand.connect(user1).enable(false),
).to.be.revertedWithCustomError(OFTSand, 'OnlyAdmin');
Expand All @@ -35,19 +35,19 @@ describe('OFT Contracts', function () {
it('OFTSand admin can enable or disable the send function', async function () {
const {OFTSand, sandAdmin} = await loadFixture(setupOFTSand);

expect(await OFTSand.enabled()).to.be.true;
expect(await OFTSand.getEnabled()).to.be.true;

await OFTSand.connect(sandAdmin).enable(false);
expect(await OFTSand.enabled()).to.be.false;
expect(await OFTSand.getEnabled()).to.be.false;

await OFTSand.connect(sandAdmin).enable(true);
expect(await OFTSand.enabled()).to.be.true;
expect(await OFTSand.getEnabled()).to.be.true;
});

it('should emit StateChanged event for OFTSand', async function () {
it('should emit Enabled event for OFTSand', async function () {
const {OFTSand, sandAdmin} = await loadFixture(setupOFTSand);
const tx = await OFTSand.connect(sandAdmin).enable(false);
await expect(tx).to.emit(OFTSand, 'StateChanged').withArgs(false);
await expect(tx).to.emit(OFTSand, 'Enabled').withArgs(false);
});

it('should return false for approvalRequired', async function () {
Expand Down Expand Up @@ -87,27 +87,27 @@ describe('OFT Contracts', function () {

it('only OFTAdapter admin can enable or disable the send function', async function () {
const {OFTAdapter, user1} = await loadFixture(setupOFTSand);
expect(await OFTAdapter.enabled()).to.be.true;
expect(await OFTAdapter.getEnabled()).to.be.true;
await expect(
OFTAdapter.connect(user1).enable(false),
).to.be.revertedWithCustomError(OFTAdapter, 'OnlyAdmin');
});

it('OFTAdapter admin can enable or disable the send function', async function () {
const {OFTAdapter, oftAdapterAdmin} = await loadFixture(setupOFTSand);
expect(await OFTAdapter.enabled()).to.be.true;
expect(await OFTAdapter.getEnabled()).to.be.true;

await OFTAdapter.connect(oftAdapterAdmin).enable(false);
expect(await OFTAdapter.enabled()).to.be.false;
expect(await OFTAdapter.getEnabled()).to.be.false;

await OFTAdapter.connect(oftAdapterAdmin).enable(true);
expect(await OFTAdapter.enabled()).to.be.true;
expect(await OFTAdapter.getEnabled()).to.be.true;
});

it('should emit StateChanged event for OFTAdapter', async function () {
it('should emit Enabled event for OFTAdapter', async function () {
const {OFTAdapter, oftAdapterAdmin} = await loadFixture(setupOFTSand);
const tx = await OFTAdapter.connect(oftAdapterAdmin).enable(false);
await expect(tx).to.emit(OFTAdapter, 'StateChanged').withArgs(false);
await expect(tx).to.emit(OFTAdapter, 'Enabled').withArgs(false);
});
});

Expand Down Expand Up @@ -146,7 +146,7 @@ describe('OFT Contracts', function () {

// disable send() in OFTAdapter
await OFTAdapter.connect(oftAdapterAdmin).enable(false);
expect(await OFTAdapter.enabled()).to.be.false;
expect(await OFTAdapter.getEnabled()).to.be.false;

const decimalConversionRate = await OFTAdapter.decimalConversionRate();

Expand Down Expand Up @@ -339,7 +339,7 @@ describe('OFT Contracts', function () {

// disable send() in OFTSand
await OFTSand.connect(sandAdmin).enable(false);
expect(await OFTSand.enabled()).to.be.false;
expect(await OFTSand.getEnabled()).to.be.false;

const sendParam2 = [
eidOFTSand2,
Expand Down

1 comment on commit a59d6ba

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coverage for this commit

91.49%

Coverage Report
FileStmtsBranchesFuncsLinesUncovered Lines
packages/oft-sand/contracts
   OFTAdapterForSand.sol92.59%100%87.50%92.31%76
   OFTSand.sol94.44%100%91.67%94.44%124
packages/oft-sand/contracts/interfaces
   IERC20Extended.sol100%100%100%100%
   IErrors.sol100%100%100%100%
packages/oft-sand/contracts/mock
   EndpointMock.sol100%100%100%100%
   MockTrustedForwarder.sol100%100%100%100%
   SandMock.sol100%100%100%100%
packages/oft-sand/contracts/oft/libraries
   OFTMsgCodec.sol70.59%50%71.43%75%27, 64, 73
packages/oft-sand/contracts/sand
   ERC20Internal.sol100%100%100%100%
   SandBaseToken.sol100%100%100%100%

Please sign in to comment.