Skip to content

Instantly share code, notes, and snippets.

@chaoky
Last active March 3, 2022 22:50
Show Gist options
  • Save chaoky/d940f3d8b954c0c6780e0668882830f3 to your computer and use it in GitHub Desktop.
Save chaoky/d940f3d8b954c0c6780e0668882830f3 to your computer and use it in GitHub Desktop.
//from: https://github.com/GenezysDigitalAssets/clashofcars-node-service/blob/main/src/routes/get_player_data.ts
use actix_web::{
get,
web::{Data, Json},
App, HttpServer, Result,
};
use serde::{Deserialize, Serialize};
use solana_account_decoder::parse_token::UiTokenAmount;
use solana_client::nonblocking::rpc_client::RpcClient;
use solana_program::pubkey::Pubkey;
use spl_associated_token_account::get_associated_token_address;
struct Settings {
clash_token_pk: Pubkey,
}
#[derive(Deserialize, Serialize)]
struct GetPlayerData {
player_wallet: Pubkey,
}
#[derive(Deserialize, Serialize)]
struct GetPlayerDataResponse {
success: bool,
balance: UiTokenAmount,
}
#[get("/getPlayerData")]
async fn get_player_data(
body: Json<GetPlayerData>,
settings: Data<Settings>,
connection: Data<RpcClient>,
) -> Result<Json<GetPlayerDataResponse>> {
let player_clash_wallet =
get_associated_token_address(&body.player_wallet, &settings.clash_token_pk);
let clash_balance = connection
.get_token_account_balance(&player_clash_wallet)
.await
.unwrap();
Ok(Json(GetPlayerDataResponse {
success: true,
balance: clash_balance,
}))
}
#[cfg(test)]
mod tests {
use super::*;
use actix_web::{
http::{self, header::ContentType},
test,
};
#[actix_web::test]
async fn test_get_player_data() {
let app = test::init_service(
App::new()
.app_data(Data::new(Settings {
clash_token_pk: "ENUPKrN5RFzCU5nfnLF6GxJ6deSRtM6whmi3s5yM2ftB"
.try_into()
.unwrap(),
}))
.app_data(Data::new(RpcClient::new(
"https://api.devnet.solana.com".to_string(),
)))
.service(get_player_data),
)
.await;
let req = test::TestRequest::default()
.insert_header(ContentType::json())
.set_json(GetPlayerData {
player_wallet: Pubkey::new_unique(),
})
.uri("/getPlayerData")
.to_request();
let resp = test::call_service(&app, req).await;
assert_eq!(resp.status(), http::StatusCode::OK);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment