Skip to content
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
3 changes: 1 addition & 2 deletions crates/sugarfunge-api-types/src/asset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,14 @@ pub struct AssetBalanceOutput {
#[derive(Serialize, Deserialize)]
pub struct AssetBalancesInput {
pub account: String,
pub class_id: Option<u64>,
}

#[derive(Serialize, Deserialize)]
pub struct AssetBalancesOutput {
pub balances: Vec<AssetBalanceItemOutput>,
}

#[derive(Serialize, Deserialize)]
#[derive(Serialize, Deserialize, Debug)]
pub struct AssetBalanceItemOutput {
pub class_id: ClassId,
pub asset_id: AssetId,
Expand Down
20 changes: 20 additions & 0 deletions src/asset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,26 @@ pub async fn balance(
}))
}

/// Get balance for given asset
pub async fn balances(
data: web::Data<AppState>,
req: web::Json<AssetBalancesInput>,
) -> error::Result<HttpResponse> {
let account = sp_core::sr25519::Public::from_str(&req.account).map_err(map_account_err)?;
let api = &data.api;
let params = &[jsonrpsee_types::to_json_value(account)?];
let result = api
.client
.rpc()
.client
.request("asset_balancesOfOwner", params)
.await;

let balances = result.map_err(map_subxt_err)?;

Ok(HttpResponse::Ok().json(AssetBalancesOutput { balances }))
}

/// Transfer asset from to accounts
pub async fn transfer_from(
data: web::Data<AppState>,
Expand Down
7 changes: 3 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use actix_web::{
};
use command::*;
use state::*;
use std::sync::{Arc};
use std::sync::Arc;
use structopt::StructOpt;
use subxt::ClientBuilder;
use sugarfunge_api_types::sugarfunge;
Expand Down Expand Up @@ -36,9 +36,7 @@ async fn main() -> std::io::Result<()> {
.map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e.to_string()))?
.to_runtime_api::<sugarfunge::RuntimeApi<sugarfunge::DefaultConfig>>();

let state = AppState {
api: Arc::new(api),
};
let state = AppState { api: Arc::new(api) };

HttpServer::new(move || {
let cors = Cors::default()
Expand All @@ -64,6 +62,7 @@ async fn main() -> std::io::Result<()> {
.route("asset/mint", web::post().to(asset::mint))
.route("asset/burn", web::post().to(asset::burn))
.route("asset/balance", web::post().to(asset::balance))
.route("asset/balances", web::post().to(asset::balances))
.route("asset/transfer_from", web::post().to(asset::transfer_from))
.route("currency/issue", web::post().to(currency::issue))
.route("currency/issuance", web::post().to(currency::issuance))
Expand Down