1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
use actix_web::{get, HttpResponse, web};
use serde::{Serialize, Deserialize};
use serde_json::{Value, json};
use reqwest::Client;
use crate::{URL, TOKEN_MINT};

#[derive(Serialize, Deserialize, Debug)]
pub struct Request {
    account_key: String,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct BalanceResponse {
    jsonrpc: String,
    result: Balance,
    id: u64,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct Balance {
    value: u64,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct TokenAccountResponse {
    jsonrpc: String,
    result: TokenAccount,
    id: u64,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct TokenAccount {
    context: TokenAccountContext,
    value: Vec<TokenAccountValue>,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct TokenAccountContext {
    slot: u64,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct TokenAccountValue {
    pubkey: String,
    account: TokenAccountAccount,
}

#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct TokenAccountAccount {
    lamports: u64,
    owner: String,
    executable: bool,
    rent_epoch: u64,
    data: TokenAccountData,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct TokenAccountData {
    parsed: TokenAccountParsed,
    program: String,
    space: u64,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct TokenAccountParsed {
    info: TokenAccountInfo, 
}

#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct TokenAccountInfo {
    is_native: bool,
    mint: String,
    owner: String,
    state: String,
    token_amount: TokenAccountAmount,
}

#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct TokenAccountAmount {
    amount: String,
    decimals: u8,
    ui_amount: Option<f64>,
    ui_amount_string: String,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct ApiResponse {
    account_key: String,
    network_balance: u64,
    token_account: Option<String>,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct ErrorResponse {
    message: String,
}

#[get("/get_user_info")]
pub async fn get_user_info(req: web::Json<Request>) -> HttpResponse {
    let body_balance = json!({
        "jsonrpc": "2.0",
        "id": 1,
        "method": "getBalance",
        "params": [
            req.account_key.to_string(),
        ]
    });

    let balance_response: Result<BalanceResponse, Box<dyn std::error::Error>> = jsonrpc_balance(body_balance, URL);

    let network_balance = balance_response.map(| value | value.result.value).unwrap_or(0);

    let body_token_account = json!({
        "jsonrpc": "2.0",
        "id": 1,
        "method": "getTokenAccountsByOwner",
        "params": [
            req.account_key.to_string(),
            {
                "mint": TOKEN_MINT
            },
            {
                "encoding": "jsonParsed"
            }
        ]
    });

    let token_account_response: Result<TokenAccountResponse, Box<dyn std::error::Error>> = jsonrpc_token_account(body_token_account, URL);

    let token_account = match token_account_response {
        Ok(res) => {
            res.result.value.iter().find(| value | {
                value.account.data.parsed.info.mint == TOKEN_MINT
            }).map(| x | x.pubkey.to_string())
        },
        Err(_) => return HttpResponse::BadRequest().json(ErrorResponse {
            message: "The wallet does not have an abion token account yet".to_string()
        }),
    };

    HttpResponse::Ok().json(ApiResponse {
        account_key: req.account_key.to_string(),
        network_balance,
        token_account,
    })
}

#[tokio::main]
async fn jsonrpc_balance(body: Value, url: &str) -> Result<BalanceResponse, Box<dyn std::error::Error>> {
    let req = Client::new()
        .post(url)
        .json(&body)
        .send().await?;

    let res: BalanceResponse = req.json().await?;

    Ok(res)
}

#[tokio::main]
async fn jsonrpc_token_account(body: Value, url: &str) -> Result<TokenAccountResponse, Box<dyn std::error::Error>> {
    let req = Client::new()
        .post(url)
        .json(&body)
        .send().await?;

    let res: TokenAccountResponse = req.json().await?;

    Ok(res)
}