Skip to content

[WIP] Leveraged Basis trade vault #64

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Apr 7, 2025
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ broadcast/
docs/

# Dotenv file
.env
../../socket/socket-plugs/.env

# IDE files
.idea/
.vscode/

lcov.info

Expand Down
4 changes: 4 additions & 0 deletions deployments/901/LBTCB.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"implementation": "0x499DFB45735907E2ea447006F66340709366f2aB",
"token": "0x0Eb95740A626500f80E1652186E32416477880b5"
}
3 changes: 2 additions & 1 deletion deployments/901/matching.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@
"transfer": "0x0CFC1a4a90741aB242cAfaCD798b409E12e68926",
"withdrawal": "0xe850641C5207dc5E9423fB15f89ae6031A05fd92",
"rfq": "0x4E4DD8Be1e461913D9A5DBC4B830e67a8694ebCa",
"liquidate": "0xfC3d7465C6Aa57d7Ba39ed70F143F89E1f823f37"
"liquidate": "0xfC3d7465C6Aa57d7Ba39ed70F143F89E1f823f37",
"tsaShareHandler": "0x05bf05e4D19DaF3234d3aE287B32212924b551Fe"
}
5 changes: 5 additions & 0 deletions deployments/901/weETHB.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"implementation": "0x6bF2f4896406d7f023245f3FAcbee9200E034D9f",
"token": "0x9ffB0Ee4447c491E3c842b62184BB0669A12b9e4",
"proxyAdmin": "0xed13a133ae5bf55de6e5e2f9908cdf298972c4ce"
}
3 changes: 2 additions & 1 deletion deployments/957/matching.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@
"transfer": "0x01259207A40925b794C8ac320456F7F6c8FE2636",
"withdrawal": "0x9d0E8f5b25384C7310CB8C6aE32C8fbeb645d083",
"liquidate": "0x66d23e59DaEEF13904eFA2D4B8658aeD05f59a92",
"rfq": "0x9371352CCef6f5b36EfDFE90942fFE622Ab77F1D"
"rfq": "0x9371352CCef6f5b36EfDFE90942fFE622Ab77F1D",
"tsaShareHandler": "0x4B759054CF4DA826e48645EfaDEfE9AfA613FDc5"
}
1 change: 1 addition & 0 deletions deployments/state.txt

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion foundry.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ optimizer = true
optimizer_runs = 50
fs_permissions = [{access = "read-write", path = "./"}]

solc_version="0.8.20"
solc_version="0.8.27"

evm_version="shanghai"

