Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"@typescript-eslint/consistent-type-assertions": "off",
"@typescript-eslint/no-unused-expressions": "off",
"@typescript-eslint/space-before-function-paren": "off",
"@typescript-eslint/no-var-requires": "off",
"@typescript-eslint/member-delimiter-style": [
"error",
{
Expand Down
16 changes: 9 additions & 7 deletions addresses.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"42069": {
"core": {
"PHO": "0x8be34D7830d446747B95Aa3ace0eaA4FF19b2007",
"TON": "0x45D97250E4F261c4116b2ebDBA1b963e55B068d3",
"Kernel": "0xB49BafEc7095d2d337A9e7DaaAE2DAA0F73d5b5A",
"ModuleManager": "0xB56Ab229bD4d9459e61BC7Bb34E394Ed6c1a8e39",
"ChainlinkPriceFeed": "0x36A7f9ca3EBD83E00c1bca3A1db378bcF039Bf85",
"PHO": "0xB7ca895F81F20e05A5eb11B05Cbaab3DAe5e23cd",
"TON": "0xd0EC100F1252a53322051a95CF05c32f0C174354",
"Kernel": "0x2d13826359803522cCe7a4Cfa2c1b582303DD0B4",
"ModuleManager": "0xCa57C1d3c2c35E667745448Fef8407dd25487ff8",
"ChainlinkPriceFeed": "0xaB837301d12cDc4b97f1E910FC56C9179894d9cf",
"CurvePool": "0xe9123CBC5d1EA65301D417193c40A72Ac8D53501"
},
"modules": {
Expand All @@ -14,7 +14,9 @@
"StablecoinDepositModuleFRAX": "0x70F804060040bAb7E443b0F4334d356B7a6D4bAc",
"StablecoinDepositModuleLUSD": "0x009eb8A8a1B7C4d48C721E47894346477d2f6647",
"MapleDepositModuleUSDC": "0x7511fAE41153Fad8A569d7Ebdcc76c120D3d5AAb",
"ZCBModuleUSDC": "0x7c098E457DA8108527bdba11da981100d5293A92"
"ZCBModuleUSDC": "0x7c098E457DA8108527bdba11da981100d5293A92",
"CDPPool_wstETH": "0x4ff1f64683785E0460c24A4EF78D582C2488704f",
"wstETHCDPWrapper": "0x0F527785e39B22911946feDf580d87a4E00465f0"
}
},
"11155111": {
Expand All @@ -27,4 +29,4 @@
},
"modules": {}
}
}
}
6 changes: 6 additions & 0 deletions cli/abis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import abiMM from '../build/abis/ModuleManager.sol/ModuleManager.json'
import abiKernel from '../build/abis/Kernel.sol/Kernel.json'
import abiCLPF from '../build/abis/ChainlinkPriceFeed.sol/ChainlinkPriceFeed.json'
import abiCP from '../build/abis/ICurvePool.sol/ICurvePool.json'
import abiWstETHCDPWrapper from '../build/abis/wstETHCDPWrapper.sol/wstETHCDPWrapper.json'
import abiCDPPool from '../build/abis/CDPPool.sol/CDPPool.json'

export const loadABI = (name: string): ContractInterface => {
switch (name) {
Expand All @@ -21,6 +23,10 @@ export const loadABI = (name: string): ContractInterface => {
return abiCLPF.abi
case 'CurvePool':
return abiCP.abi
case 'wstETHCDPWrapper':
return abiWstETHCDPWrapper.abi
case 'CDPPool':
return abiCDPPool.abi
default:
return 'ERROR_NO_ABI_FOUND' // TODO - DK - Improve error
}
Expand Down
5 changes: 3 additions & 2 deletions cli/cli.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env ts-node
import * as dotenv from 'dotenv'
import yargs from 'yargs'
import { evmCommand, coreCommand, deployCommand } from './commands'
import { evmCommand, coreCommand, deployCommand, modulesCommand } from './commands'
import { adminCommand } from './commands/admin'
import { cliOpts } from './defaults'

Expand All @@ -24,5 +24,6 @@ yargs
.command(evmCommand)
.command(adminCommand)
.command(deployCommand)
.command(modulesCommand)
.demandCommand(1, 'Choose a command from the above list')
.help().argv
.help().argv.catch
13 changes: 6 additions & 7 deletions cli/commands/deploy.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { exec } from 'child_process'
import { copyFile } from 'fs/promises'
import { writeFileSync, readdirSync, lstatSync, existsSync, mkdirSync } from 'fs'
import path from 'path'
import { logger } from '../logging'
Expand Down Expand Up @@ -42,7 +41,7 @@ export const deploy = async (cli: CLIEnvironment, cliArgs: CLIArgs): Promise<voi
})
console.log(`Finished deploying ${data.name}`)
}
await execute('npm run prettier:addresses')
await execute('npm run prettier:ts')
} catch (err) {
logger.info(err)
}
Expand Down Expand Up @@ -88,8 +87,8 @@ export function generateForgeCommand(p: CommandParams): string {
}

