Skip to content

Commit

Permalink
Merge pull request #232 from Bananapus/jango/jberc20
Browse files Browse the repository at this point in the history
jberc20 with a check fn
mejango authored Jan 19, 2025
2 parents a817c4f + c9f5e8f commit 13c85ed
Showing 6 changed files with 29 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/JBERC20.sol
Original file line number Diff line number Diff line change
@@ -43,6 +43,11 @@ contract JBERC20 is ERC20Votes, ERC20Permit, Ownable, IJBToken {
return super.balanceOf(account);
}

/// @notice This token can only be added to a project when its created by the `JBTokens` contract.
function canBeAddedTo(uint256) external pure override returns (bool) {
return false;
}

/// @notice The number of decimals used for this token's fixed point accounting.
/// @return The number of decimals.
function decimals() public view override(ERC20, IJBToken) returns (uint8) {
4 changes: 4 additions & 0 deletions src/JBTokens.sol
Original file line number Diff line number Diff line change
@@ -27,6 +27,7 @@ contract JBTokens is JBControlled, IJBTokens {
error JBTokens_OverflowAlert(uint256 value, uint256 limit);
error JBTokens_ProjectAlreadyHasToken(IJBToken token);
error JBTokens_TokenAlreadyBeingUsed(uint256 projectId);
error JBTokens_TokenCantBeAdded(uint256 projectId);
error JBTokens_TokenNotFound();
error JBTokens_TokensMustHave18Decimals(uint256 decimals);

@@ -335,6 +336,9 @@ contract JBTokens is JBControlled, IJBTokens {
// Can't change to a token that doesn't use 18 decimals.
if (token.decimals() != 18) revert JBTokens_TokensMustHave18Decimals(token.decimals());

// Make sure the token can be added to the project its being added to.
if (!token.canBeAddedTo(projectId)) revert JBTokens_TokenCantBeAdded(projectId);

// Store the new token.
tokenOf[projectId] = token;

1 change: 1 addition & 0 deletions src/interfaces/IJBToken.sol
Original file line number Diff line number Diff line change
@@ -3,6 +3,7 @@ pragma solidity ^0.8.0;

interface IJBToken {
function balanceOf(address account) external view returns (uint256);
function canBeAddedTo(uint256 projectId) external view returns (bool);
function decimals() external view returns (uint8);
function totalSupply() external view returns (uint256);

1 change: 1 addition & 0 deletions test/TestPermissions.sol
Original file line number Diff line number Diff line change
@@ -192,6 +192,7 @@ contract TestPermissions_Local is TestBaseWorkflow, JBTest {

// Will succeed when setting the correct projects token
mockExpect(token, abi.encodeCall(MockERC20.decimals, ()), abi.encode(18));
mockExpect(token, abi.encodeCall(IJBToken.canBeAddedTo, (1)), abi.encode(true));
_controller.setTokenFor(1, IJBToken(token));
}

5 changes: 5 additions & 0 deletions test/TestTokenFlow.sol
Original file line number Diff line number Diff line change
@@ -85,6 +85,11 @@ contract TestTokenFlow_Local is TestBaseWorkflow {
IJBToken _newToken = IJBToken(Clones.clone(address(new JBERC20())));
_newToken.initialize({name: "NewTestName", symbol: "NewTestSymbol", owner: address(_tokens)});

// Mock the token can be added to the project.
vm.mockCall(
address(_newToken), abi.encodeWithSelector(IJBToken.canBeAddedTo.selector, _projectId), abi.encode(true)
);

// Set the projects token to `_newToken`.
_controller.setTokenFor(_projectId, _newToken);

13 changes: 13 additions & 0 deletions test/units/static/JBTokens/TestSetTokenFor.sol
Original file line number Diff line number Diff line change
@@ -73,10 +73,23 @@ contract TestSetTokenFor_Local is JBTokensSetup {

//mock call to token decimals
mockExpect(address(_token), abi.encodeCall(IJBToken.decimals, ()), abi.encode(18));
mockExpect(address(_token), abi.encodeCall(IJBToken.canBeAddedTo, (_projectId)), abi.encode(true));

vm.expectEmit();
emit IJBTokens.SetToken(_projectId, _token, address(this));

_tokens.setTokenFor(_projectId, IJBToken(address(_token)));
}

function test_WhenCantBeAddedTo() external whenCallerIsControllerOfProject {
// it will set token states and emit SetToken

//mock call to token decimals
mockExpect(address(_token), abi.encodeCall(IJBToken.decimals, ()), abi.encode(18));
mockExpect(address(_token), abi.encodeCall(IJBToken.canBeAddedTo, (_projectId)), abi.encode(false));

vm.expectRevert(abi.encodeWithSelector(JBTokens.JBTokens_TokenCantBeAdded.selector, _projectId));

_tokens.setTokenFor(_projectId, IJBToken(address(_token)));
}
}

0 comments on commit 13c85ed

Please sign in to comment.