Skip to content
Verified contractPartialUNLICENSED

Forwarder

Address0xb185ff4ae7aa80db9c0dc968ebcbd897cb2f3bba
Balance

0 ETH

 

Transactions

0

 

Compiler

solc 0.8.37

 

Same code

Only this contract

 

Details

Compiler
solc 0.8.37+commit.f401782d
Language
solidity
Optimization
Yes, 200 runs
EVM version
paris
License
UNLICENSED
Match
Partialcreation code: yes, runtime code: yes
Verified on Sourcify
Created in block
26,408,932
Creation transaction
0xb5b4cd7ce3…a12c702abb

forwarder.sol

// SPDX-License-Identifier: UNLICENSED
pragma solidity 0.8.37;

interface IToken {
    function balanceOf(address account) external view returns (uint256);
}

// Hands what one call of a wallet's batch delivered to a later call of the same batch, read on chain
// (design 6.3, "Amount stitching"). `note` records the calling wallet's balance of a token before the
// producer. Each `forward` after it counts what the wallet now holds above that note, or its whole
// balance where `whole` is set, takes `share` of that (1e18 is all of it) and at most `cap`, and calls
// one consumer with that amount written into its calldata at byte `amountAt`; the consumer credits the
// wallet. A second `forward` after a first counts what the first left, so one increase is never spent
// twice. It moves only the calling wallet's tokens, only inside `forward`, never calls the token for
// anyone else, and holds none once `forward` returns. Ownerless, holds no funds, not upgradeable.
contract Forwarder {
    event Forwarded(address indexed wallet, address indexed token, uint256 indexed tag, uint256 amount);

    uint256 private constant one = 1e18;
    // The wallet's balance of a token when it last noted it, plus one, so a note of nothing is told
    // apart from no note. Ordinary storage: a batch's calls are also run apart, each as its own
    // transaction, where a transient note would be gone before the forward.
    mapping(address => mapping(address => uint256)) private noted;
    bool private forwarding;

    function note(address token) external {
        noted[msg.sender][token] = IToken(token).balanceOf(msg.sender) + 1;
    }

    // Reverts when the increase since the note is below `floor`. `tag` is the caller's name for the
    // consumer, echoed in `Forwarded` with the amount the consumer kept.
    function forward(
        address token,
        uint256 floor,
        bool whole,
        uint256 share,
        uint256 cap,
        address target,
        bytes calldata data,
        uint256 amountAt,
        uint256 tag
    ) external {
        require(!forwarding, "reentered");
        require(target != token && target != address(this), "target");
        require(share <= one && amountAt >= 4 && data.length >= 32 && amountAt <= data.length - 32, "call");
        uint256 amount = counted(token, floor, whole, share, cap);
        if (amount > 0) {
            forwarding = true;
            amount = hand(token, target, data, amountAt, amount);
            forwarding = false;
        }
        emit Forwarded(msg.sender, token, tag, amount);
    }

    function counted(
        address token,
        uint256 floor,
        bool whole,
        uint256 share,
        uint256 cap
    ) private view returns (uint256 amount) {
        uint256 before = noted[msg.sender][token];
        require(before != 0, "unnoted");
        uint256 held = IToken(token).balanceOf(msg.sender);
        uint256 increase = held + 1 > before ? held + 1 - before : 0;
        require(increase >= floor, "short");
        amount = ((whole ? held : increase) * share) / one;
        if (amount > cap) amount = cap;
    }

    // Pulls `amount` from the wallet, lets `target` take exactly it, calls `target` with it written at
    // `amountAt`, takes the allowance back and returns what `target` left; answers what `target` kept.
    function hand(
        address token,
        address target,
        bytes calldata data,
        uint256 amountAt,
        uint256 amount
    ) private returns (uint256) {
        uint256 kept = IToken(token).balanceOf(address(this));
        tokenCall(token, abi.encodeWithSelector(0x23b872dd, msg.sender, address(this), amount));
        tokenCall(token, abi.encodeWithSelector(0x095ea7b3, target, amount));
        bytes memory consumed = data;
        assembly {
            mstore(add(add(consumed, 32), amountAt), amount)
        }
        (bool answered, bytes memory answer) = target.call(consumed);
        if (!answered) {
            assembly {
                revert(add(answer, 32), mload(answer))
            }
        }
        tokenCall(token, abi.encodeWithSelector(0x095ea7b3, target, 0));
        uint256 back = IToken(token).balanceOf(address(this)) - kept;
        if (back > 0) tokenCall(token, abi.encodeWithSelector(0xa9059cbb, msg.sender, back));
        return amount - back;
    }

    // `transferFrom`, `approve` or `transfer` on the token: one that reverts, or answers anything but
    // nothing or true, reverts the forward.
    function tokenCall(address token, bytes memory request) private {
        (bool answered, bytes memory answer) = token.call(request);
        require(
            answered && token.code.length > 0 && (answer.length == 0 || abi.decode(answer, (bool))),
            "token"
        );
    }
}
Compiler settingsShow
{
  "metadata": {
    "appendCBOR": false,
    "bytecodeHash": "none"
  },
  "optimizer": {
    "runs": 200,
    "enabled": true
  },
  "evmVersion": "paris"
}

Source code and metadata: Sourcify.

The source code belongs to the authors of the contract.