export async function updateAddresses(p: AddressParams): Promise<void> {
await copyFile('deployments/addresses_last.example.json', 'deployments/addresses_last.json', 0)
const tempAddresses = await import('../../deployments/addresses_last.json')
// await copyFile('deployments/addresses_last.example.json', 'deployments/addresses_last.json', 0)
const tempAddresses = require('../../deployments/addresses_last.json')
const updated: MasterAddresses = prepareAddressesJson(addresses, p.networkId)
const latestLog: string | undefined = getMostRecentFile(
`broadcast/${p.contractName}.s.sol/${getCorrectNetworkId(p.networkId)}/`
Expand Down Expand Up @@ -124,15 +123,15 @@ export async function updateAddresses(p: AddressParams): Promise<void> {
resolve({ updated, tempAddresses })
}).then((res) => {
const { updated, tempAddresses } = res
writeFileSync('addresses.json', JSON.stringify(updated), { flag: 'w+' })
writeFileSync('deployments/addresses_last.json', JSON.stringify(tempAddresses), { flag: 'w+' })
writeFileSync('addresses.json', JSON.stringify(updated))
writeFileSync('deployments/addresses_last.json', JSON.stringify(tempAddresses))
if (!existsSync(`deployments/${p.networkId}`)) {
mkdirSync(`deployments/${p.networkId}`)
}
writeFileSync(
`deployments/${p.networkId}/addresses_latest.json`,
JSON.stringify(tempAddresses),
{ flag: 'w+' }
{ flag: 'w' }
)
})
}
Expand Down
26 changes: 26 additions & 0 deletions cli/commands/evm/mine.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import yargs, { Argv } from 'yargs'
import { loadEnv } from '../../env'
import { CLIArgs, CLIEnvironment } from '../../types'
import { execute } from '../deploy'

const buildHelp = (): string => {
const help = 'To fast forward -> evm fast-forward [seconds] [minutes] [hours]'
return help
}

export const mine = async (cli: CLIEnvironment): Promise<void> => {
await execute(
`curl -H "Content-Type: application/json" -X POST --data '{"jsonrpc":"2.0","method":"evm_mine","params":[],"id":67}' ${cli.providerUrl}`
)
}

export const mineCommand = {
command: 'mine',
describe: 'deploy contracts from deployParams.json',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

describe should be updated. looks like a copy paste error

builder: (yargs: Argv): yargs.Argv => {
return yargs.usage(buildHelp())
},
handler: async (argv: CLIArgs): Promise<void> => {
return await mine(await loadEnv(argv))
}
}
3 changes: 2 additions & 1 deletion cli/commands/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { coreCommand } from './core'
import { deployCommand } from './deploy'
import { evmCommand } from './evm'
import { modulesCommand } from './modules'

export { coreCommand, deployCommand, evmCommand }
export { coreCommand, deployCommand, evmCommand, modulesCommand }
30 changes: 30 additions & 0 deletions cli/commands/modules/cdp/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import yargs, { Argv } from 'yargs'
import { overviewCommand } from './read/overview'
import { positionCommand } from './read/position'
import { addCollateralCommand } from './write/addCollateral'
import { addDebtCommand } from './write/addDebt'
import { closeCommand } from './write/close'
import { liquidateCommand } from './write/liquidate'
import { openCommand } from './write/open'
import { removeCollateralCommand } from './write/removeCollateral'
import { removeDebtCommand } from './write/removeDebt'

