Skip to content

Commit

Permalink
Add MathLib and SafeTransferLib tests (#229)
Browse files Browse the repository at this point in the history
* Add mathlib and safetransferlib tests

* Fix typo

* Reorganize

* Format
  • Loading branch information
hieronx authored Dec 29, 2023
1 parent ce87116 commit b06ee59
Show file tree
Hide file tree
Showing 15 changed files with 399 additions and 205 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ jobs:
- name: Filter directories
run: |
sudo apt update && sudo apt install -y lcov
lcov --remove lcov.info 'test/*' 'script/*' --output-file lcov.info --rc lcov_branch_coverage=1
lcov --remove lcov.info 'test/*' 'script/*' 'src/util/*' --output-file lcov.info --rc lcov_branch_coverage=1
# This step posts a detailed coverage report as a comment and deletes previous comments on
# each push. The below step is used to fail coverage if the specified coverage threshold is
Expand Down
112 changes: 60 additions & 52 deletions src/gateway/Gateway.sol
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// SPDX-License-Identifier: AGPL-3.0-only
pragma solidity 0.8.21;

import {Messages} from "./Messages.sol";
import {MessagesLib} from "./../util/MessagesLib.sol";
import {Auth} from "./../util/Auth.sol";

interface InvestmentManagerLike {
Expand Down Expand Up @@ -175,11 +175,11 @@ contract Gateway is Auth {
uint128 amount
) public onlyPoolManager pauseable {
outgoingRouter.send(
Messages.formatTransferTrancheTokens(
MessagesLib.formatTransferTrancheTokens(
poolId,
trancheId,
_addressToBytes32(sender),
Messages.formatDomain(Messages.Domain.Centrifuge),
MessagesLib.formatDomain(MessagesLib.Domain.Centrifuge),
destinationAddress,
amount
)
Expand All @@ -195,11 +195,11 @@ contract Gateway is Auth {
uint128 amount
) public onlyPoolManager pauseable {
outgoingRouter.send(
Messages.formatTransferTrancheTokens(
MessagesLib.formatTransferTrancheTokens(
poolId,
trancheId,
_addressToBytes32(sender),
Messages.formatDomain(Messages.Domain.EVM, destinationChainId),
MessagesLib.formatDomain(MessagesLib.Domain.EVM, destinationChainId),
destinationAddress,
amount
)
Expand All @@ -211,7 +211,7 @@ contract Gateway is Auth {
onlyPoolManager
pauseable
{
outgoingRouter.send(Messages.formatTransfer(token, _addressToBytes32(sender), receiver, amount));
outgoingRouter.send(MessagesLib.formatTransfer(token, _addressToBytes32(sender), receiver, amount));
}

function increaseInvestOrder(
Expand All @@ -222,7 +222,9 @@ contract Gateway is Auth {
uint128 currencyAmount
) public onlyInvestmentManager pauseable {
outgoingRouter.send(
Messages.formatIncreaseInvestOrder(poolId, trancheId, _addressToBytes32(investor), currency, currencyAmount)
MessagesLib.formatIncreaseInvestOrder(
poolId, trancheId, _addressToBytes32(investor), currency, currencyAmount
)
);
}

Expand All @@ -234,7 +236,9 @@ contract Gateway is Auth {
uint128 currencyAmount
) public onlyInvestmentManager pauseable {
outgoingRouter.send(
Messages.formatDecreaseInvestOrder(poolId, trancheId, _addressToBytes32(investor), currency, currencyAmount)
MessagesLib.formatDecreaseInvestOrder(
poolId, trancheId, _addressToBytes32(investor), currency, currencyAmount
)
);
}

Expand All @@ -246,7 +250,7 @@ contract Gateway is Auth {
uint128 trancheTokenAmount
) public onlyInvestmentManager pauseable {
outgoingRouter.send(
Messages.formatIncreaseRedeemOrder(
MessagesLib.formatIncreaseRedeemOrder(
poolId, trancheId, _addressToBytes32(investor), currency, trancheTokenAmount
)
);
Expand All @@ -260,7 +264,7 @@ contract Gateway is Auth {
uint128 trancheTokenAmount
) public onlyInvestmentManager pauseable {
outgoingRouter.send(
Messages.formatDecreaseRedeemOrder(
MessagesLib.formatDecreaseRedeemOrder(
poolId, trancheId, _addressToBytes32(investor), currency, trancheTokenAmount
)
);
Expand All @@ -271,93 +275,97 @@ contract Gateway is Auth {
onlyInvestmentManager
pauseable
{
outgoingRouter.send(Messages.formatCollectInvest(poolId, trancheId, _addressToBytes32(investor), currency));
outgoingRouter.send(MessagesLib.formatCollectInvest(poolId, trancheId, _addressToBytes32(investor), currency));
}

function collectRedeem(uint64 poolId, bytes16 trancheId, address investor, uint128 currency)
public
onlyInvestmentManager
pauseable
{
outgoingRouter.send(Messages.formatCollectRedeem(poolId, trancheId, _addressToBytes32(investor), currency));
outgoingRouter.send(MessagesLib.formatCollectRedeem(poolId, trancheId, _addressToBytes32(investor), currency));
}

function cancelInvestOrder(uint64 poolId, bytes16 trancheId, address investor, uint128 currency)
public
onlyInvestmentManager
pauseable
{
outgoingRouter.send(Messages.formatCancelInvestOrder(poolId, trancheId, _addressToBytes32(investor), currency));
outgoingRouter.send(
MessagesLib.formatCancelInvestOrder(poolId, trancheId, _addressToBytes32(investor), currency)
);
}

function cancelRedeemOrder(uint64 poolId, bytes16 trancheId, address investor, uint128 currency)
public
onlyInvestmentManager
pauseable
{
outgoingRouter.send(Messages.formatCancelRedeemOrder(poolId, trancheId, _addressToBytes32(investor), currency));
outgoingRouter.send(
MessagesLib.formatCancelRedeemOrder(poolId, trancheId, _addressToBytes32(investor), currency)
);
}

// --- Incoming ---
function handle(bytes calldata message) external onlyIncomingRouter pauseable {
if (Messages.isAddCurrency(message)) {
(uint128 currency, address currencyAddress) = Messages.parseAddCurrency(message);
if (MessagesLib.isAddCurrency(message)) {
(uint128 currency, address currencyAddress) = MessagesLib.parseAddCurrency(message);
poolManager.addCurrency(currency, currencyAddress);
} else if (Messages.isAddPool(message)) {
(uint64 poolId) = Messages.parseAddPool(message);
} else if (MessagesLib.isAddPool(message)) {
(uint64 poolId) = MessagesLib.parseAddPool(message);
poolManager.addPool(poolId);
} else if (Messages.isAllowInvestmentCurrency(message)) {
(uint64 poolId, uint128 currency) = Messages.parseAllowInvestmentCurrency(message);
} else if (MessagesLib.isAllowInvestmentCurrency(message)) {
(uint64 poolId, uint128 currency) = MessagesLib.parseAllowInvestmentCurrency(message);
poolManager.allowInvestmentCurrency(poolId, currency);
} else if (Messages.isAddTranche(message)) {
} else if (MessagesLib.isAddTranche(message)) {
(
uint64 poolId,
bytes16 trancheId,
string memory tokenName,
string memory tokenSymbol,
uint8 decimals,
uint8 restrictionSet
) = Messages.parseAddTranche(message);
) = MessagesLib.parseAddTranche(message);
poolManager.addTranche(poolId, trancheId, tokenName, tokenSymbol, decimals, restrictionSet);
} else if (Messages.isUpdateMember(message)) {
(uint64 poolId, bytes16 trancheId, address user, uint64 validUntil) = Messages.parseUpdateMember(message);
} else if (MessagesLib.isUpdateMember(message)) {
(uint64 poolId, bytes16 trancheId, address user, uint64 validUntil) = MessagesLib.parseUpdateMember(message);
poolManager.updateMember(poolId, trancheId, user, validUntil);
} else if (Messages.isUpdateTrancheTokenPrice(message)) {
} else if (MessagesLib.isUpdateTrancheTokenPrice(message)) {
(uint64 poolId, bytes16 trancheId, uint128 currencyId, uint128 price, uint64 computedAt) =
Messages.parseUpdateTrancheTokenPrice(message);
MessagesLib.parseUpdateTrancheTokenPrice(message);
poolManager.updateTrancheTokenPrice(poolId, trancheId, currencyId, price, computedAt);
} else if (Messages.isTransfer(message)) {
(uint128 currency, address recipient, uint128 amount) = Messages.parseIncomingTransfer(message);
} else if (MessagesLib.isTransfer(message)) {
(uint128 currency, address recipient, uint128 amount) = MessagesLib.parseIncomingTransfer(message);
poolManager.handleTransfer(currency, recipient, amount);
} else if (Messages.isTransferTrancheTokens(message)) {
} else if (MessagesLib.isTransferTrancheTokens(message)) {
(uint64 poolId, bytes16 trancheId, address destinationAddress, uint128 amount) =
Messages.parseTransferTrancheTokens20(message);
MessagesLib.parseTransferTrancheTokens20(message);
poolManager.handleTransferTrancheTokens(poolId, trancheId, destinationAddress, amount);
} else if (Messages.isExecutedDecreaseInvestOrder(message)) {
} else if (MessagesLib.isExecutedDecreaseInvestOrder(message)) {
(
uint64 poolId,
bytes16 trancheId,
address investor,
uint128 currency,
uint128 currencyPayout,
uint128 remainingInvestOrder
) = Messages.parseExecutedDecreaseInvestOrder(message);
) = MessagesLib.parseExecutedDecreaseInvestOrder(message);
investmentManager.handleExecutedDecreaseInvestOrder(
poolId, trancheId, investor, currency, currencyPayout, remainingInvestOrder
);
} else if (Messages.isExecutedDecreaseRedeemOrder(message)) {
} else if (MessagesLib.isExecutedDecreaseRedeemOrder(message)) {
(
uint64 poolId,
bytes16 trancheId,
address investor,
uint128 currency,
uint128 trancheTokensPayout,
uint128 remainingRedeemOrder
) = Messages.parseExecutedDecreaseRedeemOrder(message);
) = MessagesLib.parseExecutedDecreaseRedeemOrder(message);
investmentManager.handleExecutedDecreaseRedeemOrder(
poolId, trancheId, investor, currency, trancheTokensPayout, remainingRedeemOrder
);
} else if (Messages.isExecutedCollectInvest(message)) {
} else if (MessagesLib.isExecutedCollectInvest(message)) {
(
uint64 poolId,
bytes16 trancheId,
Expand All @@ -366,11 +374,11 @@ contract Gateway is Auth {
uint128 currencyPayout,
uint128 trancheTokensPayout,
uint128 remainingInvestOrder
) = Messages.parseExecutedCollectInvest(message);
) = MessagesLib.parseExecutedCollectInvest(message);
investmentManager.handleExecutedCollectInvest(
poolId, trancheId, investor, currency, currencyPayout, trancheTokensPayout, remainingInvestOrder
);
} else if (Messages.isExecutedCollectRedeem(message)) {
} else if (MessagesLib.isExecutedCollectRedeem(message)) {
(
uint64 poolId,
bytes16 trancheId,
Expand All @@ -379,34 +387,34 @@ contract Gateway is Auth {
uint128 currencyPayout,
uint128 trancheTokensPayout,
uint128 remainingRedeemOrder
) = Messages.parseExecutedCollectRedeem(message);
) = MessagesLib.parseExecutedCollectRedeem(message);
investmentManager.handleExecutedCollectRedeem(
poolId, trancheId, investor, currency, currencyPayout, trancheTokensPayout, remainingRedeemOrder
);
} else if (Messages.isScheduleUpgrade(message)) {
address target = Messages.parseScheduleUpgrade(message);
} else if (MessagesLib.isScheduleUpgrade(message)) {
address target = MessagesLib.parseScheduleUpgrade(message);
root.scheduleRely(target);
} else if (Messages.isCancelUpgrade(message)) {
address target = Messages.parseCancelUpgrade(message);
} else if (MessagesLib.isCancelUpgrade(message)) {
address target = MessagesLib.parseCancelUpgrade(message);
root.cancelRely(target);
} else if (Messages.isUpdateTrancheTokenMetadata(message)) {
} else if (MessagesLib.isUpdateTrancheTokenMetadata(message)) {
(uint64 poolId, bytes16 trancheId, string memory tokenName, string memory tokenSymbol) =
Messages.parseUpdateTrancheTokenMetadata(message);
MessagesLib.parseUpdateTrancheTokenMetadata(message);
poolManager.updateTrancheTokenMetadata(poolId, trancheId, tokenName, tokenSymbol);
} else if (Messages.isTriggerIncreaseRedeemOrder(message)) {
} else if (MessagesLib.isTriggerIncreaseRedeemOrder(message)) {
(uint64 poolId, bytes16 trancheId, address investor, uint128 currency, uint128 trancheTokenAmount) =
Messages.parseTriggerIncreaseRedeemOrder(message);
MessagesLib.parseTriggerIncreaseRedeemOrder(message);
investmentManager.handleTriggerIncreaseRedeemOrder(
poolId, trancheId, investor, currency, trancheTokenAmount
);
} else if (Messages.isFreeze(message)) {
(uint64 poolId, bytes16 trancheId, address user) = Messages.parseFreeze(message);
} else if (MessagesLib.isFreeze(message)) {
(uint64 poolId, bytes16 trancheId, address user) = MessagesLib.parseFreeze(message);
poolManager.freeze(poolId, trancheId, user);
} else if (Messages.isUnfreeze(message)) {
(uint64 poolId, bytes16 trancheId, address user) = Messages.parseUnfreeze(message);
} else if (MessagesLib.isUnfreeze(message)) {
(uint64 poolId, bytes16 trancheId, address user) = MessagesLib.parseUnfreeze(message);
poolManager.unfreeze(poolId, trancheId, user);
} else if (Messages.isDisallowInvestmentCurrency(message)) {
(uint64 poolId, uint128 currency) = Messages.parseDisallowInvestmentCurrency(message);
} else if (MessagesLib.isDisallowInvestmentCurrency(message)) {
(uint64 poolId, uint128 currency) = MessagesLib.parseDisallowInvestmentCurrency(message);
poolManager.disallowInvestmentCurrency(poolId, currency);
} else {
revert("Gateway/invalid-message");
Expand Down
18 changes: 9 additions & 9 deletions src/gateway/routers/xcm/Router.sol
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// SPDX-License-Identifier: AGPL-3.0-only
pragma solidity 0.8.21;

import {Messages} from "../../Messages.sol";
import {MessagesLib} from "../../../util/MessagesLib.sol";
import {Auth} from "./../../../util/Auth.sol";

struct Multilocation {
Expand Down Expand Up @@ -154,22 +154,22 @@ contract XCMRouter is Auth {
// Obtain the Scale-encoded length of a given message. Each Liquidity Pools Message is fixed-sized and
// have thus a fixed scale-encoded length associated to which message variant (aka Call).
function messageLengthScaleEncoded(bytes memory _msg) internal pure returns (bytes memory) {
if (Messages.isTransfer(_msg)) {
if (MessagesLib.isTransfer(_msg)) {
return hex"8501";
} else if (Messages.isTransferTrancheTokens(_msg)) {
} else if (MessagesLib.isTransferTrancheTokens(_msg)) {
// A TransferTrancheTokens message is 82 bytes long which encodes to 0x4901 in Scale
return hex"4901";
} else if (Messages.isIncreaseInvestOrder(_msg)) {
} else if (MessagesLib.isIncreaseInvestOrder(_msg)) {
return hex"6501";
} else if (Messages.isDecreaseInvestOrder(_msg)) {
} else if (MessagesLib.isDecreaseInvestOrder(_msg)) {
return hex"6501";
} else if (Messages.isIncreaseRedeemOrder(_msg)) {
} else if (MessagesLib.isIncreaseRedeemOrder(_msg)) {
return hex"6501";
} else if (Messages.isDecreaseRedeemOrder(_msg)) {
} else if (MessagesLib.isDecreaseRedeemOrder(_msg)) {
return hex"6501";
} else if (Messages.isCollectInvest(_msg)) {
} else if (MessagesLib.isCollectInvest(_msg)) {
return hex"e4";
} else if (Messages.isCollectRedeem(_msg)) {
} else if (MessagesLib.isCollectRedeem(_msg)) {
return hex"e4";
} else {
revert("XCMRouter/unsupported-outgoing-message");
Expand Down
4 changes: 2 additions & 2 deletions src/gateway/Messages.sol → src/util/MessagesLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ pragma solidity 0.8.21;

import {BytesLib} from "src/util/BytesLib.sol";

/// @title Messages
/// @title MessagesLib
/// @dev Library for encoding and decoding messages.
library Messages {
library MessagesLib {
enum Call {
/// 0 - An invalid message
Invalid,
Expand Down
2 changes: 1 addition & 1 deletion src/util/SafeTransferLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ library SafeTransferLib {
}

/// @notice Approves the stipulated contract to spend the given allowance in the given token
/// @dev Errors if transfer fails
/// @dev Errors if approval fails
/// @param token The contract address of the token to be approved
/// @param to The target of the approval
/// @param value The amount of the given token the target will be allowed to spend
Expand Down
2 changes: 1 addition & 1 deletion test/TestSetup.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {TrancheToken, TrancheTokenLike} from "../src/token/Tranche.sol";
import {ERC20} from "../src/token/ERC20.sol";
import {Gateway} from "../src/gateway/Gateway.sol";
import {RestrictionManagerLike, RestrictionManager} from "../src/token/RestrictionManager.sol";
import {Messages} from "../src/gateway/Messages.sol";
import {MessagesLib} from "../src/util/MessagesLib.sol";
import {Deployer} from "../script/Deployer.sol";
import "../src/interfaces/IERC20.sol";

Expand Down
2 changes: 1 addition & 1 deletion test/integration/Deposit.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -780,7 +780,7 @@ contract DepositTest is TestSetup {

// check message was send out to centchain
lPool.cancelDepositRequest();
bytes memory cancelOrderMessage = Messages.formatCancelInvestOrder(
bytes memory cancelOrderMessage = MessagesLib.formatCancelInvestOrder(
lPool.poolId(), lPool.trancheId(), _addressToBytes32(self), defaultCurrencyId
);
assertEq(cancelOrderMessage, router.values_bytes("send"));
Expand Down
2 changes: 1 addition & 1 deletion test/integration/Redeem.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ contract RedeemTest is TestSetup {

// check message was send out to centchain
lPool.cancelRedeemRequest();
bytes memory cancelOrderMessage = Messages.formatCancelRedeemOrder(
bytes memory cancelOrderMessage = MessagesLib.formatCancelRedeemOrder(
lPool.poolId(), lPool.trancheId(), _addressToBytes32(self), defaultCurrencyId
);
assertEq(cancelOrderMessage, router.values_bytes("send"));
Expand Down
Loading

0 comments on commit b06ee59

Please sign in to comment.