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

getTokenAccount rpc method, showFungible and showZeroBalance options #224

35 changes: 32 additions & 3 deletions das_api/src/api/api_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ use digital_asset_types::{
dapi::{
get_asset, get_asset_proofs, get_asset_signatures, get_assets, get_assets_by_authority,
get_assets_by_creator, get_assets_by_group, get_assets_by_owner, get_proof_for_asset,
search_assets,
get_token_accounts, search_assets,
},
rpc::{
filter::{AssetSortBy, SearchConditionType},
response::GetGroupingResponse,
response::{GetGroupingResponse, TokenAccountList},
OwnershipModel, RoyaltyModel,
},
rpc::{OwnershipModel, RoyaltyModel},
};
use open_rpc_derive::document_rpc;
use sea_orm::{sea_query::ConditionType, ConnectionTrait, DbBackend, Statement};
Expand Down Expand Up @@ -516,4 +516,33 @@ impl ApiContract for DasApi {
group_size: gs.size,
})
}

async fn get_token_accounts(
self: &DasApi,
payload: GetTokenAccounts,
) -> Result<TokenAccountList, DasApiError> {
let GetTokenAccounts {
owner_address,
mint_address,
limit,
page,
before,
after,
options,
cursor,
} = payload;
let owner_address = validate_opt_pubkey(&owner_address)?;
let mint_address = validate_opt_pubkey(&mint_address)?;
let options = options.unwrap_or_default();
let page_options = self.validate_pagination(limit, page, &before, &after, &cursor, None)?;
get_token_accounts(
&self.db_connection,
owner_address,
mint_address,
&page_options,
&options,
)
.await
.map_err(Into::into)
}
}
27 changes: 26 additions & 1 deletion das_api/src/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::error::DasApiError;
use async_trait::async_trait;
use digital_asset_types::rpc::filter::{AssetSortDirection, SearchConditionType};
use digital_asset_types::rpc::options::Options;
use digital_asset_types::rpc::response::{AssetList, TransactionSignatureList};
use digital_asset_types::rpc::response::{AssetList, TokenAccountList, TransactionSignatureList};
use digital_asset_types::rpc::{filter::AssetSorting, response::GetGroupingResponse};
use digital_asset_types::rpc::{Asset, AssetProof, Interface, OwnershipModel, RoyaltyModel};
use open_rpc_derive::{document_rpc, rpc};
Expand Down Expand Up @@ -163,6 +163,21 @@ pub struct GetAssetSignatures {
pub sort_direction: Option<AssetSortDirection>,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(deny_unknown_fields, rename_all = "camelCase")]
pub struct GetTokenAccounts {
pub owner_address: Option<String>,
pub mint_address: Option<String>,
pub limit: Option<u32>,
pub page: Option<u32>,
pub before: Option<String>,
pub after: Option<String>,
#[serde(default, alias = "displayOptions")]
pub options: Option<Options>,
#[serde(default)]
pub cursor: Option<String>,
}

#[document_rpc]
#[async_trait]
pub trait ApiContract: Send + Sync + 'static {
Expand Down Expand Up @@ -251,4 +266,14 @@ pub trait ApiContract: Send + Sync + 'static {
summary = "Get a list of assets grouped by a specific authority"
)]
async fn get_grouping(&self, payload: GetGrouping) -> Result<GetGroupingResponse, DasApiError>;

#[rpc(
name = "getTokenAccounts",
params = "named",
summary = "Get a list of token accounts by owner or mint"
)]
async fn get_token_accounts(
&self,
payload: GetTokenAccounts,
) -> Result<TokenAccountList, DasApiError>;
}
13 changes: 13 additions & 0 deletions das_api/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,19 @@ impl RpcApiBuilder {
Ok(rpc_context.schema())
})?;

module.register_async_method(
"get_token_accounts",
|rpc_params, rpc_context| async move {
let payload = rpc_params.parse::<GetTokenAccounts>()?;
rpc_context
.get_token_accounts(payload)
.await
.map_err(Into::into)
},
)?;

module.register_alias("getTokenAccounts", "get_token_accounts")?;

Ok(module)
}
}
7 changes: 6 additions & 1 deletion digital_asset_types/src/dao/full_asset.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
use crate::dao::{asset, asset_authority, asset_creators, asset_data, asset_grouping};
use crate::dao::{
asset, asset_authority, asset_creators, asset_data, asset_grouping,
};

use super::tokens;

#[derive(Clone, Debug, PartialEq)]
pub struct FullAsset {
Expand All @@ -7,6 +11,7 @@ pub struct FullAsset {
pub authorities: Vec<asset_authority::Model>,
pub creators: Vec<asset_creators::Model>,
pub groups: Vec<asset_grouping::Model>,
pub token_info: Option<tokens::Model>,
}
#[derive(Clone, Debug, PartialEq)]
pub struct AssetRelated {
Expand Down
3 changes: 3 additions & 0 deletions digital_asset_types/src/dao/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
mod full_asset;
mod generated;
pub mod scopes;
use crate::rpc::options::Options;

use self::sea_orm_active_enums::{
OwnerType, RoyaltyTargetType, SpecificationAssetClass, SpecificationVersions,
};
Expand Down Expand Up @@ -181,6 +183,7 @@ impl SearchAssetsQuery {
joins.push(rel);
}


if let Some(a) = self.authority_address.to_owned() {
conditions = conditions.add(asset_authority::Column::Authority.eq(a));
let rel = extensions::asset_authority::Relation::Asset
Expand Down
Loading