Skip to content

Commit

Permalink
made constants generalizable
Browse files Browse the repository at this point in the history
  • Loading branch information
daryakaviani committed May 31, 2022
1 parent e572609 commit b816cf0
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 24 deletions.
41 changes: 19 additions & 22 deletions packages/hardhat/tasks/addLiquidatableVault.ts
Original file line number Diff line number Diff line change
@@ -1,45 +1,42 @@
import { task, types } from "hardhat/config";
import "@nomiclabs/hardhat-waffle";
import { BigNumber } from "ethers";
import { getUniswapDeployments, getUSDC, getWETH } from "./utils";
import {
abi as POOL_ABI,
bytecode as POOL_BYTECODE,
} from '@uniswap/v3-core/artifacts/contracts/UniswapV3Pool.sol/UniswapV3Pool.json'
import { Oracle } from "../typechain";
import { getController, getEthUSDCPool, getOracle, getUSDC, getWETH } from "./utils";
export const one = BigNumber.from(10).pow(18)

// Example execution
/**
npx hardhat addLiquidatableVault --input '50' --network ropsten
npx hardhat addLiquidatableVault --input 50 --network ropsten
*/
task("addLiquidatableVault", "Add a short position with a 150% collateralization ratio")
.addParam('input', 'amount squeeth to mint', '50', types.string)
.addParam('input', 'amount squeeth to mint', 50, types.string)
.setAction(async ({ input: squeethToMint }, hre) => {

// ROPSTEN CONSTANTS
const ORACLE = "0xBD9F4bE886653177D22fA9c79FD0DFc41407fC89"
const ETH_USDC_POOL = "0x8356AbC730a218c24446C2c85708F373f354F0D8"
// const WETH = "0xc778417e063141139fce010982780140aa0cd5ab"
// const USDC = "0x27415c30d8c87437becbd4f98474f26e712047f4"

const { getNamedAccounts, ethers, network } = hre;
const { deployer } = await getNamedAccounts();
const controller = await ethers.getContract("Controller", deployer);
const oracle = (await ethers.getContractAt("Oracle", ORACLE)) as Oracle
const ethUsdcPool = await ethers.getContractAt(POOL_ABI, ETH_USDC_POOL);
// const weth = await ethers.getContractAt("WETH9", WETH);
const controller = await getController(ethers, deployer, network.name)
const oracle = await getOracle(ethers, deployer, network.name)
const ethUsdcPool = await getEthUSDCPool(ethers, deployer, network.name)
const weth = await getWETH(ethers, deployer, network.name)
// const usdc = await ethers.getContractAt("MockErc20", USDC)
const usdc = await getUSDC(ethers, deployer, network.name)
const ethPrice = await oracle.getTwap(ethUsdcPool.address, weth.address, usdc.address, 420, true)
const normFactor = await controller.normalizationFactor()
const debtAmount = ethers.utils.parseUnits(squeethToMint)
const mintRSqueethAmount = debtAmount.mul(normFactor).div(one)
const scaledEthPrice = ethPrice.div(10000)
const debtInEth = mintRSqueethAmount.mul(scaledEthPrice).div(one)
const collateralToDeposit = debtInEth.mul(1.5)
const collateralToDeposit = debtInEth.mul(3).div(2)

const vaultId = await controller.mintWPowerPerpAmount(0, debtAmount, 0, {value: collateralToDeposit})
console.log(`Added 150% collateralization vault with ID: `, vaultId)
const shortPowerPerp = await ethers.getContractAt("ShortPowerPerp", (await controller.shortPowerPerp()))
const oldVaultId = shortPowerPerp.nextId()
let oldVaultIdInt;
oldVaultId.then((value: any) => {
oldVaultIdInt = value.toNumber()
}).catch((err: any) => {
console.log(err);
});
const newVaultId = oldVaultIdInt ? (oldVaultId + 1): null
const tx = await controller.mintWPowerPerpAmount(0, debtAmount, 0, {value: collateralToDeposit})
await ethers.provider.waitForTransaction(tx.hash, 1)
console.log(`Added 150% collateralization vault with ID: `, newVaultId)
});
64 changes: 62 additions & 2 deletions packages/hardhat/tasks/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { Contract } from "ethers"
import {
abi as POOL_ABI,
} from '@uniswap/v3-core/artifacts/contracts/UniswapV3Pool.sol/UniswapV3Pool.json'

