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

Chore/add tests and optimizations #38

Merged
merged 6 commits into from
Feb 6, 2023
Merged
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
7 changes: 4 additions & 3 deletions src/Connector.sol
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ contract CentrifugeConnector {

Tranche storage tranche = tranches[poolId][trancheId];
tranche.latestPrice = price;
tranche.lastPriceUpdate = block.timestamp;
tranche.tokenName = tokenName;
tranche.tokenSymbol = tokenSymbol;

Expand All @@ -132,7 +133,7 @@ contract CentrifugeConnector {
uint128 price
) public onlyRouter {
Tranche storage tranche = tranches[poolId][trancheId];
require(tranche.latestPrice > 0, "CentrifugeConnector/invalid-pool-or-tranche");
require(tranche.lastPriceUpdate > 0, "CentrifugeConnector/invalid-pool-or-tranche");
NunoAlexandre marked this conversation as resolved.
Show resolved Hide resolved
tranche.latestPrice = price;
tranche.lastPriceUpdate = block.timestamp;
}
Expand All @@ -144,7 +145,7 @@ contract CentrifugeConnector {
uint64 validUntil
) public onlyRouter {
Tranche storage tranche = tranches[poolId][trancheId];
require(tranche.latestPrice > 0, "CentrifugeConnector/invalid-pool-or-tranche");
require(tranche.lastPriceUpdate > 0, "CentrifugeConnector/invalid-pool-or-tranche");
hieronx marked this conversation as resolved.
Show resolved Hide resolved
RestrictedTokenLike token = RestrictedTokenLike(tranche.token);
MemberlistLike memberlist = MemberlistLike(token.memberlist());
memberlist.updateMember(user, validUntil);
Expand All @@ -157,8 +158,8 @@ contract CentrifugeConnector {
uint256 amount
) public onlyRouter {
RestrictedTokenLike token = RestrictedTokenLike(tranches[poolId][trancheId].token);
require(address(token) != address(0), "CentrifugeConnector/unknown-token");
require(token.hasMember(user), "CentrifugeConnector/not-a-member");

token.mint(user, amount);
}

Expand Down
42 changes: 42 additions & 0 deletions test/Connector.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ import {ConnectorMessages} from "src/Messages.sol";
import "forge-std/Test.sol";
import "../src/Connector.sol";

interface ERC20Like {
function balanceOf(address) external view returns (uint);
}

contract ConnectorTest is Test {

CentrifugeConnector bridgedConnector;
Expand Down Expand Up @@ -176,6 +180,44 @@ contract ConnectorTest is Test {
bridgedConnector.updateTokenPrice(poolId, trancheId, price);
}

function testTransfer(uint64 poolId, string memory tokenName, string memory tokenSymbol, bytes16 trancheId, uint128 price, uint32 destinationDomain, address user, uint256 amount, uint64 validUntil) public {
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice 👌

vm.assume(validUntil > block.timestamp + 7 days);
// 0. Add Pool
homeConnector.addPool(poolId);

// 1. Add the tranche
homeConnector.addTranche(poolId, trancheId, tokenName, tokenSymbol, price);

// 2. Then deploy the tranche
bridgedConnector.deployTranche(poolId, trancheId);

// 3. Add member
homeConnector.updateMember(poolId, trancheId, user, validUntil);

// 4. Transfer some tokens
homeConnector.transfer(poolId, trancheId, user, amount);
(address token,,,,) = bridgedConnector.tranches(poolId, trancheId);
assertEq(ERC20Like(token).balanceOf(user), amount);
}

function testTransferWithoutMemberFails(uint64 poolId, string memory tokenName, string memory tokenSymbol, bytes16 trancheId, uint128 price, uint32 destinationDomain, address user, uint256 amount, uint64 validUntil) public {
vm.assume(validUntil > block.timestamp + 7 days);
// 0. Add Pool
homeConnector.addPool(poolId);

// 1. Add the tranche
homeConnector.addTranche(poolId, trancheId, tokenName, tokenSymbol, price);

// 2. Then deploy the tranche
bridgedConnector.deployTranche(poolId, trancheId);

// 3. Transfer some tokens and expect revert
vm.expectRevert(bytes("CentrifugeConnector/not-a-member"));
homeConnector.transfer(poolId, trancheId, user, amount);
(address token,,,,) = bridgedConnector.tranches(poolId, trancheId);
assertEq(ERC20Like(token).balanceOf(user), 0);
}

// helpers
function safeAdd(uint x, uint y) internal pure returns (uint z) {
require((z = x + y) >= x, "math-add-overflow");
Expand Down