Skip to content

Commit

Permalink
Chore/add tests and optimizations (#38)
Browse files Browse the repository at this point in the history
* wip: add transfer test

* move memberlist check from connectors to restricted token

* fix tests

* revert restricted.sol changes

* revert unwanted changes to erc20 and memberlist

* fix test
  • Loading branch information
AStox committed Feb 6, 2023
1 parent aa41811 commit 78edd87
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 4 deletions.
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");
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");
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 {
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

0 comments on commit 78edd87

Please sign in to comment.