Diff
checker
文本
文本
图像
文档
Excel
文件夹
Legal
Enterprise
桌面版
定价
登录
下载 Diffchecker 桌面版
比较文本
查找两个文本文件之间的差异
工具
历史
实时编辑器
隐藏空白更改
折叠未更改行
关闭换行
视图
拆分
统一
比对精度
智能
单词
字符
文本样式
更改外观
语法高亮
选择语法
忽略
文本转换
转到第一个差异
编辑输入
Diffchecker Desktop
运行Diffchecker最安全的方式。获取Diffchecker桌面应用:您的差异永远不会离开您的电脑!
获取桌面版
PerpV2 <> PerpV2LibraryV2
创建于
4年前
差异永不过期
清除
导出
分享
解释
6 删除
行
总计
删除
字符
总计
删除
要继续使用此功能,请升级到
Diff
checker
Pro
查看价格
267 行
全部复制
87 添加
行
总计
添加
字符
总计
添加
要继续使用此功能,请升级到
Diff
checker
Pro
查看价格
347 行
全部复制
/*
/*
复制
已复制
复制
已复制
Copyright 202
1
Set Labs Inc.
Copyright 202
2
Set Labs Inc.
Licensed under the Apache License, Version 2.0 (the "License");
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
You may obtain a copy of the License at
复制
已复制
复制
已复制
http://www.apache.org/licenses/LICENSE-2.0
http://www.apache.org/licenses/LICENSE-2.0
复制
已复制
复制
已复制
Unless required by applicable law or agreed to in writing, software
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
See the License for the specific language governing permissions and
limitations under the License.
limitations under the License.
复制
已复制
复制
已复制
SPDX-License-Identifier: Apache License, Version 2.0
SPDX-License-Identifier: Apache License, Version 2.0
*/
*/
pragma solidity 0.6.10;
pragma solidity 0.6.10;
pragma experimental ABIEncoderV2;
pragma experimental ABIEncoderV2;
import { IERC20 } from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import { IERC20 } from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import { IClearingHouse } from "../../../interfaces/external/perp-v2/IClearingHouse.sol";
import { IClearingHouse } from "../../../interfaces/external/perp-v2/IClearingHouse.sol";
复制
已复制
复制
已复制
import { IVault } from "../../../interfaces/external/perp-v2/IVault.sol";
import { IQuoter } from "../../../interfaces/external/perp-v2/IQuoter.sol";
import { IQuoter } from "../../../interfaces/external/perp-v2/IQuoter.sol";
复制
已复制
复制
已复制
import { IVault } from "../../../interfaces/external/perp-v2/IVault.sol";
import { ISetToken } from "../../../interfaces/ISetToken.sol";
import { ISetToken } from "../../../interfaces/ISetToken.sol";
复制
已复制
复制
已复制
import { PreciseUnitMath } from "../../../lib/PreciseUnitMath.sol";
/**
/**
复制
已复制
复制
已复制
* @title
PerpV2
* @title
PerpV2LibraryV2
* @author Set Protocol
* @author Set Protocol
*
*
* Collection of helper functions for interacting with PerpV2 integrations.
* Collection of helper functions for interacting with PerpV2 integrations.
复制
已复制
复制
已复制
*
* CHANGELOG:
* - Add ActionInfo struct.
* - Add `executeTrade` and `simulateTrade` functions.
*/
*/
复制
已复制
复制
已复制
library
PerpV2 {
library
PerpV2LibraryV2 {
struct ActionInfo {
ISetToken setToken;
address baseToken; // Virtual token minted by the Perp protocol
bool isBuy; // When true, `baseToken` is being bought, when false, sold
uint256 baseTokenAmount; // Base token quantity in 10**18 decimals
uint256 oppositeAmountBound; // vUSDC pay or receive quantity bound
// (see `PerpV2LeverageModuleV2#_createActionInfoNotional` for details)
}
/* ============ External ============ */
/* ============ External ============ */
/**
/**
* Gets Perp vault `deposit` calldata
* Gets Perp vault `deposit` calldata
*
*
* When invoked, calldata deposits an `_amountNotional` of collateral asset into the Perp Protocol vault
* When invoked, calldata deposits an `_amountNotional` of collateral asset into the Perp Protocol vault
*
*
* @param _vault Perp protocol vault
* @param _vault Perp protocol vault
* @param _asset Collateral asset to deposit
* @param _asset Collateral asset to deposit
* @param _amountNotional Notional amount in collateral decimals to deposit
* @param _amountNotional Notional amount in collateral decimals to deposit
*
*
* @return address Vault address
* @return address Vault address
* @return uint256 Call value
* @return uint256 Call value
* @return calldata Deposit calldata
* @return calldata Deposit calldata
*/
*/
function getDepositCalldata(
function getDepositCalldata(
IVault _vault,
IVault _vault,
IERC20 _asset,
IERC20 _asset,
uint256 _amountNotional
uint256 _amountNotional
)
)
public
public
pure
pure
returns (address, uint256, bytes memory)
returns (address, uint256, bytes memory)
{
{
bytes memory callData = abi.encodeWithSignature(
bytes memory callData = abi.encodeWithSignature(
"deposit(address,uint256)",
"deposit(address,uint256)",
_asset,
_asset,
_amountNotional
_amountNotional
);
);
return (address(_vault), 0, callData);
return (address(_vault), 0, callData);
}
}
/**
/**
* Invoke `deposit` on Vault from SetToken
* Invoke `deposit` on Vault from SetToken
*
*
* Deposits an `_amountNotional` of collateral asset into the Perp Protocol vault
* Deposits an `_amountNotional` of collateral asset into the Perp Protocol vault
*
*
* @param _setToken Address of the SetToken
* @param _setToken Address of the SetToken
* @param _vault Address of Perp Protocol vault contract
* @param _vault Address of Perp Protocol vault contract
* @param _asset The address of the collateral asset to deposit
* @param _asset The address of the collateral asset to deposit
* @param _amountNotional Notional amount in collateral decimals to deposit
* @param _amountNotional Notional amount in collateral decimals to deposit
*/
*/
function invokeDeposit(
function invokeDeposit(
ISetToken _setToken,
ISetToken _setToken,
IVault _vault,
IVault _vault,
IERC20 _asset,
IERC20 _asset,
uint256 _amountNotional
uint256 _amountNotional
)
)
external
external
{
{
( , , bytes memory depositCalldata) = getDepositCalldata(
( , , bytes memory depositCalldata) = getDepositCalldata(
_vault,
_vault,
_asset,
_asset,
_amountNotional
_amountNotional
);
);
_setToken.invoke(address(_vault), 0, depositCalldata);
_setToken.invoke(address(_vault), 0, depositCalldata);
}
}
/**
/**
* Get Perp Vault `withdraw` method calldata
* Get Perp Vault `withdraw` method calldata
*
*
* When invoked, calldata withdraws an `_amountNotional` of collateral asset from the Perp protocol vault
* When invoked, calldata withdraws an `_amountNotional` of collateral asset from the Perp protocol vault
*
*
* @param _vault Address of the Perp Protocol vault contract
* @param _vault Address of the Perp Protocol vault contract
* @param _asset The address of the collateral asset to withdraw
* @param _asset The address of the collateral asset to withdraw
* @param _amountNotional The notional amount in collateral decimals to be withdrawn
* @param _amountNotional The notional amount in collateral decimals to be withdrawn
*
*
* @return address Vault contract address
* @return address Vault contract address
* @return uint256 Call value
* @return uint256 Call value
* @return bytes Withdraw calldata
* @return bytes Withdraw calldata
*/
*/
function getWithdrawCalldata(
function getWithdrawCalldata(
IVault _vault,
IVault _vault,
IERC20 _asset,
IERC20 _asset,
uint256 _amountNotional
uint256 _amountNotional
)
)
public
public
pure
pure
returns (address, uint256, bytes memory)
returns (address, uint256, bytes memory)
{
{
bytes memory callData = abi.encodeWithSignature(
bytes memory callData = abi.encodeWithSignature(
"withdraw(address,uint256)",
"withdraw(address,uint256)",
_asset,
_asset,
_amountNotional
_amountNotional
);
);
return (address(_vault), 0, callData);
return (address(_vault), 0, callData);
}
}
/**
/**
* Invoke `withdraw` on Vault from SetToken
* Invoke `withdraw` on Vault from SetToken
*
*
* Withdraws an `_amountNotional` of collateral asset from the Perp protocol vault
* Withdraws an `_amountNotional` of collateral asset from the Perp protocol vault
*
*
* @param _setToken Address of the SetToken
* @param _setToken Address of the SetToken
* @param _vault Address of the Perp Protocol vault contract
* @param _vault Address of the Perp Protocol vault contract
* @param _asset The address of the collateral asset to withdraw
* @param _asset The address of the collateral asset to withdraw
* @param _amountNotional The notional amount in collateral decimals to be withdrawn *
* @param _amountNotional The notional amount in collateral decimals to be withdrawn *
*/
*/
function invokeWithdraw(
function invokeWithdraw(
ISetToken _setToken,
ISetToken _setToken,
IVault _vault,
IVault _vault,
IERC20 _asset,
IERC20 _asset,
uint256 _amountNotional
uint256 _amountNotional
)
)
external
external
{
{
( , , bytes memory withdrawCalldata) = getWithdrawCalldata(
( , , bytes memory withdrawCalldata) = getWithdrawCalldata(
_vault,
_vault,
_asset,
_asset,
_amountNotional
_amountNotional
);
);
_setToken.invoke(address(_vault), 0, withdrawCalldata);
_setToken.invoke(address(_vault), 0, withdrawCalldata);
}
}
/**
/**
* Get Perp ClearingHouse `openPosition` method calldata
* Get Perp ClearingHouse `openPosition` method calldata
*
*
* When invoked, calldata executes a trade via the Perp protocol ClearingHouse contract
* When invoked, calldata executes a trade via the Perp protocol ClearingHouse contract
*
*
* @param _clearingHouse Address of the Clearinghouse contract
* @param _clearingHouse Address of the Clearinghouse contract
* @param _params OpenPositionParams struct. For details see definition
* @param _params OpenPositionParams struct. For details see definition
* in contracts/interfaces/external/perp-v2/IClearingHouse.sol
* in contracts/interfaces/external/perp-v2/IClearingHouse.sol
*
*
* @return address ClearingHouse contract address
* @return address ClearingHouse contract address
* @return uint256 Call value
* @return uint256 Call value
* @return bytes `openPosition` calldata
* @return bytes `openPosition` calldata
*/
*/
function getOpenPositionCalldata(
function getOpenPositionCalldata(
IClearingHouse _clearingHouse,
IClearingHouse _clearingHouse,
IClearingHouse.OpenPositionParams memory _params
IClearingHouse.OpenPositionParams memory _params
)
)
public
public
pure
pure
returns (address, uint256, bytes memory)
returns (address, uint256, bytes memory)
{
{
bytes memory callData = abi.encodeWithSignature(
bytes memory callData = abi.encodeWithSignature(
"openPosition((address,bool,bool,uint256,uint256,uint256,uint160,bytes32))",
"openPosition((address,bool,bool,uint256,uint256,uint256,uint160,bytes32))",
_params
_params
);
);
return (address(_clearingHouse), 0, callData);
return (address(_clearingHouse), 0, callData);
}
}
/**
/**
* Invoke `openPosition` on ClearingHouse from SetToken
* Invoke `openPosition` on ClearingHouse from SetToken
*
*
* Executes a trade via the Perp protocol ClearingHouse contract
* Executes a trade via the Perp protocol ClearingHouse contract
*
*
* @param _setToken Address of the SetToken
* @param _setToken Address of the SetToken
* @param _clearingHouse Address of the Clearinghouse contract
* @param _clearingHouse Address of the Clearinghouse contract
* @param _params OpenPositionParams struct. For details see definition
* @param _params OpenPositionParams struct. For details see definition
* in contracts/interfaces/external/perp-v2/IClearingHouse.sol
* in contracts/interfaces/external/perp-v2/IClearingHouse.sol
*
*
* @return deltaBase Positive or negative change in base token balance resulting from trade
* @return deltaBase Positive or negative change in base token balance resulting from trade
* @return deltaQuote Positive or negative change in quote token balance resulting from trade
* @return deltaQuote Positive or negative change in quote token balance resulting from trade
*/
*/
function invokeOpenPosition(
function invokeOpenPosition(
ISetToken _setToken,
ISetToken _setToken,
IClearingHouse _clearingHouse,
IClearingHouse _clearingHouse,
IClearingHouse.OpenPositionParams memory _params
IClearingHouse.OpenPositionParams memory _params
)
)
复制
已复制
复制
已复制
external
public
returns (uint256 deltaBase, uint256 deltaQuote)
returns (uint256 deltaBase, uint256 deltaQuote)
{
{
( , , bytes memory openPositionCalldata) = getOpenPositionCalldata(
( , , bytes memory openPositionCalldata) = getOpenPositionCalldata(
_clearingHouse,
_clearingHouse,
_params
_params
);
);
bytes memory returnValue = _setToken.invoke(address(_clearingHouse), 0, openPositionCalldata);
bytes memory returnValue = _setToken.invoke(address(_clearingHouse), 0, openPositionCalldata);
return abi.decode(returnValue, (uint256,uint256));
return abi.decode(returnValue, (uint256,uint256));
}
}
/**
/**
* Get Perp Quoter `swap` method calldata
* Get Perp Quoter `swap` method calldata
*
*
* When invoked, calldata simulates a trade on the Perp exchange via the Perp periphery contract Quoter
* When invoked, calldata simulates a trade on the Perp exchange via the Perp periphery contract Quoter
*
*
* @param _quoter Address of the Quoter contract
* @param _quoter Address of the Quoter contract
* @param _params SwapParams struct. For details see definition
* @param _params SwapParams struct. For details see definition
* in contracts/interfaces/external/perp-v2/IQuoter.sol
* in contracts/interfaces/external/perp-v2/IQuoter.sol
*
*
* @return address ClearingHouse contract address
* @return address ClearingHouse contract address
* @return uint256 Call value
* @return uint256 Call value
* @return bytes `swap` calldata
* @return bytes `swap` calldata
*/
*/
function getSwapCalldata(
function getSwapCalldata(
IQuoter _quoter,
IQuoter _quoter,
IQuoter.SwapParams memory _params
IQuoter.SwapParams memory _params
)
)
public
public
pure
pure
returns (address, uint256, bytes memory)
returns (address, uint256, bytes memory)
{
{
bytes memory callData = abi.encodeWithSignature(
bytes memory callData = abi.encodeWithSignature(
"swap((address,bool,bool,uint256,uint160))",
"swap((address,bool,bool,uint256,uint160))",
_params
_params
);
);
return (address(_quoter), 0, callData);
return (address(_quoter), 0, callData);
}
}
/**
/**
* Invoke `swap` method on Perp Quoter contract
* Invoke `swap` method on Perp Quoter contract
*
*
* Simulates a trade on the Perp exchange via the Perp periphery contract Quoter
* Simulates a trade on the Perp exchange via the Perp periphery contract Quoter
*
*
* @param _setToken Address of the SetToken
* @param _setToken Address of the SetToken
* @param _quoter Address of the Quoter contract
* @param _quoter Address of the Quoter contract
* @param _params SwapParams struct. For details see definition
* @param _params SwapParams struct. For details see definition
* in contracts/interfaces/external/perp-v2/IQuoter.sol
* in contracts/interfaces/external/perp-v2/IQuoter.sol
*
*
* @return swapResponse Struct which includes deltaAvailableBase and deltaAvailableQuote
* @return swapResponse Struct which includes deltaAvailableBase and deltaAvailableQuote
* properties (equiv. to deltaQuote, deltaBase) returned from `openPostion`
* properties (equiv. to deltaQuote, deltaBase) returned from `openPostion`
*/
*/
function invokeSwap(
function invokeSwap(
ISetToken _setToken,
ISetToken _setToken,
IQuoter _quoter,
IQuoter _quoter,
IQuoter.SwapParams memory _params
IQuoter.SwapParams memory _params
)
)
复制
已复制
复制
已复制
external
public
returns (IQuoter.SwapResponse memory)
returns (IQuoter.SwapResponse memory)
{
{
( , , bytes memory swapCalldata) = getSwapCalldata(
( , , bytes memory swapCalldata) = getSwapCalldata(
_quoter,
_quoter,
_params
_params
);
);
bytes memory returnValue = _setToken.invoke(address(_quoter), 0, swapCalldata);
bytes memory returnValue = _setToken.invoke(address(_quoter), 0, swapCalldata);
return abi.decode(returnValue, (IQuoter.SwapResponse));
return abi.decode(returnValue, (IQuoter.SwapResponse));
}
}
复制
已复制
复制
已复制
/**
* @dev Formats Perp Periphery Quoter.swap call and executes via SetToken.
*
* See `executeTrade` method comments for details about `isBaseToQuote` and `isExactInput` configuration.
*
* @param _perpQuoter Instance of PerpV2 quoter
* @param _actionInfo ActionInfo object
* @return uint256 The base position delta resulting from the trade
* @return uint256 The quote asset position delta resulting from the trade
*/
function simulateTrade(ActionInfo memory _actionInfo, IQuoter _perpQuoter) external returns (uint256, uint256) {
IQuoter.SwapParams memory params = IQuoter.SwapParams({
baseToken: _actionInfo.baseToken,
isBaseToQuote: !_actionInfo.isBuy,
isExactInput: !_actionInfo.isBuy,
amount: _actionInfo.baseTokenAmount,
sqrtPriceLimitX96: 0
});
IQuoter.SwapResponse memory swapResponse = invokeSwap(_actionInfo.setToken, _perpQuoter, params);
return (swapResponse.deltaAvailableBase, swapResponse.deltaAvailableQuote);
}
/**
* @dev Formats Perp Protocol openPosition call and executes via SetToken.
*
* `isBaseToQuote`, `isExactInput` and `oppositeAmountBound` are configured as below:
* | ---------------------------------------------------|---------------------------- |
* | Action | isBuy | isB2Q | Exact In / Out | Opposite Bound Description |
* | ------- |-------- |--------|-----------------------|---------------------------- |
* | Buy | true | false | exact output (false) | Max quote to pay |
* | Sell | false | true | exact input (true) | Min quote to receive |
* |----------------------------------------------------|---------------------------- |
*
* @param _perpClearingHouse Instance of PerpV2 ClearingHouse
* @param _actionInfo PerpV2.ActionInfo object
* @return uint256 The base position delta resulting from the trade
* @return uint256 The quote asset position delta resulting from the trade
*/
function executeTrade(
ActionInfo memory _actionInfo,
IClearingHouse _perpClearingHouse
)
external
returns (uint256, uint256)
{
// When isBaseToQuote is true, `baseToken` is being sold, when false, bought
// When isExactInput is true, `amount` is the swap input, when false, the swap output
IClearingHouse.OpenPositionParams memory params = IClearingHouse.OpenPositionParams({
baseToken: _actionInfo.baseToken,
isBaseToQuote: !_actionInfo.isBuy,
isExactInput: !_actionInfo.isBuy,
amount: _actionInfo.baseTokenAmount,
oppositeAmountBound: _actionInfo.oppositeAmountBound,
deadline: PreciseUnitMath.maxUint256(),
sqrtPriceLimitX96: 0,
referralCode: bytes32(0)
});
return invokeOpenPosition(_actionInfo.setToken, _perpClearingHouse, params);
}
}
}
已保存差异
原始文本
打开文件
/* Copyright 2021 Set Labs Inc. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. SPDX-License-Identifier: Apache License, Version 2.0 */ pragma solidity 0.6.10; pragma experimental ABIEncoderV2; import { IERC20 } from "@openzeppelin/contracts/token/ERC20/IERC20.sol"; import { IClearingHouse } from "../../../interfaces/external/perp-v2/IClearingHouse.sol"; import { IVault } from "../../../interfaces/external/perp-v2/IVault.sol"; import { IQuoter } from "../../../interfaces/external/perp-v2/IQuoter.sol"; import { ISetToken } from "../../../interfaces/ISetToken.sol"; /** * @title PerpV2 * @author Set Protocol * * Collection of helper functions for interacting with PerpV2 integrations. */ library PerpV2 { /* ============ External ============ */ /** * Gets Perp vault `deposit` calldata * * When invoked, calldata deposits an `_amountNotional` of collateral asset into the Perp Protocol vault * * @param _vault Perp protocol vault * @param _asset Collateral asset to deposit * @param _amountNotional Notional amount in collateral decimals to deposit * * @return address Vault address * @return uint256 Call value * @return calldata Deposit calldata */ function getDepositCalldata( IVault _vault, IERC20 _asset, uint256 _amountNotional ) public pure returns (address, uint256, bytes memory) { bytes memory callData = abi.encodeWithSignature( "deposit(address,uint256)", _asset, _amountNotional ); return (address(_vault), 0, callData); } /** * Invoke `deposit` on Vault from SetToken * * Deposits an `_amountNotional` of collateral asset into the Perp Protocol vault * * @param _setToken Address of the SetToken * @param _vault Address of Perp Protocol vault contract * @param _asset The address of the collateral asset to deposit * @param _amountNotional Notional amount in collateral decimals to deposit */ function invokeDeposit( ISetToken _setToken, IVault _vault, IERC20 _asset, uint256 _amountNotional ) external { ( , , bytes memory depositCalldata) = getDepositCalldata( _vault, _asset, _amountNotional ); _setToken.invoke(address(_vault), 0, depositCalldata); } /** * Get Perp Vault `withdraw` method calldata * * When invoked, calldata withdraws an `_amountNotional` of collateral asset from the Perp protocol vault * * @param _vault Address of the Perp Protocol vault contract * @param _asset The address of the collateral asset to withdraw * @param _amountNotional The notional amount in collateral decimals to be withdrawn * * @return address Vault contract address * @return uint256 Call value * @return bytes Withdraw calldata */ function getWithdrawCalldata( IVault _vault, IERC20 _asset, uint256 _amountNotional ) public pure returns (address, uint256, bytes memory) { bytes memory callData = abi.encodeWithSignature( "withdraw(address,uint256)", _asset, _amountNotional ); return (address(_vault), 0, callData); } /** * Invoke `withdraw` on Vault from SetToken * * Withdraws an `_amountNotional` of collateral asset from the Perp protocol vault * * @param _setToken Address of the SetToken * @param _vault Address of the Perp Protocol vault contract * @param _asset The address of the collateral asset to withdraw * @param _amountNotional The notional amount in collateral decimals to be withdrawn * */ function invokeWithdraw( ISetToken _setToken, IVault _vault, IERC20 _asset, uint256 _amountNotional ) external { ( , , bytes memory withdrawCalldata) = getWithdrawCalldata( _vault, _asset, _amountNotional ); _setToken.invoke(address(_vault), 0, withdrawCalldata); } /** * Get Perp ClearingHouse `openPosition` method calldata * * When invoked, calldata executes a trade via the Perp protocol ClearingHouse contract * * @param _clearingHouse Address of the Clearinghouse contract * @param _params OpenPositionParams struct. For details see definition * in contracts/interfaces/external/perp-v2/IClearingHouse.sol * * @return address ClearingHouse contract address * @return uint256 Call value * @return bytes `openPosition` calldata */ function getOpenPositionCalldata( IClearingHouse _clearingHouse, IClearingHouse.OpenPositionParams memory _params ) public pure returns (address, uint256, bytes memory) { bytes memory callData = abi.encodeWithSignature( "openPosition((address,bool,bool,uint256,uint256,uint256,uint160,bytes32))", _params ); return (address(_clearingHouse), 0, callData); } /** * Invoke `openPosition` on ClearingHouse from SetToken * * Executes a trade via the Perp protocol ClearingHouse contract * * @param _setToken Address of the SetToken * @param _clearingHouse Address of the Clearinghouse contract * @param _params OpenPositionParams struct. For details see definition * in contracts/interfaces/external/perp-v2/IClearingHouse.sol * * @return deltaBase Positive or negative change in base token balance resulting from trade * @return deltaQuote Positive or negative change in quote token balance resulting from trade */ function invokeOpenPosition( ISetToken _setToken, IClearingHouse _clearingHouse, IClearingHouse.OpenPositionParams memory _params ) external returns (uint256 deltaBase, uint256 deltaQuote) { ( , , bytes memory openPositionCalldata) = getOpenPositionCalldata( _clearingHouse, _params ); bytes memory returnValue = _setToken.invoke(address(_clearingHouse), 0, openPositionCalldata); return abi.decode(returnValue, (uint256,uint256)); } /** * Get Perp Quoter `swap` method calldata * * When invoked, calldata simulates a trade on the Perp exchange via the Perp periphery contract Quoter * * @param _quoter Address of the Quoter contract * @param _params SwapParams struct. For details see definition * in contracts/interfaces/external/perp-v2/IQuoter.sol * * @return address ClearingHouse contract address * @return uint256 Call value * @return bytes `swap` calldata */ function getSwapCalldata( IQuoter _quoter, IQuoter.SwapParams memory _params ) public pure returns (address, uint256, bytes memory) { bytes memory callData = abi.encodeWithSignature( "swap((address,bool,bool,uint256,uint160))", _params ); return (address(_quoter), 0, callData); } /** * Invoke `swap` method on Perp Quoter contract * * Simulates a trade on the Perp exchange via the Perp periphery contract Quoter * * @param _setToken Address of the SetToken * @param _quoter Address of the Quoter contract * @param _params SwapParams struct. For details see definition * in contracts/interfaces/external/perp-v2/IQuoter.sol * * @return swapResponse Struct which includes deltaAvailableBase and deltaAvailableQuote * properties (equiv. to deltaQuote, deltaBase) returned from `openPostion` */ function invokeSwap( ISetToken _setToken, IQuoter _quoter, IQuoter.SwapParams memory _params ) external returns (IQuoter.SwapResponse memory) { ( , , bytes memory swapCalldata) = getSwapCalldata( _quoter, _params ); bytes memory returnValue = _setToken.invoke(address(_quoter), 0, swapCalldata); return abi.decode(returnValue, (IQuoter.SwapResponse)); } }
更改后文本
打开文件
/* Copyright 2022 Set Labs Inc. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. SPDX-License-Identifier: Apache License, Version 2.0 */ pragma solidity 0.6.10; pragma experimental ABIEncoderV2; import { IERC20 } from "@openzeppelin/contracts/token/ERC20/IERC20.sol"; import { IClearingHouse } from "../../../interfaces/external/perp-v2/IClearingHouse.sol"; import { IQuoter } from "../../../interfaces/external/perp-v2/IQuoter.sol"; import { IVault } from "../../../interfaces/external/perp-v2/IVault.sol"; import { ISetToken } from "../../../interfaces/ISetToken.sol"; import { PreciseUnitMath } from "../../../lib/PreciseUnitMath.sol"; /** * @title PerpV2LibraryV2 * @author Set Protocol * * Collection of helper functions for interacting with PerpV2 integrations. * * CHANGELOG: * - Add ActionInfo struct. * - Add `executeTrade` and `simulateTrade` functions. */ library PerpV2LibraryV2 { struct ActionInfo { ISetToken setToken; address baseToken; // Virtual token minted by the Perp protocol bool isBuy; // When true, `baseToken` is being bought, when false, sold uint256 baseTokenAmount; // Base token quantity in 10**18 decimals uint256 oppositeAmountBound; // vUSDC pay or receive quantity bound // (see `PerpV2LeverageModuleV2#_createActionInfoNotional` for details) } /* ============ External ============ */ /** * Gets Perp vault `deposit` calldata * * When invoked, calldata deposits an `_amountNotional` of collateral asset into the Perp Protocol vault * * @param _vault Perp protocol vault * @param _asset Collateral asset to deposit * @param _amountNotional Notional amount in collateral decimals to deposit * * @return address Vault address * @return uint256 Call value * @return calldata Deposit calldata */ function getDepositCalldata( IVault _vault, IERC20 _asset, uint256 _amountNotional ) public pure returns (address, uint256, bytes memory) { bytes memory callData = abi.encodeWithSignature( "deposit(address,uint256)", _asset, _amountNotional ); return (address(_vault), 0, callData); } /** * Invoke `deposit` on Vault from SetToken * * Deposits an `_amountNotional` of collateral asset into the Perp Protocol vault * * @param _setToken Address of the SetToken * @param _vault Address of Perp Protocol vault contract * @param _asset The address of the collateral asset to deposit * @param _amountNotional Notional amount in collateral decimals to deposit */ function invokeDeposit( ISetToken _setToken, IVault _vault, IERC20 _asset, uint256 _amountNotional ) external { ( , , bytes memory depositCalldata) = getDepositCalldata( _vault, _asset, _amountNotional ); _setToken.invoke(address(_vault), 0, depositCalldata); } /** * Get Perp Vault `withdraw` method calldata * * When invoked, calldata withdraws an `_amountNotional` of collateral asset from the Perp protocol vault * * @param _vault Address of the Perp Protocol vault contract * @param _asset The address of the collateral asset to withdraw * @param _amountNotional The notional amount in collateral decimals to be withdrawn * * @return address Vault contract address * @return uint256 Call value * @return bytes Withdraw calldata */ function getWithdrawCalldata( IVault _vault, IERC20 _asset, uint256 _amountNotional ) public pure returns (address, uint256, bytes memory) { bytes memory callData = abi.encodeWithSignature( "withdraw(address,uint256)", _asset, _amountNotional ); return (address(_vault), 0, callData); } /** * Invoke `withdraw` on Vault from SetToken * * Withdraws an `_amountNotional` of collateral asset from the Perp protocol vault * * @param _setToken Address of the SetToken * @param _vault Address of the Perp Protocol vault contract * @param _asset The address of the collateral asset to withdraw * @param _amountNotional The notional amount in collateral decimals to be withdrawn * */ function invokeWithdraw( ISetToken _setToken, IVault _vault, IERC20 _asset, uint256 _amountNotional ) external { ( , , bytes memory withdrawCalldata) = getWithdrawCalldata( _vault, _asset, _amountNotional ); _setToken.invoke(address(_vault), 0, withdrawCalldata); } /** * Get Perp ClearingHouse `openPosition` method calldata * * When invoked, calldata executes a trade via the Perp protocol ClearingHouse contract * * @param _clearingHouse Address of the Clearinghouse contract * @param _params OpenPositionParams struct. For details see definition * in contracts/interfaces/external/perp-v2/IClearingHouse.sol * * @return address ClearingHouse contract address * @return uint256 Call value * @return bytes `openPosition` calldata */ function getOpenPositionCalldata( IClearingHouse _clearingHouse, IClearingHouse.OpenPositionParams memory _params ) public pure returns (address, uint256, bytes memory) { bytes memory callData = abi.encodeWithSignature( "openPosition((address,bool,bool,uint256,uint256,uint256,uint160,bytes32))", _params ); return (address(_clearingHouse), 0, callData); } /** * Invoke `openPosition` on ClearingHouse from SetToken * * Executes a trade via the Perp protocol ClearingHouse contract * * @param _setToken Address of the SetToken * @param _clearingHouse Address of the Clearinghouse contract * @param _params OpenPositionParams struct. For details see definition * in contracts/interfaces/external/perp-v2/IClearingHouse.sol * * @return deltaBase Positive or negative change in base token balance resulting from trade * @return deltaQuote Positive or negative change in quote token balance resulting from trade */ function invokeOpenPosition( ISetToken _setToken, IClearingHouse _clearingHouse, IClearingHouse.OpenPositionParams memory _params ) public returns (uint256 deltaBase, uint256 deltaQuote) { ( , , bytes memory openPositionCalldata) = getOpenPositionCalldata( _clearingHouse, _params ); bytes memory returnValue = _setToken.invoke(address(_clearingHouse), 0, openPositionCalldata); return abi.decode(returnValue, (uint256,uint256)); } /** * Get Perp Quoter `swap` method calldata * * When invoked, calldata simulates a trade on the Perp exchange via the Perp periphery contract Quoter * * @param _quoter Address of the Quoter contract * @param _params SwapParams struct. For details see definition * in contracts/interfaces/external/perp-v2/IQuoter.sol * * @return address ClearingHouse contract address * @return uint256 Call value * @return bytes `swap` calldata */ function getSwapCalldata( IQuoter _quoter, IQuoter.SwapParams memory _params ) public pure returns (address, uint256, bytes memory) { bytes memory callData = abi.encodeWithSignature( "swap((address,bool,bool,uint256,uint160))", _params ); return (address(_quoter), 0, callData); } /** * Invoke `swap` method on Perp Quoter contract * * Simulates a trade on the Perp exchange via the Perp periphery contract Quoter * * @param _setToken Address of the SetToken * @param _quoter Address of the Quoter contract * @param _params SwapParams struct. For details see definition * in contracts/interfaces/external/perp-v2/IQuoter.sol * * @return swapResponse Struct which includes deltaAvailableBase and deltaAvailableQuote * properties (equiv. to deltaQuote, deltaBase) returned from `openPostion` */ function invokeSwap( ISetToken _setToken, IQuoter _quoter, IQuoter.SwapParams memory _params ) public returns (IQuoter.SwapResponse memory) { ( , , bytes memory swapCalldata) = getSwapCalldata( _quoter, _params ); bytes memory returnValue = _setToken.invoke(address(_quoter), 0, swapCalldata); return abi.decode(returnValue, (IQuoter.SwapResponse)); } /** * @dev Formats Perp Periphery Quoter.swap call and executes via SetToken. * * See `executeTrade` method comments for details about `isBaseToQuote` and `isExactInput` configuration. * * @param _perpQuoter Instance of PerpV2 quoter * @param _actionInfo ActionInfo object * @return uint256 The base position delta resulting from the trade * @return uint256 The quote asset position delta resulting from the trade */ function simulateTrade(ActionInfo memory _actionInfo, IQuoter _perpQuoter) external returns (uint256, uint256) { IQuoter.SwapParams memory params = IQuoter.SwapParams({ baseToken: _actionInfo.baseToken, isBaseToQuote: !_actionInfo.isBuy, isExactInput: !_actionInfo.isBuy, amount: _actionInfo.baseTokenAmount, sqrtPriceLimitX96: 0 }); IQuoter.SwapResponse memory swapResponse = invokeSwap(_actionInfo.setToken, _perpQuoter, params); return (swapResponse.deltaAvailableBase, swapResponse.deltaAvailableQuote); } /** * @dev Formats Perp Protocol openPosition call and executes via SetToken. * * `isBaseToQuote`, `isExactInput` and `oppositeAmountBound` are configured as below: * | ---------------------------------------------------|---------------------------- | * | Action | isBuy | isB2Q | Exact In / Out | Opposite Bound Description | * | ------- |-------- |--------|-----------------------|---------------------------- | * | Buy | true | false | exact output (false) | Max quote to pay | * | Sell | false | true | exact input (true) | Min quote to receive | * |----------------------------------------------------|---------------------------- | * * @param _perpClearingHouse Instance of PerpV2 ClearingHouse * @param _actionInfo PerpV2.ActionInfo object * @return uint256 The base position delta resulting from the trade * @return uint256 The quote asset position delta resulting from the trade */ function executeTrade( ActionInfo memory _actionInfo, IClearingHouse _perpClearingHouse ) external returns (uint256, uint256) { // When isBaseToQuote is true, `baseToken` is being sold, when false, bought // When isExactInput is true, `amount` is the swap input, when false, the swap output IClearingHouse.OpenPositionParams memory params = IClearingHouse.OpenPositionParams({ baseToken: _actionInfo.baseToken, isBaseToQuote: !_actionInfo.isBuy, isExactInput: !_actionInfo.isBuy, amount: _actionInfo.baseTokenAmount, oppositeAmountBound: _actionInfo.oppositeAmountBound, deadline: PreciseUnitMath.maxUint256(), sqrtPriceLimitX96: 0, referralCode: bytes32(0) }); return invokeOpenPosition(_actionInfo.setToken, _perpClearingHouse, params); } }
查找差异