Skip to content

Commit

Permalink
feat: add back transferAmountFrom and remove SafeTransferLib
Browse files Browse the repository at this point in the history
  • Loading branch information
ilpepepig committed Aug 23, 2024
1 parent 32fbcb2 commit 606ea5c
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 65 deletions.
33 changes: 14 additions & 19 deletions contracts/core/Allo.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import "./interfaces/IAllo.sol";
import {Clone} from "./libraries/Clone.sol";
import {Errors} from "./libraries/Errors.sol";
import {Native} from "./libraries/Native.sol";
import {Transfer, SafeTransferLib} from "./libraries/Transfer.sol";
import {Transfer} from "./libraries/Transfer.sol";

// ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣾⣿⣷⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣼⣿⣿⣷⣄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢸⣿⣿⣿⣗⠀⠀⠀⢸⣿⣿⣿⡯⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
// ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣿⣿⣿⣿⣷⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣼⣿⣿⣿⣿⣿⡄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢸⣿⣿⣿⣗⠀⠀⠀⢸⣿⣿⣿⡯⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
Expand All @@ -36,7 +36,6 @@ import {Transfer, SafeTransferLib} from "./libraries/Transfer.sol";
/// @dev The contract must be initialized with the 'initialize()' function.
contract Allo is IAllo, Native, Initializable, Ownable, AccessControlUpgradeable, ReentrancyGuardUpgradeable, Errors {
using Transfer for address;
using SafeTransferLib for address;

// ==========================
// === Storage Variables ====
Expand Down Expand Up @@ -565,7 +564,7 @@ contract Allo is IAllo, Native, Initializable, Ownable, AccessControlUpgradeable
if (_token == NATIVE && (baseFee + _amount != _msgValue)) revert NOT_ENOUGH_FUNDS();
if (_token != NATIVE && baseFee != _msgValue) revert NOT_ENOUGH_FUNDS();

address(treasury).safeTransferETH(baseFee);
address(treasury).transferAmountNative(baseFee);

emit BaseFeePaid(poolId, baseFee);
}
Expand Down Expand Up @@ -611,26 +610,22 @@ contract Allo is IAllo, Native, Initializable, Ownable, AccessControlUpgradeable
Pool storage pool = pools[_poolId];
address _token = pool.token;

