Diff
checker
Text
Text
Bilder
Dokumente
Excel
Ordner
Legal
Enterprise
Desktop-App
Preise
Einloggen
Diffchecker Desktop herunterladen
Texte vergleichen
Finde den Unterschied zwischen zwei Textdateien
Werkzeuge
Verlauf
Live-Editor
Leerzeichen ausblenden
Gleiches ausblenden
Zeilenumbruch aus
Ansicht
Zweispaltig
Einspaltig
Vergleichsgenauigkeit
Intelligent
Wort
Zeichen
Textstile
Darstellung ändern
Syntaxhervorhebung
Syntax auswählen
Ignorieren
Text umwandeln
Zur ersten Änderung
Eingabe bearbeiten
Diffchecker Desktop
Der sicherste Weg, Diffchecker zu nutzen. Hol dir die Desktop-App: Deine Diffs verlassen nie deinen Computer!
Desktop holen
PerpV2 <> PerpV2LibraryV2
Erstellt
vor 4 Jahren
Diff läuft nie ab
Löschen
Exportieren
Teilen
Erklären
6 Entfernungen
Zeilen
Gesamt
Entfernt
Zeichen
Gesamt
Entfernt
Um diese Funktion weiterhin zu nutzen, aktualisiere auf
Diff
checker
Pro
Preise anzeigen
267 Zeilen
Kopieren
87 Hinzufügungen
Zeilen
Gesamt
Hinzugefügt
Zeichen
Gesamt
Hinzugefügt
Um diese Funktion weiterhin zu nutzen, aktualisiere auf
Diff
checker
Pro
Preise anzeigen
347 Zeilen
Kopieren
/*
/*
Kopieren
Kopiert
Kopieren
Kopiert
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
Kopieren
Kopiert
Kopieren
Kopiert
http://www.apache.org/licenses/LICENSE-2.0
http://www.apache.org/licenses/LICENSE-2.0
Kopieren
Kopiert
Kopieren
Kopiert
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.
Kopieren
Kopiert
Kopieren
Kopiert
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";
Kopieren
Kopiert
Kopieren
Kopiert
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";
Kopieren
Kopiert
Kopieren
Kopiert
import { IVault } from "../../../interfaces/external/perp-v2/IVault.sol";
import { ISetToken } from "../../../interfaces/ISetToken.sol";
import { ISetToken } from "../../../interfaces/ISetToken.sol";
Kopieren
Kopiert
Kopieren
Kopiert
import { PreciseUnitMath } from "../../../lib/PreciseUnitMath.sol";
/**
/**
Kopieren
Kopiert
Kopieren
Kopiert
* @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.
Kopieren
Kopiert
Kopieren
Kopiert
*
* CHANGELOG:
* - Add ActionInfo struct.
* - Add `executeTrade` and `simulateTrade` functions.
*/
*/
Kopieren
Kopiert
Kopieren
Kopiert
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
)
)
Kopieren
Kopiert
Kopieren
Kopiert
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
)
)
Kopieren
Kopiert
Kopieren
Kopiert
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));
}
}
Kopieren
Kopiert
Kopieren
Kopiert
/**
* @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);
}
}
}
Gespeicherte Diffs
Originaltext
Datei öffnen
/* 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)); } }
Bearbeitung
Datei öffnen
/* 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); } }
Unterschied finden