eliotstock / dro

Uniswap v3 dynamic range orders
0 stars 1 forks source link

CLI tool to calculate APY% for a given account #22

Open eliotstock opened 2 years ago

eliotstock commented 2 years ago

Based on analysis-positions code.

Nice to have: table of positions, gross and net yield for each, real cost of swaps built in, total account value in WETH and USDC over time.

eliotstock commented 2 years ago

Depends on https://github.com/eliotstock/dro/issues/27, really.

eliotstock commented 2 years ago

// Data required for calculating the APY% of a given position: // Time position was open in days (from timestamp of add and remove txs) // Opening liquidity (in WETH and USDC, from add tx logs) // * Fees claimed (in WETH and USDC, from remove tx logs)

// Available data sources: // 1. Regular Ethers provider (eg. Alchemy, local node) // Cons: Can't get tx logs, according to Ethers dev. // 2. Etherscan Ethers provider // Cons: Only filterable by address (NFT, not own address), fromBlock, toBlock. // Will only be performant if we can get the block numbers in advance. // 3. Etherscan API, using API key (and possibly JS client) // Cons: JS client is crappy // Not suitable for web UI // 4. Google BigQuery dataset // Cons: Not suitable for web UI // 5. Uniswap NonfungiblePositionManager contract. // Cons: no tx hashes, no tx logs, none of above data. // Only good for getting token IDs. // 6. GraphQL. // Used on info.uniswap.org. // Official v3 subgraph: // Docs: https://docs.uniswap.org/sdk/subgraph/subgraph-data // Code: https://github.com/Uniswap/v3-subgraph/blob/main/src/mappings/core.ts // Explorer: https://thegraph.com/hosted-service/subgraph/uniswap/uniswap-v3 // Test token IDs: // Open: 220351 // Closed: 220176 // Get token IDs from NonfungiblePositionManager contract first? // Given a known token ID: // Opening liquidity is from position.depositedToken0 and position.depositedtoken1, but PositionSnapshot is more useful. // Get tx hash for add tx only is from position.transaction.id: 0xa2354b0583650128d0ad3cc7bbcc1f1edf8b76821ee25637ab16e136fc7357d1 // From that get mint ID (concat of tx has and pool tx count): 0xa2354b0583650128d0ad3cc7bbcc1f1edf8b76821ee25637ab16e136fc7357d1#1679991 // Not much of use on Mint. // Can't get fees claimed directly from Position. // PositionSnapshot only queriable by ID, which is concat of token Id and block number. So we need block numbers of add and remove txs. // Token ID: 220176 // block number for add tx: 14564160 // Position snapshot ID, add tx: 220176#14564160 // Position snapshot ID, remove tx: 220176#14566821. Does NOT give fees claimed directly.

// Design // 1. From Etherscan Provider, get all transactions for this address // 2. For each tx, get block number // 3. From Etherscan Ethers provider, for each block number, get all tx logs for these blocks only // 4. Use analytics-positions code to build Position instance // 5. Get all token IDs for this account from Uniswap position manager contract // 6. Filter Position instances to those that are in set of our own token IDs // 7. Calc APY% from that set of Position instances