Expand Down
26 changes: 16 additions & 10 deletions scripts/deploy-all.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import {SubAccountCreator} from "../src/periphery/SubAccountCreator.sol";
import {LyraSettlementUtils} from "../src/periphery/LyraSettlementUtils.sol";
import {LyraAuctionUtils} from "../src/periphery/LyraAuctionUtils.sol";
import {DutchAuction} from "v2-core/src/liquidation/DutchAuction.sol";
import {TSAShareHandler} from "../src/tokenizedSubaccounts/TSAShareHandler.sol";
import {AtomicSigningExecutor} from "../src/AtomicSigningExecutor.sol";
import {ISubAccounts} from "v2-core/src/interfaces/ISubAccounts.sol";
import {IAsset} from "v2-core/src/interfaces/IAsset.sol";
import {ICashAsset} from "v2-core/src/interfaces/ICashAsset.sol";
Expand Down Expand Up @@ -49,6 +51,9 @@ contract DeployAll is Utils {

deployment.matching = new Matching(ISubAccounts(config.subAccounts));

////
// Modules

deployment.deposit = new DepositModule(deployment.matching);
deployment.trade = new TradeModule(deployment.matching, IAsset(config.cash), defaultFeeRecipient);
deployment.transfer = new TransferModule(deployment.matching);
Expand All @@ -64,21 +69,20 @@ contract DeployAll is Utils {
deployment.matching.setAllowedModule(address(deployment.liquidate), true);
deployment.matching.setAllowedModule(address(deployment.rfq), true);

deployment.matching.setTradeExecutor(0xf00A105BC009eA3a250024cbe1DCd0509c71C52b, true);

console2.log("subAccounts", address(config.subAccounts));
console2.log("cash", address(config.cash));

deployment.subAccountCreator = new SubAccountCreator(ISubAccounts(config.subAccounts), deployment.matching);
console2.log("subAccountCreator: ", address(deployment.subAccountCreator));
/////
// helpers

deployment.settlementUtil = new LyraSettlementUtils();
console2.log("settlementUtil: ", address(deployment.settlementUtil));

deployment.subAccountCreator = new SubAccountCreator(ISubAccounts(config.subAccounts), deployment.matching);
deployment.auctionUtil = new LyraAuctionUtils(
ISubAccounts(config.subAccounts), DutchAuction(config.auction), config.srm
);
console2.log("settlementUtil: ", address(deployment.settlementUtil));
deployment.tsaShareHandler = new TSAShareHandler();
deployment.atomicSigningExecutor = new AtomicSigningExecutor(deployment.matching);

// whitelist helpers
deployment.matching.setTradeExecutor(0xf00A105BC009eA3a250024cbe1DCd0509c71C52b, true);
deployment.matching.setTradeExecutor(address(deployment.atomicSigningExecutor), true);

// write to output
__writeToDeploymentsJson(deployment);
Expand All @@ -102,6 +106,8 @@ contract DeployAll is Utils {
vm.serializeAddress(objKey, "auctionUtil", address(deployment.auctionUtil));
vm.serializeAddress(objKey, "liquidate", address(deployment.liquidate));
vm.serializeAddress(objKey, "rfq", address(deployment.rfq));
vm.serializeAddress(objKey, "tsaShareHandler", address(deployment.tsaShareHandler));
vm.serializeAddress(objKey, "atomicSigningExecutor", address(deployment.atomicSigningExecutor));

string memory finalObj = vm.serializeAddress(objKey, "withdrawal", address(deployment.withdrawal));

Expand Down
5 changes: 3 additions & 2 deletions scripts/deploy-and-dump.sh
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ cp lib/v2-core/deployments/$chainId/strands.json deployments/$chainId/strands.js
cp lib/v2-core/deployments/$chainId/SFP.json deployments/$chainId/SFP.json
cp lib/v2-core/deployments/$chainId/SOL.json deployments/$chainId/SOL.json
cp lib/v2-core/deployments/$chainId/DOGE.json deployments/$chainId/DOGE.json
cp lib/v2-core/scripts/input/$chainId/config.json deployments/$chainId/shared.json

cp lib/v2-core/deployments/$chainId/shared.json deployments/$chainId/shared.json

echo "Deployed core contracts"

# Deploy matching contracts
forge script scripts/deploy-all.s.sol --rpc-url $ETH_RPC_URL --broadcast
Expand All @@ -59,6 +59,7 @@ MARKET_NAME=SOL forge script scripts/add-perp-to-modules.s.sol --rpc-url $ETH_RP
MARKET_NAME=DOGE forge script scripts/add-perp-to-modules.s.sol --rpc-url $ETH_RPC_URL --broadcast

forge script scripts/deploy-tsa.s.sol --rpc-url $ETH_RPC_URL --broadcast
DEPOSIT_ASSET=WSTETH PERP_ASSET=ETH forge script scripts/deploy-lbtsa.s.sol --rpc-url $ETH_RPC_URL --broadcast

if [[ "$ETH_RPC_URL" =~ .*local.* ]]; then
forge script scripts/update-callees.s.sol --rpc-url $ETH_RPC_URL --broadcast
Expand Down
56 changes: 56 additions & 0 deletions scripts/deploy-atomic-signer.s.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// SPDX-License-Identifier: GPL-3.0-only
pragma solidity ^0.8.0;


import {Matching} from "../src/Matching.sol";
import {DepositModule} from "../src/modules/DepositModule.sol";
import {TradeModule} from "../src/modules/TradeModule.sol";
import {TransferModule} from "../src/modules/TransferModule.sol";
import {LiquidateModule} from "../src/modules/LiquidateModule.sol";
import {RfqModule} from "../src/modules/RfqModule.sol";
import {WithdrawalModule} from "../src/modules/WithdrawalModule.sol";
import {SubAccountCreator} from "../src/periphery/SubAccountCreator.sol";
import {LyraSettlementUtils} from "../src/periphery/LyraSettlementUtils.sol";
import {LyraAuctionUtils} from "../src/periphery/LyraAuctionUtils.sol";
import {DutchAuction} from "v2-core/src/liquidation/DutchAuction.sol";
import {TSAShareHandler} from "../src/tokenizedSubaccounts/TSAShareHandler.sol";
import {AtomicSigningExecutor} from "../src/AtomicSigningExecutor.sol";
import {ISubAccounts} from "v2-core/src/interfaces/ISubAccounts.sol";
import {IAsset} from "v2-core/src/interfaces/IAsset.sol";
import {ICashAsset} from "v2-core/src/interfaces/ICashAsset.sol";

import "forge-std/console.sol";
import {Deployment, NetworkConfig} from "./types.sol";
import {Utils} from "./utils.sol";


contract DeployAtomicSigner is Utils {

/// @dev main function
function run() external {

uint256 deployerPrivateKey = vm.envUint("PRIVATE_KEY");
vm.startBroadcast(deployerPrivateKey);

address deployer = vm.addr(deployerPrivateKey);
console.log("Start deploying matching contract and modules! deployer: ", deployer);

_deployAllContracts();

vm.stopBroadcast();
}


/// @dev deploy and initiate contracts
function _deployAllContracts() internal {
uint defaultFeeRecipient = 1;

Matching matching = Matching(0x3cc154e220c2197c5337b7Bd13363DD127Bc0C6E);

AtomicSigningExecutor atomicSigningExecutor = new AtomicSigningExecutor(matching);

matching.setTradeExecutor(address(atomicSigningExecutor), true);

console.log("AtomicSigningExecutor deployed at: ", address(atomicSigningExecutor));
}
}
130 changes: 130 additions & 0 deletions scripts/deploy-lbtsa.s.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
// SPDX-License-Identifier: GPL-3.0-only
pragma solidity ^0.8.0;

import "forge-std/console.sol";
import {Utils} from "./utils.sol";
import "../src/periphery/LyraSettlementUtils.sol";
import {BaseTSA} from "../src/tokenizedSubaccounts/BaseTSA.sol";
import {ISubAccounts} from "v2-core/src/interfaces/ISubAccounts.sol";
import {CashAsset} from "v2-core/src/assets/CashAsset.sol";
import {DutchAuction} from "v2-core/src/liquidation/DutchAuction.sol";
import {ILiquidatableManager} from "v2-core/src/interfaces/ILiquidatableManager.sol";
import {IMatching} from "../src/interfaces/IMatching.sol";
import {IDepositModule} from "../src/interfaces/IDepositModule.sol";
import {IWithdrawalModule} from "../src/interfaces/IWithdrawalModule.sol";
import {ITradeModule} from "../src/interfaces/ITradeModule.sol";
import {ISpotFeed} from "v2-core/src/interfaces/ISpotFeed.sol";
import {IWrappedERC20Asset} from "v2-core/src/interfaces/IWrappedERC20Asset.sol";
import {IPerpAsset} from "v2-core/src/interfaces/IPerpAsset.sol";
import "openzeppelin/proxy/transparent/TransparentUpgradeableProxy.sol";
import {TokenizedSubAccount} from "../src/tokenizedSubaccounts/TSA.sol";
import "openzeppelin/proxy/transparent/ProxyAdmin.sol";
import {TSAShareHandler} from "../src/tokenizedSubaccounts/TSAShareHandler.sol";
import {LeveragedBasisTSA, CollateralManagementTSA} from "../src/tokenizedSubaccounts/LevBasisTSA.sol";


contract DeployLBTSA is Utils {

LeveragedBasisTSA.CollateralManagementParams public defaultCollateralManagementParams = CollateralManagementTSA
.CollateralManagementParams({
feeFactor: 0.01e17,
spotTransactionLeniency: 1.01e18,
worstSpotSellPrice: 0.985e18,
worstSpotBuyPrice: 1.015e18
});

LeveragedBasisTSA.LBTSAParams public defaultLbtsaTSAParams = LeveragedBasisTSA.LBTSAParams({
maxPerpFee: 0.01e18,
maxBaseLossPerBase: 0.02e18,
maxBaseLossPerPerp: 0.02e18,
deltaFloor: 0.5e18,
deltaCeil: 1.5e18,
leverageFloor: 0.9e18,
leverageCeil: 3e18,
emaDecayFactor: 0.0002e18,
markLossEmaTarget: 0.02e18,
minSignatureExpiry: 60,
maxSignatureExpiry: 60 * 60 * 24 * 7
});

/// @dev main function
function run() external {
deployLBTSA();
}

function deployLBTSA() private {
string memory marketName = vm.envString("DEPOSIT_ASSET");
string memory vaultTokenName = string.concat(marketName, "B");
string memory perpMarketName = vm.envString("PERP_ASSET");

uint256 deployerPrivateKey = vm.envUint("PRIVATE_KEY");
vm.startBroadcast(deployerPrivateKey);

address deployer = vm.addr(deployerPrivateKey);
console.log("deployer: ", deployer);

LeveragedBasisTSA lbtsaImplementation = new LeveragedBasisTSA();
ProxyAdmin proxyAdmin = new ProxyAdmin();

TransparentUpgradeableProxy proxy = new TransparentUpgradeableProxy(
address(lbtsaImplementation),
address(proxyAdmin),
abi.encodeWithSelector(
lbtsaImplementation.initialize.selector,
deployer,
BaseTSA.BaseTSAInitParams({
subAccounts: ISubAccounts(_getCoreContract("subAccounts")),
auction: DutchAuction(_getCoreContract("auction")),
cash: CashAsset(_getCoreContract("cash")),
wrappedDepositAsset: IWrappedERC20Asset(_getMarketAddress(marketName, "base")),
manager: ILiquidatableManager(_getCoreContract("srm")),
matching: IMatching(_getMatchingModule("matching")),
symbol: vaultTokenName,
name: string.concat(marketName, " Basis Trade")
}),
LeveragedBasisTSA.LBTSAInitParams({
baseFeed: ISpotFeed(_getMarketAddress(marketName, "spotFeed")),
depositModule: IDepositModule(_getMatchingModule("deposit")),
withdrawalModule: IWithdrawalModule(_getMatchingModule("withdrawal")),
tradeModule: ITradeModule(_getMatchingModule("trade")),
perpAsset: IPerpAsset(_getMarketAddress(perpMarketName, "perp"))
})
)
);

LeveragedBasisTSA lbtsa = LeveragedBasisTSA(address(proxy));

lbtsa.setTSAParams(
BaseTSA.TSAParams({
depositCap: 10000000e18,
minDepositValue: 0,
depositScale: 1e18,
// slight withdrawal fee
withdrawScale: 0.998e18,
managementFee: 0,
feeRecipient: address(0)
})
);
lbtsa.setLBTSAParams(defaultLbtsaTSAParams);
lbtsa.setCollateralManagementParams(defaultCollateralManagementParams);

string memory objKey = "tsa-deployment";
vm.serializeAddress(objKey, "token", address(proxy));
string memory finalObj = vm.serializeAddress(objKey, "implementation", address(lbtsaImplementation));

// build path
_writeToDeployments(vaultTokenName, finalObj);
}

function _getMatchingModule(string memory module) internal returns (address) {
return abi.decode(vm.parseJson(_readMatchingDeploymentFile("matching"), string.concat(".", module)), (address));
}

function _getMarketAddress(string memory marketName, string memory contractName) internal returns (address) {
return abi.decode(vm.parseJson(_readV2CoreDeploymentFile(marketName), string.concat(".", contractName)), (address));
}

function _getCoreContract(string memory contractName) internal returns (address) {
return abi.decode(vm.parseJson(_readV2CoreDeploymentFile("core"), string.concat(".", contractName)), (address));
}
}
32 changes: 14 additions & 18 deletions scripts/deploy-tsa.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,15 @@ contract DeployTSA is Utils {

CollateralManagementTSA.CollateralManagementParams public defaultCollateralManagementParams = CollateralManagementTSA
.CollateralManagementParams({
feeFactor: 10000000000000000,
spotTransactionLeniency: 1050000000000000000,
worstSpotSellPrice: 985000000000000000,
worstSpotBuyPrice: 1015000000000000000
feeFactor: 0.01e17,
spotTransactionLeniency: 1.01e18,
worstSpotSellPrice: 0.985e18,
worstSpotBuyPrice: 1.015e18
});

CoveredCallTSA.CCTSAParams public defaultLrtccTSAParams = CoveredCallTSA.CCTSAParams({
minSignatureExpiry: 5 minutes,
maxSignatureExpiry: 30 minutes,
spotTransactionLeniency: 1.01e18,
optionVolSlippageFactor: 0.5e18,
optionMaxDelta: 0.4e18,
optionMaxNegCash: -100e18,
Expand All @@ -45,25 +44,25 @@ contract DeployTSA is Utils {
});

PrincipalProtectedTSA.PPTSAParams public defaultLrtppTSAParams = PrincipalProtectedTSA.PPTSAParams({
maxMarkValueToStrikeDiffRatio : 700000000000000000,
minMarkValueToStrikeDiffRatio : 100000000000000000,
strikeDiff : 200000000000000000000,
maxTotalCostTolerance : 2000000000000000000,
maxLossOrGainPercentOfTVL : 20000000000000000,
negMaxCashTolerance : 20000000000000000,
maxMarkValueToStrikeDiffRatio : 0.7e18,
minMarkValueToStrikeDiffRatio : 0.1e18,
strikeDiff : 200e18,
maxTotalCostTolerance : 2e18,
maxLossOrGainPercentOfTVL : 0.02e18,
negMaxCashTolerance : 0.02e18,
minSignatureExpiry : 300,
maxSignatureExpiry : 1800,
optionMinTimeToExpiry : 21000,
optionMaxTimeToExpiry : 691200,
maxNegCash : -100000000000000000000000,
rfqFeeFactor : 1000000000000000000
maxNegCash : -100000e18,
rfqFeeFactor : 1e18
});


/// @dev main function
function run() external {
// deployCoveredCall();
deployPrincipalProtected();
deployCoveredCall();
// deployPrincipalProtected();
}

function deployCoveredCall() private {
Expand Down Expand Up @@ -199,11 +198,8 @@ contract DeployTSA is Utils {
pptsa.setPPTSAParams(defaultLrtppTSAParams);
pptsa.setCollateralManagementParams(defaultCollateralManagementParams);

TSAShareHandler shareHandler = new TSAShareHandler();

string memory objKey = "tsa-deployment";

vm.serializeAddress(objKey, "shareHandler", address(shareHandler));
vm.serializeAddress(objKey, "proxyAdmin", address(proxyAdmin));
vm.serializeAddress(objKey, "implementation", address(lrtpptsaImplementation));
string memory finalObj = vm.serializeAddress(objKey, "DNWETH", address(proxy));
Expand Down
Loading
Loading