-
Notifications
You must be signed in to change notification settings - Fork 0
Niv/cdp cli #99
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
base: main
Are you sure you want to change the base?
Niv/cdp cli #99
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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', | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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)) | ||
| } | ||
| } | ||
| 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 } |
| 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', | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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() | ||
| } | ||
| } | ||
| 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()]) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. right now I just see |
||
| table.push(['Total Collateral', toReadablePrice(totalCollateral)]) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it would be helpful to include |
||
| table.push(['Total Debt', toReadablePrice(totalDebt)]) | ||
| table.push(['Collateral Ratio', collRatio.slice(0, -3).concat('%')]) | ||
| table.push(['feesCollected', toReadablePrice(feesCollected)]) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| 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) | ||
| } | ||
| } | ||
| 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)]) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
| } | ||
| } | ||
| 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) { | ||
davekay100 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| 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) | ||
| } | ||
| } | ||

Uh oh!
There was an error while loading. Please reload this page.