编译器
0.8.22+commit.4fc1097e
文件 1 的 14:Address.sol
pragma solidity ^0.8.1;
library Address {
function isContract(address account) internal view returns (bool) {
return account.code.length > 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 functionCallWithValue(target, data, 0, "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");
(bool success, bytes memory returndata) = target.call{value: value}(data);
return verifyCallResultFromTarget(target, 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) {
(bool success, bytes memory returndata) = target.staticcall(data);
return verifyCallResultFromTarget(target, 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) {
(bool success, bytes memory returndata) = target.delegatecall(data);
return verifyCallResultFromTarget(target, success, returndata, errorMessage);
}
function verifyCallResultFromTarget(
address target,
bool success,
bytes memory returndata,
string memory errorMessage
) internal view returns (bytes memory) {
if (success) {
if (returndata.length == 0) {
require(isContract(target), "Address: call to non-contract");
}
return returndata;
} else {
_revert(returndata, errorMessage);
}
}
function verifyCallResult(
bool success,
bytes memory returndata,
string memory errorMessage
) internal pure returns (bytes memory) {
if (success) {
return returndata;
} else {
_revert(returndata, errorMessage);
}
}
function _revert(bytes memory returndata, string memory errorMessage) private pure {
if (returndata.length > 0) {
assembly {
let returndata_size := mload(returndata)
revert(add(32, returndata), returndata_size)
}
} else {
revert(errorMessage);
}
}
}
文件 2 的 14: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) {
return msg.data;
}
}
文件 3 的 14:IERC20.sol
pragma solidity ^0.8.0;
interface IERC20 {
event Transfer(address indexed from, address indexed to, uint256 value);
event Approval(address indexed owner, address indexed spender, uint256 value);
function totalSupply() external view returns (uint256);
function balanceOf(address account) external view returns (uint256);
function transfer(address to, 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 from, address to, uint256 amount) external returns (bool);
}
文件 4 的 14:IMagpieRouterV2.sol
pragma solidity 0.8.22;
interface IMagpieRouterV2 {
function updateSelector(uint16 commandId, bytes4 selector) external;
event Swap(
address indexed fromAddress,
address indexed toAddress,
address fromAssetAddress,
address toAssetAddress,
uint256 amountIn,
uint256 amountOut
);
function estimateSwapGas(bytes calldata swapArgs) external payable returns (uint256 amountOut, uint256 gasUsed);
function swap(bytes calldata swapArgs) external payable returns (uint256 amountOut);
function silentSwap(bytes calldata swapArgs) external payable returns (uint256 amountOut);
}
文件 5 的 14:IWETH.sol
pragma solidity 0.8.22;
interface IWETH {
function deposit() external payable;
function transfer(address to, uint256 value) external returns (bool);
function withdraw(uint256) external;
}
文件 6 的 14:LibAsset.sol
pragma solidity 0.8.22;
import "openzeppelin-solidity/contracts/token/ERC20/IERC20.sol";
import "../interfaces/IWETH.sol";
error AssetNotReceived();
error ApprovalFailed();
error TransferFromFailed();
error TransferFailed();
library LibAsset {
using LibAsset for address;
address constant NATIVE_ASSETID = address(0);
function isNative(address self) internal pure returns (bool) {
return self == NATIVE_ASSETID;
}
function getBalance(address self) internal view returns (uint256) {
return self.isNative() ? address(this).balance : IERC20(self).balanceOf(address(this));
}
function getBalanceOf(address self, address targetAddress) internal view returns (uint256) {
return self.isNative() ? targetAddress.balance : IERC20(self).balanceOf(targetAddress);
}
function transferFrom(address self, address from, address to, uint256 amount) internal {
IERC20 token = IERC20(self);
bool success = execute(self, abi.encodeWithSelector(token.transferFrom.selector, from, to, amount));
if (!success) revert TransferFromFailed();
}
function transfer(address self, address recipient, uint256 amount) internal {
IERC20 token = IERC20(self);
bool success;
if (self.isNative()) {
(success, ) = payable(recipient).call{value: amount}("");
} else {
success = execute(self, abi.encodeWithSelector(token.transfer.selector, recipient, amount));
}
if (!success) {
revert TransferFailed();
}
}
function approve(address self, address spender, uint256 amount) internal {
IERC20 token = IERC20(self);
if (!execute(self, abi.encodeWithSelector(token.approve.selector, spender, amount))) {
if (
!execute(self, abi.encodeWithSelector(token.approve.selector, spender, 0)) ||
!(execute(self, abi.encodeWithSelector(token.approve.selector, spender, amount)))
) {
revert ApprovalFailed();
}
}
}
function isSuccessful(address target, bool success, bytes memory data) private view returns (bool result) {
if (success) {
if (data.length == 0) {
if (target.code.length > 0) {
result = true;
}
} else {
assembly {
result := mload(add(data, 32))
}
}
}
}
function execute(address self, bytes memory params) private returns (bool) {
(bool success, bytes memory data) = self.call(params);
return isSuccessful(self, success, data);
}
function deposit(address self, address weth, uint256 amount) internal {
if (self.isNative()) {
if (msg.value < amount) {
revert AssetNotReceived();
}
IWETH(weth).deposit{value: amount}();
} else {
self.transferFrom(msg.sender, address(this), amount);
}
}
function withdraw(address self, address weth, address to, uint256 amount) internal {
if (self.isNative()) {
IWETH(weth).withdraw(amount);
}
self.transfer(payable(to), amount);
}
function getDecimals(address self) internal view returns (uint8 tokenDecimals) {
tokenDecimals = 18;
if (!self.isNative()) {
(, bytes memory queriedDecimals) = self.staticcall(abi.encodeWithSignature("decimals()"));
tokenDecimals = abi.decode(queriedDecimals, (uint8));
}
}
}
文件 7 的 14:LibCommand.sol
pragma solidity 0.8.22;
import "../interfaces/IWETH.sol";
import {LibAsset} from "../libraries/LibAsset.sol";
import {AppStorage, LibMagpieRouterV2} from "../libraries/LibMagpieRouterV2.sol";
import {LibSwap, SwapData} from "../libraries/LibSwap.sol";
enum MathOperation {
None,
Add,
Sub,
Mul,
Div,
Pow,
Abs128,
Abs256,
Shr,
Shl
}
enum CommandAction {
Call,
Approval,
TransferFrom,
Transfer,
Wrap,
Unwrap,
Balance,
Math,
EstimateGasStart,
EstimateGasEnd
}
enum SequenceType {
NativeAmount,
Selector,
Address,
Amount,
Data,
CommandOutput,
RouterAddress,
SenderAddress
}
struct CommandData {
CommandAction commandAction;
uint16 inputLength;
uint16 outputLength;
uint16 sequencesPosition;
uint16 sequencesPositionEnd;
address targetAddress;
}
error CommandFailed(bytes data);
error InvalidAmountOut();
error InvalidSequenceType();
error InvalidSelector();
error InvalidSequencesLength();
error InvalidTransferFrom();
library LibCommand {
using LibAsset for address;
function getData(uint16 i) internal pure returns (CommandData memory commandData) {
assembly {
mstore(commandData, shr(248, calldataload(i)))
mstore(add(commandData, 32), shr(240, calldataload(add(i, 1))))
mstore(add(commandData, 64), shr(240, calldataload(add(i, 3))))
mstore(add(commandData, 96), shr(240, calldataload(add(i, 5))))
mstore(add(commandData, 128), shr(240, calldataload(add(i, 7))))
let targetPosition := shr(240, calldataload(add(i, 9)))
mstore(add(commandData, 160), shr(96, calldataload(targetPosition)))
}
}
function getInput(
bytes memory commandOutput,
CommandData memory commandData
) internal view returns (uint256 nativeAmount, bytes memory input) {
AppStorage storage s = LibMagpieRouterV2.getStorage();
input = new bytes(commandData.inputLength);
SequenceType sequenceType;
uint16 p;
uint16 l;
uint16 inputOffset = 32;
bytes4 selector;
for (uint16 i = commandData.sequencesPosition; i < commandData.sequencesPositionEnd; ) {
assembly {
sequenceType := shr(248, calldataload(i))
}
if (sequenceType == SequenceType.NativeAmount) {
assembly {
p := shr(240, calldataload(add(i, 1)))
l := shr(240, calldataload(add(i, 3)))
switch l
case 1 {
nativeAmount := mload(add(add(commandOutput, 32), p))
}
default {
nativeAmount := calldataload(p)
}
}
unchecked {
i += 5;
}
} else if (sequenceType == SequenceType.Selector) {
assembly {
p := shr(240, calldataload(add(i, 1)))
}
selector = s.selectors[p];
assembly {
mstore(add(input, inputOffset), selector)
}
inputOffset += 4;
unchecked {
i += 3;
}
} else if (sequenceType == SequenceType.Address) {
assembly {
p := shr(240, calldataload(add(i, 1)))
mstore(add(input, inputOffset), shr(96, calldataload(p)))
}
inputOffset += 32;
unchecked {
i += 3;
}
} else if (sequenceType == SequenceType.Amount) {
assembly {
p := shr(240, calldataload(add(i, 1)))
mstore(add(input, inputOffset), calldataload(p))
}
inputOffset += 32;
unchecked {
i += 3;
}
} else if (sequenceType == SequenceType.Data) {
assembly {
p := shr(240, calldataload(add(i, 1)))
l := shr(240, calldataload(add(i, 3)))
calldatacopy(add(input, inputOffset), p, l)
}
inputOffset += l;
unchecked {
i += 5;
}
} else if (sequenceType == SequenceType.CommandOutput) {
assembly {
p := shr(240, calldataload(add(i, 1)))
mstore(add(input, inputOffset), mload(add(add(commandOutput, 32), p)))
}
inputOffset += 32;
unchecked {
i += 3;
}
} else if (sequenceType == SequenceType.RouterAddress) {
assembly {
mstore(add(input, inputOffset), address())
}
inputOffset += 32;
unchecked {
i += 1;
}
} else if (sequenceType == SequenceType.SenderAddress) {
assembly {
mstore(add(input, inputOffset), caller())
}
inputOffset += 32;
unchecked {
i += 1;
}
} else {
revert InvalidSequenceType();
}
}
if (inputOffset - 32 != commandData.inputLength) {
revert InvalidSequencesLength();
}
if (commandData.commandAction == CommandAction.Call) {
if (selector == 0) {
revert InvalidSelector();
} else if (selector == 0x23b872dd) {
revert InvalidTransferFrom();
}
}
}
function math(bytes memory input) internal pure returns (uint256) {
uint256[] memory localOutputs = new uint256[](10);
bytes32 amount0;
bytes32 amount1;
MathOperation operation;
for (uint8 i = 0; i <= 10; ) {
assembly {
let pos := add(add(input, 32), mul(i, 3))
let amount0Index := shr(248, mload(add(pos, 1)))
if lt(amount0Index, 10) {
amount0 := mload(add(add(localOutputs, 32), mul(amount0Index, 32)))
}
if gt(amount0Index, 9) {
amount0Index := sub(amount0Index, 10)
amount0 := mload(add(add(input, 64), mul(amount0Index, 32)))
}
let amount1Index := shr(248, mload(add(pos, 2)))
if lt(amount1Index, 10) {
amount1 := mload(add(add(localOutputs, 32), mul(amount1Index, 32)))
}
if gt(amount1Index, 9) {
amount1Index := sub(amount1Index, 10)
amount1 := mload(add(add(input, 64), mul(amount1Index, 32)))
}
operation := shr(248, mload(pos))
}
if (operation == MathOperation.None) {
return localOutputs[i - 1];
} else if (operation == MathOperation.Add) {
localOutputs[i] = uint256(amount0) + uint256(amount1);
} else if (operation == MathOperation.Sub) {
localOutputs[i] = uint256(amount0) - uint256(amount1);
} else if (operation == MathOperation.Mul) {
localOutputs[i] = uint256(amount0) * uint256(amount1);
} else if (operation == MathOperation.Div) {
localOutputs[i] = uint256(amount0) / uint256(amount1);
} else if (operation == MathOperation.Pow) {
localOutputs[i] = uint256(amount0) ** uint256(amount1);
} else if (operation == MathOperation.Abs128) {
int128 amount;
assembly {
amount := amount0
}
if (amount < 0) {
localOutputs[i] = uint256(uint128(-(amount)));
} else {
localOutputs[i] = uint256(uint128(amount));
}
} else if (operation == MathOperation.Abs256) {
int256 amount;
assembly {
amount := amount0
}
if (amount < 0) {
localOutputs[i] = uint256(-(amount));
} else {
localOutputs[i] = uint256(amount);
}
} else if (operation == MathOperation.Shr) {
assembly {
mstore(add(add(localOutputs, 32), mul(i, 32)), shr(amount0, amount1))
}
} else if (operation == MathOperation.Shl) {
assembly {
mstore(add(add(localOutputs, 32), mul(i, 32)), shl(amount0, amount1))
}
}
unchecked {
i++;
}
}
return localOutputs[9];
}
function approve(bytes memory input) internal {
address assetAddress;
address spenderAddress;
uint256 amount;
assembly {
assetAddress := mload(add(input, 32))
spenderAddress := mload(add(input, 64))
amount := mload(add(input, 96))
}
if (amount == 0) {
return;
}
assetAddress.approve(spenderAddress, amount);
}
function transferFrom(bytes memory input) internal {
address assetAddress;
address fromAddress;
address toAddress;
uint256 amount;
assembly {
assetAddress := mload(add(input, 32))
fromAddress := mload(add(input, 64))
toAddress := mload(add(input, 96))
amount := mload(add(input, 128))
}
if (fromAddress != msg.sender) {
revert InvalidTransferFrom();
}
if (amount == 0) {
return;
}
assetAddress.transferFrom(fromAddress, toAddress, amount);
}
function transfer(bytes memory input) internal {
address assetAddress;
address toAddress;
uint256 amount;
assembly {
assetAddress := mload(add(input, 32))
toAddress := mload(add(input, 64))
amount := mload(add(input, 96))
}
if (amount == 0) {
return;
}
assetAddress.transfer(toAddress, amount);
}
function wrap(bytes memory input) internal {
address assetAddress;
uint256 amount;
assembly {
assetAddress := mload(add(input, 32))
amount := mload(add(input, 64))
}
IWETH(assetAddress).deposit{value: amount}();
}
function unwrap(bytes memory input) internal {
address assetAddress;
uint256 amount;
assembly {
assetAddress := mload(add(input, 32))
amount := mload(add(input, 64))
}
IWETH(assetAddress).withdraw(amount);
}
function balance(bytes memory input) internal view returns (uint256) {
address assetAddress;
assembly {
assetAddress := mload(add(input, 32))
}
return assetAddress.getBalanceOf(address(this));
}
}
文件 8 的 14:LibMagpieRouterV2.sol
pragma solidity 0.8.22;
struct AppStorage {
mapping(uint16 => bytes4) selectors;
}
library LibMagpieRouterV2 {
function getStorage() internal pure returns (AppStorage storage s) {
assembly {
s.slot := 0
}
}
}
文件 9 的 14:LibSwap.sol
pragma solidity 0.8.22;
import {LibAsset} from "../libraries/LibAsset.sol";
struct SwapData {
uint16 amountsOffset;
uint16 dataOffset;
uint16 commandsOffset;
uint16 commandsOffsetEnd;
uint16 outputsLength;
uint256 amountIn;
address toAddress;
address fromAssetAddress;
address toAssetAddress;
uint256 deadline;
uint256 amountOutMin;
}
library LibSwap {
using LibAsset for address;
uint16 constant SWAP_ARGS_OFFSET = 68;
function getAmountIn(
uint16 startOffset,
uint16 endOffset,
uint16 positionOffset
) internal pure returns (uint256 amountIn) {
for (uint16 i = startOffset; i < endOffset; ) {
uint256 currentAmountIn;
assembly {
let p := shr(240, calldataload(i))
currentAmountIn := calldataload(add(p, positionOffset))
}
amountIn += currentAmountIn;
unchecked {
i += 2;
}
}
}
function getFirstAmountIn(uint16 swapArgsOffset) internal pure returns (uint256 amountIn) {
uint16 position = swapArgsOffset + 4;
assembly {
amountIn := calldataload(position)
}
}
function getData(uint16 swapArgsOffset) internal pure returns (SwapData memory swapData) {
uint16 dataLength;
uint16 amountsLength;
uint16 dataOffset;
uint16 swapArgsLength;
assembly {
dataLength := shr(240, calldataload(swapArgsOffset))
amountsLength := shr(240, calldataload(add(swapArgsOffset, 2)))
swapArgsLength := calldataload(sub(swapArgsOffset, 32))
}
dataOffset = swapArgsOffset + 4;
swapData.dataOffset = dataOffset;
swapData.amountsOffset = swapData.dataOffset + dataLength;
swapData.commandsOffset = swapData.amountsOffset + amountsLength;
swapData.commandsOffsetEnd = swapArgsLength + swapArgsOffset;
uint256 amountIn = getAmountIn(
swapData.amountsOffset,
swapData.commandsOffset,
swapArgsOffset - SWAP_ARGS_OFFSET
);
assembly {
mstore(add(swapData, 128), shr(240, calldataload(add(dataOffset, 32))))
mstore(add(swapData, 160), amountIn)
mstore(add(swapData, 192), shr(96, calldataload(add(dataOffset, 34))))
mstore(add(swapData, 224), shr(96, calldataload(add(dataOffset, 54))))
mstore(add(swapData, 256), shr(96, calldataload(add(dataOffset, 74))))
mstore(add(swapData, 288), calldataload(add(dataOffset, 94)))
mstore(add(swapData, 320), calldataload(add(dataOffset, 126)))
}
}
}
文件 10 的 14:LibUniswapV3.sol
pragma solidity 0.8.22;
import {LibAsset} from "../libraries/LibAsset.sol";
error UniswapV3InvalidAmount();
library LibUniswapV3 {
using LibAsset for address;
function uniswapV3SwapCallback(int256 amount0Delta, int256 amount1Delta, address assetIn) internal {
if (amount0Delta <= 0 && amount1Delta <= 0) {
revert UniswapV3InvalidAmount();
}
assetIn.transfer(msg.sender, amount0Delta > 0 ? uint256(amount0Delta) : uint256(amount1Delta));
}
}
文件 11 的 14:MagpieRouterV2.sol
pragma solidity 0.8.22;
import {Ownable2Step} from "openzeppelin-solidity/contracts/access/Ownable2Step.sol";
import {Multicall} from "openzeppelin-solidity/contracts/utils/Multicall.sol";
import {IMagpieRouterV2} from "./interfaces/IMagpieRouterV2.sol";
import {LibAsset} from "./libraries/LibAsset.sol";
import {AppStorage, LibMagpieRouterV2} from "./libraries/LibMagpieRouterV2.sol";
import {LibSwap, SwapData} from "./libraries/LibSwap.sol";
import {LibCommand, CommandAction, CommandData} from "./router/LibCommand.sol";
import {LibUniswapV3} from "./router/LibUniswapV3.sol";
error ExpiredTransaction();
error InsufficientAmountOut();
error InvalidCall();
error InvalidCommand();
contract MagpieRouterV2 is IMagpieRouterV2, Ownable2Step, Multicall {
using LibAsset for address;
function updateSelector(uint16 commandType, bytes4 selector) external onlyOwner {
AppStorage storage s = LibMagpieRouterV2.getStorage();
s.selectors[commandType] = selector;
}
function enforceDeadline(uint256 deadline) private view {
if (deadline < block.timestamp) {
revert ExpiredTransaction();
}
}
fallback() external {
int256 amount0Delta;
int256 amount1Delta;
address assetIn;
uint256 callDataSize;
assembly {
amount0Delta := calldataload(4)
amount1Delta := calldataload(36)
assetIn := shr(96, calldataload(132))
callDataSize := calldatasize()
}
if (callDataSize != 164) {
revert InvalidCall();
}
LibUniswapV3.uniswapV3SwapCallback(amount0Delta, amount1Delta, assetIn);
}
function isTokenMovement(CommandAction commandAction) private pure returns (bool) {
return
commandAction == CommandAction.Approval ||
commandAction == CommandAction.TransferFrom ||
commandAction == CommandAction.Transfer ||
commandAction == CommandAction.Wrap ||
commandAction == CommandAction.Unwrap;
}
function estimateSwapGas(bytes calldata) external payable returns (uint256 amountOut, uint256 gasUsed) {
(amountOut, gasUsed) = execute(true);
}
function swap(bytes calldata) external payable returns (uint256 amountOut) {
(amountOut, ) = execute(true);
}
function silentSwap(bytes calldata) external payable returns (uint256 amountOut) {
(amountOut, ) = execute(false);
}
function execute(bool triggerEvent) private returns (uint256 amountOut, uint256 gasUsed) {
SwapData memory swapData = LibSwap.getData(LibSwap.SWAP_ARGS_OFFSET);
enforceDeadline(swapData.deadline);
amountOut = swapData.toAssetAddress.getBalanceOf(swapData.toAddress);
bytes memory commandOutput = new bytes(swapData.outputsLength);
uint16 i;
CommandData memory commandData;
uint256 nativeAmount;
uint256 tmpAmount;
bytes memory input;
uint256 commandOutputOffset;
assembly {
commandOutputOffset := add(commandOutput, 32)
}
for (i = swapData.commandsOffset; i < swapData.commandsOffsetEnd; ) {
commandData = LibCommand.getData(i);
(nativeAmount, input) = LibCommand.getInput(commandOutput, commandData);
if (commandData.commandAction == CommandAction.Call) {
assembly {
let outputLength := mload(add(commandData, 64))
if iszero(
call(
gas(),
mload(add(commandData, 160)),
nativeAmount,
add(input, 32),
mload(input),
commandOutputOffset,
outputLength
)
) {
returndatacopy(0, 0, returndatasize())
revert(0, returndatasize())
}
commandOutputOffset := add(commandOutputOffset, outputLength)
}
} else if (commandData.commandAction == CommandAction.Approval) {
LibCommand.approve(input);
} else if (commandData.commandAction == CommandAction.TransferFrom) {
LibCommand.transferFrom(input);
} else if (commandData.commandAction == CommandAction.Transfer) {
LibCommand.transfer(input);
} else if (commandData.commandAction == CommandAction.Wrap) {
LibCommand.wrap(input);
} else if (commandData.commandAction == CommandAction.Unwrap) {
LibCommand.unwrap(input);
} else if (commandData.commandAction == CommandAction.Balance) {
tmpAmount = LibCommand.balance(input);
assembly {
mstore(commandOutputOffset, tmpAmount)
commandOutputOffset := add(commandOutputOffset, 32)
}
} else if (commandData.commandAction == CommandAction.Math) {
tmpAmount = LibCommand.math(input);
assembly {
mstore(commandOutputOffset, tmpAmount)
commandOutputOffset := add(commandOutputOffset, 32)
}
} else if (commandData.commandAction == CommandAction.EstimateGasStart) {
gasUsed = gasleft();
} else if (commandData.commandAction == CommandAction.EstimateGasEnd) {
gasUsed -= gasleft();
} else {
revert InvalidCommand();
}
assembly {
if gt(commandOutputOffset, add(add(commandOutput, 32), mload(commandOutput))) {
revert(0, 0)
}
}
unchecked {
i += 11;
}
}
amountOut = swapData.toAssetAddress.getBalanceOf(swapData.toAddress) - amountOut;
if (amountOut < swapData.amountOutMin) {
revert InsufficientAmountOut();
}
if (triggerEvent) {
emit Swap(
msg.sender,
swapData.toAddress,
swapData.fromAssetAddress,
swapData.toAssetAddress,
swapData.amountIn,
amountOut
);
}
}
receive() external payable {}
}
文件 12 的 14:Multicall.sol
pragma solidity ^0.8.0;
import "./Address.sol";
abstract contract Multicall {
function multicall(bytes[] calldata data) external virtual returns (bytes[] memory results) {
results = new bytes[](data.length);
for (uint256 i = 0; i < data.length; i++) {
results[i] = Address.functionDelegateCall(address(this), data[i]);
}
return results;
}
}
文件 13 的 14: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() {
_transferOwnership(_msgSender());
}
modifier onlyOwner() {
_checkOwner();
_;
}
function owner() public view virtual returns (address) {
return _owner;
}
function _checkOwner() internal view virtual {
require(owner() == _msgSender(), "Ownable: caller is not the owner");
}
function renounceOwnership() public virtual onlyOwner {
_transferOwnership(address(0));
}
function transferOwnership(address newOwner) public virtual onlyOwner {
require(newOwner != address(0), "Ownable: new owner is the zero address");
_transferOwnership(newOwner);
}
function _transferOwnership(address newOwner) internal virtual {
address oldOwner = _owner;
_owner = newOwner;
emit OwnershipTransferred(oldOwner, newOwner);
}
}
文件 14 的 14:Ownable2Step.sol
pragma solidity ^0.8.0;
import "./Ownable.sol";
abstract contract Ownable2Step is Ownable {
address private _pendingOwner;
event OwnershipTransferStarted(address indexed previousOwner, address indexed newOwner);
function pendingOwner() public view virtual returns (address) {
return _pendingOwner;
}
function transferOwnership(address newOwner) public virtual override onlyOwner {
_pendingOwner = newOwner;
emit OwnershipTransferStarted(owner(), newOwner);
}
function _transferOwnership(address newOwner) internal virtual override {
delete _pendingOwner;
super._transferOwnership(newOwner);
}
function acceptOwnership() public virtual {
address sender = _msgSender();
require(pendingOwner() == sender, "Ownable2Step: caller is not the new owner");
_transferOwnership(sender);
}
}
{
"compilationTarget": {
"contracts/MagpieRouterV2.sol": "MagpieRouterV2"
},
"evmVersion": "paris",
"libraries": {},
"metadata": {
"bytecodeHash": "ipfs",
"useLiteralContent": true
},
"optimizer": {
"enabled": true,
"runs": 200
},
"remappings": []
}
[{"inputs":[],"name":"ApprovalFailed","type":"error"},{"inputs":[],"name":"ExpiredTransaction","type":"error"},{"inputs":[],"name":"InsufficientAmountOut","type":"error"},{"inputs":[],"name":"InvalidCall","type":"error"},{"inputs":[],"name":"InvalidCommand","type":"error"},{"inputs":[],"name":"InvalidSelector","type":"error"},{"inputs":[],"name":"InvalidSequenceType","type":"error"},{"inputs":[],"name":"InvalidSequencesLength","type":"error"},{"inputs":[],"name":"InvalidTransferFrom","type":"error"},{"inputs":[],"name":"TransferFailed","type":"error"},{"inputs":[],"name":"TransferFromFailed","type":"error"},{"inputs":[],"name":"UniswapV3InvalidAmount","type":"error"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"previousOwner","type":"address"},{"indexed":true,"internalType":"address","name":"newOwner","type":"address"}],"name":"OwnershipTransferStarted","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":true,"internalType":"address","name":"fromAddress","type":"address"},{"indexed":true,"internalType":"address","name":"toAddress","type":"address"},{"indexed":false,"internalType":"address","name":"fromAssetAddress","type":"address"},{"indexed":false,"internalType":"address","name":"toAssetAddress","type":"address"},{"indexed":false,"internalType":"uint256","name":"amountIn","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"amountOut","type":"uint256"}],"name":"Swap","type":"event"},{"stateMutability":"nonpayable","type":"fallback"},{"inputs":[],"name":"acceptOwnership","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"bytes","name":"","type":"bytes"}],"name":"estimateSwapGas","outputs":[{"internalType":"uint256","name":"amountOut","type":"uint256"},{"internalType":"uint256","name":"gasUsed","type":"uint256"}],"stateMutability":"payable","type":"function"},{"inputs":[{"internalType":"bytes[]","name":"data","type":"bytes[]"}],"name":"multicall","outputs":[{"internalType":"bytes[]","name":"results","type":"bytes[]"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"owner","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"pendingOwner","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"renounceOwnership","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"bytes","name":"","type":"bytes"}],"name":"silentSwap","outputs":[{"internalType":"uint256","name":"amountOut","type":"uint256"}],"stateMutability":"payable","type":"function"},{"inputs":[{"internalType":"bytes","name":"","type":"bytes"}],"name":"swap","outputs":[{"internalType":"uint256","name":"amountOut","type":"uint256"}],"stateMutability":"payable","type":"function"},{"inputs":[{"internalType":"address","name":"newOwner","type":"address"}],"name":"transferOwnership","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint16","name":"commandType","type":"uint16"},{"internalType":"bytes4","name":"selector","type":"bytes4"}],"name":"updateSelector","outputs":[],"stateMutability":"nonpayable","type":"function"},{"stateMutability":"payable","type":"receive"}]