Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Magnetar BTT tests #376

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions foundry.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ remappings = [
"yieldbox/=gitmodule/tap-yieldbox/contracts/",
"tapioca-lbp/=gitmodule/tapioca-lbp/contracts/",
"permitc/=gitmodule/permitc/src/",
"test/=test/",
]


Expand Down
5 changes: 3 additions & 2 deletions test/LZSetup/TestHelper.sol
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,16 @@ import {EndpointV2} from "@layerzerolabs/lz-evm-protocol-v2/contracts/EndpointV2
import {ExecutorOptions} from "@layerzerolabs/lz-evm-protocol-v2/contracts/messagelib/libs/ExecutorOptions.sol";
import {PacketV1Codec} from "@layerzerolabs/lz-evm-protocol-v2/contracts/messagelib/libs/PacketV1Codec.sol";
import {Origin} from "@layerzerolabs/lz-evm-protocol-v2/contracts/interfaces/ILayerZeroEndpointV2.sol";

import {OApp} from "@layerzerolabs/lz-evm-oapp-v2/contracts/oapp/OApp.sol";
import {OptionsBuilder} from "@layerzerolabs/lz-evm-oapp-v2/contracts/oapp/libs/OptionsBuilder.sol";

import {OptionsHelper} from "./mocks/OptionsHelper.sol";
import {SendUln302Mock as SendUln302} from "./mocks/SendUln302Mock.sol";
import {SimpleMessageLibMock} from "./mocks/SimpleMessageLibMock.sol";
import "./mocks/ExecutorFeeLibMock.sol";

// solhint-disable-next-line
import "forge-std/console.sol";