export const cdpCommand = {
command: 'cdp',
describe: 'Photon protocol modules',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this describe message needs to be updated

builder: (yargs: Argv): yargs.Argv => {
return yargs
.command(openCommand)
.command(addCollateralCommand)
.command(removeCollateralCommand)
.command(addDebtCommand)
.command(removeDebtCommand)
.command(closeCommand)
.command(liquidateCommand)
.command(overviewCommand)
.command(positionCommand)
},
handler: (): void => {
yargs.showHelp()
}
}
54 changes: 54 additions & 0 deletions cli/commands/modules/cdp/read/overview.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import Table from 'cli-table3'
import { logger } from 'ethers'
import { moduleDictionary } from '../../../../defaults'
import { loadEnv } from '../../../../env'
import { getModuleAddress, toReadablePrice } from '../../../../helpers'
import { CLIArgs, CLIEnvironment } from '../../../../types'
import { execute } from '../../../deploy'

const getOverview = async (cli: CLIEnvironment, cliArgs: CLIArgs): Promise<void> => {
const moduleName = moduleDictionary.cdp[cliArgs.tokenType].default
const cdpAddress = getModuleAddress(cliArgs.c, 'cdp', cliArgs.tokenType, 'default')

const moduleData = await cli.contracts.ModuleManager.modules(cdpAddress)
const [phoMinted, startTime, status] = moduleData.slice(-3)
const balances = await execute(
`cast call --rpc-url ${cli.providerUrl} ${cdpAddress} "pool()((uint256,uint256))"`
)
const feesCollected = await execute(
`cast call --rpc-url ${cli.providerUrl} ${cdpAddress} "feesCollected()(uint256)"`
)

const table = new Table({
head: [moduleName, 'Result'],
colWidths: [30, 50]
})

const [totalDebt, totalCollateral]: string[] = balances
.substring(1, balances.length - 1)
.split(',')
const collRatio: string = await execute(
`cast call --rpc-url ${cli.providerUrl} ${cdpAddress} "computeCR(uint256,uint256)(uint256)" ${totalCollateral} ${totalDebt}`
)

table.push(['Address', cdpAddress])
table.push(['PHO Mined', toReadablePrice(phoMinted.toLocaleString())])
table.push([
'startTime',
startTime.toString().concat(` (${new Date(Number(startTime) * 1000).toLocaleDateString()})`)
])
table.push(['status', status.toString()])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

right now I just see status: 1. i think cuz this is a table for displaying reading, we should just replace all the numbers like 1 to their actual status ACTIVE, which an enum

table.push(['Total Collateral', toReadablePrice(totalCollateral)])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it would be helpful to include Total Collateral in ${cliArgs.tokenType} or something like that, just as an indicator what the collateral is. for me it just says 27.228

table.push(['Total Debt', toReadablePrice(totalDebt)])
table.push(['Collateral Ratio', collRatio.slice(0, -3).concat('%')])
table.push(['feesCollected', toReadablePrice(feesCollected)])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Screenshot 2023-01-03 at 5 36 33 PM

i think this is being displayed wrong with toReadablePrice(). i see this massive number, i think it should be 10^18 less

logger.info(table.toString())
}

export const overviewCommand = {
command: 'overview [tokenType]',
describe: 'CDP Mechanism overview',
handler: async (argv: CLIArgs): Promise<void> => {
return await getOverview(await loadEnv(argv), argv)
}
}
40 changes: 40 additions & 0 deletions cli/commands/modules/cdp/read/position.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import Table from 'cli-table3'
import { logger } from 'ethers'
import { moduleDictionary } from '../../../../defaults'
import { loadEnv } from '../../../../env'
import { getModuleAddress, toReadablePrice } from '../../../../helpers'
import { CLIArgs, CLIEnvironment } from '../../../../types'
import { execute } from '../../../deploy'

const getPosition = async (cli: CLIEnvironment, cliArgs: CLIArgs): Promise<void> => {
const cdpOwner: string = cliArgs.cdpOwner
const moduleName = moduleDictionary.cdp[cliArgs.tokenType].default
const cdpAddress = getModuleAddress(cliArgs.c, 'cdp', cliArgs.tokenType, 'default')

const cdpData: string = await execute(
`cast call --rpc-url ${cli.providerUrl} ${cdpAddress} "cdps(address)((uint256,uint256))" ${cdpOwner}`
)
const [debt, collateral]: string[] = cdpData.substring(1, cdpData.length - 1).split(',')
const collRatio: string = await execute(
`cast call --rpc-url ${cli.providerUrl} ${cdpAddress} "computeCR(uint256,uint256)(uint256)" ${collateral} ${debt}`
)

const table = new Table({
head: [moduleName, 'Result'],
colWidths: [30, 50]
})

table.push(['CDP Owner', cdpOwner])
table.push(['Debt', toReadablePrice(debt)])
table.push(['Collateral', toReadablePrice(collateral)])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as suggested above, this makes it easier to read:

  table.push([`Collateral in ${cliArgs.tokenType}`, toReadablePrice(collateral)])

table.push(['Collateral Ratio', collRatio.slice(0, -3).concat('%')])
logger.info(table.toString())
}

