Skip to content

Commit 8cbd468

Browse files
author
Sachin
authored
Basis trading strategy deployment scripts (#33)
* Update set-v2-strategies to 0.0.7-basis-.0 * Update existing scripts * Add deployment script for perp basis trading system * Add unit tests * Deploy to kovan * Update set-v2-strategies to 0.0.7-basis.2 * Redeploy to kovan * Deployed to staging mainnet * Fix dependencies and deploy to kovan * Updated to latest packages * Deploy on staging-mainnet
1 parent 813fb1d commit 8cbd468

File tree

12 files changed

+1163
-40
lines changed

12 files changed

+1163
-40
lines changed

optimism/deploy/000_perpSetToken.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ import {
2323
CONTRACT_NAMES,
2424
SET_TOKEN_CREATOR,
2525
STREAMING_FEE_MODULE,
26-
PERPV2_LIBRARY,
26+
PERPV2_LIBRARY_V2,
27+
POSITION_V2,
28+
PERP_V2_POSITIONS,
2729
PERPV2_LEVERAGE_MODULE,
2830
SLIPPAGE_ISSUANCE_MODULE,
2931
USDC,
@@ -77,7 +79,9 @@ const func: DeployFunction = trackFinishedStage(CURRENT_STAGE, async function (h
7779
await usdcTokenInstance.connect(uniSigner).transfer(deployer, usdc(500));
7880
}
7981

80-
const perpV2LibAddress = await findDependency(PERPV2_LIBRARY);
82+
const perpV2LibraryV2Address = await findDependency(PERPV2_LIBRARY_V2);
83+
const positionV2LibAddress = await findDependency(POSITION_V2);
84+
const perpV2PositionsLibAddress = await findDependency(PERP_V2_POSITIONS);
8185
const perpV2LeverageModuleAddress = await findDependency(PERPV2_LEVERAGE_MODULE);
8286
const slippageIssuanceModuleAddress = await findDependency(SLIPPAGE_ISSUANCE_MODULE);
8387
const streamingFeeModuleAddress = await findDependency(STREAMING_FEE_MODULE);
@@ -216,7 +220,12 @@ const func: DeployFunction = trackFinishedStage(CURRENT_STAGE, async function (h
216220
// Initialize SetToken on PerpLeverageModule
217221
// =========================================
218222
const perpV2LeverageModuleInstance = await instanceGetter
219-
.getPerpV2LeverageModule(perpV2LibAddress, perpV2LeverageModuleAddress);
223+
.getPerpV2LeverageModuleV2(
224+
positionV2LibAddress,
225+
perpV2LibraryV2Address,
226+
perpV2PositionsLibAddress,
227+
perpV2LeverageModuleAddress
228+
);
220229

221230
const isPerpLeveragModuleInitialized = await perpSetTokenInstance.
222231
isInitializedModule(perpV2LeverageModuleAddress);
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
import "module-alias/register";
2+
3+
import { HardhatRuntimeEnvironment as HRE } from "hardhat/types";
4+
import { DeployFunction } from "hardhat-deploy/types";
5+
import { ZERO } from "@utils/constants";
6+
import { BigNumber } from "ethers";
7+
import { solidityPack } from "ethers/lib/utils";
8+
9+
import {
10+
findDependency,
11+
getContractAddress,
12+
getCurrentStage,
13+
saveContractDeployment,
14+
getNetworkId
15+
} from "@utils/outputHelper";
16+
17+
import {
18+
PerpV2BasisContractSettings
19+
} from "./../deployments/utils/types";
20+
21+
import {
22+
addExtension,
23+
deployBaseManager,
24+
prepareDeployment,
25+
stageAlreadyFinished,
26+
trackFinishedStage,
27+
updateSetManager,
28+
} from "@utils/deploys/deployUtils";
29+
30+
import {
31+
DEPENDENCY
32+
} from "./../deployments/utils/dependencies";
33+
34+
import {
35+
CONTRACT_NAMES,
36+
EXCHANGE_SETTINGS,
37+
EXECUTION_SETTINGS,
38+
INCENTIVE_SETTINGS,
39+
METHODOLOGY_SETTINGS,
40+
ETH_DECIMALS,
41+
ETH_USDC_PRICE_FEED_DECIMALS,
42+
KOVAN_TESTNET_ID,
43+
PERP_TEST_USDC
44+
} from "./../deployments/constants/004_perp_basis_trading_system";
45+
46+
const {
47+
UNISWAP_V3_QUOTER,
48+
TRADE_MODULE,
49+
TEST_BASIS_TOKEN,
50+
PERPV2_ACCOUNT_BALANCE,
51+
PERPV2_BASIS_TRADING_MODULE,
52+
WETH,
53+
USDC,
54+
V_ETH,
55+
V_USD,
56+
ETHUSD_PERP_CHAINLINK_ORACLE,
57+
} = DEPENDENCY;
58+
const CURRENT_STAGE = getCurrentStage(__filename);
59+
60+
61+
const func: DeployFunction = trackFinishedStage(CURRENT_STAGE, async function (hre: HRE) {
62+
const {
63+
deploy,
64+
deployer,
65+
networkConstant
66+
} = await prepareDeployment(hre);
67+
68+
// Deploy BaseManager
69+
await deployBaseManager(
70+
hre,
71+
CONTRACT_NAMES.BASE_MANAGER,
72+
TEST_BASIS_TOKEN,
73+
deployer,
74+
deployer
75+
);
76+
const networkId = getNetworkId();
77+
78+
// Perpetual Protocol has a fully deployed system on Optimistic Kovan. You can get the
79+
// kovan test USDC it uses from faucet at: https://kovan.optifaucet.com/
80+
const usdcTokenAddress = (networkId === KOVAN_TESTNET_ID)
81+
? await findDependency(PERP_TEST_USDC)
82+
: await findDependency(USDC);
83+
84+
// Deploy strategy extension
85+
await deployBasisTradingStrategyExtension();
86+
87+
// Add strategy extension to manager
88+
await addExtension(hre, CONTRACT_NAMES.BASE_MANAGER, CONTRACT_NAMES.STRATEGY_EXTENSION);
89+
90+
// Set manager to BaseManager
91+
if (networkConstant !== "development") {
92+
await updateSetManager(hre, TEST_BASIS_TOKEN, CONTRACT_NAMES.BASE_MANAGER);
93+
}
94+
95+
async function deployBasisTradingStrategyExtension(): Promise<void> {
96+
const checkStrategyExtensionAddress = await getContractAddress(CONTRACT_NAMES.STRATEGY_EXTENSION);
97+
if (checkStrategyExtensionAddress == "") {
98+
const manager = await getContractAddress(CONTRACT_NAMES.BASE_MANAGER);
99+
100+
const contractSettings: PerpV2BasisContractSettings = {
101+
setToken: await findDependency(TEST_BASIS_TOKEN),
102+
basisTradingModule: await findDependency(PERPV2_BASIS_TRADING_MODULE),
103+
tradeModule: await findDependency(TRADE_MODULE),
104+
quoter: await findDependency(UNISWAP_V3_QUOTER),
105+
perpV2AccountBalance: await findDependency(PERPV2_ACCOUNT_BALANCE),
106+
baseUSDPriceOracle: await findDependency(ETHUSD_PERP_CHAINLINK_ORACLE),
107+
twapInterval: ZERO,
108+
basePriceDecimalAdjustment: BigNumber.from(ETH_DECIMALS).sub(ETH_USDC_PRICE_FEED_DECIMALS),
109+
virtualBaseAddress: await findDependency(V_ETH),
110+
virtualQuoteAddress: await findDependency(V_USD),
111+
spotAssetAddress: await findDependency(WETH)
112+
};
113+
114+
const methodlogySettings = METHODOLOGY_SETTINGS;
115+
const executionSettings = EXECUTION_SETTINGS;
116+
const exchangeSettings = EXCHANGE_SETTINGS;
117+
const incentiveSettings = INCENTIVE_SETTINGS;
118+
119+
exchangeSettings.buyExactSpotTradeData = solidityPack(
120+
["address", "uint24", "address", "bool"],
121+
[await findDependency(WETH), BigNumber.from(3000), usdcTokenAddress, false]
122+
);
123+
exchangeSettings.sellExactSpotTradeData = solidityPack(
124+
["address", "uint24", "address", "bool"],
125+
[await findDependency(WETH), BigNumber.from(3000), usdcTokenAddress, true],
126+
);
127+
exchangeSettings.buySpotQuoteExactInputPath = solidityPack(
128+
["address", "uint24", "address"],
129+
[usdcTokenAddress, BigNumber.from(3000), await findDependency(WETH)]
130+
);
131+
132+
const constructorArgs = [
133+
manager,
134+
contractSettings,
135+
methodlogySettings,
136+
executionSettings,
137+
incentiveSettings,
138+
exchangeSettings
139+
];
140+
141+
const extensionDeploy = await deploy(CONTRACT_NAMES.STRATEGY_EXTENSION, {
142+
from: deployer,
143+
args: constructorArgs,
144+
log: true
145+
});
146+
147+
extensionDeploy.receipt && await saveContractDeployment({
148+
name: CONTRACT_NAMES.STRATEGY_EXTENSION,
149+
contractAddress: extensionDeploy.address,
150+
id: extensionDeploy.receipt.transactionHash,
151+
description: "Deployed DeltaNeutralBasisTradingStrategyExtension",
152+
constructorArgs
153+
});
154+
}
155+
}
156+
});
157+
158+
func.skip = stageAlreadyFinished(CURRENT_STAGE);
159+
160+
export default func;

optimism/deployments/constants/000_perpSetToken.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ export const CONTRACT_NAMES = {
88
};
99

1010
export const SET_TOKEN_CREATOR = "SET_TOKEN_CREATOR";
11-
export const PERPV2_LIBRARY = "PERPV2_LIBRARY";
11+
export const PERPV2_LIBRARY_V2 = "PERPV2_LIBRARY_V2";
12+
export const POSITION_V2 = "POSITION_V2";
13+
export const PERP_V2_POSITIONS = "PERP_V2_POSITIONS";
1214
export const PERPV2_LEVERAGE_MODULE = "PERPV2_LEVERAGE_MODULE";
1315
export const SLIPPAGE_ISSUANCE_MODULE = "SLIPPAGE_ISSUANCE_MODULE";
1416
export const STREAMING_FEE_MODULE = "STREAMING_FEE_MODULE";
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { BigNumber } from "@ethersproject/bignumber";
2+
import { ether } from "@utils/index";
3+
4+
import { ONE_HOUR_IN_SECONDS } from "@utils/constants";
5+
6+
export const CONTRACT_NAMES = {
7+
BASE_MANAGER: "PerpV2BaseManager",
8+
STANDARD_TOKEN_MOCK: "StandardTokenMock",
9+
STRATEGY_EXTENSION: "DeltaNeutralBasisTradingStrategyExtension"
10+
};
11+
12+
export const METHODOLOGY_SETTINGS = {
13+
targetLeverageRatio: ether(-1),
14+
minLeverageRatio: ether(-0.9),
15+
maxLeverageRatio: ether(-1.1),
16+
recenteringSpeed: ether(0.1),
17+
rebalanceInterval: ONE_HOUR_IN_SECONDS.mul(2),
18+
reinvestInterval: ONE_HOUR_IN_SECONDS
19+
};
20+
21+
export const EXECUTION_SETTINGS = {
22+
twapCooldownPeriod: BigNumber.from(30), // 30 sec cooldown
23+
slippageTolerance: ether(0.02), // 2% max slippage on regular rebalances
24+
};
25+
26+
export const INCENTIVE_SETTINGS = {
27+
incentivizedTwapCooldownPeriod: BigNumber.from(1), // 1 sec cooldown on ripcord
28+
incentivizedSlippageTolerance: ether(0.05), // 5% max slippage on ripcord
29+
etherReward: ether(1),
30+
incentivizedLeverageRatio: ether(-1.8),
31+
};
32+
33+
export const EXCHANGE_SETTINGS = {
34+
exchangeName: "UniswapV3ExchangeAdapterV2",
35+
buyExactSpotTradeData: "", // will be overriden in the deploy script
36+
sellExactSpotTradeData: "", // will be overriden in the deploy script
37+
buySpotQuoteExactInputPath: "", // will be overriden in the deploy script
38+
twapMaxTradeSize: ether(10),
39+
incentivizedTwapMaxTradeSize: ether(20)
40+
};
41+
42+
export const ETH_DECIMALS = 18;
43+
export const ETH_USDC_PRICE_FEED_DECIMALS = 8;
44+
45+
export const PERP_TEST_USDC = "PERP_TEST_USDC";
46+
export const KOVAN_TESTNET_ID = 69;

0 commit comments

Comments
 (0)