contract TestHelper is Test, OptionsHelper {
using OptionsBuilder for bytes;

Expand Down
52 changes: 28 additions & 24 deletions test/LZSetup/mocks/OFTAdapterMock.sol
Original file line number Diff line number Diff line change
@@ -1,32 +1,36 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.0;

import {OFTAdapter} from "@layerzerolabs/lz-evm-oapp-v2/contracts/oft/OFTAdapter.sol";
// import {OFTAdapter} from "@layerzerolabs/lz-evm-oapp-v2/contracts/oft/OFTAdapter.sol";

contract OFTAdapterMock is OFTAdapter {
constructor(address _token, address _lzEndpoint, address _owner) OFTAdapter(_token, _lzEndpoint, _owner) {}
// contract OFTAdapterMock is OFTAdapter {
// constructor(address _token, address _lzEndpoint, address _owner) OFTAdapter(_token, _lzEndpoint, _owner) {}

// @dev expose internal functions for testing purposes
function debit(uint256 _amountToSendLD, uint256 _minAmountToCreditLD, uint32 _dstEid)
public
returns (uint256 amountDebitedLD, uint256 amountToCreditLD)
{
return _debit(msg.sender, _amountToSendLD, _minAmountToCreditLD, _dstEid);
}
// // @dev expose internal functions for testing purposes
// function debit(uint256 _amountToSendLD, uint256 _minAmountToCreditLD, uint32 _dstEid)
// public
// returns (uint256 amountDebitedLD, uint256 amountToCreditLD)
// {
// return _debit(_amountToSendLD, _minAmountToCreditLD, _dstEid);
// }

function debitView(uint256 _amountToSendLD, uint256 _minAmountToCreditLD, uint32 _dstEid)
public
view
returns (uint256 amountDebitedLD, uint256 amountToCreditLD)
{
return _debitView(_amountToSendLD, _minAmountToCreditLD, _dstEid);
}
// function debitView(uint256 _amountToSendLD, uint256 _minAmountToCreditLD, uint32 _dstEid)
// public
// view
// returns (uint256 amountDebitedLD, uint256 amountToCreditLD)
// {
// return _debitView(_amountToSendLD, _minAmountToCreditLD, _dstEid);
// }

function credit(address _to, uint256 _amountToCreditLD, uint32 _srcEid) public returns (uint256 amountReceivedLD) {
return _credit(_to, _amountToCreditLD, _srcEid);
}
// function credit(address _to, uint256 _amountToCreditLD, uint32 _srcEid) public returns (uint256 amountReceivedLD) {
// return _credit(_to, _amountToCreditLD, _srcEid);
// }

function removeDust(uint256 _amountLD) public view returns (uint256 amountLD) {
return _removeDust(_amountLD);
}
}
// function increaseOutboundAmount(uint256 _amount) public {
// outboundAmount += _amount;
// }

// function removeDust(uint256 _amountLD) public view returns (uint256 amountLD) {
// return _removeDust(_amountLD);
// }
// }
11 changes: 6 additions & 5 deletions test/LZSetup/mocks/OFTMock.sol
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,12 @@ contract OFTMock is OFT {
return _credit(_to, _amountToCreditLD, _srcEid);
}

function buildMsgAndOptions(SendParam calldata _sendParam, uint256 _amountToCreditLD)
public
view
returns (bytes memory message, bytes memory options)
{
function buildMsgAndOptions(
SendParam calldata _sendParam,
bytes calldata _extraOptions,
bytes calldata _composeMsg,
uint256 _amountToCreditLD
) public view returns (bytes memory message, bytes memory options) {
return _buildMsgAndOptions(_sendParam, _amountToCreditLD);
}
}
74 changes: 74 additions & 0 deletions test/btt/Magnetar/Magnetar_burst.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity 0.8.22;

import {
MagnetarAction,
MagnetarModule,
MagnetarCall,
IMagnetarModuleExtender
} from "tapioca-periph/interfaces/periph/IMagnetar.sol";
import {MagnetarBaseTest, Magnetar} from "test/btt/MagnetarBaseTest.sol";
import {MagnetarStorage} from "contracts/Magnetar/MagnetarStorage.sol";

contract Magnetar_burst is MagnetarBaseTest {
function test_RevertWhen_CurrentAddressIsNotWhitelisted() external {
// it should revert
MagnetarCall[] memory magnetarCall = new MagnetarCall[](1);
magnetarCall[0] = MagnetarCall({id: 0, target: address(0x1), value: 0, call: hex""});

vm.prank(adminAddr);
cluster.updateContract(0, address(magnetar), false);
vm.expectRevert(
abi.encodeWithSelector(MagnetarStorage.Magnetar_TargetNotWhitelisted.selector, address(magnetar))
);
magnetar.burst(magnetarCall);
}

function test_RevertWhen_Paused() external {
// it should revert
vm.prank(adminAddr);
magnetar.setPause(true);

MagnetarCall[] memory magnetarCall = new MagnetarCall[](1);
magnetarCall[0] = MagnetarCall({id: 0, target: address(0x1), value: 0, call: hex""});

vm.expectRevert("Pausable: paused");
magnetar.burst(magnetarCall);
}

modifier whenCallingUsingMagnetarExtender() {
vm.prank(adminAddr);
magnetar.setMagnetarModuleExtender(IMagnetarModuleExtender(address(magnetarExtender)));
_;
}

function test_RevertWhen_SuccessReturnsFalse() external whenCallingUsingMagnetarExtender {
// it should revert
MagnetarCall[] memory magnetarCall = new MagnetarCall[](1);
magnetarCall[0] = MagnetarCall({id: 100, target: address(0x1), value: 0, call: hex""});

vm.expectRevert("Invalid action id");
magnetar.burst(magnetarCall);
}

function test_RevertWhen_ActionNotValid() external {
// it should revert
MagnetarCall[] memory magnetarCall = new MagnetarCall[](1);
magnetarCall[0] = MagnetarCall({id: 50, target: address(0x1), value: 0, call: hex""});

vm.expectRevert(abi.encodeWithSelector(Magnetar.Magnetar_ActionNotValid.selector, 50, hex""));
magnetar.burst(magnetarCall);
}

function test_RevertWhen_MsgValueNotMatchingAccumulator() external {
// it should revert
vm.prank(adminAddr);
magnetar.setMagnetarModuleExtender(IMagnetarModuleExtender(address(magnetarExtender)));

MagnetarCall[] memory magnetarCall = new MagnetarCall[](1);
magnetarCall[0] = MagnetarCall({id: 200, target: address(0x1), value: 0, call: hex""});

vm.expectRevert(abi.encodeWithSelector(Magnetar.Magnetar_ValueMismatch.selector, 1e18, 0));
magnetar.burst{value: 1e18}(magnetarCall);
}
}
12 changes: 12 additions & 0 deletions test/btt/Magnetar/Magnetar_burst.tree
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
Magnetar_burst
├── when current address is not whitelisted
│ └── it should revert
├── when paused
│ └── it should revert
├── when calling using MagnetarExtender
│ └── when success returns false
│ └── it should revert
├── when action not valid
│ └── it should revert
└── when msg value not matching accumulator
└── it should revert
81 changes: 81 additions & 0 deletions test/btt/Magnetar/Magnetar_permitOperation.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity 0.8.22;

import {ERC20PermitApprovalMsg, ERC20PermitStruct} from "tapioca-periph/interfaces/periph/ITapiocaOmnichainEngine.sol";
import {MagnetarAction, MagnetarCall} from "tapioca-periph/interfaces/periph/IMagnetar.sol";
import {MagnetarBaseTest, Magnetar} from "test/btt/MagnetarBaseTest.sol";
import {MagnetarStorage} from "contracts/Magnetar/MagnetarStorage.sol";

contract Magnetar_permitOperation is MagnetarBaseTest {
function test_RevertWhen_TargetIsNotWhitelisted() external {
// it should revert
cluster.updateContract(0, address(aToeOFT), false);

MagnetarCall[] memory magnetarCall = new MagnetarCall[](1);
magnetarCall[0] = MagnetarCall({id: 0, target: address(aToeOFT), value: 0, call: hex""});
vm.expectRevert(
abi.encodeWithSelector(MagnetarStorage.Magnetar_TargetNotWhitelisted.selector, address(aToeOFT))
);
magnetar.burst(magnetarCall);
}

function test_RevertWhen_SelectorIsNotValid() external {
// it should revert
MagnetarCall[] memory magnetarCall = new MagnetarCall[](1);
magnetarCall[0] = MagnetarCall({id: 0, target: address(aToeOFT), value: 0, call: hex""});
vm.expectRevert(abi.encodeWithSelector(Magnetar.Magnetar_ActionNotValid.selector, 0, hex""));
magnetar.burst(magnetarCall);
}

function test_WhenSelectorIsPermit() external {
// it should work
cluster.updateContract(0, address(this), true);
ERC20PermitStruct memory permit_ =
ERC20PermitStruct({owner: aliceAddr, spender: address(this), value: 1e18, nonce: 0, deadline: 1 days});

bytes32 digest_ = aToeOFT.getTypedDataHash(permit_);
ERC20PermitApprovalMsg memory permitApproval_ =
getERC20PermitData(permit_, digest_, address(aToeOFT), alicePKey);

aToeOFT.permit(
permit_.owner,
permit_.spender,
permit_.value,
permit_.deadline,
permitApproval_.v,
permitApproval_.r,
permitApproval_.s
);

MagnetarCall[] memory magnetarCall = new MagnetarCall[](1);
magnetarCall[0] = MagnetarCall({
id: 0,
target: address(aToeOFT),
value: 0,
call: abi.encodeWithSelector(
aToeOFT.permit.selector,
permit_.owner,
permit_.spender,
permit_.value,
permit_.deadline,
permitApproval_.v,
permitApproval_.r,
permitApproval_.s
)
});
magnetar.burst(magnetarCall);
assertEq(aToeOFT.allowance(aliceAddr, address(this)), 1e18);
}

function test_WhenSelectorIsRevoke() external {
// it should work
}

function test_WhenSelectorIsPermitAll() external {
// it should work
}

function test_WhenSelectorIsRevokeAll() external {
// it should work
}
}
13 changes: 13 additions & 0 deletions test/btt/Magnetar/Magnetar_permitOperation.tree
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Magnetar_permitOperation
├── when target is not whitelisted
│ └── it should revert
├── when selector is not valid
│ └── it should revert
├── when selector is permit
│ └── it should work
├── when selector is revoke
│ └── it should work
├── when selector is permitAll
│ └── it should work
└── when selector is revokeAll
└── it should work
Loading