export const positionCommand = {
command: 'position [tokenType] [cdpOwner]',
describe: 'Get information regarding an open position',
handler: async (argv: CLIArgs): Promise<void> => {
return await getPosition(await loadEnv(argv), argv)
}
}
106 changes: 106 additions & 0 deletions cli/commands/modules/cdp/write/addCollateral.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import { ethers } from 'ethers'
import { moduleDictionary, tokenAddresses } from '../../../../defaults'
import { loadEnv } from '../../../../env'
import { getModuleAddress } from '../../../../helpers'
import { logger } from '../../../../logging'
import { CDPCollateralParams, CLIArgs, CLIEnvironment } from '../../../../types'
import { execute } from '../../../deploy'
import addresses from '../../../../../addresses.json'

const getParams = (cliArgs: CLIArgs): CDPCollateralParams => {
let { _: parameters, c: networkId } = cliArgs
parameters = parameters.slice(3)
const collateralToken = parameters[0]
if (!moduleDictionary.cdp[collateralToken]) {
logger.error('addCollateral: Collateral token does not have a corresponding CDP')
return {} as CDPCollateralParams
}
const contractAddress: string = getModuleAddress(networkId, 'cdp', collateralToken, 'deposit')

if (collateralToken === 'wsteth') {
parameters = parameters.slice(1)
if (!['steth', 'weth', 'eth'].includes(parameters[0])) {
logger.error('Deposit token is not supported')
return {} as CDPCollateralParams
}
}

if (parameters.length !== 2) {
logger.error('addCollateral: Not enough parameters were supplied')
return {} as CDPCollateralParams
}

if (isNaN(parameters[1])) {
logger.error('addCollateral: parameters supplied are in the wrong type')
return {} as CDPCollateralParams
}

const collateralAmount = ethers.utils.parseUnits(parameters[1], 18)

return {
contractAddress,
collateralToken,
depositToken: parameters[0],
collateralAmount
}
}

const addCollateral = async (cli: CLIEnvironment, cliArgs: CLIArgs): Promise<void> => {
const params: CDPCollateralParams = getParams(cliArgs)
if (!params.collateralToken) {
logger.error('addCollateral: bad parameters')
return
}
if (params.collateralToken === 'wsteth') {
return await depositWithWrapper(params, cli)
}
}

const depositWithWrapper = async (
params: CDPCollateralParams,
cli: CLIEnvironment
): Promise<void> => {
const depositTokenAddress: string = tokenAddresses[params.depositToken]
if (!depositTokenAddress) {
logger.error('addCollateral: deposit token address not found')
return
}

if (cli.argv.c === 42069) {
const approveCommand: string = `cast send --rpc-url ${
cli.providerUrl
} ${depositTokenAddress} "approve(address,uint256)" ${
params.contractAddress
} ${params.collateralAmount.toString()} --from ${cli.wallet.address} --json`
const res = JSON.parse(await execute(approveCommand))
if (res.status === '0x1') {
logger.info(
`${
cli.wallet.address
} approved ${params.collateralAmount.toString()} for ${depositTokenAddress}`
)
}
}

const addCollateralCommand = `cast send --rpc-url ${cli.providerUrl} ${
params.contractAddress
} "addCollateral(uint256,address)" ${params.collateralAmount.toString()} ${depositTokenAddress} --from ${
cli.wallet.address
} --json`
const receipt = JSON.parse(await execute(addCollateralCommand))
if (receipt.status === '0x1') {
logger.info(`Added collateral to position for ${cli.wallet.address} successfully.`)
const cdpAddress: string = addresses[cli.argv.c].modules.CDPPool_wstETH
const positionCommand: string = `cast call --rpc-url ${cli.providerUrl} ${cdpAddress} "cdps(address)((uint256,uint256))" ${cli.wallet.address}`
const positionReceipt: string = await execute(positionCommand)
logger.info(positionReceipt)
}
}

export const addCollateralCommand = {
command: 'add-collateral',
describe: 'Add collateral to a position',
handler: async (argv: CLIArgs): Promise<void> => {
return await addCollateral(await loadEnv(argv), argv)
}
}
Loading