编译器
0.8.24+commit.e11b9ed9
文件 1 的 19:Address.sol
pragma solidity ^0.8.0;
library Address {
function isContract(address account) internal view returns (bool) {
uint256 size;
assembly { size := extcodesize(account) }
return size > 0;
}
function sendValue(address payable recipient, uint256 amount) internal {
require(address(this).balance >= amount, "Address: insufficient balance");
(bool success, ) = recipient.call{ value: amount }("");
require(success, "Address: unable to send value, recipient may have reverted");
}
function functionCall(address target, bytes memory data) internal returns (bytes memory) {
return functionCall(target, data, "Address: low-level call failed");
}
function functionCall(address target, bytes memory data, string memory errorMessage) internal returns (bytes memory) {
return functionCallWithValue(target, data, 0, errorMessage);
}
function functionCallWithValue(address target, bytes memory data, uint256 value) internal returns (bytes memory) {
return functionCallWithValue(target, data, value, "Address: low-level call with value failed");
}
function functionCallWithValue(address target, bytes memory data, uint256 value, string memory errorMessage) internal returns (bytes memory) {
require(address(this).balance >= value, "Address: insufficient balance for call");
require(isContract(target), "Address: call to non-contract");
(bool success, bytes memory returndata) = target.call{ value: value }(data);
return _verifyCallResult(success, returndata, errorMessage);
}
function functionStaticCall(address target, bytes memory data) internal view returns (bytes memory) {
return functionStaticCall(target, data, "Address: low-level static call failed");
}
function functionStaticCall(address target, bytes memory data, string memory errorMessage) internal view returns (bytes memory) {
require(isContract(target), "Address: static call to non-contract");
(bool success, bytes memory returndata) = target.staticcall(data);
return _verifyCallResult(success, returndata, errorMessage);
}
function functionDelegateCall(address target, bytes memory data) internal returns (bytes memory) {
return functionDelegateCall(target, data, "Address: low-level delegate call failed");
}
function functionDelegateCall(address target, bytes memory data, string memory errorMessage) internal returns (bytes memory) {
require(isContract(target), "Address: delegate call to non-contract");
(bool success, bytes memory returndata) = target.delegatecall(data);
return _verifyCallResult(success, returndata, errorMessage);
}
function _verifyCallResult(bool success, bytes memory returndata, string memory errorMessage) private pure returns(bytes memory) {
if (success) {
return returndata;
} else {
if (returndata.length > 0) {
assembly {
let returndata_size := mload(returndata)
revert(add(32, returndata), returndata_size)
}
} else {
revert(errorMessage);
}
}
}
}
文件 2 的 19:CCTPFundManager.sol
pragma solidity ^0.8.24;
import "../common/signature/SigCheckable.sol";
import "../common/WithAdmin.sol";
import "../common/SafeAmount.sol";
import "../common/cctp/ICCTPTokenMessenger.sol";
contract CCTPFundManager is SigCheckable, WithAdmin {
using SafeERC20 for IERC20;
address public usdcToken;
address public cctpTokenMessenger;
address public fiberRouter;
uint32 constant WEEK = 3600 * 24 * 7;
string public constant NAME = "FUND_MANAGER";
string public constant VERSION = "000.004";
mapping(address => bool) public signers;
mapping(bytes32 => bool) public usedSalt;
mapping(uint256 => TargetNetwork) public targetNetworks;
bytes32 constant WITHDRAW_SIGNED_METHOD =
keccak256(
"WithdrawSigned(address token,address payee,uint256 amount,bytes32 salt,uint256 expiry)"
);
bytes32 constant WITHDRAW_SIGNED_WITH_SWAP_METHOD =
keccak256(
"withdrawSignedAndSwapRouter(address to,uint256 amountIn,uint256 minAmountOut,address foundryToken,address targetToken,address router,bytes32 salt,uint256 expiry)"
);
struct TargetNetwork {
uint32 targetNetworkDomain;
address targetCCTPFundManager;
}
event TransferBySignature(
address signer,
address receiver,
address token,
uint256 amount
);
modifier onlyRouter() {
require(msg.sender == fiberRouter, "FM: Only fiberRouter method");
_;
}
constructor() EIP712(NAME, VERSION) {
}
function setRouter(address _fiberRouter) external onlyOwner {
require(_fiberRouter != address(0), "FM: fiberRouter requried");
fiberRouter = _fiberRouter;
}
function addSigner(address _signer) public onlyOwner {
require(_signer != address(0), "Bad signer");
signers[_signer] = true;
}
function removeSigner(address _signer) external onlyOwner {
require(_signer != address(0), "Bad signer");
delete signers[_signer];
}
function initCCTP(
address _cctpTokenMessenger,
address _usdcToken
) external onlyOwner {
require(_cctpTokenMessenger != address(0), "FR: Invalid CCTP Token Messenger address");
require(_usdcToken != address(0), "FR: Invalid USDC Token address");
cctpTokenMessenger = _cctpTokenMessenger;
usdcToken = _usdcToken;
}
function setTargetCCTPNetwork(uint256 _chainID, uint32 _targetNetworkDomain, address _targetCCTPFundManager) external onlyOwner {
require(_targetNetworkDomain != 0, "FR: Invalid Target Network Domain");
require(_chainID != 0, "FR: Invalid Target Network ChainID");
require(_targetCCTPFundManager != address(0), "FR: Invalid Target CCTP Fund Manager address");
targetNetworks[_chainID] = TargetNetwork(_targetNetworkDomain, _targetCCTPFundManager);
}
function withdrawSigned(
address token,
address payee,
uint256 amount,
bytes32 salt,
uint256 expiry,
bytes memory signature
) external onlyRouter returns (uint256) {
require(token != address(0), "FM: bad token");
require(payee != address(0), "FM: bad payee");
require(salt != 0, "FM: bad salt");
require(amount != 0, "FM: bad amount");
require(block.timestamp < expiry, "FM: signature timed out");
require(expiry < block.timestamp + WEEK, "FM: expiry too far");
bytes32 message = keccak256(
abi.encode(WITHDRAW_SIGNED_METHOD, token, payee, amount, salt, expiry)
);
address _signer = signerUnique(message, signature);
require(signers[_signer], "FM: Invalid signer");
require(!usedSalt[salt], "FM: salt already used");
usedSalt[salt] = true;
IERC20(token).safeTransfer(payee, amount);
emit TransferBySignature(_signer, payee, token, amount);
return amount;
}
function withdrawSignedAndSwapRouter(
address to,
uint256 amountIn,
uint256 minAmountOut,
address foundryToken,
address targetToken,
address router,
bytes32 salt,
uint256 expiry,
bytes memory signature
) external onlyRouter returns (uint256) {
require(targetToken != address(0), "FM: bad token");
require(foundryToken != address(0), "FM: bad token");
require(to != address(0), "FM: bad payee");
require(salt != 0, "FM: bad salt");
require(amountIn != 0, "FM: bad amount");
require(minAmountOut != 0, "FM: bad amount");
require(block.timestamp < expiry, "FM: signature timed out");
require(expiry < block.timestamp + WEEK, "FM: expiry too far");
bytes32 message = keccak256(
abi.encode(
WITHDRAW_SIGNED_WITH_SWAP_METHOD,
to,
amountIn,
minAmountOut,
foundryToken,
targetToken,
router,
salt,
expiry
)
);
address _signer = signerUnique(message, signature);
require(signers[_signer], "FM: Invalid signer");
require(!usedSalt[salt], "FM: salt already used");
usedSalt[salt] = true;
IERC20(foundryToken).safeTransfer(msg.sender, amountIn);
emit TransferBySignature(_signer, msg.sender, foundryToken, amountIn);
return amountIn;
}
function withdrawSignedVerify(
address token,
address payee,
uint256 amount,
bytes32 salt,
uint256 expiry,
bytes calldata signature
) external view returns (bytes32, address) {
bytes32 message = keccak256(
abi.encode(WITHDRAW_SIGNED_METHOD, token, payee, amount, salt, expiry)
);
(bytes32 digest, address _signer) = signer(message, signature);
return (digest, _signer);
}
function withdrawSignedAndSwapRouterVerify(
address to,
uint256 amountIn,
uint256 minAmountOut,
address foundryToken,
address targetToken,
address router,
bytes32 salt,
uint256 expiry,
bytes calldata signature
) external view returns (bytes32, address) {
bytes32 message = keccak256(
abi.encode(
WITHDRAW_SIGNED_WITH_SWAP_METHOD,
to,
amountIn,
minAmountOut,
foundryToken,
targetToken,
router,
salt,
expiry
)
);
(bytes32 digest, address _signer) = signer(message, signature);
return (digest, _signer);
}
function swapCCTP(uint256 amountIn, address token, uint256 targetNetwork) external onlyRouter returns (uint64 depositNonce){
TargetNetwork memory target = targetNetworks[targetNetwork];
require(target.targetNetworkDomain != 0, "FR: Target network not found");
require(target.targetCCTPFundManager != address(0), "FR: Target CCTP FundManager address not found");
require(token == usdcToken, "FR: Invalid token");
require(IERC20(token).approve(cctpTokenMessenger, amountIn), "Approval failed");
depositNonce = ICCTPTokenMessenger(cctpTokenMessenger).depositForBurn(
amountIn,
target.targetNetworkDomain,
addressToBytes32(target.targetCCTPFundManager),
usdcToken
);
}
function addressToBytes32(address addr) internal pure returns (bytes32) {
return bytes32(uint256(uint160(addr)));
}
}
文件 3 的 19:Context.sol
pragma solidity ^0.8.0;
abstract contract Context {
function _msgSender() internal view virtual returns (address) {
return msg.sender;
}
function _msgData() internal view virtual returns (bytes calldata) {
this;
return msg.data;
}
}
文件 4 的 19:ECDSA.sol
pragma solidity ^0.8.0;
library ECDSA {
function recover(bytes32 hash, bytes memory signature) internal pure returns (address) {
bytes32 r;
bytes32 s;
uint8 v;
if (signature.length == 65) {
assembly {
r := mload(add(signature, 0x20))
s := mload(add(signature, 0x40))
v := byte(0, mload(add(signature, 0x60)))
}
} else if (signature.length == 64) {
assembly {
let vs := mload(add(signature, 0x40))
r := mload(add(signature, 0x20))
s := and(vs, 0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff)
v := add(shr(255, vs), 27)
}
} else {
revert("ECDSA: invalid signature length");
}
return recover(hash, v, r, s);
}
function recover(bytes32 hash, uint8 v, bytes32 r, bytes32 s) internal pure returns (address) {
require(uint256(s) <= 0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF5D576E7357A4501DDFE92F46681B20A0, "ECDSA: invalid signature 's' value");
require(v == 27 || v == 28, "ECDSA: invalid signature 'v' value");
address signer = ecrecover(hash, v, r, s);
require(signer != address(0), "ECDSA: invalid signature");
return signer;
}
function toEthSignedMessageHash(bytes32 hash) internal pure returns (bytes32) {
return keccak256(abi.encodePacked("\x19Ethereum Signed Message:\n32", hash));
}
function toTypedDataHash(bytes32 domainSeparator, bytes32 structHash) internal pure returns (bytes32) {
return keccak256(abi.encodePacked("\x19\x01", domainSeparator, structHash));
}
}
文件 5 的 19:FeeDistributor.sol
pragma solidity ^0.8.24;
import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import "@openzeppelin/contracts/utils/cryptography/ECDSA.sol";
import "@openzeppelin/contracts/utils/cryptography/draft-EIP712.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
contract FeeDistributor is EIP712, Ownable {
using SafeERC20 for IERC20;
using ECDSA for bytes32;
string public constant NAME = "FEE_DISTRIBUTOR";
string public constant VERSION = "000.001";
uint32 constant MINUTE = 60;
address public feeWallet;
uint256 public platformFee;
mapping(address => bool) public signers;
mapping(bytes32 => bool) public usedSalt;
bytes32 constant DISTRIBUTE_FEES_TYPEHASH = keccak256(
"DistributeFees(address token,address referral,uint256 referralFee,uint256 referralDiscount,uint256 sourceAmountIn,uint256 sourceAmountOut,uint256 destinationAmountIn,uint256 destinationAmountOut,bytes32 salt,uint256 expiry)"
);
struct FeeDistributionData {
address referral;
uint256 referralFee;
uint256 referralDiscount;
uint256 sourceAmountIn;
uint256 sourceAmountOut;
uint256 destinationAmountIn;
uint256 destinationAmountOut;
bytes32 salt;
uint256 expiry;
bytes signature;
}
event FeesDistributed(
address indexed token,
uint256 preFeeAmount,
uint256 afterFeeAmount,
uint256 totalPlatformFee
);
constructor() EIP712(NAME, VERSION) {}
function addSigner(address _signer) external onlyOwner {
require(_signer != address(0), "FD: Bad signer");
signers[_signer] = true;
}
function removeSigner(address _signer) external onlyOwner {
delete signers[_signer];
}
function setFeeWallet(address _feeWallet) external onlyOwner {
require(_feeWallet != address(0), "FD: Bad fee wallet address");
feeWallet = _feeWallet;
}
function setPlatformFee(uint256 _platformFee) external onlyOwner {
require(_platformFee > 0, "FD: Platform fee must be greater than zero");
platformFee = _platformFee;
}
function _distributeFees(
address token,
uint256 preFeeAmount,
FeeDistributionData memory fdd
) internal returns (uint256) {
require(_verify(token, fdd), "FD: Invalid signature");
uint256 totalAmount = preFeeAmount;
uint256 remainingAmount = totalAmount - platformFee;
uint256 referralDiscountAmount = 0;
uint256 referralFeeAmount = 0;
uint256 feeWalletShare = platformFee;
if (fdd.referral != address(0)) {
if (fdd.referralDiscount > 0) {
referralDiscountAmount = (platformFee * fdd.referralDiscount) / 100;
feeWalletShare -= referralDiscountAmount;
remainingAmount += referralDiscountAmount;
}
if (fdd.referralFee > 0) {
referralFeeAmount = (feeWalletShare * fdd.referralFee) / 100;
feeWalletShare -= referralFeeAmount;
IERC20(token).safeTransfer(fdd.referral, referralFeeAmount);
}
}
require(feeWalletShare + referralFeeAmount + referralDiscountAmount <= platformFee, "FD: Total fee exceeds platform fee");
IERC20(token).safeTransfer(feeWallet, feeWalletShare);
emit FeesDistributed(token, preFeeAmount, remainingAmount, platformFee);
return remainingAmount;
}
function _verify(
address token,
FeeDistributionData memory fdd
) private returns (bool) {
require(block.timestamp < fdd.expiry, "FD: Signature timed out");
require(fdd.expiry < block.timestamp + (20 * MINUTE), "FD: Expiry too far");
require(!usedSalt[fdd.salt], "FM: Salt already used");
usedSalt[fdd.salt] = true;
bytes32 structHash = keccak256(
abi.encode(
DISTRIBUTE_FEES_TYPEHASH,
token,
fdd.referral,
fdd.referralFee,
fdd.referralDiscount,
fdd.sourceAmountIn,
fdd.sourceAmountOut,
fdd.destinationAmountIn,
fdd.destinationAmountOut,
fdd.salt,
fdd.expiry
)
);
bytes32 digest = _hashTypedDataV4(structHash);
address signer = ECDSA.recover(digest, fdd.signature);
return signers[signer];
}
}
文件 6 的 19:FiberRouter.sol
pragma solidity ^0.8.24;
import "@openzeppelin/contracts/access/Ownable.sol";
import "../common/tokenReceiveable.sol";
import "../common/SafeAmount.sol";
import "../common/IWETH.sol";
import "./FundManager.sol";
import "./CCTPFundManager.sol";
import "./FeeDistributor.sol";
contract FiberRouter is Ownable, TokenReceivable, FeeDistributor {
using SafeERC20 for IERC20;
address private constant NATIVE_CURRENCY = 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE;
address public weth;
address public fundManager;
address public cctpFundManager;
address payable public gasWallet;
mapping(bytes32 => bool) private routerAllowList;
mapping(uint256 => TargetNetwork) public targetNetworks;
struct SwapCrossData {
uint256 targetNetwork;
address targetToken;
address targetAddress;
}
struct TargetNetwork {
uint32 targetNetworkDomain;
address targetFundManager;
}
event Swap(
address sourceToken,
address targetToken,
uint256 sourceChainId,
uint256 targetChainId,
uint256 sourceAmount,
address sourceAddress,
address targetAddress,
uint256 settledAmount,
bytes32 withdrawalData,
uint256 gasAmount,
uint256 depositNonce
);
event SwapSameNetwork(
address sourceToken,
address targetToken,
uint256 sourceAmount,
uint256 settledAmount,
address sourceAddress,
address targetAddress
);
event Withdraw(
address token,
address receiver,
uint256 amount,
bytes32 salt,
bytes signature
);
event WithdrawRouter(
address to,
uint256 amountIn,
uint256 amountOut,
address foundryToken,
address targetToken,
address router,
bytes routerCalldata,
bytes32 salt,
bytes multiSignature
);
event RouterAndSelectorWhitelisted(address router, bytes4 selector);
event RouterAndSelectorRemoved(address router, bytes selector);
function setWeth(address _weth) external onlyOwner {
require(_weth != address(0), "weth address cannot be zero");
weth = _weth;
}
function setFundManager(address _fundManager) external onlyOwner {
require(_fundManager != address(0), "Swap pool address cannot be zero");
fundManager = _fundManager;
}
function setCCTPFundManager(address _cctpFundManager) external onlyOwner {
require(_cctpFundManager != address(0), "Swap pool address cannot be zero");
cctpFundManager = _cctpFundManager;
}
function setGasWallet(address payable _gasWallet) external onlyOwner {
require(
_gasWallet != address(0),
"FR: Gas Wallet address cannot be zero"
);
gasWallet = _gasWallet;
}
function addRouterAndSelectors(address router, bytes4[] memory selectors) external onlyOwner {
for (uint256 i = 0; i < selectors.length; i++) {
routerAllowList[_getKey(router, abi.encodePacked(selectors[i]))] = true;
emit RouterAndSelectorWhitelisted(router, selectors[i]);
}
}
function removeRouterAndSelector(address router, bytes calldata selector) external onlyOwner {
routerAllowList[_getKey(router, selector)] = false;
emit RouterAndSelectorRemoved(router, selector);
}
function swapOnSameNetwork(
uint256 amountIn,
uint256 minAmountOut,
address fromToken,
address toToken,
address targetAddress,
address router,
bytes memory routerCalldata
) external nonReentrant {
require(fromToken != address(0), "FR: From token address cannot be zero");
require(toToken != address(0), "FR: To token address cannot be zero");
require(amountIn != 0, "FR: Amount in must be greater than zero");
require(minAmountOut != 0, "FR: Amount out must be greater than zero");
require(targetAddress != address(0), "FR: Target address cannot be zero");
amountIn = SafeAmount.safeTransferFrom(fromToken, _msgSender(), address(this), amountIn);
uint256 amountOut = _swapAndCheckSlippage(
targetAddress,
fromToken,
toToken,
amountIn,
minAmountOut,
router,
routerCalldata
);
emit SwapSameNetwork(
fromToken,
toToken,
amountIn,
amountOut,
_msgSender(),
targetAddress
);
}
function swapOnSameNetworkETH(
uint256 minAmountOut,
address toToken,
address targetAddress,
address router,
bytes memory routerCalldata
) external payable {
uint256 amountIn = msg.value;
require(toToken != address(0), "FR: To token address cannot be zero");
require(amountIn != 0, "FR: Amount in must be greater than zero");
require(minAmountOut != 0, "FR: Amount out must be greater than zero");
require(targetAddress != address(0), "FR: Target address cannot be zero");
require(bytes(routerCalldata).length != 0, "FR: Calldata cannot be empty");
IWETH(weth).deposit{value: amountIn}();
uint256 amountOut = _swapAndCheckSlippage(
targetAddress,
weth,
toToken,
amountIn,
minAmountOut,
router,
routerCalldata
);
emit SwapSameNetwork(
NATIVE_CURRENCY,
toToken,
amountIn,
amountOut,
_msgSender(),
targetAddress
);
}
function swapSigned(
address token,
uint256 amount,
SwapCrossData memory sd,
bytes32 withdrawalData,
bool cctpType,
FeeDistributionData memory fd
) external payable nonReentrant {
require(token != address(0), "FR: Token address cannot be zero");
require(sd.targetToken != address(0), "FR: Target token address cannot be zero");
require(sd.targetNetwork != 0, "FR: Target network is required");
require(sd.targetAddress != address(0), "FR: Target address cannot be zero");
require(amount != 0, "FR: Amount must be greater than zero");
require(withdrawalData != 0, "FR: Withdraw data cannot be empty");
require(msg.value != 0, "FR: Gas Amount must be greater than zero");
amount = SafeAmount.safeTransferFrom(token, _msgSender(), address(this), amount);
uint256 amountOut = _distributeFees(token, amount, fd);
uint64 depositNonce;
if (cctpType) {
SafeERC20.safeTransfer(IERC20(token), cctpFundManager, amountOut);
depositNonce = CCTPFundManager(cctpFundManager).swapCCTP(amountOut, token, sd.targetNetwork);
} else {
SafeERC20.safeTransfer(IERC20(token), fundManager, amountOut);
FundManager(fundManager).swapToAddress(
token,
amount,
sd.targetNetwork,
sd.targetAddress
);
}
SafeAmount.safeTransferETH(gasWallet, msg.value);
emit Swap(
token,
sd.targetToken,
block.chainid,
sd.targetNetwork,
amount,
_msgSender(),
sd.targetAddress,
amountOut,
withdrawalData,
msg.value,
depositNonce
);
}
function swapSignedAndCrossRouter(
uint256 amountIn,
uint256 minAmountOut,
address fromToken,
address foundryToken,
address router,
bytes memory routerCalldata,
SwapCrossData memory sd,
bytes32 withdrawalData,
bool cctpType,
FeeDistributionData memory fd
) external payable nonReentrant {
require(amountIn != 0, "FR: Amount in must be greater than zero");
require(fromToken != address(0), "FR: From token address cannot be zero");
require(foundryToken != address(0), "FR: Foundry token address cannot be zero");
require(sd.targetToken != address(0), "FR: Cross target token address cannot be zero");
require(minAmountOut != 0, "FR: Amount out must be greater than zero");
require(withdrawalData != 0, "FR: withdraw data cannot be empty");
require(msg.value != 0, "FR: Gas Amount must be greater than zero");
uint256 _amountIn = SafeAmount.safeTransferFrom(fromToken, _msgSender(), address(this), amountIn);
uint256 amountOut = _swapAndCheckSlippage(
address(this),
fromToken,
foundryToken,
_amountIn,
minAmountOut,
router,
routerCalldata
);
amountOut = _distributeFees(foundryToken, amountOut, fd);
uint64 depositNonce;
if (cctpType) {
SafeERC20.safeTransfer(IERC20(foundryToken), cctpFundManager, amountOut);
depositNonce = CCTPFundManager(cctpFundManager).swapCCTP(amountOut, foundryToken, sd.targetNetwork);
} else {
SafeERC20.safeTransfer(IERC20(foundryToken), fundManager, amountOut);
FundManager(fundManager).swapToAddress(
foundryToken,
amountOut,
sd.targetNetwork,
sd.targetAddress
);
}
SafeAmount.safeTransferETH(gasWallet, msg.value);
emit Swap(
fromToken,
sd.targetToken,
block.chainid,
sd.targetNetwork,
_amountIn,
_msgSender(),
sd.targetAddress,
amountOut,
withdrawalData,
msg.value,
depositNonce
);
}
function swapSignedAndCrossRouterETH(
uint256 minAmountOut,
address foundryToken,
uint256 gasFee,
address router,
bytes memory routerCalldata,
SwapCrossData memory sd,
bytes32 withdrawalData,
bool cctpType,
FeeDistributionData memory fd
) external payable {
require(msg.value - gasFee != 0, "FR: Amount in must be greater than zero");
require(gasFee != 0, "FR: Gas fee must be greater than zero");
require(minAmountOut != 0, "FR: Amount out must be greater than zero");
require(sd.targetToken != address(0), "FR: Cross target token address cannot be zero");
require(foundryToken != address(0), "FR: Foundry token address cannot be zero");
require(withdrawalData != 0, "FR: Withdraw data cannot be empty");
IWETH(weth).deposit{value: msg.value - gasFee}();
uint256 amountOut = _swapAndCheckSlippage(
address(this),
weth,
foundryToken,
msg.value - gasFee,
minAmountOut,
router,
routerCalldata
);
amountOut = _distributeFees(foundryToken, amountOut, fd);
uint64 depositNonce;
if (cctpType) {
SafeERC20.safeTransfer(IERC20(foundryToken), cctpFundManager, amountOut);
depositNonce = CCTPFundManager(cctpFundManager).swapCCTP(amountOut, foundryToken, sd.targetNetwork);
} else {
SafeERC20.safeTransfer(IERC20(foundryToken), fundManager, amountOut);
FundManager(fundManager).swapToAddress(
foundryToken,
amountOut,
sd.targetNetwork,
sd.targetAddress
);
}
(bool success, ) = payable(gasWallet).call{value: gasFee}("");
require(success, "FR: Gas fee transfer failed");
uint256 _gasFee = gasFee;
emit Swap(
weth,
sd.targetToken,
block.chainid,
sd.targetNetwork,
msg.value - _gasFee,
_msgSender(),
sd.targetAddress,
amountOut,
withdrawalData,
_gasFee,
depositNonce
);
}
function withdrawSigned(
address token,
address payee,
uint256 amount,
bytes32 salt,
uint256 expiry,
bytes memory multiSignature,
bool cctpType
) public virtual nonReentrant {
require(token != address(0), "FR: Token address cannot be zero");
require(payee != address(0), "FR: Payee address cannot be zero");
require(amount != 0, "FR: Amount must be greater than zero");
require(salt > bytes32(0), "FR: Salt must be greater than zero bytes");
address _pool = cctpType ? cctpFundManager : fundManager;
amount = FundManager(_pool).withdrawSigned(
token,
payee,
amount,
salt,
expiry,
multiSignature
);
emit Withdraw(token, payee, amount, salt, multiSignature);
}
function withdrawSignedAndSwapRouter(
address payable to,
uint256 amountIn,
uint256 minAmountOut,
address foundryToken,
address targetToken,
address router,
bytes memory routerCalldata,
bytes32 salt,
uint256 expiry,
bytes memory multiSignature,
bool cctpType
) public virtual nonReentrant {
require(foundryToken != address(0), "Bad Token Address");
require(targetToken != address(0), "FR: Target token address cannot be zero");
require(amountIn != 0, "Amount in must be greater than zero");
require(minAmountOut != 0, "Amount out minimum must be greater than zero");
require(foundryToken != address(0), "Bad Token Address");
address _pool = cctpType ? cctpFundManager : fundManager;
amountIn = FundManager(_pool).withdrawSignedAndSwapRouter(
to,
amountIn,
minAmountOut,
foundryToken,
targetToken,
router,
salt,
expiry,
multiSignature
);
uint256 amountOut = _swapAndCheckSlippage(
to,
foundryToken,
targetToken,
amountIn,
minAmountOut,
router,
routerCalldata
);
emit WithdrawRouter(
to,
amountIn,
amountOut,
foundryToken,
targetToken,
router,
routerCalldata,
salt,
multiSignature
);
}
function isAllowListed(address router, bytes memory selector) public view returns (bool) {
return routerAllowList[_getKey(router, selector)];
}
function _swapAndCheckSlippage(
address targetAddress,
address fromToken,
address toToken,
uint256 amountIn,
uint256 minAmountOut,
address router,
bytes memory data
) internal returns (uint256) {
require(isAllowListed(router, data), "FR: Router and selector not whitelisted");
_approveAggregatorRouter(fromToken, router, amountIn);
uint256 balanceBefore = _getBalance(toToken, targetAddress);
_makeRouterCall(router, data);
uint256 amountOut = _getBalance(toToken, targetAddress) - balanceBefore;
require(amountOut >= minAmountOut, "FR: Slippage check failed");
return amountOut;
}
function _getBalance(address token, address account) private view returns (uint256) {
return token == NATIVE_CURRENCY ? account.balance : IERC20(token).balanceOf(account);
}
function _approveAggregatorRouter(address token, address router, uint256 amount) private {
if (IERC20(token).allowance(address(this), router) != 0) {
IERC20(token).safeApprove(router, 0);
}
IERC20(token).safeApprove(router, amount);
}
function _getKey(address router, bytes memory data) private pure returns (bytes32) {
bytes32 key;
assembly {
key := or(
and(mload(add(data, 0x20)), 0xffffffff00000000000000000000000000000000000000000000000000000000),
router
)
}
return key;
}
function _makeRouterCall(address router, bytes memory data) private {
(bool success, bytes memory returnData) = router.call(data);
if (!success) {
if (returnData.length > 0) {
assembly {
let returnDataSize := mload(returnData)
revert(add(32, returnData), returnDataSize)
}
} else {
revert("FR: Call to router failed");
}
}
}
function isCctp(uint256 cdPtr) public pure returns (bool cctp) {
assembly {
cctp := shr(252, calldataload(cdPtr))
}
}
}
文件 7 的 19:FundManager.sol
pragma solidity ^0.8.24;
import "../common/signature/SigCheckable.sol";
import "./LiquidityManagerRole.sol";
import "@openzeppelin/contracts/security/ReentrancyGuard.sol";
contract FundManager is SigCheckable, LiquidityManagerRole {
using SafeERC20 for IERC20;
address public fiberRouter;
address public settlementManager;
uint32 constant WEEK = 3600 * 24 * 7;
string public constant NAME = "FUND_MANAGER";
string public constant VERSION = "000.004";
bytes32 constant WITHDRAW_SIGNED_METHOD =
keccak256(
"WithdrawSigned(address token,address payee,uint256 amount,bytes32 salt,uint256 expiry)"
);
bytes32 constant WITHDRAW_SIGNED_WITH_SWAP_METHOD =
keccak256(
"withdrawSignedAndSwapRouter(address to,uint256 amountIn,uint256 minAmountOut,address foundryToken,address targetToken,address router,bytes32 salt,uint256 expiry)"
);
event TransferBySignature(
address signer,
address receiver,
address token,
uint256 amount
);
event FailedWithdrawalCancelled(
address indexed settlementManager,
address indexed receiver,
address indexed token,
uint256 amount,
bytes32 salt
);
event BridgeLiquidityAdded(address actor, address token, uint256 amount);
event BridgeLiquidityRemoved(address actor, address token, uint256 amount);
event BridgeSwap(
address from,
address indexed token,
uint256 targetNetwork,
address targetToken,
address targetAddrdess,
uint256 amount
);
mapping(address => bool) public signers;
mapping(address => mapping(address => uint256)) private liquidities;
mapping(address => mapping(uint256 => address)) public allowedTargets;
mapping(address => bool) public isFoundryAsset;
mapping(bytes32=>bool) public usedSalt;
modifier onlyRouter() {
require(msg.sender == fiberRouter, "FM: Only fiberRouter method");
_;
}
modifier onlySettlementManager() {
require(msg.sender == settlementManager, "FM: Only Settlement Manager");
_;
}
constructor() EIP712(NAME, VERSION) {}
function setSettlementManager(address _settlementManager) external onlyOwner {
require(_settlementManager != address(0), "FM: Bad settlement manager");
settlementManager = _settlementManager;
}
function setRouter(address _fiberRouter) external onlyOwner {
require(_fiberRouter != address(0), "FM: fiberRouter required");
fiberRouter = _fiberRouter;
}
function addSigner(address _signer) public onlyOwner {
require(_signer != address(0), "Bad signer");
signers[_signer] = true;
}
function removeSigner(address _signer) external onlyOwner {
require(_signer != address(0), "Bad signer");
delete signers[_signer];
}
function allowTarget(
address token,
uint256 chainId,
address targetToken
) external onlyAdmin {
require(token != address(0), "Bad token");
require(targetToken != address(0), "Bad targetToken");
require(chainId != 0, "Bad chainId");
allowedTargets[token][chainId] = targetToken;
}
function disallowTarget(address token, uint256 chainId) external onlyAdmin {
require(token != address(0), "Bad token");
require(chainId != 0, "Bad chainId");
delete allowedTargets[token][chainId];
}
function addFoundryAsset(address token) external onlyAdmin {
require(token != address(0), "Bad token");
isFoundryAsset[token] = true;
}
function removeFoundryAsset(address token) external onlyAdmin {
require(token != address(0), "Bad token");
isFoundryAsset[token] = false;
}
function swapToAddress(
address token,
uint256 amount,
uint256 targetNetwork,
address targetAddress
) external onlyRouter returns(uint256) {
address targetToken = allowedTargets[token][targetNetwork];
require(token != address(0), "FM: bad token");
require(targetNetwork != 0, "FM: targetNetwork is requried");
require(targetToken != address(0), "FM: bad target token");
require(targetAddress != address(0), "FM: targetAddress is required");
require(amount != 0, "FM: bad amount");
amount = TokenReceivable.sync(token);
emit BridgeSwap(
msg.sender,
token,
targetNetwork,
targetToken,
targetAddress,
amount
);
return amount;
}
function withdrawSigned(
address token,
address payee,
uint256 amount,
bytes32 salt,
uint256 expiry,
bytes memory signature
) external onlyRouter returns (uint256) {
require(token != address(0), "FM: bad token");
require(payee != address(0), "FM: bad payee");
require(salt != 0, "FM: bad salt");
require(amount != 0, "FM: bad amount");
require(block.timestamp < expiry, "FM: signature timed out");
require(expiry < block.timestamp + WEEK, "FM: expiry too far");
bytes32 message = keccak256(abi.encode(WITHDRAW_SIGNED_METHOD, token, payee, amount, salt, expiry));
address _signer = signerUnique(message, signature);
require(signers[_signer], "FM: Invalid signer");
require(!usedSalt[salt], "FM: salt already used");
usedSalt[salt] = true;
TokenReceivable.sendToken(token, payee, amount);
emit TransferBySignature(_signer, payee, token, amount);
return amount;
}
function withdrawSignedAndSwapRouter(
address to,
uint256 amountIn,
uint256 minAmountOut,
address foundryToken,
address targetToken,
address router,
bytes32 salt,
uint256 expiry,
bytes memory signature
) external onlyRouter returns (uint256) {
require(targetToken != address(0), "FM: bad token");
require(foundryToken != address(0), "FM: bad token");
require(to != address(0), "FM: bad payee");
require(salt != 0, "FM: bad salt");
require(amountIn != 0, "FM: bad amount");
require(minAmountOut != 0, "FM: bad amount");
require(block.timestamp < expiry, "FM: signature timed out");
require(expiry < block.timestamp + WEEK, "FM: expiry too far");
bytes32 message = keccak256(
abi.encode(
WITHDRAW_SIGNED_WITH_SWAP_METHOD,
to,
amountIn,
minAmountOut,
foundryToken,
targetToken,
router,
salt,
expiry
)
);
address _signer = signerUnique(message, signature);
require(signers[_signer], "FM: Invalid signer");
require(!usedSalt[salt], "FM: salt already used");
usedSalt[salt] = true;
TokenReceivable.sendToken(foundryToken, msg.sender, amountIn);
emit TransferBySignature(_signer, msg.sender, foundryToken, amountIn);
return amountIn;
}
function withdrawSignedVerify(
address token,
address payee,
uint256 amount,
bytes32 salt,
uint256 expiry,
bytes calldata signature
) external view returns (bytes32, address) {
bytes32 message = keccak256(
abi.encode(WITHDRAW_SIGNED_METHOD, token, payee, amount, salt, expiry)
);
(bytes32 digest, address _signer) = signer(message, signature);
return (digest, _signer);
}
function withdrawRouter(address token, uint256 amount, address recipient) external onlyRouter {
IERC20(token).transfer(recipient, amount);
}
function withdrawSignedAndSwapRouterVerify(
address to,
uint256 amountIn,
uint256 minAmountOut,
address foundryToken,
address targetToken,
address router,
bytes32 salt,
uint256 expiry,
bytes calldata signature
) external view returns (bytes32, address) {
bytes32 message = keccak256(
abi.encode(
WITHDRAW_SIGNED_WITH_SWAP_METHOD,
to,
amountIn,
minAmountOut,
foundryToken,
targetToken,
router,
salt,
expiry
)
);
(bytes32 digest, address _signer) = signer(message, signature);
return (digest, _signer);
}
function cancelFailedWithdrawSigned(
address token,
address payee,
uint256 amount,
bytes32 salt,
uint256 expiry,
bytes memory signature
) external onlySettlementManager {
require(token != address(0), "FM: bad token");
require(payee != address(0), "FM: bad payee");
require(salt != 0, "FM: bad salt");
require(amount != 0, "FM: bad amount");
require(block.timestamp < expiry, "FM: signature timed out");
require(expiry < block.timestamp + WEEK, "FM: expiry too far");
bytes32 message = keccak256(
abi.encode(WITHDRAW_SIGNED_METHOD, token, payee, amount, salt, expiry)
);
address _signer = signerUnique(message, signature);
require(signers[_signer], "FM: Invalid signer");
require(!usedSalt[salt], "FM: salt already used");
usedSalt[salt] = true;
emit FailedWithdrawalCancelled(settlementManager, payee, token, amount, salt);
}
function cancelFailedwithdrawSignedAndSwapRouter(
address to,
uint256 amountIn,
uint256 minAmountOut,
address foundryToken,
address targetToken,
address router,
bytes32 salt,
uint256 expiry,
bytes memory signature
) external onlySettlementManager {
require(targetToken != address(0), "FM: bad token");
require(foundryToken != address(0), "FM: bad token");
require(to != address(0), "FM: bad payee");
require(salt != 0, "FM: bad salt");
require(amountIn != 0, "FM: bad amount");
require(minAmountOut != 0, "FM: bad amount");
require(block.timestamp < expiry, "FM: signature timed out");
require(expiry < block.timestamp + WEEK, "FM: expiry too far");
bytes32 message = keccak256(
abi.encode(
WITHDRAW_SIGNED_WITH_SWAP_METHOD,
to,
amountIn,
minAmountOut,
foundryToken,
targetToken,
router,
salt,
expiry
)
);
address _signer = signerUnique(message, signature);
require(signers[_signer], "FM: Invalid signer");
require(!usedSalt[salt], "FM: salt already used");
usedSalt[salt] = true;
emit FailedWithdrawalCancelled(settlementManager, to, targetToken, amountIn, salt);
}
function addLiquidity(address token, uint256 amount) external {
require(amount != 0, "FM: Amount must be positive");
require(token != address(0), "FM: Bad token");
require(
isFoundryAsset[token] == true,
"FM: Only foundry assets can be added"
);
liquidities[token][msg.sender] += amount;
amount = SafeAmount.safeTransferFrom(
token,
msg.sender,
address(this),
amount
);
amount = TokenReceivable.sync(token);
emit BridgeLiquidityAdded(msg.sender, token, amount);
}
function removeLiquidityIfPossible(address token, uint256 amount)
external
returns (uint256)
{
require(amount != 0, "FM: Amount must be positive");
require(token != address(0), "FM: Bad token");
require(
isFoundryAsset[token] == true,
"FM: Only foundry assets can be removed"
);
uint256 liq = liquidities[token][msg.sender];
require(liq >= amount, "FM: Not enough liquidity");
uint256 balance = IERC20(token).balanceOf(address(this));
uint256 actualLiq = balance > amount ? amount : balance;
if (actualLiq != 0) {
liquidities[token][msg.sender] -= actualLiq;
TokenReceivable.sendToken(token, msg.sender, actualLiq);
emit BridgeLiquidityRemoved(msg.sender, token, amount);
}
return actualLiq;
}
function liquidity(address token, address liquidityAdder)
external
view
returns (uint256)
{
return liquidities[token][liquidityAdder];
}
}
文件 8 的 19:ICCTPTokenMessenger.sol
pragma solidity ^0.8.24;
interface ICCTPTokenMessenger {
function depositForBurn(
uint256 amount,
uint32 destinationDomain,
bytes32 mintRecipient,
address burnToken
) external returns (uint64 nonce);
function depositForBurnWithCaller(
uint256 amount,
uint32 destinationDomain,
bytes32 mintRecipient,
address burnToken,
bytes32 destinationCaller
) external returns (uint64 nonce);
function replaceDepositForBurn(
bytes calldata originalMessage,
bytes calldata originalAttestation,
bytes32 newDestinationCaller,
bytes32 newMintRecipient
) external;
function handleReceiveMessage(
uint32 remoteDomain,
bytes32 sender,
bytes calldata messageBody
) external returns (bool);
function addRemoteTokenMessenger(uint32 domain, bytes32 tokenMessenger) external;
function removeRemoteTokenMessenger(uint32 domain) external;
function addLocalMinter(address newLocalMinter) external;
function removeLocalMinter() external;
function localMessageTransmitter() external view returns (address);
function messageBodyVersion() external view returns (uint32);
function localMinter() external view returns (address);
function remoteTokenMessengers(uint32 domain) external view returns (bytes32);
}
文件 9 的 19:IERC20.sol
pragma solidity ^0.8.0;
interface IERC20 {
function totalSupply() external view returns (uint256);
function balanceOf(address account) external view returns (uint256);
function transfer(address recipient, uint256 amount) external returns (bool);
function allowance(address owner, address spender) external view returns (uint256);
function approve(address spender, uint256 amount) external returns (bool);
function transferFrom(address sender, address recipient, uint256 amount) external returns (bool);
event Transfer(address indexed from, address indexed to, uint256 value);
event Approval(address indexed owner, address indexed spender, uint256 value);
}
文件 10 的 19:IWETH.sol
pragma solidity ^0.8.0;
interface IWETH {
function deposit() external payable;
function transfer(address to, uint value) external returns (bool);
function withdraw(uint) external;
function approve(address guy, uint wad) external returns (bool);
}
文件 11 的 19:LiquidityManagerRole.sol
pragma solidity ^0.8.0;
import "../common/WithAdmin.sol";
import "../common/SafeAmount.sol";
import "../common/tokenReceiveable.sol";
abstract contract LiquidityManagerRole is WithAdmin, TokenReceivable {
using SafeERC20 for IERC20;
address public liquidityManager;
address public liquidityManagerBot;
address public withdrawalAddress;
event LiquidityAddedByManager(address token, uint256 amount);
event LiquidityRemovedByManager(address token, uint256 amount, address withdrawalAddress);
modifier onlyLiquidityManager() {
require(
msg.sender == liquidityManager || msg.sender == liquidityManagerBot,
"FM: Only liquidity managers"
);
_;
}
function setLiquidityManagers(address _liquidityManager, address _liquidityManagerBot) external onlyOwner {
require(_liquidityManager != address(0), "FM: Bad liquidity manager");
require(_liquidityManagerBot != address(0), "FM: Bad liquidity manager bot");
liquidityManager = _liquidityManager;
liquidityManagerBot = _liquidityManagerBot;
}
function setWithdrawalAddress(address _withdrawalAddress) external onlyOwner {
withdrawalAddress = _withdrawalAddress;
}
function addLiquidityByManager(address token, uint256 amount) external onlyLiquidityManager {
require(amount != 0, "FM: Amount must be positive");
require(token != address(0), "FM: Bad token");
SafeAmount.safeTransferFrom(token, msg.sender, address(this), amount);
amount = TokenReceivable.sync(token);
emit LiquidityAddedByManager(token, amount);
}
function removeLiquidityByManager(address token, uint256 amount) external onlyLiquidityManager returns (uint256) {
require(amount != 0, "FM: Amount must be positive");
require(token != address(0), "FM: Bad token");
require(IERC20(token).balanceOf(address(this)) >= amount, "FM: Insufficient balance");
TokenReceivable.sendToken(token, withdrawalAddress, amount);
emit LiquidityRemovedByManager(token, amount, withdrawalAddress);
return amount;
}
}
文件 12 的 19:Ownable.sol
pragma solidity ^0.8.0;
import "../utils/Context.sol";
abstract contract Ownable is Context {
address private _owner;
event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);
constructor () {
address msgSender = _msgSender();
_owner = msgSender;
emit OwnershipTransferred(address(0), msgSender);
}
function owner() public view virtual returns (address) {
return _owner;
}
modifier onlyOwner() {
require(owner() == _msgSender(), "Ownable: caller is not the owner");
_;
}
function renounceOwnership() public virtual onlyOwner {
emit OwnershipTransferred(_owner, address(0));
_owner = address(0);
}
function transferOwnership(address newOwner) public virtual onlyOwner {
require(newOwner != address(0), "Ownable: new owner is the zero address");
emit OwnershipTransferred(_owner, newOwner);
_owner = newOwner;
}
}
文件 13 的 19:ReentrancyGuard.sol
pragma solidity ^0.8.0;
abstract contract ReentrancyGuard {
uint256 private constant _NOT_ENTERED = 1;
uint256 private constant _ENTERED = 2;
uint256 private _status;
constructor () {
_status = _NOT_ENTERED;
}
modifier nonReentrant() {
require(_status != _ENTERED, "ReentrancyGuard: reentrant call");
_status = _ENTERED;
_;
_status = _NOT_ENTERED;
}
}
文件 14 的 19:SafeAmount.sol
pragma solidity ^0.8.24;
import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
library SafeAmount {
using SafeERC20 for IERC20;
function safeTransferFrom(
address token,
address from,
address to,
uint256 amount) internal returns (uint256 result) {
uint256 preBalance = IERC20(token).balanceOf(to);
IERC20(token).safeTransferFrom(from, to, amount);
uint256 postBalance = IERC20(token).balanceOf(to);
result = postBalance - preBalance;
require(result <= amount, "SA: actual amount larger than transfer amount");
}
function safeTransferETH(address to, uint256 value) internal {
(bool success, ) = to.call{value: value}(new bytes(0));
require(success, 'TransferHelper::safeTransferETH: ETH transfer failed');
}
}
文件 15 的 19:SafeERC20.sol
pragma solidity ^0.8.0;
import "../IERC20.sol";
import "../../../utils/Address.sol";
library SafeERC20 {
using Address for address;
function safeTransfer(IERC20 token, address to, uint256 value) internal {
_callOptionalReturn(token, abi.encodeWithSelector(token.transfer.selector, to, value));
}
function safeTransferFrom(IERC20 token, address from, address to, uint256 value) internal {
_callOptionalReturn(token, abi.encodeWithSelector(token.transferFrom.selector, from, to, value));
}
function safeApprove(IERC20 token, address spender, uint256 value) internal {
require((value == 0) || (token.allowance(address(this), spender) == 0),
"SafeERC20: approve from non-zero to non-zero allowance"
);
_callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, value));
}
function safeIncreaseAllowance(IERC20 token, address spender, uint256 value) internal {
uint256 newAllowance = token.allowance(address(this), spender) + value;
_callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, newAllowance));
}
function safeDecreaseAllowance(IERC20 token, address spender, uint256 value) internal {
unchecked {
uint256 oldAllowance = token.allowance(address(this), spender);
require(oldAllowance >= value, "SafeERC20: decreased allowance below zero");
uint256 newAllowance = oldAllowance - value;
_callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, newAllowance));
}
}
function _callOptionalReturn(IERC20 token, bytes memory data) private {
bytes memory returndata = address(token).functionCall(data, "SafeERC20: low-level call failed");
if (returndata.length > 0) {
require(abi.decode(returndata, (bool)), "SafeERC20: ERC20 operation did not succeed");
}
}
}
文件 16 的 19:SigCheckable.sol
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/utils/cryptography/draft-EIP712.sol";
import "@openzeppelin/contracts/utils/cryptography/ECDSA.sol";
abstract contract SigCheckable is EIP712 {
function signerUnique(
bytes32 message,
bytes memory signature) internal view returns (address _signer) {
bytes32 digest;
(digest, _signer) = signer(message, signature);
}
function signer(
bytes32 message,
bytes memory signature) internal view returns (bytes32 digest, address _signer) {
digest = _hashTypedDataV4(message);
_signer = ECDSA.recover(digest, signature);
}
}
文件 17 的 19:WithAdmin.sol
pragma solidity ^0.8.24;
import "@openzeppelin/contracts/access/Ownable.sol";
contract WithAdmin is Ownable {
address public admin;
event AdminSet(address admin);
function setAdmin(address _admin) external onlyOwner {
admin = _admin;
emit AdminSet(_admin);
}
modifier onlyAdmin() {
require(msg.sender == admin || msg.sender == owner(), "WA: not admin");
_;
}
}
文件 18 的 19:draft-EIP712.sol
pragma solidity ^0.8.0;
import "./ECDSA.sol";
abstract contract EIP712 {
bytes32 private immutable _CACHED_DOMAIN_SEPARATOR;
uint256 private immutable _CACHED_CHAIN_ID;
bytes32 private immutable _HASHED_NAME;
bytes32 private immutable _HASHED_VERSION;
bytes32 private immutable _TYPE_HASH;
constructor(string memory name, string memory version) {
bytes32 hashedName = keccak256(bytes(name));
bytes32 hashedVersion = keccak256(bytes(version));
bytes32 typeHash = keccak256("EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)");
_HASHED_NAME = hashedName;
_HASHED_VERSION = hashedVersion;
_CACHED_CHAIN_ID = block.chainid;
_CACHED_DOMAIN_SEPARATOR = _buildDomainSeparator(typeHash, hashedName, hashedVersion);
_TYPE_HASH = typeHash;
}
function _domainSeparatorV4() internal view returns (bytes32) {
if (block.chainid == _CACHED_CHAIN_ID) {
return _CACHED_DOMAIN_SEPARATOR;
} else {
return _buildDomainSeparator(_TYPE_HASH, _HASHED_NAME, _HASHED_VERSION);
}
}
function _buildDomainSeparator(bytes32 typeHash, bytes32 name, bytes32 version) private view returns (bytes32) {
return keccak256(
abi.encode(
typeHash,
name,
version,
block.chainid,
address(this)
)
);
}
function _hashTypedDataV4(bytes32 structHash) internal view virtual returns (bytes32) {
return ECDSA.toTypedDataHash(_domainSeparatorV4(), structHash);
}
}
文件 19 的 19:tokenReceiveable.sol
pragma solidity ^0.8.24;
import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import "@openzeppelin/contracts/security/ReentrancyGuard.sol";
abstract contract TokenReceivable is ReentrancyGuard {
using SafeERC20 for IERC20;
mapping(address => uint256) public inventory;
function sync(address token) internal nonReentrant returns (uint256 amount) {
uint256 inv = inventory[token];
uint256 balance = IERC20(token).balanceOf(address(this));
amount = balance - inv;
inventory[token] = balance;
}
function sendToken(address token, address payee, uint256 amount) internal nonReentrant {
inventory[token] = inventory[token] - amount;
IERC20(token).safeTransfer(payee, amount);
}
}
{
"compilationTarget": {
"contracts/multiswap-contracts/FiberRouter.sol": "FiberRouter"
},
"evmVersion": "paris",
"libraries": {},
"metadata": {
"bytecodeHash": "ipfs"
},
"optimizer": {
"enabled": true,
"runs": 200
},
"remappings": []
}
[{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"token","type":"address"},{"indexed":false,"internalType":"uint256","name":"preFeeAmount","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"afterFeeAmount","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"totalPlatformFee","type":"uint256"}],"name":"FeesDistributed","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"previousOwner","type":"address"},{"indexed":true,"internalType":"address","name":"newOwner","type":"address"}],"name":"OwnershipTransferred","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address","name":"router","type":"address"},{"indexed":false,"internalType":"bytes","name":"selector","type":"bytes"}],"name":"RouterAndSelectorRemoved","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address","name":"router","type":"address"},{"indexed":false,"internalType":"bytes4","name":"selector","type":"bytes4"}],"name":"RouterAndSelectorWhitelisted","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address","name":"sourceToken","type":"address"},{"indexed":false,"internalType":"address","name":"targetToken","type":"address"},{"indexed":false,"internalType":"uint256","name":"sourceChainId","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"targetChainId","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"sourceAmount","type":"uint256"},{"indexed":false,"internalType":"address","name":"sourceAddress","type":"address"},{"indexed":false,"internalType":"address","name":"targetAddress","type":"address"},{"indexed":false,"internalType":"uint256","name":"settledAmount","type":"uint256"},{"indexed":false,"internalType":"bytes32","name":"withdrawalData","type":"bytes32"},{"indexed":false,"internalType":"uint256","name":"gasAmount","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"depositNonce","type":"uint256"}],"name":"Swap","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address","name":"sourceToken","type":"address"},{"indexed":false,"internalType":"address","name":"targetToken","type":"address"},{"indexed":false,"internalType":"uint256","name":"sourceAmount","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"settledAmount","type":"uint256"},{"indexed":false,"internalType":"address","name":"sourceAddress","type":"address"},{"indexed":false,"internalType":"address","name":"targetAddress","type":"address"}],"name":"SwapSameNetwork","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address","name":"token","type":"address"},{"indexed":false,"internalType":"address","name":"receiver","type":"address"},{"indexed":false,"internalType":"uint256","name":"amount","type":"uint256"},{"indexed":false,"internalType":"bytes32","name":"salt","type":"bytes32"},{"indexed":false,"internalType":"bytes","name":"signature","type":"bytes"}],"name":"Withdraw","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address","name":"to","type":"address"},{"indexed":false,"internalType":"uint256","name":"amountIn","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"amountOut","type":"uint256"},{"indexed":false,"internalType":"address","name":"foundryToken","type":"address"},{"indexed":false,"internalType":"address","name":"targetToken","type":"address"},{"indexed":false,"internalType":"address","name":"router","type":"address"},{"indexed":false,"internalType":"bytes","name":"routerCalldata","type":"bytes"},{"indexed":false,"internalType":"bytes32","name":"salt","type":"bytes32"},{"indexed":false,"internalType":"bytes","name":"multiSignature","type":"bytes"}],"name":"WithdrawRouter","type":"event"},{"inputs":[],"name":"NAME","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"VERSION","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"router","type":"address"},{"internalType":"bytes4[]","name":"selectors","type":"bytes4[]"}],"name":"addRouterAndSelectors","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_signer","type":"address"}],"name":"addSigner","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"cctpFundManager","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"feeWallet","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"fundManager","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"gasWallet","outputs":[{"internalType":"address payable","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"}],"name":"inventory","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"router","type":"address"},{"internalType":"bytes","name":"selector","type":"bytes"}],"name":"isAllowListed","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"cdPtr","type":"uint256"}],"name":"isCctp","outputs":[{"internalType":"bool","name":"cctp","type":"bool"}],"stateMutability":"pure","type":"function"},{"inputs":[],"name":"owner","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"platformFee","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"router","type":"address"},{"internalType":"bytes","name":"selector","type":"bytes"}],"name":"removeRouterAndSelector","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_signer","type":"address"}],"name":"removeSigner","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"renounceOwnership","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_cctpFundManager","type":"address"}],"name":"setCCTPFundManager","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_feeWallet","type":"address"}],"name":"setFeeWallet","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_fundManager","type":"address"}],"name":"setFundManager","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address payable","name":"_gasWallet","type":"address"}],"name":"setGasWallet","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"_platformFee","type":"uint256"}],"name":"setPlatformFee","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_weth","type":"address"}],"name":"setWeth","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"}],"name":"signers","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"amountIn","type":"uint256"},{"internalType":"uint256","name":"minAmountOut","type":"uint256"},{"internalType":"address","name":"fromToken","type":"address"},{"internalType":"address","name":"toToken","type":"address"},{"internalType":"address","name":"targetAddress","type":"address"},{"internalType":"address","name":"router","type":"address"},{"internalType":"bytes","name":"routerCalldata","type":"bytes"}],"name":"swapOnSameNetwork","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"minAmountOut","type":"uint256"},{"internalType":"address","name":"toToken","type":"address"},{"internalType":"address","name":"targetAddress","type":"address"},{"internalType":"address","name":"router","type":"address"},{"internalType":"bytes","name":"routerCalldata","type":"bytes"}],"name":"swapOnSameNetworkETH","outputs":[],"stateMutability":"payable","type":"function"},{"inputs":[{"internalType":"address","name":"token","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"},{"components":[{"internalType":"uint256","name":"targetNetwork","type":"uint256"},{"internalType":"address","name":"targetToken","type":"address"},{"internalType":"address","name":"targetAddress","type":"address"}],"internalType":"struct FiberRouter.SwapCrossData","name":"sd","type":"tuple"},{"internalType":"bytes32","name":"withdrawalData","type":"bytes32"},{"internalType":"bool","name":"cctpType","type":"bool"},{"components":[{"internalType":"address","name":"referral","type":"address"},{"internalType":"uint256","name":"referralFee","type":"uint256"},{"internalType":"uint256","name":"referralDiscount","type":"uint256"},{"internalType":"uint256","name":"sourceAmountIn","type":"uint256"},{"internalType":"uint256","name":"sourceAmountOut","type":"uint256"},{"internalType":"uint256","name":"destinationAmountIn","type":"uint256"},{"internalType":"uint256","name":"destinationAmountOut","type":"uint256"},{"internalType":"bytes32","name":"salt","type":"bytes32"},{"internalType":"uint256","name":"expiry","type":"uint256"},{"internalType":"bytes","name":"signature","type":"bytes"}],"internalType":"struct FeeDistributor.FeeDistributionData","name":"fd","type":"tuple"}],"name":"swapSigned","outputs":[],"stateMutability":"payable","type":"function"},{"inputs":[{"internalType":"uint256","name":"amountIn","type":"uint256"},{"internalType":"uint256","name":"minAmountOut","type":"uint256"},{"internalType":"address","name":"fromToken","type":"address"},{"internalType":"address","name":"foundryToken","type":"address"},{"internalType":"address","name":"router","type":"address"},{"internalType":"bytes","name":"routerCalldata","type":"bytes"},{"components":[{"internalType":"uint256","name":"targetNetwork","type":"uint256"},{"internalType":"address","name":"targetToken","type":"address"},{"internalType":"address","name":"targetAddress","type":"address"}],"internalType":"struct FiberRouter.SwapCrossData","name":"sd","type":"tuple"},{"internalType":"bytes32","name":"withdrawalData","type":"bytes32"},{"internalType":"bool","name":"cctpType","type":"bool"},{"components":[{"internalType":"address","name":"referral","type":"address"},{"internalType":"uint256","name":"referralFee","type":"uint256"},{"internalType":"uint256","name":"referralDiscount","type":"uint256"},{"internalType":"uint256","name":"sourceAmountIn","type":"uint256"},{"internalType":"uint256","name":"sourceAmountOut","type":"uint256"},{"internalType":"uint256","name":"destinationAmountIn","type":"uint256"},{"internalType":"uint256","name":"destinationAmountOut","type":"uint256"},{"internalType":"bytes32","name":"salt","type":"bytes32"},{"internalType":"uint256","name":"expiry","type":"uint256"},{"internalType":"bytes","name":"signature","type":"bytes"}],"internalType":"struct FeeDistributor.FeeDistributionData","name":"fd","type":"tuple"}],"name":"swapSignedAndCrossRouter","outputs":[],"stateMutability":"payable","type":"function"},{"inputs":[{"internalType":"uint256","name":"minAmountOut","type":"uint256"},{"internalType":"address","name":"foundryToken","type":"address"},{"internalType":"uint256","name":"gasFee","type":"uint256"},{"internalType":"address","name":"router","type":"address"},{"internalType":"bytes","name":"routerCalldata","type":"bytes"},{"components":[{"internalType":"uint256","name":"targetNetwork","type":"uint256"},{"internalType":"address","name":"targetToken","type":"address"},{"internalType":"address","name":"targetAddress","type":"address"}],"internalType":"struct FiberRouter.SwapCrossData","name":"sd","type":"tuple"},{"internalType":"bytes32","name":"withdrawalData","type":"bytes32"},{"internalType":"bool","name":"cctpType","type":"bool"},{"components":[{"internalType":"address","name":"referral","type":"address"},{"internalType":"uint256","name":"referralFee","type":"uint256"},{"internalType":"uint256","name":"referralDiscount","type":"uint256"},{"internalType":"uint256","name":"sourceAmountIn","type":"uint256"},{"internalType":"uint256","name":"sourceAmountOut","type":"uint256"},{"internalType":"uint256","name":"destinationAmountIn","type":"uint256"},{"internalType":"uint256","name":"destinationAmountOut","type":"uint256"},{"internalType":"bytes32","name":"salt","type":"bytes32"},{"internalType":"uint256","name":"expiry","type":"uint256"},{"internalType":"bytes","name":"signature","type":"bytes"}],"internalType":"struct FeeDistributor.FeeDistributionData","name":"fd","type":"tuple"}],"name":"swapSignedAndCrossRouterETH","outputs":[],"stateMutability":"payable","type":"function"},{"inputs":[{"internalType":"uint256","name":"","type":"uint256"}],"name":"targetNetworks","outputs":[{"internalType":"uint32","name":"targetNetworkDomain","type":"uint32"},{"internalType":"address","name":"targetFundManager","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"newOwner","type":"address"}],"name":"transferOwnership","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"bytes32","name":"","type":"bytes32"}],"name":"usedSalt","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"weth","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"token","type":"address"},{"internalType":"address","name":"payee","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"},{"internalType":"bytes32","name":"salt","type":"bytes32"},{"internalType":"uint256","name":"expiry","type":"uint256"},{"internalType":"bytes","name":"multiSignature","type":"bytes"},{"internalType":"bool","name":"cctpType","type":"bool"}],"name":"withdrawSigned","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address payable","name":"to","type":"address"},{"internalType":"uint256","name":"amountIn","type":"uint256"},{"internalType":"uint256","name":"minAmountOut","type":"uint256"},{"internalType":"address","name":"foundryToken","type":"address"},{"internalType":"address","name":"targetToken","type":"address"},{"internalType":"address","name":"router","type":"address"},{"internalType":"bytes","name":"routerCalldata","type":"bytes"},{"internalType":"bytes32","name":"salt","type":"bytes32"},{"internalType":"uint256","name":"expiry","type":"uint256"},{"internalType":"bytes","name":"multiSignature","type":"bytes"},{"internalType":"bool","name":"cctpType","type":"bool"}],"name":"withdrawSignedAndSwapRouter","outputs":[],"stateMutability":"nonpayable","type":"function"}]