Skip to content
Lattice

AdapterBaseLib

Git Source

Title: AdapterBaseLib

Authors: David Dada <daveproxy80@gmail.com> (https://github.com/dadadave80), Modified from OpenZeppelin (https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC20/utils/SafeERC20.sol)

Shared, security-critical primitives reused by every Lattice protocol adapter: exact-amount forceApprove (USDT/no-return safe), raw reward forwarding that reports the real received delta (fee-on-transfer safe), and shortfall-honest transfers. Stateless utility library — no own ERC-7201 slot, no facet, no interface file. Errors are declared here.

Operates on address(this) of the calling adapter (these are internal and run in the adapter's context). External-protocol targets must be freshly resolved by the caller before invoking forceApprove (re-resolve PoolAddressesProvider each call).

Functions

balanceOfSelf

Returns the calling adapter's balance of token.

function balanceOfSelf(address token) internal view returns (uint256);

Parameters

NameTypeDescription
tokenaddress

Returns

NameTypeDescription
<none>uint256

forceApprove

Sets spender's allowance over the adapter's token to exactly amount.

Resets to 0 first to support tokens (USDT) that revert on non-zero→non-zero approvals; tolerates tokens that return no data. Never grants an infinite allowance. Callers pass the freshly-resolved protocol target as spender.

function forceApprove(address token, address spender, uint256 amount) internal;

Parameters

NameTypeDescription
tokenaddress
spenderaddress
amountuint256

forwardRewardRaw

Transfers the adapter's entire rewardToken balance to recipient and returns the real amount the recipient received (measured by balance delta).

Zero balance is a no-op returning 0 (so a stuck/zero-claim reward never reverts the caller). Fee-on-transfer is handled by measuring the recipient's delta. Reverts AdapterSafeERC20FailedOperation only on an explicit transfer failure.

function forwardRewardRaw(address rewardToken, address recipient) internal returns (uint256 forwarded);

Parameters

NameTypeDescription
rewardTokenaddress
recipientaddress

Returns

NameTypeDescription
forwardeduint256

transferHonest

Transfers up to amount of token to to, capped at the adapter's balance, and returns the real amount transferred.

Shortfall-honest: when the adapter holds less than requested (e.g. after a partial liquidation), it sends what it has and reports that. The StrategyManager accepts an honest partial recall and reverts with StrategyManagerWithdrawShortfall only when the strategy's reported balance drops by more than it delivered.

function transferHonest(address token, address to, uint256 amount) internal returns (uint256 sent);

Parameters

NameTypeDescription
tokenaddress
toaddress
amountuint256

Returns

NameTypeDescription
sentuint256

Errors

AdapterSafeERC20FailedOperation

A low-level ERC-20 op (transfer/approve) failed.

error AdapterSafeERC20FailedOperation(address token);

Parameters

NameTypeDescription
tokenaddress