From 0e89f85ef67254539932821d6ebe86cbf2071a18 Mon Sep 17 00:00:00 2001 From: Bernardo Garces Chapero Date: Wed, 30 Aug 2023 13:06:25 +0100 Subject: [PATCH] support for filtering --- README.md | 41 +++++ package-lock.json | 10 ++ package.json | 18 ++- .../products/exampleProduct/exampleAdapter.ts | 2 +- src/adapters/metadataBuilders.ts | 2 +- src/adapters/stargate/buildMetadata.ts | 2 +- .../products/pool/stargatePoolAdapter.ts | 2 +- .../vesting/stargateVestingAdapter.ts | 2 +- src/core/constants/protocols.ts | 5 - src/index.ts | 3 +- src/scripts/buildMetadata.ts | 2 +- src/scripts/featureCommands.ts | 145 ++++++++++++++++++ src/scripts/getApr.ts | 15 -- src/scripts/getApy.ts | 15 -- src/scripts/getDeposits.ts | 19 --- src/scripts/getPositions.ts | 19 --- src/scripts/getPrices.ts | 15 -- src/scripts/getProfits.ts | 19 --- src/scripts/getTotalValueLocks.ts | 15 -- src/scripts/getWithdrawals.ts | 19 --- src/scripts/index.ts | 10 ++ src/types/adapter.ts | 2 +- 22 files changed, 224 insertions(+), 158 deletions(-) delete mode 100644 src/core/constants/protocols.ts create mode 100644 src/scripts/featureCommands.ts delete mode 100644 src/scripts/getApr.ts delete mode 100644 src/scripts/getApy.ts delete mode 100644 src/scripts/getDeposits.ts delete mode 100644 src/scripts/getPositions.ts delete mode 100644 src/scripts/getPrices.ts delete mode 100644 src/scripts/getProfits.ts delete mode 100644 src/scripts/getTotalValueLocks.ts delete mode 100644 src/scripts/getWithdrawals.ts create mode 100644 src/scripts/index.ts diff --git a/README.md b/README.md index c9183272a..c5aaa4efe 100644 --- a/README.md +++ b/README.md @@ -44,6 +44,23 @@ npm run build:watch ``` # Running the app +## CLI help +Specific details on what commands are available can be shown running: +``` +npm run adapters-cli +``` +For getting the arguments and options for specific commands: +``` +npm run positions -- --help +``` + +## Filtering +It is possible to get results for specific protocols and chains for every command by adding `--chain ` and/or `--protocol ` + +For example: +``` +npm run positions 0x6b8Be925ED8277fE4D27820aE4677e76Ebf4c255 -- --protocol stargate --chain 1 +``` ## Get positions @@ -69,6 +86,30 @@ npm run prices npm run tvl ``` +## Get APR + +``` +npm run apr +``` + +## Get APY + +``` +npm run apy +``` + +## Get deposits + +``` +npm run deposits 0x6b8Be925ED8277fE4D27820aE4677e76Ebf4c255 17719334 17719336 +``` + +## Get withdrawals + +``` +npm run withdrawals 0x6b8Be925ED8277fE4D27820aE4677e76Ebf4c255 17719334 17719336 +``` + # Support for a new protocol ``` diff --git a/package-lock.json b/package-lock.json index 4c65b80f2..cc23094ed 100644 --- a/package-lock.json +++ b/package-lock.json @@ -21,6 +21,7 @@ "@types/node": "^18.17.1", "@typescript-eslint/eslint-plugin": "^6.2.1", "@typescript-eslint/parser": "^6.2.1", + "commander": "^11.0.0", "eslint": "^8.46.0", "eslint-config-prettier": "^9.0.0", "eslint-plugin-prettier": "^5.0.0", @@ -3559,6 +3560,15 @@ "node": ">=8" } }, + "node_modules/commander": { + "version": "11.0.0", + "resolved": "https://registry.npmjs.org/commander/-/commander-11.0.0.tgz", + "integrity": "sha512-9HMlXtt/BNoYr8ooyjjNRdIilOTkVJXB+GhxMTtOKwk0R4j4lS4NpjuqmRxroBfnfTSHQIHQB7wryHhXarNjmQ==", + "dev": true, + "engines": { + "node": ">=16" + } + }, "node_modules/concat-map": { "version": "0.0.1", "resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz", diff --git a/package.json b/package.json index 75a9bc1c1..a126ad61d 100644 --- a/package.json +++ b/package.json @@ -17,14 +17,15 @@ "build:watch": "npm run build -- --watch", "build-types": "ts-node src/scripts/buildTypes.ts", "metadata": "ts-node src/scripts/buildMetadata.ts", - "positions": "node lib/main/scripts/getPositions.js", - "profits": "node lib/main/scripts/getProfits.js", - "prices": "node lib/main/scripts/getPrices.js", - "tvl": "node lib/main/scripts/getTotalValueLocks.js", - "apy": "node lib/main/scripts/getApy.js", - "apr": "node lib/main/scripts/getApr.js", - "deposits": "node lib/main/scripts/getDeposits.js", - "withdrawals": "node lib/main/scripts/getWithdrawals.js", + "adapters-cli": "ts-node src/scripts/index.ts", + "positions": "npm run adapters-cli positions --", + "profits": "npm run adapters-cli profits --", + "prices": "npm run adapters-cli prices --", + "tvl": "npm run adapters-cli tvl --", + "apy": "npm run adapters-cli apy --", + "apr": "npm run adapters-cli apr --", + "deposits": "npm run adapters-cli deposits --", + "withdrawals": "npm run adapters-cli withdrawals --", "fix": "run-s fix:*", "fix:lint": "eslint \"src/**/*.ts\" --fix", "fix:prettier": "prettier \"src/**/*.ts\" --write", @@ -40,6 +41,7 @@ "@types/node": "^18.17.1", "@typescript-eslint/eslint-plugin": "^6.2.1", "@typescript-eslint/parser": "^6.2.1", + "commander": "^11.0.0", "eslint": "^8.46.0", "eslint-config-prettier": "^9.0.0", "eslint-plugin-prettier": "^5.0.0", diff --git a/src/adapters/example/products/exampleProduct/exampleAdapter.ts b/src/adapters/example/products/exampleProduct/exampleAdapter.ts index f18244326..e3373f1d8 100644 --- a/src/adapters/example/products/exampleProduct/exampleAdapter.ts +++ b/src/adapters/example/products/exampleProduct/exampleAdapter.ts @@ -1,5 +1,5 @@ import { Chain } from '../../../../core/constants/chains' -import { Protocol } from '../../../../core/constants/protocols' +import { Protocol } from '../../..' import { GetEventsInput, GetPositionsInput, diff --git a/src/adapters/metadataBuilders.ts b/src/adapters/metadataBuilders.ts index ffba4fc69..5a72fd411 100644 --- a/src/adapters/metadataBuilders.ts +++ b/src/adapters/metadataBuilders.ts @@ -1,5 +1,5 @@ import { Chain } from '../core/constants/chains' -import { Protocol } from '../core/constants/protocols' +import { Protocol } from '.' import { buildMetadata } from './stargate/buildMetadata' export type ProtocolMetadataBuilders = { diff --git a/src/adapters/stargate/buildMetadata.ts b/src/adapters/stargate/buildMetadata.ts index a42a169bc..7092e2803 100644 --- a/src/adapters/stargate/buildMetadata.ts +++ b/src/adapters/stargate/buildMetadata.ts @@ -7,7 +7,7 @@ import { StargateVotingEscrow__factory, } from '../../contracts' import { Chain, ChainNames } from '../../core/constants/chains' -import { Protocol } from '../../core/constants/protocols' +import { Protocol } from '..' import { chainProviders } from '../../core/utils/chainProviders' import { ERC20, getTokenMetadata } from '../../core/utils/getTokenMetadata' import { Json } from '../../types/json' diff --git a/src/adapters/stargate/products/pool/stargatePoolAdapter.ts b/src/adapters/stargate/products/pool/stargatePoolAdapter.ts index 1d9dd13f8..dcfb4bc4b 100644 --- a/src/adapters/stargate/products/pool/stargatePoolAdapter.ts +++ b/src/adapters/stargate/products/pool/stargatePoolAdapter.ts @@ -4,7 +4,7 @@ import { StargateToken__factory } from '../../../../contracts' import { TransferEvent } from '../../../../contracts/Erc20' import { AVERAGE_BLOCKS_PER_DAY } from '../../../../core/constants/AVERAGE_BLOCKS_PER_DAY' import { Chain } from '../../../../core/constants/chains' -import { Protocol } from '../../../../core/constants/protocols' +import { Protocol } from '../../..' import { ZERO_ADDRESS } from '../../../../core/constants/ZERO_ADDRESS' import { getBalances } from '../../../../core/utils/getBalances' import { calculateProfit } from '../../../../core/utils/calculateProfit' diff --git a/src/adapters/stargate/products/vesting/stargateVestingAdapter.ts b/src/adapters/stargate/products/vesting/stargateVestingAdapter.ts index 6d9decb6a..f5e2c9108 100644 --- a/src/adapters/stargate/products/vesting/stargateVestingAdapter.ts +++ b/src/adapters/stargate/products/vesting/stargateVestingAdapter.ts @@ -2,7 +2,7 @@ import { ethers } from 'ethers' import { formatUnits } from 'ethers/lib/utils' import { StargateVotingEscrow__factory } from '../../../../contracts' import { Chain } from '../../../../core/constants/chains' -import { Protocol } from '../../../../core/constants/protocols' +import { Protocol } from '../../..' import { IProtocolAdapter, ProtocolDetails, diff --git a/src/core/constants/protocols.ts b/src/core/constants/protocols.ts deleted file mode 100644 index 42cee57c1..000000000 --- a/src/core/constants/protocols.ts +++ /dev/null @@ -1,5 +0,0 @@ -export const Protocol = { - Stargate: 'stargate', - Example: 'example', -} as const -export type Protocol = (typeof Protocol)[keyof typeof Protocol] diff --git a/src/index.ts b/src/index.ts index 9f8a7468b..5e048a68f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,9 +1,8 @@ import 'dotenv/config' import { ethers } from 'ethers' -import { supportedProtocols } from './adapters' +import { Protocol, supportedProtocols } from './adapters' import { Chain } from './core/constants/chains' -import { Protocol } from './core/constants/protocols' import { chainProviders } from './core/utils/chainProviders' import { fulfilledPromises } from './core/utils/filters' import { logger } from './core/utils/logger' diff --git a/src/scripts/buildMetadata.ts b/src/scripts/buildMetadata.ts index ba18f932d..47db56739 100644 --- a/src/scripts/buildMetadata.ts +++ b/src/scripts/buildMetadata.ts @@ -1,6 +1,6 @@ import 'dotenv/config' import { protocolMetadataBuilders } from '../adapters/metadataBuilders' -import { Protocol } from '../core/constants/protocols' +import { Protocol } from '../adapters' const buildMetadata = async ({ protocolId }: { protocolId?: Protocol }) => { const metadataBuilders = Object.entries(protocolMetadataBuilders) diff --git a/src/scripts/featureCommands.ts b/src/scripts/featureCommands.ts new file mode 100644 index 000000000..d3cf3cc3e --- /dev/null +++ b/src/scripts/featureCommands.ts @@ -0,0 +1,145 @@ +import { Command } from 'commander' +import { + getApr, + getApy, + getDeposits, + getPositions, + getPrices, + getTodaysProfits, + getTotalValueLocked, + getWithdrawals, +} from '..' +import { Chain } from '../core/constants/chains' +import { Protocol } from '../adapters' + +export function featureCommands(program: Command) { + addressCommand( + program, + 'positions', + getPositions, + '0x6b8Be925ED8277fE4D27820aE4677e76Ebf4c255', + ) + addressCommand( + program, + 'profits', + getTodaysProfits, + '0xB0D502E938ed5f4df2E681fE6E419ff29631d62b', + ) + + addressEventsCommand( + program, + 'deposits', + getDeposits, + '0x2C5D4A0943e9cF4C597a76464396B0bF84C24C45', + 17719334, + 17719336, + ) + addressEventsCommand( + program, + 'withdrawals', + getWithdrawals, + '0x4Ffc5F22770ab6046c8D66DABAe3A9CD1E7A03e7', + 17979753, + 17979755, + ) + + protocolCommand(program, 'prices', getPrices) + protocolCommand(program, 'tvl', getTotalValueLocked) + protocolCommand(program, 'apr', getApr) + protocolCommand(program, 'apy', getApy) +} + +function addressCommand( + program: Command, + commandName: string, + feature: (input: { + userAddress: string + filterProtocolId?: Protocol + filterChainId?: Chain + }) => Promise, + defaultAddress: string, +) { + program + .command(commandName) + .argument('[userAddress]', 'Address of the target account', defaultAddress) + .option('-p, --protocol ', 'protocol filter') + .option('-c, --chain ', 'chain filter') + .showHelpAfterError() + .action(async (userAddress, { protocol, chain }) => { + const data = await feature({ + userAddress, + filterProtocolId: protocol, + filterChainId: chain, + }) + + beautifyJsonOutput(data) + }) +} + +function addressEventsCommand( + program: Command, + commandName: string, + feature: (input: { + userAddress: string + fromBlock: number + toBlock: number + filterProtocolId?: Protocol + filterChainId?: Chain + }) => Promise, + defaultAddress: string, + defaultFromBlock: number, + defaultToBlock: number, +) { + program + .command(commandName) + .argument('[userAddress]', 'Address of the target account', defaultAddress) + .argument('[fromBlock]', 'From block', defaultFromBlock) + .argument('[toBlock]', 'To block', defaultToBlock) + .option('-p, --protocol ', 'protocol filter') + .option('-c, --chain ', 'chain filter') + .showHelpAfterError() + .action(async (userAddress, fromBlock, toBlock, { protocol, chain }) => { + const data = await feature({ + userAddress, + filterProtocolId: protocol, + filterChainId: chain, + fromBlock: parseInt(fromBlock, 10), + toBlock: parseInt(toBlock, 10), + }) + + beautifyJsonOutput(data) + }) +} + +function protocolCommand( + program: Command, + commandName: string, + feature: (input: { + filterProtocolId?: Protocol + filterChainId?: Chain + }) => Promise, +) { + program + .command(commandName) + .option('-p, --protocol ', 'protocol filter') + .option('-c, --chain ', 'chain filter') + .showHelpAfterError() + .action(async ({ protocol, chain }) => { + const data = await feature({ + filterProtocolId: protocol, + filterChainId: chain, + }) + + beautifyJsonOutput(data) + }) +} + +function beautifyJsonOutput(jsonString: T) { + console.log( + JSON.stringify( + jsonString, + (_, value) => (typeof value === 'bigint' ? value.toString() : value), + 2, + ), + ) +} diff --git a/src/scripts/getApr.ts b/src/scripts/getApr.ts deleted file mode 100644 index 4344d8486..000000000 --- a/src/scripts/getApr.ts +++ /dev/null @@ -1,15 +0,0 @@ -import { getApy } from '..' - -async function run() { - const data = await getApy({}) - - console.log( - JSON.stringify( - data, - (_, value) => (typeof value === 'bigint' ? value.toString() : value), - 2, - ), - ) -} - -run() diff --git a/src/scripts/getApy.ts b/src/scripts/getApy.ts deleted file mode 100644 index 4344d8486..000000000 --- a/src/scripts/getApy.ts +++ /dev/null @@ -1,15 +0,0 @@ -import { getApy } from '..' - -async function run() { - const data = await getApy({}) - - console.log( - JSON.stringify( - data, - (_, value) => (typeof value === 'bigint' ? value.toString() : value), - 2, - ), - ) -} - -run() diff --git a/src/scripts/getDeposits.ts b/src/scripts/getDeposits.ts deleted file mode 100644 index dcc35d576..000000000 --- a/src/scripts/getDeposits.ts +++ /dev/null @@ -1,19 +0,0 @@ -import { getDeposits } from '..' - -async function run() { - const data = await getDeposits({ - userAddress: '0x2C5D4A0943e9cF4C597a76464396B0bF84C24C45', - fromBlock: 17719334, - toBlock: 17719336, - }) - - console.log( - JSON.stringify( - data, - (_, value) => (typeof value === 'bigint' ? value.toString() : value), - 2, - ), - ) -} - -run() diff --git a/src/scripts/getPositions.ts b/src/scripts/getPositions.ts deleted file mode 100644 index 7cece23d0..000000000 --- a/src/scripts/getPositions.ts +++ /dev/null @@ -1,19 +0,0 @@ -import { getPositions } from '..' - -async function run(userAddress: string) { - const data = await getPositions({ - userAddress, - }) - - console.log( - JSON.stringify( - data, - (_, value) => (typeof value === 'bigint' ? value.toString() : value), - 2, - ), - ) -} - -const userAddress = - process.argv[2] || '0x6b8Be925ED8277fE4D27820aE4677e76Ebf4c255' -run(userAddress) diff --git a/src/scripts/getPrices.ts b/src/scripts/getPrices.ts deleted file mode 100644 index 0790cb097..000000000 --- a/src/scripts/getPrices.ts +++ /dev/null @@ -1,15 +0,0 @@ -import { getPrices } from '..' - -async function run() { - const data = await getPrices({}) - - console.log( - JSON.stringify( - data, - (_, value) => (typeof value === 'bigint' ? value.toString() : value), - 2, - ), - ) -} - -run() diff --git a/src/scripts/getProfits.ts b/src/scripts/getProfits.ts deleted file mode 100644 index b257d8df5..000000000 --- a/src/scripts/getProfits.ts +++ /dev/null @@ -1,19 +0,0 @@ -import { getTodaysProfits } from '..' - -async function run(userAddress: string) { - const data = await getTodaysProfits({ - userAddress, - }) - - console.log( - JSON.stringify( - data, - (_, value) => (typeof value === 'bigint' ? value.toString() : value), - 2, - ), - ) -} - -const userAddress = - process.argv[2] || '0xB0D502E938ed5f4df2E681fE6E419ff29631d62b' -run(userAddress) diff --git a/src/scripts/getTotalValueLocks.ts b/src/scripts/getTotalValueLocks.ts deleted file mode 100644 index 8aa02f461..000000000 --- a/src/scripts/getTotalValueLocks.ts +++ /dev/null @@ -1,15 +0,0 @@ -import { getTotalValueLocked } from '..' - -async function run() { - const data = await getTotalValueLocked({}) - - console.log( - JSON.stringify( - data, - (_, value) => (typeof value === 'bigint' ? value.toString() : value), - 2, - ), - ) -} - -run() diff --git a/src/scripts/getWithdrawals.ts b/src/scripts/getWithdrawals.ts deleted file mode 100644 index 5afda8797..000000000 --- a/src/scripts/getWithdrawals.ts +++ /dev/null @@ -1,19 +0,0 @@ -import { getWithdrawals } from '..' - -async function run() { - const data = await getWithdrawals({ - userAddress: '0x4Ffc5F22770ab6046c8D66DABAe3A9CD1E7A03e7', - fromBlock: 17979753, - toBlock: 17979755, - }) - - console.log( - JSON.stringify( - data, - (_, value) => (typeof value === 'bigint' ? value.toString() : value), - 2, - ), - ) -} - -run() diff --git a/src/scripts/index.ts b/src/scripts/index.ts new file mode 100644 index 000000000..04841087d --- /dev/null +++ b/src/scripts/index.ts @@ -0,0 +1,10 @@ +#!/usr/bin/env node + +import { Command } from 'commander' +import { featureCommands } from './featureCommands' + +const program = new Command('mmi-adapters') + +featureCommands(program) + +program.parseAsync() diff --git a/src/types/adapter.ts b/src/types/adapter.ts index 64314c7f1..808c6a7a6 100644 --- a/src/types/adapter.ts +++ b/src/types/adapter.ts @@ -1,6 +1,6 @@ import { ethers } from 'ethers' +import { Protocol } from '../adapters' import { Chain } from '../core/constants/chains' -import { Protocol } from '../core/constants/protocols' import { ERC20 } from '../core/utils/getTokenMetadata' export const TokenType = {