Skip to content
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

support for filtering #18

Merged
merged 1 commit into from
Aug 30, 2023
Merged
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
41 changes: 41 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <chainId>` and/or `--protocol <protocolName>`

For example:
```
npm run positions 0x6b8Be925ED8277fE4D27820aE4677e76Ebf4c255 -- --protocol stargate --chain 1
```

## Get positions

Expand All @@ -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

```
Expand Down
10 changes: 10 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 10 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Chain } from '../../../../core/constants/chains'
import { Protocol } from '../../../../core/constants/protocols'
import { Protocol } from '../../..'
import {
GetEventsInput,
GetPositionsInput,
Expand Down
2 changes: 1 addition & 1 deletion src/adapters/metadataBuilders.ts
Original file line number Diff line number Diff line change
@@ -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 = {
Expand Down
2 changes: 1 addition & 1 deletion src/adapters/stargate/buildMetadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
5 changes: 0 additions & 5 deletions src/core/constants/protocols.ts

This file was deleted.

3 changes: 1 addition & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down
2 changes: 1 addition & 1 deletion src/scripts/buildMetadata.ts
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
145 changes: 145 additions & 0 deletions src/scripts/featureCommands.ts
Original file line number Diff line number Diff line change
@@ -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<unknown>,
defaultAddress: string,
) {
program
.command(commandName)
.argument('[userAddress]', 'Address of the target account', defaultAddress)
.option('-p, --protocol <protocolId>', 'protocol filter')
.option('-c, --chain <chainId>', '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<unknown>,
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 <protocolId>', 'protocol filter')
.option('-c, --chain <chainId>', '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<unknown>,
) {
program
.command(commandName)
.option('-p, --protocol <protocolId>', 'protocol filter')
.option('-c, --chain <chainId>', 'chain filter')
.showHelpAfterError()
.action(async ({ protocol, chain }) => {
const data = await feature({
filterProtocolId: protocol,
filterChainId: chain,
})

beautifyJsonOutput(data)
})
}

function beautifyJsonOutput<T>(jsonString: T) {
console.log(
JSON.stringify(
jsonString,
(_, value) => (typeof value === 'bigint' ? value.toString() : value),
2,
),
)
}
15 changes: 0 additions & 15 deletions src/scripts/getApr.ts

This file was deleted.

15 changes: 0 additions & 15 deletions src/scripts/getApy.ts

This file was deleted.

19 changes: 0 additions & 19 deletions src/scripts/getDeposits.ts

This file was deleted.

19 changes: 0 additions & 19 deletions src/scripts/getPositions.ts

This file was deleted.

Loading