Skip to content

Commit

Permalink
test: embark
Browse files Browse the repository at this point in the history
  • Loading branch information
pegahcarter committed Jul 26, 2024
1 parent 92f7a41 commit ca8aee9
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 7 deletions.
13 changes: 6 additions & 7 deletions src/test/BaseTest.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ contract BaseTest is FraxTest {
address public firstOfficer = address(0x222222);
address public crewmember = address(0x333333);
address public owner = address(0x4444444);
address public testerA = address(0x555555);
address public testerB = address(0x666666);
address public userA = address(0x555555);
address public userB = address(0x666666);

constructor() {
// Setup fraxtal / fraxtal testnet L1 addresses
Expand Down Expand Up @@ -135,18 +135,17 @@ contract BaseTest is FraxTest {
_targetChain: _chainIdTo
});

vm.startPrank(owner);

ferry.setCaptain(captain);
ferry.setFirstOfficer(firstOfficer);
ferry.setCrewmember(crewmember, true);
ferry.nominateNewOwner(owner);

vm.startPrank(owner);
_tokenFrom.addMinter(owner);
_tokenFrom.minter_mint(owner, 1e24);
_tokenFrom.minter_mint(testerA, 1e24);
_tokenFrom.minter_mint(testerB, 1e24);
_tokenFrom.minter_mint(userA, 1e24);
_tokenFrom.minter_mint(userB, 1e24);
_tokenFrom.minter_mint(address(ferry), 1e24);

vm.stopPrank();
}
}
77 changes: 77 additions & 0 deletions src/test/FerryTest.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import "./BaseTest.t.sol";

contract FerryTest is BaseTest {
uint256 bridgeAmount = 1000 * 1e18;
bytes32 hashZero = 0x0000000000000000000000000000000000000000000000000000000000000000;

function setUp() public virtual override {
super.setUp();

maxApproveFerries();
}

function maxApproveFerries() internal {
maxApproveFerries(userA);
maxApproveFerries(userB);
}

function maxApproveFerries(address who) internal {
vm.startPrank(who);
token0.approve(address(ferry0To1), type(uint256).max);
token1.approve(address(ferry1To0), type(uint256).max);
vm.stopPrank();
}

function test_embark() public {
uint256 userBalanceBefore = token0.balanceOf(userA);
uint256 ferryBalanceBefore = token0.balanceOf(address(ferry0To1));

vm.prank(userA);
ferry0To1.embark(bridgeAmount);

assertEq({
a: userBalanceBefore - token0.balanceOf(userA),
b: bridgeAmount,
err: "Bridge amount not transferred"
});
assertEq({
a: token0.balanceOf(address(ferry0To1)) - ferryBalanceBefore,
b: bridgeAmount,
err: "Ferry did not receive bridge amount"
});
assertEq({ a: ferry0To1.noTransactions(), b: 1, err: "Ferry should have one tx" });

uint256 fee = min(
max((bridgeAmount * ferry0To1.FEE_RATE()) / 10_000, ferry0To1.FEE_MIN()),
ferry0To1.FEE_MAX()
);
(address user, uint64 amount, uint32 timestamp) = ferry0To1.transactions(0);

assertEq({ a: user, b: userA, err: "userA != tx.user" });
assertEq({
a: uint256(amount) * ferry0To1.REDUCED_DECIMALS(),
b: bridgeAmount - fee,
err: "bridgeAmount != tx.amount"
});
assertEq({ a: uint256(timestamp), b: block.timestamp, err: "block.timestamp != tx.timestamp" });
}

function min(uint256 a, uint256 b) internal returns (uint256) {
if (a < b) {
return a;
} else {
return b;
}
}

function max(uint256 a, uint256 b) internal returns (uint256) {
if (a > b) {
return a;
} else {
return b;
}
}
}

0 comments on commit ca8aee9

Please sign in to comment.