- 
                Notifications
    
You must be signed in to change notification settings  - Fork 19
 
Simple Calculator Tool for Accessed Funds Total #250
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
          
     Closed
      
      
    
  
     Closed
                    Changes from all commits
      Commits
    
    
            Show all changes
          
          
            17 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      6a9b9c5
              
                beginnings of proof of concept
              
              
                rswanson af4c208
              
                beginnings of refactor, playing around with structure
              
              
                rswanson 290ef6c
              
                adding some more wip functionality to get token prices plus some refa…
              
              
                rswanson da58d4d
              
                formatting
              
              
                rswanson 9d29a40
              
                more hardcoded values as we progress to fully functional
              
              
                rswanson b34e308
              
                updating to correct enum value
              
              
                rswanson 26106e6
              
                bumping to latest action versions
              
              
                rswanson f2ed2cc
              
                cleaning up quite a bit
              
              
                rswanson 9f96ca4
              
                cleaning up linter complaints
              
              
                rswanson 0747138
              
                cleaning up linter complaints
              
              
                rswanson 00ee6f1
              
                cleaning up linter complaints
              
              
                rswanson 7067f67
              
                cleaning up linter complaints
              
              
                rswanson 2681ec4
              
                refactor into a more testable structure
              
              
                rswanson e5f6d94
              
                forgot to fmt
              
              
                rswanson 12b0c4a
              
                adding documentation
              
              
                rswanson eee860c
              
                forgot to finish a few variable name refactors in tests
              
              
                rswanson 5d32b0e
              
                minor readme updates and more modularizing
              
              
                rswanson File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
      
      Oops, something went wrong.
      
    
  
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| # Changelog | ||
| 
     | 
||
| ### 1.0 | ||
| 
     | 
||
| - Added initial cli tool to calculate the total amount of recovered funds that have been accessed | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| [package] | ||
| name = "accessed-funds-calculator" | ||
| version = "0.1.0" | ||
| edition = "2021" | ||
| 
     | 
||
| # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
| 
     | 
||
| [dependencies] | ||
| reqwest = { version = "0.11", features = ["json"] } | ||
| tokio = { version = "1", features = ["full"] } | ||
| serde = { version = "1.0", features = ["derive"] } | ||
| serde_json = "1.0" | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| # Accessed Funds Calculator | ||
| 
     | 
||
| A quick and simple (and hopefully portable-ish) calculator to answer the question "how much has been accessed from the contract so far" | ||
| ## Requirements | ||
| 
     | 
||
| - Needs a valid Etherscan API key to be configured in your local environment, or to be included with the run command | ||
| ``` | ||
| export ETHERSCAN_KEY=YOUR_KEY | ||
| ``` | ||
| - Requires a valid etherscan api url | ||
| ``` | ||
| export ETHERSCAN_API=https://api.etherscan.io/api | ||
| ``` | ||
| - Requires a valid token price api | ||
| ``` | ||
| export PRICING_API=https://api.coingecko.com/api/v3/simple/price | ||
| ``` | ||
| 
     | 
||
| 
     | 
||
| ## Usage | ||
| *Using the binary:* | ||
| ``` | ||
| ./accessed_funds_calculator | ||
| ``` | ||
| *Using cargo:* | ||
| ``` | ||
| cargo run -p accessed-funds-calculator | ||
| ``` | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| use std::collections::HashMap; | ||
| use std::env; | ||
| 
     | 
||
| mod tokens; | ||
| 
     | 
||
| use crate::tokens::{Token, TokenName}; | ||
| 
     | 
||
| #[tokio::main] | ||
| async fn main() -> Result<(), Box<dyn std::error::Error>> { | ||
| let address = "0xa4B86BcbB18639D8e708d6163a0c734aFcDB770c"; | ||
| let usdc = Token::get_instance_of(TokenName::Usdc); | ||
| let usdt = Token::get_instance_of(TokenName::Usdt); | ||
| let cqt = Token::get_instance_of(TokenName::Cqt); | ||
| let wbtc = Token::get_instance_of(TokenName::Wbtc); | ||
| let frax = Token::get_instance_of(TokenName::Frax); | ||
| let iag = Token::get_instance_of(TokenName::Iag); | ||
| let weth = Token::get_instance_of(TokenName::Weth); | ||
| let dai = Token::get_instance_of(TokenName::Dai); | ||
| let c3 = Token::get_instance_of(TokenName::C3); | ||
| let fxs = Token::get_instance_of(TokenName::Fxs); | ||
| let cards = Token::get_instance_of(TokenName::Cards); | ||
| let hbot = Token::get_instance_of(TokenName::Hbot); | ||
| let sdl = Token::get_instance_of(TokenName::Sdl); | ||
| let gero = Token::get_instance_of(TokenName::Gero); | ||
| 
     | 
||
| let tokens: Vec<Token> = vec![ | ||
| usdc, cqt, usdt, wbtc, frax, iag, weth, dai, c3, fxs, cards, hbot, sdl, gero, | ||
| ]; | ||
| 
     | 
||
| let mut total_accessed_value: f64 = 0.0; | ||
| for token in tokens { | ||
| let balance: f64 = get_token_balance(&token, address).await?; | ||
| let accessed: f64 = token.recovered_total - balance; | ||
| let token_price: f64 = get_token_price(&token).await?; | ||
| total_accessed_value += accessed * token_price; | ||
| println!("{}:{} accessed, price: {}", token.id, accessed, token_price); | ||
| } | ||
| 
     | 
||
| println!(); | ||
| println!("#################################################"); | ||
| println!("#################################################"); | ||
| println!("### total accessed value: ${} ###", total_accessed_value); | ||
| println!("#################################################"); | ||
| println!("#################################################"); | ||
| 
     | 
||
| Ok(()) | ||
| } | ||
| 
     | 
||
| async fn get_token_balance( | ||
| token: &Token, | ||
| address: &str, | ||
| ) -> Result<f64, Box<dyn std::error::Error>> { | ||
| let etherscan_url = env::var("ETHERSCAN_API")?; | ||
| let module = "account"; | ||
| let action = "tokenbalance"; | ||
| let api_key = env::var("ETHERSCAN_KEY")?; | ||
| let request_url = format!( | ||
| "{}?module={}&action={}&contractaddress={}&address={}&apiKey={}", | ||
| ðerscan_url, &module, &action, &token.contract_address, &address, &api_key | ||
| ); | ||
| let resp = reqwest::get(request_url) | ||
| .await? | ||
| .json::<HashMap<String, String>>() | ||
| .await?; | ||
| let balance: f64 = resp["result"].parse().unwrap(); | ||
| let balance = balance / ((10.0_f64).powf(token.decimals)); | ||
| 
     | 
||
| Ok(balance) | ||
| } | ||
| 
     | 
||
| async fn get_token_price(token: &Token) -> Result<f64, Box<dyn std::error::Error>> { | ||
| let coingecko_url = env::var("PRICING_API")?; | ||
| let vs_currency = "usd"; | ||
| 
     | 
||
| let request_url = format!( | ||
| "{}?ids={}&vs_currencies={}", | ||
| coingecko_url, &token.id, vs_currency | ||
| ); | ||
| let resp = reqwest::get(request_url) | ||
| .await? | ||
| .json::<HashMap<String, HashMap<String, f64>>>() | ||
| .await?; | ||
| let price = resp[&token.id].clone(); | ||
| let price = price["usd"]; | ||
| 
     | 
||
| Ok(price) | ||
| } | ||
      
      Oops, something went wrong.
        
    
  
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
danger math :o