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

Simple Calculator Tool for Accessed Funds Total #250

Open
wants to merge 17 commits into
base: main
Choose a base branch
from
Open
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
10 changes: 5 additions & 5 deletions .github/workflows/docker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ jobs:
runs-on: ubuntu-22.04

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
- uses: actions-rs/toolchain@v1
with:
toolchain: stable
- uses: Swatinem/rust-cache@v1
- uses: Swatinem/rust-cache@v2
with:
# Add a key to prevent rust cache collision with rust.yml workflows
key: 'release'

- name: Build agents (release)
run: cargo build --release

Expand All @@ -43,14 +43,14 @@ jobs:
type=sha

- name: Login to Docker repository
uses: docker/login-action@v1
uses: docker/login-action@v2
with:
registry: gcr.io
username: _json_key
password: ${{ secrets.GCLOUD_SERVICE_KEY }}

- name: Build and push container
uses: docker/build-push-action@v2
uses: docker/build-push-action@v3
with:
context: .
push: true
Expand Down
10 changes: 10 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ members = [
"tools/nomad-cli",
"tools/balance-exporter",
"tools/killswitch",
"tools/accessed-funds-calculator",
]
5 changes: 5 additions & 0 deletions tools/accessed-funds-calculator/CHANGELOG.md
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
12 changes: 12 additions & 0 deletions tools/accessed-funds-calculator/Cargo.toml
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"
28 changes: 28 additions & 0 deletions tools/accessed-funds-calculator/README.md
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
```
87 changes: 87 additions & 0 deletions tools/accessed-funds-calculator/src/main.rs
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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

danger math :o

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={}",
&etherscan_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)
}
Loading