if (_token == NATIVE) {
if (msg.value < _amount) revert ETH_MISMATCH();
if (feeAmount > 0) address(treasury).safeTransferETH(feeAmount);
address(_strategy).safeTransferETH(amountAfterFee);
} else {
if (feeAmount > 0) {
uint256 balanceBeforeFee = _token.balanceOf(treasury);
_token.safeTransferFrom(_funder, treasury, feeAmount);
uint256 balanceAfterFee = _token.balanceOf(treasury);
// Track actual fee paid to account for fee on ERC20 token transfers
feeAmount = balanceAfterFee - balanceBeforeFee;
}
if (_token == NATIVE && msg.value < _amount) revert ETH_MISMATCH();

uint256 balanceBeforeFundingPool = _token.balanceOf(address(_strategy));
_token.safeTransferFrom(_funder, address(_strategy), amountAfterFee);
uint256 balanceAfterFundingPool = _token.balanceOf(address(_strategy));
if (feeAmount > 0) {
uint256 balanceBeforeFee = _token.getBalance(treasury);
_token.transferAmountFrom(_funder, treasury, feeAmount);
uint256 balanceAfterFee = _token.getBalance(treasury);
// Track actual fee paid to account for fee on ERC20 token transfers
amountAfterFee = balanceAfterFundingPool - balanceBeforeFundingPool;
feeAmount = balanceAfterFee - balanceBeforeFee;
}

uint256 balanceBeforeFundingPool = _token.getBalance(address(_strategy));
_token.transferAmountFrom(_funder, address(_strategy), amountAfterFee);
uint256 balanceAfterFundingPool = _token.getBalance(address(_strategy));
// Track actual fee paid to account for fee on ERC20 token transfers
amountAfterFee = balanceAfterFundingPool - balanceBeforeFundingPool;

_strategy.increasePoolAmount(amountAfterFee);

emit PoolFunded(_poolId, amountAfterFee, feeAmount);
Expand Down
22 changes: 22 additions & 0 deletions contracts/core/libraries/Transfer.sol
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,21 @@ library Transfer {
/// @notice Address of the native token
address public constant NATIVE = 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE;

/// @notice Transfer an amount of a token to an address
/// @dev When this function is used, it must be checked that balances or msg.value is correct for native tokens
/// @param _token The token to transfer
/// @param _from The address to transfer to
/// @param _to The address to transfer to
/// @param _amount The amount to transfer
function transferAmountFrom(address _token, address _from, address _to, uint256 _amount) internal {
if (_token == NATIVE) {
// '_from' is ignored. The contract's balance is used.
if (_to != address(this)) _to.safeTransferETH(_amount);
} else {
_token.safeTransferFrom(_from, _to, _amount);
}
}

/// @notice Transfer an amount of a token to an address
/// @param _token The token to transfer
/// @param _to The address to transfer to
Expand All @@ -40,6 +55,13 @@ library Transfer {
}
}

/// @notice Transfer an amount of native token to an address
/// @param _to The address to transfer to
/// @param _amount The amount to transfer
function transferAmountNative(address _to, uint256 _amount) internal {
_to.safeTransferETH(_amount);
}

/// @notice Get the balance of a token for an account
/// @param _token The token to get the balance of
/// @param _account The account to get the balance for
Expand Down
13 changes: 4 additions & 9 deletions contracts/strategies/DirectAllocation.sol
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.19;

// External Libraries
import {SafeTransferLib} from "solady/utils/SafeTransferLib.sol";
// Core Contracts
import {CoreBaseStrategy} from "contracts/strategies/CoreBaseStrategy.sol";
// Internal Libraries
import {Native} from "contracts/core/libraries/Native.sol";
import {Errors} from "contracts/core/libraries/Errors.sol";
import {Transfer} from "contracts/core/libraries/Transfer.sol";

/// @title DirectAllocationStrategy
/// @dev The strategy only implements the allocate logic
/// @notice A strategy that directly allocates funds to a recipient
contract DirectAllocationStrategy is CoreBaseStrategy, Native, Errors {
using SafeTransferLib for address;
using Transfer for address;

/// ===============================
/// ============ Errors ===========
Expand Down Expand Up @@ -77,12 +76,8 @@ contract DirectAllocationStrategy is CoreBaseStrategy, Native, Errors {
uint256 _totalNativeAmount;
for (uint256 _i = 0; _i < _recipientsLength;) {
/// Direct allocate the funds
if (_tokens[_i] == NATIVE) {
_totalNativeAmount += _amounts[_i];
_recipients[_i].safeTransferETH(_amounts[_i]);
} else {
_tokens[_i].safeTransferFrom(_sender, _recipients[_i], _amounts[_i]);
}
if (_tokens[_i] == NATIVE) _totalNativeAmount += _amounts[_i];
_tokens[_i].transferAmountFrom(_sender, _recipients[_i], _amounts[_i]);

emit DirectAllocated(_recipients[_i], _amounts[_i], _tokens[_i], _sender);
unchecked {
Expand Down
5 changes: 2 additions & 3 deletions contracts/strategies/DonationVotingOffchain.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {IAllo} from "../core/interfaces/IAllo.sol";
import {CoreBaseStrategy} from "./CoreBaseStrategy.sol";
import {RecipientsExtension} from "../extensions/contracts/RecipientsExtension.sol";
// Internal Libraries
import {Transfer, SafeTransferLib} from "contracts/core/libraries/Transfer.sol";
import {Transfer} from "contracts/core/libraries/Transfer.sol";
import {Native} from "contracts/core/libraries/Native.sol";

// ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣾⣿⣷⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣼⣿⣿⣷⣄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢸⣿⣿⣿⣗⠀⠀⠀⢸⣿⣿⣿⡯⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
Expand All @@ -30,7 +30,6 @@ import {Native} from "contracts/core/libraries/Native.sol";
/// by the pool manager.
contract DonationVotingOffchain is CoreBaseStrategy, RecipientsExtension, Native {
using Transfer for address;
using SafeTransferLib for address;

/// ===============================
/// ========== Events =============
Expand Down Expand Up @@ -280,7 +279,7 @@ contract DonationVotingOffchain is CoreBaseStrategy, RecipientsExtension, Native
if (tokens[i] == NATIVE) {
totalNativeAmount += _amounts[i];
} else {
tokens[i].safeTransferFrom(_sender, address(this), _amounts[i]);
tokens[i].transferAmountFrom(_sender, address(this), _amounts[i]);
}

emit Allocated(_recipients[i], _sender, _amounts[i], abi.encode(tokens[i]));
Expand Down
5 changes: 2 additions & 3 deletions contracts/strategies/DonationVotingOnchain.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {RecipientsExtension} from "../extensions/contracts/RecipientsExtension.s
// Internal Libraries
import {QFHelper} from "../core/libraries/QFHelper.sol";
import {Native} from "contracts/core/libraries/Native.sol";
import {Transfer, SafeTransferLib} from "contracts/core/libraries/Transfer.sol";
import {Transfer} from "contracts/core/libraries/Transfer.sol";

// ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣾⣿⣷⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣼⣿⣿⣷⣄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢸⣿⣿⣿⣗⠀⠀⠀⢸⣿⣿⣿⡯⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
// ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣿⣿⣿⣿⣷⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣼⣿⣿⣿⣿⣿⡄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢸⣿⣿⣿⣗⠀⠀⠀⢸⣿⣿⣿⡯⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
Expand All @@ -32,7 +32,6 @@ import {Transfer, SafeTransferLib} from "contracts/core/libraries/Transfer.sol";
contract DonationVotingOnchain is CoreBaseStrategy, RecipientsExtension, Native {
using QFHelper for QFHelper.State;
using Transfer for address;
using SafeTransferLib for address;

/// ===============================
/// ========== Events =============
Expand Down Expand Up @@ -189,7 +188,7 @@ contract DonationVotingOnchain is CoreBaseStrategy, RecipientsExtension, Native
if (allocationToken == NATIVE) {
if (msg.value != totalAmount) revert ETH_MISMATCH();
} else {
allocationToken.safeTransferFrom(_sender, address(this), totalAmount);
allocationToken.transferAmountFrom(_sender, address(this), totalAmount);
}

QFState.fund(_recipients, _amounts);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ pragma solidity 0.8.19;

// External Libraries
import {ReentrancyGuard} from "openzeppelin-contracts/contracts/security/ReentrancyGuard.sol";
import {SafeTransferLib} from "solady/utils/SafeTransferLib.sol";
// Interfaces
import {IAllo} from "../../../core/interfaces/IAllo.sol";
import {IRegistry} from "../../../core/interfaces/IRegistry.sol";
Expand Down Expand Up @@ -31,6 +30,7 @@ import {Transfer} from "../../../core/libraries/Transfer.sol";

contract DonationVotingStrategy is BaseStrategy, ReentrancyGuard, Native {
using Transfer for address;

/// ================================
/// ========== Struct ==============
/// ================================
Expand Down Expand Up @@ -479,20 +479,12 @@ contract DonationVotingStrategy is BaseStrategy, ReentrancyGuard, Native {
revert INVALID();
}

uint256 transferredAmount = amount;
if (token == NATIVE) {
if (msg.value < amount) {
revert ETH_MISMATCH();
}
SafeTransferLib.safeTransferETH(address(this), amount);
} else {
uint256 balanceBefore = SafeTransferLib.balanceOf(token, address(this));
if (token == NATIVE && msg.value < amount) revert ETH_MISMATCH();

token.transferAmount(address(this), amount);

uint256 balanceAfter = SafeTransferLib.balanceOf(token, address(this));
transferredAmount = balanceAfter - balanceBefore;
}
uint256 balanceBefore = token.getBalance(address(this));
token.transferAmount(address(this), amount);
uint256 balanceAfter = token.getBalance(address(this));
uint256 transferredAmount = balanceAfter - balanceBefore;

// Update the total payout amount for the claim and the total claimable amount
claims[recipientId][token] += transferredAmount;
Expand Down
13 changes: 4 additions & 9 deletions contracts/strategies/direct-grants-lite/DirectGrantsLite.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {BaseStrategy} from "../BaseStrategy.sol";
// Internal Libraries
import {Metadata} from "../../core/libraries/Metadata.sol";
import {Native} from "../../core/libraries/Native.sol";
import {Transfer, SafeTransferLib} from "../../core/libraries/Transfer.sol";
import {Transfer} from "../../core/libraries/Transfer.sol";

// ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣾⣿⣷⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣼⣿⣿⣷⣄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢸⣿⣿⣿⣗⠀⠀⠀⢸⣿⣿⣿⡯⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
// ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣿⣿⣿⣿⣷⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣼⣿⣿⣿⣿⣿⡄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢸⣿⣿⣿⣗⠀⠀⠀⢸⣿⣿⣿⡯⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
Expand All @@ -32,7 +32,6 @@ import {Transfer, SafeTransferLib} from "../../core/libraries/Transfer.sol";
/// @notice Strategy for direct grants
contract DirectGrantsLiteStrategy is Native, BaseStrategy, Multicall {
using Transfer for address;
using SafeTransferLib for address;

/// ================================
/// ========== Struct ==============
Expand Down Expand Up @@ -463,12 +462,8 @@ contract DirectGrantsLiteStrategy is Native, BaseStrategy, Multicall {
revert RECIPIENT_NOT_ACCEPTED();
}

if (token == NATIVE) {
nativeAmount -= amount;
recipientAddress.safeTransferETH(amount);
} else {
token.safeTransferFrom(_sender, recipientAddress, amount);
}
if (token == NATIVE) nativeAmount -= amount;
token.transferAmountFrom(_sender, recipientAddress, amount);

emit Allocated(recipientId, amount, token, _sender);

Expand All @@ -477,7 +472,7 @@ contract DirectGrantsLiteStrategy is Native, BaseStrategy, Multicall {
}
}

if (nativeAmount > 0) _sender.safeTransferETH(nativeAmount);
if (nativeAmount > 0) _sender.transferAmountNative(nativeAmount);
}

/// @notice Check if sender is profile owner or member.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ pragma solidity 0.8.19;
import {ISignatureTransfer} from "permit2/ISignatureTransfer.sol";
import {DonationVotingMerkleDistributionBaseStrategy} from
"../donation-voting-merkle-base/DonationVotingMerkleDistributionBaseStrategy.sol";
import {SafeTransferLib} from "solady/utils/SafeTransferLib.sol";
import {IERC20Permit} from "@openzeppelin/contracts/token/ERC20/extensions/IERC20Permit.sol";
import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import {IDAI} from "../donation-voting-merkle-base/IDAI.sol";
Expand All @@ -28,7 +27,6 @@ import {Transfer} from "../../core/libraries/Transfer.sol";
/// @author @thelostone-mc <[email protected]>, @0xKurt <[email protected]>, @codenamejason <[email protected]>, @0xZakk <[email protected]>, @nfrgosselin <[email protected]>
/// @notice Strategy for donation voting allocation with a merkle distribution
contract DonationVotingMerkleDistributionDirectTransferStrategy is DonationVotingMerkleDistributionBaseStrategy {
using SafeTransferLib for address;
using Transfer for address;

/// ===============================
Expand Down Expand Up @@ -60,12 +58,8 @@ contract DonationVotingMerkleDistributionDirectTransferStrategy is DonationVotin
address recipientAddress = _recipients[recipientId].recipientAddress;
// Native or already approved
if (permitType == PermitType.None) {
if (token == NATIVE) {
if (msg.value < amount) revert ETH_MISMATCH();
recipientAddress.safeTransferETH(amount);
} else {
token.safeTransferFrom(_sender, recipientAddress, amount);
}
if (token == NATIVE && msg.value < amount) revert ETH_MISMATCH();
token.transferAmountFrom(_sender, recipientAddress, amount);
} else if (permitType == PermitType.Permit2) {
PERMIT2.permitTransferFrom( // The permit message.
p2Data.permit,
Expand Down

0 comments on commit 606ea5c

Please sign in to comment.