Skip to main content

addToBalanceOf

Contract: JBETHERC20SplitsPayer

Interface: IJBSplitsPayer

Add to the balance of the specified project.

Definition​

function addToBalanceOf(
uint256 _projectId,
address _token,
uint256 _amount,
uint256 _decimals,
string calldata _memo,
bytes calldata _metadata
) public payable virtual override nonReentrant { ... }
  • Arguments:
    • _projectId is the ID of the project that is being paid after.
    • _token is the token being paid in.
    • _amount is the amount of tokens being paid, as a fixed point number. If the token is ETH, this is ignored and msg.value is used in its place.
    • _decimals is the number of decimals in the _amount fixed point number. If the token is ETH, this is ignored and 18 is used in its place, which corresponds to the amount of decimals expected in msg.value.
    • _memo is a memo to pass along to the emitted event.
    • _metadata is @param extra data to pass along to the terminal.
  • The function can be accessed externally by anyone, or internally from this contract or one that inherits it.
  • The function can be overriden by inheriting contracts.
  • The function overrides a function definition from the IJBSplitsPayer interface.
  • The function doesn't return anything.

Body​

  1. If the token isn't ETH, make sure ETH wasn't sent to the function, then transfer the amount of tokens from the message sender to this contract. If the token is ETH, override the specified amount and decimals values with with amount of ETH sent to the function, which is denoted as a fixed point number with 18 decimals.

    // ETH shouldn't be sent if the token isn't ETH.
    if (address(_token) != JBTokens.ETH) {
    if (msg.value > 0) revert NO_MSG_VALUE_ALLOWED();

    // Transfer tokens to this contract from the msg sender.
    IERC20(_token).transferFrom(msg.sender, address(this), _amount);
    } else {
    // If ETH is being paid, set the amount to the message value, and decimals to 18.
    _amount = msg.value;
    _decimals = 18;
    }

    Library references:

    External references:

  2. Send the funds to the splits and get a reference to the leftover amount.

    // Pay the splits and get a reference to the amount leftover.
    uint256 _leftoverAmount = _payToSplits(
    defaultSplitsProjectId,
    defaultSplitsDomain,
    defaultSplitsGroup,
    _token,
    _amount,
    _decimals,
    defaultBeneficiary != address(0) ? defaultBeneficiary : msg.sender
    );

    Internal references:

  3. If there's any leftover amount, add to balance of the specified project. If no project is specified, send the leftover funds to the beneficiary or the msg.sender.

    // Distribute any leftover amount.
    if (_leftoverAmount > 0) {
    // If there's a default project ID, try to add to its balance.
    if (_projectId != 0)
    // Add to the project's balance.
    _addToBalanceOf(_projectId, _token, _leftoverAmount, _decimals, _memo, _metadata);

    // Otherwise, send a payment to the beneficiary.
    else {
    // Transfer the ETH.
    if (_token == JBTokens.ETH)
    Address.sendValue(
    // If there's a default beneficiary, send the funds directly to the beneficiary. Otherwise send to the msg.sender.
    defaultBeneficiary != address(0) ? defaultBeneficiary : payable(msg.sender),
    _leftoverAmount
    );
    // Or, transfer the ERC20.
    else
    IERC20(_token).transfer(
    // If there's a default beneficiary, send the funds directly to the beneficiary. Otherwise send to the msg.sender.
    defaultBeneficiary != address(0) ? defaultBeneficiary : msg.sender,
    _leftoverAmount
    );
    }
    }

    Library references:

    Internal references:

    External references:

  4. Emit a AddToBalance event with the relevant parameters.

    emit AddToBalance(
    _projectId,
    defaultBeneficiary != address(0) ? defaultBeneficiary : msg.sender,
    _token,
    _amount,
    _decimals,
    _leftoverAmount,
    _memo,
    msg.sender
    );

    Event references: