Skip to content

Commit b816cf0

Browse files
committed
made constants generalizable
1 parent e572609 commit b816cf0

File tree

2 files changed

+81
-24
lines changed

2 files changed

+81
-24
lines changed
+19-22
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,42 @@
11
import { task, types } from "hardhat/config";
22
import "@nomiclabs/hardhat-waffle";
33
import { BigNumber } from "ethers";
4-
import { getUniswapDeployments, getUSDC, getWETH } from "./utils";
5-
import {
6-
abi as POOL_ABI,
7-
bytecode as POOL_BYTECODE,
8-
} from '@uniswap/v3-core/artifacts/contracts/UniswapV3Pool.sol/UniswapV3Pool.json'
9-
import { Oracle } from "../typechain";
4+
import { getController, getEthUSDCPool, getOracle, getUSDC, getWETH } from "./utils";
105
export const one = BigNumber.from(10).pow(18)
116

127
// Example execution
138
/**
14-
npx hardhat addLiquidatableVault --input '50' --network ropsten
9+
npx hardhat addLiquidatableVault --input 50 --network ropsten
1510
*/
1611
task("addLiquidatableVault", "Add a short position with a 150% collateralization ratio")
17-
.addParam('input', 'amount squeeth to mint', '50', types.string)
12+
.addParam('input', 'amount squeeth to mint', 50, types.string)
1813
.setAction(async ({ input: squeethToMint }, hre) => {
19-
20-
// ROPSTEN CONSTANTS
21-
const ORACLE = "0xBD9F4bE886653177D22fA9c79FD0DFc41407fC89"
22-
const ETH_USDC_POOL = "0x8356AbC730a218c24446C2c85708F373f354F0D8"
23-
// const WETH = "0xc778417e063141139fce010982780140aa0cd5ab"
24-
// const USDC = "0x27415c30d8c87437becbd4f98474f26e712047f4"
2514

2615
const { getNamedAccounts, ethers, network } = hre;
2716
const { deployer } = await getNamedAccounts();
28-
const controller = await ethers.getContract("Controller", deployer);
29-
const oracle = (await ethers.getContractAt("Oracle", ORACLE)) as Oracle
30-
const ethUsdcPool = await ethers.getContractAt(POOL_ABI, ETH_USDC_POOL);
31-
// const weth = await ethers.getContractAt("WETH9", WETH);
17+
const controller = await getController(ethers, deployer, network.name)
18+
const oracle = await getOracle(ethers, deployer, network.name)
19+
const ethUsdcPool = await getEthUSDCPool(ethers, deployer, network.name)
3220
const weth = await getWETH(ethers, deployer, network.name)
33-
// const usdc = await ethers.getContractAt("MockErc20", USDC)
3421
const usdc = await getUSDC(ethers, deployer, network.name)
3522
const ethPrice = await oracle.getTwap(ethUsdcPool.address, weth.address, usdc.address, 420, true)
3623
const normFactor = await controller.normalizationFactor()
3724
const debtAmount = ethers.utils.parseUnits(squeethToMint)
3825
const mintRSqueethAmount = debtAmount.mul(normFactor).div(one)
3926
const scaledEthPrice = ethPrice.div(10000)
4027
const debtInEth = mintRSqueethAmount.mul(scaledEthPrice).div(one)
41-
const collateralToDeposit = debtInEth.mul(1.5)
28+
const collateralToDeposit = debtInEth.mul(3).div(2)
4229

43-
const vaultId = await controller.mintWPowerPerpAmount(0, debtAmount, 0, {value: collateralToDeposit})
44-
console.log(`Added 150% collateralization vault with ID: `, vaultId)
30+
const shortPowerPerp = await ethers.getContractAt("ShortPowerPerp", (await controller.shortPowerPerp()))
31+
const oldVaultId = shortPowerPerp.nextId()
32+
let oldVaultIdInt;
33+
oldVaultId.then((value: any) => {
34+
oldVaultIdInt = value.toNumber()
35+
}).catch((err: any) => {
36+
console.log(err);
37+
});
38+
const newVaultId = oldVaultIdInt ? (oldVaultId + 1): null
39+
const tx = await controller.mintWPowerPerpAmount(0, debtAmount, 0, {value: collateralToDeposit})
40+
await ethers.provider.waitForTransaction(tx.hash, 1)
41+
console.log(`Added 150% collateralization vault with ID: `, newVaultId)
4542
});

packages/hardhat/tasks/utils.ts

+62-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
import { Contract } from "ethers"
2+
import {
3+
abi as POOL_ABI,
4+
} from '@uniswap/v3-core/artifacts/contracts/UniswapV3Pool.sol/UniswapV3Pool.json'
25

36
export const networkNameToUniRouter = (name: string) => {
47
switch (name) {
@@ -51,14 +54,71 @@ export const networkNameToWeth = (name: string) => {
5154
}
5255
}
5356

57+
export const networkNameToOracle = (name: string) => {
58+
switch (name) {
59+
case 'mainnet': return '0x65D66c76447ccB45dAf1e8044e918fA786A483A1'
60+
case 'ropsten': return '0xBD9F4bE886653177D22fA9c79FD0DFc41407fC89'
61+
case 'rinkebyArbitrum': return '0xe790Afe86c0bdc4Dd7C6CBb7dB087552Ec85F6fB'
62+
default: return undefined
63+
}
64+
}
65+
66+
export const networkNameToController = (name: string) => {
67+
switch (name) {
68+
case 'mainnet': return '0x64187ae08781B09368e6253F9E94951243A493D5'
69+
case 'ropsten': return '0x59F0c781a6eC387F09C40FAA22b7477a2950d209'
70+
case 'rinkebyArbitrum': return '0x6FBbc7eBd7E421839915e8e4fAcC9947dC32F4dE'
71+
default: return undefined
72+
}
73+
}
74+
75+
export const networkNameToEthUSDCPool = (name: string) => {
76+
switch (name) {
77+
case 'mainnet': return '0x8ad599c3A0ff1De082011EFDDc58f1908eb6e6D8'
78+
case 'ropsten': return '0x8356AbC730a218c24446C2c85708F373f354F0D8'
79+
case 'rinkebyArbitrum': return '0xe7715b01a0B16E3e38A7d9b78F6Bd2b163D7f319'
80+
default: return undefined
81+
}
82+
}
83+
5484
export const getWETH = async (ethers: any, deployer: string, networkName: string)=> {
5585
const wethAddr = networkNameToWeth(networkName)
5686
if (wethAddr === undefined) {
5787
// get from deployed network
5888
return ethers.getContract("WETH9", deployer);
5989
}
6090
// get contract instance at address
61-
return ethers.getContract('WETH9', wethAddr)
91+
return ethers.getContractAt('WETH9', wethAddr)
92+
}
93+
94+
export const getOracle = async (ethers: any, deployer: string, networkName: string)=> {
95+
const oracleAddr = networkNameToOracle(networkName)
96+
if (oracleAddr === undefined) {
97+
// get from deployed network
98+
return ethers.getContract("Oracle", deployer);
99+
}
100+
// get contract instance at address
101+
return ethers.getContractAt('Oracle', oracleAddr)
102+
}
103+
104+
export const getController = async (ethers: any, deployer: string, networkName: string)=> {
105+
const controllerAddr = networkNameToController(networkName)
106+
if (controllerAddr === undefined) {
107+
// get from deployed network
108+
return ethers.getContract("Controller", deployer);
109+
}
110+
// get contract instance at address
111+
return ethers.getContractAt('Controller', controllerAddr)
112+
}
113+
114+
export const getEthUSDCPool = async (ethers: any, deployer: string, networkName: string)=> {
115+
const ethUSDCPoolAddress = networkNameToEthUSDCPool(networkName)
116+
if (ethUSDCPoolAddress === undefined) {
117+
// get from deployed network
118+
return ethers.getContract(POOL_ABI, deployer);
119+
}
120+
// get contract instance at address
121+
return ethers.getContractAt(POOL_ABI, ethUSDCPoolAddress)
62122
}
63123

64124
export const getUSDC = async (ethers: any, deployer: string, networkName: string)=> {
@@ -68,7 +128,7 @@ export const getUSDC = async (ethers: any, deployer: string, networkName: string
68128
return ethers.getContract("MockErc20", deployer);
69129
}
70130
// get contract instance at address
71-
return ethers.getContract('MockErc20', usdcAddress)
131+
return ethers.getContractAt('MockErc20', usdcAddress)
72132
}
73133

74134
/**

0 commit comments

Comments
 (0)