Verified contractExactMIT
GenericBridgeAdapter
Address0xe3fad885f3c0661dbd8e8a0e10d00edba210dd63
- Balance
0 ETH
- Transactions
0
- Compiler
solc 0.8.30
- Same code
Only this contract
Details
- Compiler
- solc 0.8.30+commit.73712a01
- Language
- solidity
- Optimization
- Yes, 44,444,444 runs
- EVM version
- prague
- Via IR
- yes
- License
- MIT
- Match
- Exactcreation code: no, runtime code: yes
- Verified on Sourcify
- Created in block
- 24,846,446
- Creation transaction
- 0xa6a9edca17…20666f215d
14 files
- src/adapters/generic/GenericBridgeAdapter.solmain1.73 KB
- lib/ks-common-sc/lib/openzeppelin-contracts/contracts/interfaces/IERC20.sol182 B
- lib/ks-common-sc/lib/openzeppelin-contracts/contracts/token/ERC20/IERC20.sol2.78 KB
- lib/ks-common-sc/lib/openzeppelin-contracts/contracts/utils/Address.sol6.8 KB
- lib/ks-common-sc/lib/openzeppelin-contracts/contracts/utils/Errors.sol896 B
- lib/ks-common-sc/lib/openzeppelin-contracts/contracts/utils/LowLevelCall.sol5.51 KB
- lib/ks-common-sc/src/libraries/CustomRevert.sol4.75 KB
- lib/ks-common-sc/src/libraries/token/TokenHelper.sol8.42 KB
- src/adapters/BaseBridgeAdapter.sol401 B
- src/interfaces/adapters/IBridgeAdapter.sol802 B
- src/interfaces/adapters/generic/IGenericBridgeAdapter.sol605 B
- src/libraries/BitMask.sol388 B
- src/libraries/BytesHelper.sol465 B
- src/types/BoolAddress.sol1.19 KB
src/adapters/generic/GenericBridgeAdapter.sol
// SPDX-License-Identifier: MIT
pragma solidity 0.8.30;
import {IBridgeAdapter} from '../../interfaces/adapters/IBridgeAdapter.sol';
import {IGenericBridgeAdapter} from '../../interfaces/adapters/generic/IGenericBridgeAdapter.sol';
import {BaseBridgeAdapter} from '../BaseBridgeAdapter.sol';
import {BytesHelper} from '../../libraries/BytesHelper.sol';
import {TokenHelper} from 'ks-common-sc/src/libraries/token/TokenHelper.sol';
import {Address} from 'openzeppelin-contracts/contracts/utils/Address.sol';
contract GenericBridgeAdapter is BaseBridgeAdapter, IGenericBridgeAdapter {
using TokenHelper for address;
using BytesHelper for bytes;
using Address for address;
/// @inheritdoc IBridgeAdapter
function bridge(
address inputToken,
uint256 inputAmount,
bytes32,
bytes32,
bytes calldata bridgeData
) external payable onlyDelegateCall returns (bytes memory result) {
BridgeAdapterParams calldata params;
assembly ('memory-safe') {
params := add(bridgeData.offset, calldataload(bridgeData.offset))
}
(bool toApprove, address approveOrTransferTarget) = params.approveOrTransferTarget.unpack();
if (approveOrTransferTarget != address(0)) {
if (toApprove) {
inputToken.forceApprove(approveOrTransferTarget, type(uint256).max);
} else {
inputToken.safeTransfer(approveOrTransferTarget, inputAmount);
}
}
bytes memory callData = params.callData;
if (callData.length != 0) {
if (params.amountOffset != 0) {
callData.mstore(params.amountOffset, bytes32(inputAmount));
}
return
params.callTarget.functionCallWithValue(callData, inputToken.isNative() ? inputAmount : 0);
}
}
}
lib/ks-common-sc/lib/openzeppelin-contracts/contracts/interfaces/IERC20.sol
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.4.0) (interfaces/IERC20.sol)
pragma solidity >=0.4.16;
import {IERC20} from "../token/ERC20/IERC20.sol";
lib/ks-common-sc/lib/openzeppelin-contracts/contracts/token/ERC20/IERC20.sol
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.4.0) (token/ERC20/IERC20.sol)
pragma solidity >=0.4.16;
/**
* @dev Interface of the ERC-20 standard as defined in the ERC.
*/
interface IERC20 {
/**
* @dev Emitted when `value` tokens are moved from one account (`from`) to
* another (`to`).
*
* Note that `value` may be zero.
*/
event Transfer(address indexed from, address indexed to, uint256 value);
/**
* @dev Emitted when the allowance of a `spender` for an `owner` is set by
* a call to {approve}. `value` is the new allowance.
*/
event Approval(address indexed owner, address indexed spender, uint256 value);
/**
* @dev Returns the value of tokens in existence.
*/
function totalSupply() external view returns (uint256);
/**
* @dev Returns the value of tokens owned by `account`.
*/
function balanceOf(address account) external view returns (uint256);
/**
* @dev Moves a `value` amount of tokens from the caller's account to `to`.
*
* Returns a boolean value indicating whether the operation succeeded.
*
* Emits a {Transfer} event.
*/
function transfer(address to, uint256 value) external returns (bool);
/**
* @dev Returns the remaining number of tokens that `spender` will be
* allowed to spend on behalf of `owner` through {transferFrom}. This is
* zero by default.
*
* This value changes when {approve} or {transferFrom} are called.
*/
function allowance(address owner, address spender) external view returns (uint256);
/**
* @dev Sets a `value` amount of tokens as the allowance of `spender` over the
* caller's tokens.
*
* Returns a boolean value indicating whether the operation succeeded.
*
* IMPORTANT: Beware that changing an allowance with this method brings the risk
* that someone may use both the old and the new allowance by unfortunate
* transaction ordering. One possible solution to mitigate this race
* condition is to first reduce the spender's allowance to 0 and set the
* desired value afterwards:
* https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729
*
* Emits an {Approval} event.
*/
function approve(address spender, uint256 value) external returns (bool);
/**
* @dev Moves a `value` amount of tokens from `from` to `to` using the
* allowance mechanism. `value` is then deducted from the caller's
* allowance.
*
* Returns a boolean value indicating whether the operation succeeded.
*
* Emits a {Transfer} event.
*/
function transferFrom(address from, address to, uint256 value) external returns (bool);
}
lib/ks-common-sc/lib/openzeppelin-contracts/contracts/utils/Address.sol
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.5.0) (utils/Address.sol)
pragma solidity ^0.8.20;
import {Errors} from "./Errors.sol";
import {LowLevelCall} from "./LowLevelCall.sol";
/**
* @dev Collection of functions related to the address type
*/
library Address {
/**
* @dev There's no code at `target` (it is not a contract).
*/
error AddressEmptyCode(address target);
/**
* @dev Replacement for Solidity's `transfer`: sends `amount` wei to
* `recipient`, forwarding all available gas and reverting on errors.
*
* https://eips.ethereum.org/EIPS/eip-1884[EIP1884] increases the gas cost
* of certain opcodes, possibly making contracts go over the 2300 gas limit
* imposed by `transfer`, making them unable to receive funds via
* `transfer`. {sendValue} removes this limitation.
*
* https://consensys.net/diligence/blog/2019/09/stop-using-soliditys-transfer-now/[Learn more].
*
* IMPORTANT: because control is transferred to `recipient`, care must be
* taken to not create reentrancy vulnerabilities. Consider using
* {ReentrancyGuard} or the
* https://solidity.readthedocs.io/en/v0.8.20/security-considerations.html#use-the-checks-effects-interactions-pattern[checks-effects-interactions pattern].
*/
function sendValue(address payable recipient, uint256 amount) internal {
if (address(this).balance < amount) {
revert Errors.InsufficientBalance(address(this).balance, amount);
}
if (LowLevelCall.callNoReturn(recipient, amount, "")) {
// call successful, nothing to do
return;
} else if (LowLevelCall.returnDataSize() > 0) {
LowLevelCall.bubbleRevert();
} else {
revert Errors.FailedCall();
}
}
/**
* @dev Performs a Solidity function call using a low level `call`. A
* plain `call` is an unsafe replacement for a function call: use this
* function instead.
*
* If `target` reverts with a revert reason or custom error, it is bubbled
* up by this function (like regular Solidity function calls). However, if
* the call reverted with no returned reason, this function reverts with a
* {Errors.FailedCall} error.
*
* Returns the raw returned data. To convert to the expected return value,
* use https://solidity.readthedocs.io/en/latest/units-and-global-variables.html?highlight=abi.decode#abi-encoding-and-decoding-functions[`abi.decode`].
*
* Requirements:
*
* - `target` must be a contract.
* - calling `target` with `data` must not revert.
*/
function functionCall(address target, bytes memory data) internal returns (bytes memory) {
return functionCallWithValue(target, data, 0);
}
/**
* @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],
* but also transferring `value` wei to `target`.
*
* Requirements:
*
* - the calling contract must have an ETH balance of at least `value`.
* - the called Solidity function must be `payable`.
*/
function functionCallWithValue(address target, bytes memory data, uint256 value) internal returns (bytes memory) {
if (address(this).balance < value) {
revert Errors.InsufficientBalance(address(this).balance, value);
}
bool success = LowLevelCall.callNoReturn(target, value, data);
if (success && (LowLevelCall.returnDataSize() > 0 || target.code.length > 0)) {
return LowLevelCall.returnData();
} else if (success) {
revert AddressEmptyCode(target);
} else if (LowLevelCall.returnDataSize() > 0) {
LowLevelCall.bubbleRevert();
} else {
revert Errors.FailedCall();
}
}
/**
* @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],
* but performing a static call.
*/
function functionStaticCall(address target, bytes memory data) internal view returns (bytes memory) {
bool success = LowLevelCall.staticcallNoReturn(target, data);
if (success && (LowLevelCall.returnDataSize() > 0 || target.code.length > 0)) {
return LowLevelCall.returnData();
} else if (success) {
revert AddressEmptyCode(target);
} else if (LowLevelCall.returnDataSize() > 0) {
LowLevelCall.bubbleRevert();
} else {
revert Errors.FailedCall();
}
}
/**
* @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],
* but performing a delegate call.
*/
function functionDelegateCall(address target, bytes memory data) internal returns (bytes memory) {
bool success = LowLevelCall.delegatecallNoReturn(target, data);
if (success && (LowLevelCall.returnDataSize() > 0 || target.code.length > 0)) {
return LowLevelCall.returnData();
} else if (success) {
revert AddressEmptyCode(target);
} else if (LowLevelCall.returnDataSize() > 0) {
LowLevelCall.bubbleRevert();
} else {
revert Errors.FailedCall();
}
}
/**
* @dev Tool to verify that a low level call to smart-contract was successful, and reverts if the target
* was not a contract or bubbling up the revert reason (falling back to {Errors.FailedCall}) in case
* of an unsuccessful call.
*
* NOTE: This function is DEPRECATED and may be removed in the next major release.
*/
function verifyCallResultFromTarget(
address target,
bool success,
bytes memory returndata
) internal view returns (bytes memory) {
// only check if target is a contract if the call was successful and the return data is empty
// otherwise we already know that it was a contract
if (success && (returndata.length > 0 || target.code.length > 0)) {
return returndata;
} else if (success) {
revert AddressEmptyCode(target);
} else if (returndata.length > 0) {
LowLevelCall.bubbleRevert(returndata);
} else {
revert Errors.FailedCall();
}
}
/**
* @dev Tool to verify that a low level call was successful, and reverts if it wasn't, either by bubbling the
* revert reason or with a default {Errors.FailedCall} error.
*/
function verifyCallResult(bool success, bytes memory returndata) internal pure returns (bytes memory) {
if (success) {
return returndata;
} else if (returndata.length > 0) {
LowLevelCall.bubbleRevert(returndata);
} else {
revert Errors.FailedCall();
}
}
}
lib/ks-common-sc/lib/openzeppelin-contracts/contracts/utils/Errors.sol
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.1.0) (utils/Errors.sol)
pragma solidity ^0.8.20;
/**
* @dev Collection of common custom errors used in multiple contracts
*
* IMPORTANT: Backwards compatibility is not guaranteed in future versions of the library.
* It is recommended to avoid relying on the error API for critical functionality.
*
* _Available since v5.1._
*/
library Errors {
/**
* @dev The ETH balance of the account is not enough to perform the operation.
*/
error InsufficientBalance(uint256 balance, uint256 needed);
/**
* @dev A call to an address target failed. The target may have reverted.
*/
error FailedCall();
/**
* @dev The deployment failed.
*/
error FailedDeployment();
/**
* @dev A necessary precompile is missing.
*/
error MissingPrecompile(address);
}
lib/ks-common-sc/lib/openzeppelin-contracts/contracts/utils/LowLevelCall.sol
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.5.0) (utils/LowLevelCall.sol)
pragma solidity ^0.8.20;
/**
* @dev Library of low level call functions that implement different calling strategies to deal with the return data.
*
* WARNING: Using this library requires an advanced understanding of Solidity and how the EVM works. It is recommended
* to use the {Address} library instead.
*/
library LowLevelCall {
/// @dev Performs a Solidity function call using a low level `call` and ignoring the return data.
function callNoReturn(address target, bytes memory data) internal returns (bool success) {
return callNoReturn(target, 0, data);
}
/// @dev Same as {callNoReturn}, but allows to specify the value to be sent in the call.
function callNoReturn(address target, uint256 value, bytes memory data) internal returns (bool success) {
assembly ("memory-safe") {
success := call(gas(), target, value, add(data, 0x20), mload(data), 0x00, 0x00)
}
}
/// @dev Performs a Solidity function call using a low level `call` and returns the first 64 bytes of the result
/// in the scratch space of memory. Useful for functions that return a tuple of single-word values.
///
/// WARNING: Do not assume that the results are zero if `success` is false. Memory can be already allocated
/// and this function doesn't zero it out.
function callReturn64Bytes(
address target,
bytes memory data
) internal returns (bool success, bytes32 result1, bytes32 result2) {
return callReturn64Bytes(target, 0, data);
}
/// @dev Same as {callReturnBytes32Pair}, but allows to specify the value to be sent in the call.
function callReturn64Bytes(
address target,
uint256 value,
bytes memory data
) internal returns (bool success, bytes32 result1, bytes32 result2) {
assembly ("memory-safe") {
success := call(gas(), target, value, add(data, 0x20), mload(data), 0x00, 0x40)
result1 := mload(0x00)
result2 := mload(0x20)
}
}
/// @dev Performs a Solidity function call using a low level `staticcall` and ignoring the return data.
function staticcallNoReturn(address target, bytes memory data) internal view returns (bool success) {
assembly ("memory-safe") {
success := staticcall(gas(), target, add(data, 0x20), mload(data), 0x00, 0x00)
}
}
/// @dev Performs a Solidity function call using a low level `staticcall` and returns the first 64 bytes of the result
/// in the scratch space of memory. Useful for functions that return a tuple of single-word values.
///
/// WARNING: Do not assume that the results are zero if `success` is false. Memory can be already allocated
/// and this function doesn't zero it out.
function staticcallReturn64Bytes(
address target,
bytes memory data
) internal view returns (bool success, bytes32 result1, bytes32 result2) {
assembly ("memory-safe") {
success := staticcall(gas(), target, add(data, 0x20), mload(data), 0x00, 0x40)
result1 := mload(0x00)
result2 := mload(0x20)
}
}
/// @dev Performs a Solidity function call using a low level `delegatecall` and ignoring the return data.
function delegatecallNoReturn(address target, bytes memory data) internal returns (bool success) {
assembly ("memory-safe") {
success := delegatecall(gas(), target, add(data, 0x20), mload(data), 0x00, 0x00)
}
}
/// @dev Performs a Solidity function call using a low level `delegatecall` and returns the first 64 bytes of the result
/// in the scratch space of memory. Useful for functions that return a tuple of single-word values.
///
/// WARNING: Do not assume that the results are zero if `success` is false. Memory can be already allocated
/// and this function doesn't zero it out.
function delegatecallReturn64Bytes(
address target,
bytes memory data
) internal returns (bool success, bytes32 result1, bytes32 result2) {
assembly ("memory-safe") {
success := delegatecall(gas(), target, add(data, 0x20), mload(data), 0x00, 0x40)
result1 := mload(0x00)
result2 := mload(0x20)
}
}
/// @dev Returns the size of the return data buffer.
function returnDataSize() internal pure returns (uint256 size) {
assembly ("memory-safe") {
size := returndatasize()
}
}
/// @dev Returns a buffer containing the return data from the last call.
function returnData() internal pure returns (bytes memory result) {
assembly ("memory-safe") {
result := mload(0x40)
mstore(result, returndatasize())
returndatacopy(add(result, 0x20), 0x00, returndatasize())
mstore(0x40, add(result, add(0x20, returndatasize())))
}
}
/// @dev Revert with the return data from the last call.
function bubbleRevert() internal pure {
assembly ("memory-safe") {
let fmp := mload(0x40)
returndatacopy(fmp, 0x00, returndatasize())
revert(fmp, returndatasize())
}
}
function bubbleRevert(bytes memory returndata) internal pure {
assembly ("memory-safe") {
revert(add(returndata, 0x20), mload(returndata))
}
}
}
lib/ks-common-sc/src/libraries/CustomRevert.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
/// @title Library for reverting with custom errors efficiently
/// @notice Contains functions for reverting with custom errors with different argument types efficiently
/// @dev To use this library, declare `using CustomRevert for bytes4;` and replace `revert CustomError()` with
/// `CustomError.selector.revertWith()`
/// @dev The functions may tamper with the free memory pointer but it is fine since the call context is exited immediately
library CustomRevert {
/// @dev ERC-7751 error for wrapping bubbled up reverts
error WrappedError(address target, bytes4 selector, bytes reason, bytes details);
/// @dev Reverts with the selector of a custom error in the scratch space
function revertWith(bytes4 selector) internal pure {
assembly ('memory-safe') {
mstore(0, selector)
revert(0, 0x04)
}
}
/// @dev Reverts with a custom error with an address argument in the scratch space
function revertWith(bytes4 selector, address addr) internal pure {
assembly ('memory-safe') {
mstore(0, selector)
mstore(0x04, and(addr, 0xffffffffffffffffffffffffffffffffffffffff))
revert(0, 0x24)
}
}
/// @dev Reverts with a custom error with an int24 argument in the scratch space
function revertWith(bytes4 selector, int24 value) internal pure {
assembly ('memory-safe') {
mstore(0, selector)
mstore(0x04, signextend(2, value))
revert(0, 0x24)
}
}
/// @dev Reverts with a custom error with a uint160 argument in the scratch space
function revertWith(bytes4 selector, uint160 value) internal pure {
assembly ('memory-safe') {
mstore(0, selector)
mstore(0x04, and(value, 0xffffffffffffffffffffffffffffffffffffffff))
revert(0, 0x24)
}
}
/// @dev Reverts with a custom error with two int24 arguments
function revertWith(bytes4 selector, int24 value1, int24 value2) internal pure {
assembly ('memory-safe') {
let fmp := mload(0x40)
mstore(fmp, selector)
mstore(add(fmp, 0x04), signextend(2, value1))
mstore(add(fmp, 0x24), signextend(2, value2))
revert(fmp, 0x44)
}
}
/// @dev Reverts with a custom error with two uint160 arguments
function revertWith(bytes4 selector, uint160 value1, uint160 value2) internal pure {
assembly ('memory-safe') {
let fmp := mload(0x40)
mstore(fmp, selector)
mstore(add(fmp, 0x04), and(value1, 0xffffffffffffffffffffffffffffffffffffffff))
mstore(add(fmp, 0x24), and(value2, 0xffffffffffffffffffffffffffffffffffffffff))
revert(fmp, 0x44)
}
}
/// @dev Reverts with a custom error with two address arguments
function revertWith(bytes4 selector, address value1, address value2) internal pure {
assembly ('memory-safe') {
let fmp := mload(0x40)
mstore(fmp, selector)
mstore(add(fmp, 0x04), and(value1, 0xffffffffffffffffffffffffffffffffffffffff))
mstore(add(fmp, 0x24), and(value2, 0xffffffffffffffffffffffffffffffffffffffff))
revert(fmp, 0x44)
}
}
/// @notice bubble up the revert message returned by a call and revert with a wrapped ERC-7751 error
/// @dev this method can be vulnerable to revert data bombs
function bubbleUpAndRevertWith(
address revertingContract,
bytes4 revertingFunctionSelector,
bytes4 additionalContext
) internal pure {
bytes4 wrappedErrorSelector = WrappedError.selector;
assembly ('memory-safe') {
// Ensure the size of the revert data is a multiple of 32 bytes
let encodedDataSize := mul(div(add(returndatasize(), 31), 32), 32)
let fmp := mload(0x40)
// Encode wrapped error selector, address, function selector, offset, additional context, size, revert reason
mstore(fmp, wrappedErrorSelector)
mstore(add(fmp, 0x04), and(revertingContract, 0xffffffffffffffffffffffffffffffffffffffff))
mstore(
add(fmp, 0x24),
and(
revertingFunctionSelector,
0xffffffff00000000000000000000000000000000000000000000000000000000
)
)
// offset revert reason
mstore(add(fmp, 0x44), 0x80)
// offset additional context
mstore(add(fmp, 0x64), add(0xa0, encodedDataSize))
// size revert reason
mstore(add(fmp, 0x84), returndatasize())
// revert reason
returndatacopy(add(fmp, 0xa4), 0, returndatasize())
// size additional context
mstore(add(fmp, add(0xa4, encodedDataSize)), 0x04)
// additional context
mstore(
add(fmp, add(0xc4, encodedDataSize)),
and(additionalContext, 0xffffffff00000000000000000000000000000000000000000000000000000000)
)
revert(fmp, add(0xe4, encodedDataSize))
}
}
}
lib/ks-common-sc/src/libraries/token/TokenHelper.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import {CustomRevert} from '../CustomRevert.sol';
import {IERC20} from 'openzeppelin-contracts/contracts/interfaces/IERC20.sol';
/// @title Library for transferring, approving and holding native tokens and ERC20 tokens
/// @dev This library is based on CurrencyLibrary.sol from Uniswap/v4-core and SafeTransferLib.sol from transmissions11/solmate
library TokenHelper {
/// @notice Additional context for ERC-7751 wrapped error when a native transfer fails
error NativeTransferFailed();
/// @notice Additional context for ERC-7751 wrapped error when an ERC20 transfer fails
error ERC20TransferFailed();
/// @notice Additional context for ERC-7751 wrapped error when an ERC20 approve fails
error ERC20ApproveFailed();
address internal constant NATIVE_ADDRESS = 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE;
function isNative(address token) internal pure returns (bool) {
return token == NATIVE_ADDRESS || token == address(0);
}
function safeTransferNative(address to, uint256 amount) internal {
if (amount == 0) return;
bool success;
assembly ('memory-safe') {
// Transfer the ETH and revert if it fails.
success := call(gas(), to, amount, 0, 0, 0, 0)
}
// revert with NativeTransferFailed, containing the bubbled up error as an argument
if (!success) {
CustomRevert.bubbleUpAndRevertWith(to, bytes4(0), NativeTransferFailed.selector);
}
}
function safeTransferERC20(address token, address to, uint256 amount) internal {
if (amount == 0) return;
bool success;
assembly ('memory-safe') {
// Get a pointer to some free memory.
let fmp := mload(0x40)
// Write the abi-encoded calldata into memory, beginning with the function selector.
mstore(fmp, 0xa9059cbb00000000000000000000000000000000000000000000000000000000)
mstore(add(fmp, 4), and(to, 0xffffffffffffffffffffffffffffffffffffffff)) // Append and mask the "to" argument.
mstore(add(fmp, 36), amount) // Append the "amount" argument. Masking not required as it's a full 32 byte type.
success := and(
// Set success to whether the call reverted, if not we check it either
// returned exactly 1 (can't just be non-zero data), or had no return data.
or(and(eq(mload(0), 1), gt(returndatasize(), 31)), iszero(returndatasize())),
// We use 68 because the length of our calldata totals up like so: 4 + 32 * 2.
// We use 0 and 32 to copy up to 32 bytes of return data into the scratch space.
// Counterintuitively, this call must be positioned second to the or() call in the
// surrounding and() call or else returndatasize() will be zero during the computation.
call(gas(), token, 0, fmp, 68, 0, 32)
)
// Now clean the memory we used
mstore(fmp, 0) // 4 byte `selector` and 28 bytes of `to` were stored here
mstore(add(fmp, 0x20), 0) // 4 bytes of `to` and 28 bytes of `amount` were stored here
mstore(add(fmp, 0x40), 0) // 4 bytes of `amount` were stored here
}
// revert with ERC20TransferFailed, containing the bubbled up error as an argument
if (!success) {
CustomRevert.bubbleUpAndRevertWith(
token, IERC20.transfer.selector, ERC20TransferFailed.selector
);
}
}
function safeTransfer(address token, address to, uint256 amount) internal {
if (isNative(token)) {
safeTransferNative(to, amount);
} else {
safeTransferERC20(token, to, amount);
}
}
function safeTransferFrom(address token, address from, address to, uint256 amount) internal {
if (amount == 0) return;
bool success;
assembly ('memory-safe') {
// Get a pointer to some free memory.
let fmp := mload(0x40)
// Write the abi-encoded calldata into memory, beginning with the function selector.
mstore(fmp, 0x23b872dd00000000000000000000000000000000000000000000000000000000)
mstore(add(fmp, 4), and(from, 0xffffffffffffffffffffffffffffffffffffffff)) // Append and mask the "from" argument.
mstore(add(fmp, 36), and(to, 0xffffffffffffffffffffffffffffffffffffffff)) // Append and mask the "to" argument.
mstore(add(fmp, 68), amount) // Append the "amount" argument. Masking not required as it's a full 32 byte type.
success := and(
// Set success to whether the call reverted, if not we check it either
// returned exactly 1 (can't just be non-zero data), or had no return data.
or(and(eq(mload(0), 1), gt(returndatasize(), 31)), iszero(returndatasize())),
// We use 100 because the length of our calldata totals up like so: 4 + 32 * 3.
// We use 0 and 32 to copy up to 32 bytes of return data into the scratch space.
// Counterintuitively, this call must be positioned second to the or() call in the
// surrounding and() call or else returndatasize() will be zero during the computation.
call(gas(), token, 0, fmp, 100, 0, 32)
)
// Now clean the memory we used
mstore(fmp, 0) // 4 byte `selector` and 28 bytes of `from` were stored here
mstore(add(fmp, 0x20), 0) // 4 bytes of `from` and 28 bytes of `to` were stored here
mstore(add(fmp, 0x40), 0) // 4 bytes of `to` and 28 bytes of `amount` were stored here
mstore(add(fmp, 0x60), 0) // 4 bytes of `amount` were stored here
}
// revert with ERC20TransferFailed, containing the bubbled up error as an argument
if (!success) {
CustomRevert.bubbleUpAndRevertWith(
token, IERC20.transferFrom.selector, ERC20TransferFailed.selector
);
}
}
function trySafeApprove(address token, address spender, uint256 amount)
internal
returns (bool success)
{
assembly ('memory-safe') {
// Get a pointer to some free memory.
let fmp := mload(0x40)
// Write the abi-encoded calldata into memory, beginning with the function selector.
mstore(fmp, 0x095ea7b300000000000000000000000000000000000000000000000000000000)
mstore(add(fmp, 4), and(spender, 0xffffffffffffffffffffffffffffffffffffffff)) // Append and mask the "spender" argument.
mstore(add(fmp, 36), amount) // Append the "amount" argument. Masking not required as it's a full 32 byte type.
success := and(
// Set success to whether the call reverted, if not we check it either
// returned exactly 1 (can't just be non-zero data), or had no return data.
or(and(eq(mload(0), 1), gt(returndatasize(), 31)), iszero(returndatasize())),
// We use 68 because the length of our calldata totals up like so: 4 + 32 * 2.
// We use 0 and 32 to copy up to 32 bytes of return data into the scratch space.
// Counterintuitively, this call must be positioned second to the or() call in the
// surrounding and() call or else returndatasize() will be zero during the computation.
call(gas(), token, 0, fmp, 68, 0, 32)
)
// Now clean the memory we used
mstore(fmp, 0) // 4 byte `selector` and 28 bytes of `to` were stored here
mstore(add(fmp, 0x20), 0) // 4 bytes of `to` and 28 bytes of `amount` were stored here
mstore(add(fmp, 0x40), 0) // 4 bytes of `amount` were stored here
}
}
function safeApprove(address token, address spender, uint256 amount) internal {
// revert with ERC20ApproveFailed, containing the bubbled up error as an argument
if (!trySafeApprove(token, spender, amount)) {
CustomRevert.bubbleUpAndRevertWith(
token, IERC20.approve.selector, ERC20ApproveFailed.selector
);
}
}
function forceApprove(address token, address spender, uint256 amount) internal {
// meant to be used with tokens that require the approval to be set to zero before setting it to a non-zero value, such as USDT
if (!trySafeApprove(token, spender, amount)) {
safeApprove(token, spender, 0);
safeApprove(token, spender, amount);
}
}
function balanceOf(address token, address account) internal view returns (uint256) {
if (isNative(token)) {
return account.balance;
} else {
return IERC20(token).balanceOf(account);
}
}
function selfBalance(address token) internal view returns (uint256) {
if (isNative(token)) {
return address(this).balance;
} else {
return IERC20(token).balanceOf(address(this));
}
}
}
src/adapters/BaseBridgeAdapter.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import {IBridgeAdapter} from '../interfaces/adapters/IBridgeAdapter.sol';
abstract contract BaseBridgeAdapter is IBridgeAdapter {
address internal immutable original;
constructor() {
original = address(this);
}
modifier onlyDelegateCall() {
if (original == address(this)) {
revert NotDelegateCall();
}
_;
}
}
src/interfaces/adapters/IBridgeAdapter.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
interface IBridgeAdapter {
/// @notice Thrown when the function is not called via delegate call.
error NotDelegateCall();
/**
* @notice Bridges the tokens.
* @param inputToken The address of the input token.
* @param inputAmount The amount of the input token.
* @param destinationChainId The ID of the destination chain.
* @param bridgeOutputToken The address of the bridge output token.
* @param bridgeData The data for the bridge operation.
* @return result The result of the bridge operation.
*/
function bridge(
address inputToken,
uint256 inputAmount,
bytes32 destinationChainId,
bytes32 bridgeOutputToken,
bytes calldata bridgeData
) external payable returns (bytes memory result);
}
src/interfaces/adapters/generic/IGenericBridgeAdapter.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import {BoolAddress} from '../../../types/BoolAddress.sol';
interface IGenericBridgeAdapter {
/**
* @notice Parameters for generic bridging.
* @param approveOrTransferTarget The address to approve or transfer the input token to.
* @param callTarget The address to call.
* @param callData The data to call.
* @param amountOffset The offset of the input amount in the callData.
*/
struct BridgeAdapterParams {
BoolAddress approveOrTransferTarget;
address callTarget;
bytes callData;
uint256 amountOffset;
}
}
src/libraries/BitMask.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
uint256 constant MASK_1_BIT = 0x1;
uint256 constant MASK_8_BITS = 0xff;
uint256 constant MASK_24_BITS = 0xffffff;
uint256 constant MASK_127_BITS = 0x7fffffffffffffffffffffffffffffff;
uint256 constant MASK_128_BITS = 0xffffffffffffffffffffffffffffffff;
uint160 constant MASK_160_BITS = 0x00ffffffffffffffffffffffffffffffffffffffff;
src/libraries/BytesHelper.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
library BytesHelper {
function mload(bytes memory data, uint256 offset) internal pure returns (bytes32 result) {
assembly {
result := mload(add(data, add(offset, 0x20)))
}
}
function mstore(bytes memory data, uint256 offset, bytes32 value)
internal
pure
returns (bytes memory)
{
assembly {
mstore(add(data, add(offset, 0x20)), value)
}
return data;
}
}
src/types/BoolAddress.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import {MASK_160_BITS} from '../libraries/BitMask.sol';
/**
* @notice BoolAddress is packed version of solidity structure.
*
* Layout: 1 bit boolean value | 160 bits address value
*/
type BoolAddress is uint256;
using BoolAddressLibrary for BoolAddress global;
function toBoolAddress(bool boolValue, address addressValue) pure returns (BoolAddress result) {
assembly ('memory-safe') {
result := or(shl(160, boolValue), addressValue)
}
}
library BoolAddressLibrary {
uint256 internal constant BOOL_VALUE_OFFSET = 160;
function boolValue(BoolAddress self) internal pure returns (bool _boolValue) {
assembly ('memory-safe') {
_boolValue := shr(BOOL_VALUE_OFFSET, self)
}
}
function addressValue(BoolAddress self) internal pure returns (address _addressValue) {
assembly ('memory-safe') {
_addressValue := and(self, MASK_160_BITS)
}
}
function unpack(BoolAddress self) internal pure returns (bool _boolValue, address _addressValue) {
assembly ('memory-safe') {
_boolValue := shr(BOOL_VALUE_OFFSET, self)
_addressValue := and(self, MASK_160_BITS)
}
}
}
Compiler settingsShow
{
"viaIR": true,
"metadata": {
"appendCBOR": true,
"bytecodeHash": "ipfs",
"useLiteralContent": false
},
"optimizer": {
"runs": 44444444,
"enabled": true
},
"evmVersion": "prague",
"remappings": [
"forge-std/=lib/ks-common-sc/lib/forge-std/src/",
"hardhat/=node_modules/hardhat/",
"ks-common-sc/=lib/ks-common-sc/",
"ks-allowance-hub/=lib/ks-allowance-hub/",
"openzeppelin-contracts/=lib/ks-common-sc/lib/openzeppelin-contracts/",
"@openzeppelin/contracts/=lib/ks-common-sc/lib/openzeppelin-contracts/contracts/",
"erc4626-tests/=lib/ks-common-sc/lib/openzeppelin-contracts/lib/erc4626-tests/",
"halmos-cheatcodes/=lib/ks-common-sc/lib/openzeppelin-contracts/lib/halmos-cheatcodes/src/"
]
}Source code and metadata: Sourcify.
The source code belongs to the authors of the contract.