Skip to content

Commit

Permalink
protocol project instead of jbx
Browse files Browse the repository at this point in the history
  • Loading branch information
mejango committed Jun 23, 2023
1 parent 084a560 commit 7c3452b
Show file tree
Hide file tree
Showing 22 changed files with 24,762 additions and 24,445 deletions.
54 changes: 27 additions & 27 deletions contracts/DefifaDelegate.sol
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ contract DefifaDelegate is JB721Delegate, Ownable, IDefifaDelegate {
/// _tierId The ID of the tier being checked.
mapping(uint256 => Checkpoints.History) internal _totalTierCheckpoints;

/// @notice The amount of $DEFIFA and $JBX tokens this game was allocated from paying the network fee, packed into a uint256.
/// @notice The amount of $DEFIFA and $BASE_PROTOCOL tokens this game was allocated from paying the network fee, packed into a uint256.
uint256 internal _packedTokenAllocation;

/// @notice The first owner of each token ID, stored on first transfer out.
Expand All @@ -109,8 +109,8 @@ contract DefifaDelegate is JB721Delegate, Ownable, IDefifaDelegate {
/// @notice The $DEFIFA token that is expected to be issued from paying fees.
IERC20 public immutable override defifaToken;

/// @notice The $JBX token that is expected to be issued from paying fees.
IERC20 public immutable override jbxToken;
/// @notice The $BASE_PROTOCOL token that is expected to be issued from paying fees.
IERC20 public immutable override baseProtocolToken;

//*********************************************************************//
// --------------------- public stored properties -------------------- //
Expand Down Expand Up @@ -305,23 +305,23 @@ contract DefifaDelegate is JB721Delegate, Ownable, IDefifaDelegate {
return TOTAL_REDEMPTION_WEIGHT;
}

/// @notice The amount of $DEFIFA and $JBX tokens this game was allocated from paying the network fee.
/// @notice The amount of $DEFIFA and $BASE_PROTOCOL tokens this game was allocated from paying the network fee.
/// @return defifaTokenAllocation The $DEFIFA token allocation.
/// @return jbxTokenAllocation The $JBX token allocation.
function tokenAllocations() public view returns (uint256 defifaTokenAllocation, uint256 jbxTokenAllocation) {
/// @return baseProtocolTokenAllocation The $BASE_PROTOCOL token allocation.
function tokenAllocations() public view returns (uint256 defifaTokenAllocation, uint256 baseProtocolTokenAllocation) {
// Get a reference to the pakced token allocation.
uint256 _packed = _packedTokenAllocation;

// defifa token allocation in bits 0-127 (128 bits).
uint256 _defifaTokenAllocation = uint256(uint128(_packed));

// jbx token allocation in bits 128-255 (128 bits).
uint256 _jbxTokenAllocation = uint256(uint128(_packed >> 128));
// base protocol token allocation in bits 128-255 (128 bits).
uint256 _baseProtocolTokenAllocation = uint256(uint128(_packed >> 128));

// Return the values.
defifaTokenAllocation =
(_defifaTokenAllocation != 0) ? _defifaTokenAllocation : defifaToken.balanceOf(address(this));
jbxTokenAllocation = (_jbxTokenAllocation != 0) ? _jbxTokenAllocation : jbxToken.balanceOf(address(this));
baseProtocolTokenAllocation = (_baseProtocolTokenAllocation != 0) ? _baseProtocolTokenAllocation : baseProtocolToken.balanceOf(address(this));
}

/// @notice Part of IJBFundingCycleDataSource, this function gets called when a project's token holders redeem.
Expand Down Expand Up @@ -382,28 +382,28 @@ contract DefifaDelegate is JB721Delegate, Ownable, IDefifaDelegate {
);
}

/// @notice The amount of $DEFIFA and $JBX tokens claimable for a set of token IDs.
/// @notice The amount of $DEFIFA and $BASE_PROTOCOL tokens claimable for a set of token IDs.
/// @param _tokenIds The IDs of the tokens that justify a $DEFIFA claim.
/// @return defifaTokenAmount The amount of $DEFIFA that can be claimed.
/// @return jbxTokenAmount The amount of $JBX that can be claimed.
/// @return baseProtocolTokenAmount The amount of $BASE_PROTOCOL that can be claimed.
function tokensClaimableFor(uint256[] memory _tokenIds)
public
view
returns (uint256 defifaTokenAmount, uint256 jbxTokenAmount)
returns (uint256 defifaTokenAmount, uint256 baseProtocolTokenAmount)
{
// Set the amount of total $DEFIFA and $JBX token allocation if it hasn't been set yet.
(uint256 _defifaTokenAllocation, uint256 _jbxTokenAllocation) = tokenAllocations();
// Set the amount of total $DEFIFA and $BASE_PROTOCOL token allocation if it hasn't been set yet.
(uint256 _defifaTokenAllocation, uint256 _baseProtocolTokenAllocation) = tokenAllocations();

// If there's no $DEFIFA in this contract, return 0.
if (_defifaTokenAllocation == 0 && _jbxTokenAllocation == 0) return (0, 0);
if (_defifaTokenAllocation == 0 && _baseProtocolTokenAllocation == 0) return (0, 0);

// Get a reference to the game's current pot, including any fulfilled commitments.
(uint256 _pot,,) = gamePotReporter.currentGamePotOf(projectId, true);

// If there's no usable pot left, the rest of the $DEFIFA and $JBX is available.
// If there's no usable pot left, the rest of the $DEFIFA and $BASE_PROTOCOL is available.
if (_pot - gamePotReporter.fulfilledCommitmentsOf(projectId) == 0) {
defifaTokenAmount = defifaToken.balanceOf(address(this));
jbxTokenAmount = jbxToken.balanceOf(address(this));
baseProtocolTokenAmount = baseProtocolToken.balanceOf(address(this));
} else {
// Keep a reference to the number of tokens being used for claims.
uint256 _numberOfTokens = _tokenIds.length;
Expand All @@ -423,9 +423,9 @@ contract DefifaDelegate is JB721Delegate, Ownable, IDefifaDelegate {
}
}

// The amount of $DEFIFA and $JBX to send is the same proportion as the amount being redeemed to the total pot before any amount redeemed.
// The amount of $DEFIFA and $BASE_PROTOCOL to send is the same proportion as the amount being redeemed to the total pot before any amount redeemed.
defifaTokenAmount = PRBMath.mulDiv(_defifaTokenAllocation, _cumulativePrice, _pot + amountRedeemed);
jbxTokenAmount = PRBMath.mulDiv(_jbxTokenAllocation, _cumulativePrice, _pot + amountRedeemed);
baseProtocolTokenAmount = PRBMath.mulDiv(_baseProtocolTokenAllocation, _cumulativePrice, _pot + amountRedeemed);
}
}

Expand All @@ -441,11 +441,11 @@ contract DefifaDelegate is JB721Delegate, Ownable, IDefifaDelegate {
//*********************************************************************//

/// @notice The $DEFIFA token that is expected to be issued from paying fees.
/// @notice The $JBX token that is expected to be issued from paying fees.
constructor(IERC20 _defifaToken, IERC20 _jbxToken) {
/// @notice The $BASE_PROTOCOL token that is expected to be issued from paying fees.
constructor(IERC20 _defifaToken, IERC20 _baseProtocolToken) {
codeOrigin = address(this);
defifaToken = _defifaToken;
jbxToken = _jbxToken;
baseProtocolToken = _baseProtocolToken;
}

//*********************************************************************//
Expand Down Expand Up @@ -985,7 +985,7 @@ contract DefifaDelegate is JB721Delegate, Ownable, IDefifaDelegate {
}
}

/// @notice Claim $DEFIFA and $JBX tokens to an account for a certain redeemed amount.
/// @notice Claim $DEFIFA and $BASE_PROTOCOL tokens to an account for a certain redeemed amount.
/// @param _beneficiary The beneficiary of the $DEFIFA tokens.
/// @param _tokenIds The IDs of the tokens being redeemed that are justifying a $DEFIFA claim.
function _claimDefifaTokensFor(address _beneficiary, uint256[] memory _tokenIds) internal {
Expand All @@ -994,18 +994,18 @@ contract DefifaDelegate is JB721Delegate, Ownable, IDefifaDelegate {
uint256 _packed;
// defifa token allocation in bits 0-127 (128 bits).
_packed |= defifaToken.balanceOf(address(this));
// jbx token allocation in bits 128-255 (48 bits).
_packed |= uint256(uint128(jbxToken.balanceOf(address(this)))) << 128;
// base protocol token allocation in bits 128-255 (48 bits).
_packed |= uint256(uint128(baseProtocolToken.balanceOf(address(this)))) << 128;
// Store the packed values.
_packedTokenAllocation = _packed;
}

// Get a reference to the amounts to send.
(uint256 _defifaTokenAmount, uint256 _jbxTokenAmount) = tokensClaimableFor(_tokenIds);
(uint256 _defifaTokenAmount, uint256 _baseProtocolTokenAmount) = tokensClaimableFor(_tokenIds);

// Send the tokens.
defifaToken.transfer(_beneficiary, _defifaTokenAmount);
jbxToken.transfer(_beneficiary, _jbxTokenAmount);
baseProtocolToken.transfer(_beneficiary, _baseProtocolTokenAmount);
}

/// @notice User the hook to register the first owner if it's not yet registered.
Expand Down
33 changes: 19 additions & 14 deletions contracts/DefifaDeployer.sol
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,13 @@ contract DefifaDeployer is IDefifaDeployer, IDefifaGamePhaseReporter, IDefifaGam
/// @dev This could be any fixed number.
uint256 public immutable override splitGroup;

/// @notice The project ID relative to which splits are stored.
/// @notice The project ID that'll receive game fees, and relative to which splits are stored.
/// @dev The owner of this project ID must give this contract operator permissions over the SET_SPLITS operation.
uint256 public immutable override defifaProjectId;

/// @notice The project ID that'll receive protocol fees as commitments are fulfilled.
uint256 public immutable override baseProtocolProjectId;

/// @notice The original code for the Defifa delegate to base subsequent instances on.
address public immutable override delegateCodeOrigin;

Expand Down Expand Up @@ -219,20 +222,23 @@ contract DefifaDeployer is IDefifaDeployer, IDefifaGamePhaseReporter, IDefifaGam
/// @param _controller The controller to use to launch the game from.
/// @param _delegatesRegistry The contract storing references to the deployer of each delegate.
/// @param _defifaProjectId The ID of the project that should take the fee from the games.
/// @param _baseProtocolProjectId The ID of the protocol project that'll receive fees from fulfilling commitments.
constructor(
address _delegateCodeOrigin,
IJB721TokenUriResolver _tokenUriResolver,
IDefifaGovernor _governor,
IJBController3_1 _controller,
IJBDelegatesRegistry _delegatesRegistry,
uint256 _defifaProjectId
uint256 _defifaProjectId,
uint256 _baseProtocolProjectId
) {
delegateCodeOrigin = _delegateCodeOrigin;
tokenUriResolver = _tokenUriResolver;
governor = _governor;
controller = _controller;
delegatesRegistry = _delegatesRegistry;
defifaProjectId = _defifaProjectId;
baseProtocolProjectId = _baseProtocolProjectId;
splitGroup = uint256(uint160(address(this)));
}

Expand Down Expand Up @@ -535,11 +541,10 @@ contract DefifaDeployer is IDefifaDeployer, IDefifaGamePhaseReporter, IDefifaGam
// Trigger the allocator's `allocate` function.
try _split.allocator.allocate{value: _payableValue}(_data) {}
catch (bytes memory) {
_splitAmount = 0;

if (_token != JBTokens.ETH) {
IERC20(_token).safeDecreaseAllowance(address(_split.allocator), _splitAmount);
}
_splitAmount = 0;
}

// Otherwise, if a project is specified, make a payment to it.
Expand Down Expand Up @@ -659,23 +664,23 @@ contract DefifaDeployer is IDefifaDeployer, IDefifaGamePhaseReporter, IDefifaGam
_defifaToken.transferFrom(msg.sender, _metadata.dataSource, _defifaToken.balanceOf(address(this)));
}

// Get a reference to any unclaimed JBX.
uint256 _unclaimedJbx = controller.tokenStore().unclaimedBalanceOf(address(this), 1);
// Get a reference to any unclaimed base protocol tokens.
uint256 _unclaimedBaseProtocolTokens = controller.tokenStore().unclaimedBalanceOf(address(this), baseProtocolProjectId);

// Claim any $JBX that's unclaimed.
if (_unclaimedJbx != 0) {
controller.tokenStore().claimFor(address(this), 1, _unclaimedJbx);
if (_unclaimedBaseProtocolTokens != 0) {
controller.tokenStore().claimFor(address(this), baseProtocolProjectId, _unclaimedBaseProtocolTokens);
}

// Get a reference to the $JBX token.
IERC20 _jbxToken = IDefifaDelegate(_metadata.dataSource).jbxToken();
// Get a reference to the $BASE_PROTOCOL token.
IERC20 _baseProtocolToken = IDefifaDelegate(_metadata.dataSource).baseProtocolToken();

// Get the jbx balance.
uint256 _jbxBalance = _jbxToken.balanceOf(address(this));
// Get the $BASE_PROTOCOL token balance.
uint256 _baseProtocolBalance = _baseProtocolToken.balanceOf(address(this));

// Transfer the amount of $JBX tokens aquired to the delegate.
if (_jbxBalance != 0) {
_jbxToken.transferFrom(msg.sender, _metadata.dataSource, _jbxBalance);
if (_baseProtocolBalance != 0) {
_baseProtocolToken.transferFrom(msg.sender, _metadata.dataSource, _baseProtocolBalance);
}

// Set the amount of fulfillments for this game.
Expand Down
2 changes: 1 addition & 1 deletion contracts/interfaces/IDefifaDelegate.sol
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ interface IDefifaDelegate is IJB721Delegate {

function defifaToken() external view returns (IERC20);

function jbxToken() external view returns (IERC20);
function baseProtocolToken() external view returns (IERC20);

function name() external view returns (string memory);

Expand Down
2 changes: 2 additions & 0 deletions contracts/interfaces/IDefifaDeployer.sol
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ interface IDefifaDeployer {
function splitGroup() external view returns (uint256);

function defifaProjectId() external view returns (uint256);

function baseProtocolProjectId() external view returns (uint256);

function delegateCodeOrigin() external view returns (address);

Expand Down
20 changes: 11 additions & 9 deletions contracts/scripts/Deploy.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,18 @@ contract DeployMainnet is Script {
ITypeface _typeface = ITypeface(0xA77b7D93E79f1E6B4f77FaB29d9ef85733A3D44A);

uint256 _defifaProjectId = 369;
uint256 _jbxProjectId = 1;
uint256 _baseProtocolProjectId = 1;

IERC20 _defifaToken = IERC20(address(_controller.tokenStore().tokenOf(_defifaProjectId)));
IERC20 _jbxToken = IERC20(address(_controller.tokenStore().tokenOf(_jbxProjectId)));
IERC20 _baseProtocolToken = IERC20(address(_controller.tokenStore().tokenOf(_baseProtocolProjectId)));

uint256 _blockTime = 12;

function run() external {
vm.startBroadcast();

// Deploy the codeOrigin for the delegate.
DefifaDelegate _defifaDelegateCodeOrigin = new DefifaDelegate(_defifaToken, _jbxToken);
DefifaDelegate _defifaDelegateCodeOrigin = new DefifaDelegate(_defifaToken, _baseProtocolToken);

// Deploy the token uri resolver.
DefifaTokenUriResolver _defifaTokenUriResolver = new DefifaTokenUriResolver(_typeface);
Expand All @@ -50,7 +50,8 @@ contract DeployMainnet is Script {
_defifaGovernor,
_controller,
_delegateRegistry,
_defifaProjectId
_defifaProjectId,
_baseProtocolProjectId
);

new DefifaProjectOwner(IJBOperatable(address(_controller)).operatorStore(), _controller.projects(), _defifaDeployer);
Expand All @@ -66,19 +67,19 @@ contract DeployGoerli is Script {
IJBDelegatesRegistry _delegateRegistry = IJBDelegatesRegistry(0xCe3Ebe8A7339D1f7703bAF363d26cD2b15D23C23);
ITypeface _typeface = ITypeface(0x8Df17136B20DA6D1E23dB2DCdA8D20Aa4ebDcda7);

uint256 _defifaProjectId = 1;
uint256 _jbxProjectId = 1;
uint256 _defifaProjectId = 1068;
uint256 _baseProtocolProjectId = 1;

IERC20 _defifaToken = IERC20(address(_controller.tokenStore().tokenOf(_defifaProjectId)));
IERC20 _jbxToken = IERC20(address(_controller.tokenStore().tokenOf(_defifaProjectId)));
IERC20 _baseProtocolToken = IERC20(address(_controller.tokenStore().tokenOf(_baseProtocolProjectId)));

uint256 _blockTime = 12;

function run() external {
vm.startBroadcast();

// Deploy the codeOrigin for the delegate
DefifaDelegate _defifaDelegateCodeOrigin = new DefifaDelegate(_defifaToken, _jbxToken);
DefifaDelegate _defifaDelegateCodeOrigin = new DefifaDelegate(_defifaToken, _baseProtocolToken);

// Deploy the token uri resolver.
DefifaTokenUriResolver _defifaTokenUriResolver = new DefifaTokenUriResolver(_typeface);
Expand All @@ -93,7 +94,8 @@ contract DeployGoerli is Script {
_defifaGovernor,
_controller,
_delegateRegistry,
_defifaProjectId
_defifaProjectId,
_baseProtocolProjectId
);

_defifaGovernor.transferOwnership(address(_defifaDeployer));
Expand Down
7,336 changes: 3,668 additions & 3,668 deletions out/DefifaDelegate.sol/DefifaDelegate.json

Large diffs are not rendered by default.

13,353 changes: 6,757 additions & 6,596 deletions out/DefifaDeployer.sol/DefifaDeployer.json

Large diffs are not rendered by default.

Loading

0 comments on commit 7c3452b

Please sign in to comment.