export const networkNameToUniRouter = (name: string) => {
switch (name) {
Expand Down Expand Up @@ -51,14 +54,71 @@ export const networkNameToWeth = (name: string) => {
}
}

export const networkNameToOracle = (name: string) => {
switch (name) {
case 'mainnet': return '0x65D66c76447ccB45dAf1e8044e918fA786A483A1'
case 'ropsten': return '0xBD9F4bE886653177D22fA9c79FD0DFc41407fC89'
case 'rinkebyArbitrum': return '0xe790Afe86c0bdc4Dd7C6CBb7dB087552Ec85F6fB'
default: return undefined
}
}

export const networkNameToController = (name: string) => {
switch (name) {
case 'mainnet': return '0x64187ae08781B09368e6253F9E94951243A493D5'
case 'ropsten': return '0x59F0c781a6eC387F09C40FAA22b7477a2950d209'
case 'rinkebyArbitrum': return '0x6FBbc7eBd7E421839915e8e4fAcC9947dC32F4dE'
default: return undefined
}
}

export const networkNameToEthUSDCPool = (name: string) => {
switch (name) {
case 'mainnet': return '0x8ad599c3A0ff1De082011EFDDc58f1908eb6e6D8'
case 'ropsten': return '0x8356AbC730a218c24446C2c85708F373f354F0D8'
case 'rinkebyArbitrum': return '0xe7715b01a0B16E3e38A7d9b78F6Bd2b163D7f319'
default: return undefined
}
}

export const getWETH = async (ethers: any, deployer: string, networkName: string)=> {
const wethAddr = networkNameToWeth(networkName)
if (wethAddr === undefined) {
// get from deployed network
return ethers.getContract("WETH9", deployer);
}
// get contract instance at address
return ethers.getContract('WETH9', wethAddr)
return ethers.getContractAt('WETH9', wethAddr)
}

export const getOracle = async (ethers: any, deployer: string, networkName: string)=> {
const oracleAddr = networkNameToOracle(networkName)
if (oracleAddr === undefined) {
// get from deployed network
return ethers.getContract("Oracle", deployer);
}
// get contract instance at address
return ethers.getContractAt('Oracle', oracleAddr)
}

export const getController = async (ethers: any, deployer: string, networkName: string)=> {
const controllerAddr = networkNameToController(networkName)
if (controllerAddr === undefined) {
// get from deployed network
return ethers.getContract("Controller", deployer);
}
// get contract instance at address
return ethers.getContractAt('Controller', controllerAddr)
}

export const getEthUSDCPool = async (ethers: any, deployer: string, networkName: string)=> {
const ethUSDCPoolAddress = networkNameToEthUSDCPool(networkName)
if (ethUSDCPoolAddress === undefined) {
// get from deployed network
return ethers.getContract(POOL_ABI, deployer);
}
// get contract instance at address
return ethers.getContractAt(POOL_ABI, ethUSDCPoolAddress)
}

export const getUSDC = async (ethers: any, deployer: string, networkName: string)=> {
Expand All @@ -68,7 +128,7 @@ export const getUSDC = async (ethers: any, deployer: string, networkName: string
return ethers.getContract("MockErc20", deployer);
}
// get contract instance at address
return ethers.getContract('MockErc20', usdcAddress)
return ethers.getContractAt('MockErc20', usdcAddress)
}

/**
Expand Down

0 comments on commit b816cf0

Please sign in to comment.