changeFor
Contract: JBTokenStore
Interface: IJBTokenStore
- Step by step
- Code
- Errors
- Events
- Bug bounty
Swap the current project's token for another, and transfer ownership of the current token to another address if needed.
Only a project's current controller can change its token.
This contract must have access to all of the token's IJBToken
interface functions.
Can't change to a token that's currently being used by another project.
Definition
function changeFor(
uint256 _projectId,
IJBToken _token,
address _newOwner
) external override onlyController(_projectId) { ... }
- Arguments:
_projectId
is the ID of the project to which the changed token belongs._token
is the new token. Send an empty address to remove the project's current token without adding a new one, if claiming tokens isn't currency required by the project_newOwner
is an address to transfer the current token's ownership to. This is optional, but it cannot be done later.
- Through the
onlyController
modifier, the function can only be accessed by the controller of the_projectId
. - The function overrides a function definition from the
IJBTokenStore
interface. - The function doesn't return anything.
Body
-
Make sure claiming isn't required if removing the token.
// Can't remove the project's token if the project requires claiming tokens.
if (_token == IJBToken(address(0)) && requireClaimFor[_projectId])
revert CANT_REMOVE_TOKEN_IF_ITS_REQUIRED();Internal references:
-
Make sure the token being changed to isn't being used by another project.
// Can't change to a token already in use.
if (projectOf[_token] != 0) revert TOKEN_ALREADY_IN_USE();Internal references:
-
Make sure the token has 18 decimals.
// Can't change to a token that doesn't use 18 decimals.
if (_token != IJBToken(address(0)) && _token.decimals() != 18)
revert TOKENS_MUST_HAVE_18_DECIMALS();External references:
-
Get a reference to the project's current token.
// Get a reference to the current token for the project.
oldToken = tokenOf[_projectId];Internal references:
-
Store the provided token as the token of the project.
// Store the new token.
tokenOf[_projectId] = _token;Internal references:
-
Store the project the new token is being used for.
// Store the project for the new token.
if (_token != IJBToken(address(0)))
projectOf[_token] = _projectId;Internal references:
-
Reset the project for the project's old token.
// Reset the project for the old token.
projectOf[oldToken] = 0;Internal references:
-
If there's a current token and a new owner address was provided, transfer the ownership of the current token from this contract to the new owner.
// If there's a current token and a new owner was provided, transfer ownership of the old token to the new owner.
if (_newOwner != address(0) && oldToken != IJBToken(address(0)))
oldToken.transferOwnership(_projectId, _newOwner);External references:
-
Emit a
Change
event with the relevant parameters.emit Change(_projectId, _token, oldToken, _newOwner, msg.sender);
Event references:
/**
@notice
Swap the current project's token for another, and transfer ownership of the current token to another address if needed.
@dev
Only a project's current controller can change its token.
@dev
This contract must have access to all of the token's `IJBToken` interface functions.
@dev
Can't change to a token that's currently being used by another project.
@param _projectId The ID of the project to which the changed token belongs.
@param _token The new token. Send an empty address to remove the project's current token without adding a new one, if claiming tokens isn't currency required by the project.
@param _newOwner An address to transfer the current token's ownership to. This is optional, but it cannot be done later.
@return oldToken The token that was removed as the project's token.
*/
function changeFor(
uint256 _projectId,
IJBToken _token,
address _newOwner
) external override onlyController(_projectId) returns (IJBToken oldToken) {
// Can't remove the project's token if the project requires claiming tokens.
if (_token == IJBToken(address(0)) && requireClaimFor[_projectId])
revert CANT_REMOVE_TOKEN_IF_ITS_REQUIRED();
// Can't change to a token already in use.
if (projectOf[_token] != 0) revert TOKEN_ALREADY_IN_USE();
// Can't change to a token that doesn't use 18 decimals.
if (_token != IJBToken(address(0)) && _token.decimals() != 18)
revert TOKENS_MUST_HAVE_18_DECIMALS();
// Get a reference to the current token for the project.
oldToken = tokenOf[_projectId];
// Store the new token.
tokenOf[_projectId] = _token;
// Store the project for the new token.
if (_token != IJBToken(address(0)))
projectOf[_token] = _projectId;
// Reset the project for the old token.
projectOf[oldToken] = 0;
// If there's a current token and a new owner was provided, transfer ownership of the old token to the new owner.
if (_newOwner != address(0) && oldToken != IJBToken(address(0)))
oldToken.transferOwnership(_projectId, _newOwner);
emit Change(_projectId, _token, oldToken, _newOwner, msg.sender);
}
String | Description |
---|---|
CANT_REMOVE_TOKEN_IF_ITS_REQUIRED | Thrown if the token is being removed but claiming is required. |
TOKEN_ALREADY_IN_USE | Thrown if the token being attached is already being used by another project. |
TOKENS_MUST_HAVE_18_DECIMALS | Thrown if the token being attached doesn't use 18 decimals. |
Name | Data |
---|---|
Change |
Category | Description | Reward |
---|---|---|
Optimization | Help make this operation more efficient. | 0.5ETH |
Low severity | Identify a vulnerability in this operation that could lead to an inconvenience for a user of the protocol or for a protocol developer. | 1ETH |
High severity | Identify a vulnerability in this operation that could lead to data corruption or loss of funds. | 5+ETH |