Skip to content

Commit

Permalink
feat: support option show_native_balance in AssetList response
Browse files Browse the repository at this point in the history
  • Loading branch information
xoac committed Sep 25, 2024
1 parent 35045a9 commit fcbd954
Show file tree
Hide file tree
Showing 10 changed files with 142 additions and 2 deletions.
9 changes: 9 additions & 0 deletions src/types/enhanced_transaction_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,15 @@ pub struct NativeBalanceChange {
pub amount: Number,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct NativeBalance {
// helius does not return this field if account don't exist on chain
#[serde(default)]
pub lamports: u64,
pub price_per_sol: Option<f64>,
pub total_price: Option<f64>,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct ProgramInfo {
pub source: Source,
Expand Down
6 changes: 4 additions & 2 deletions src/types/types.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use super::{
enums::{AssetSortBy, AssetSortDirection, Context, Interface, OwnershipModel, RoyaltyModel, Scope, UseMethod},
AccountWebhookEncoding, CollectionIdentifier, PriorityLevel, SearchAssetsOptions, SearchConditionType, TokenType,
TransactionStatus, TransactionType, UiTransactionEncoding, WebhookType,
AccountWebhookEncoding, CollectionIdentifier, NativeBalance, PriorityLevel, SearchAssetsOptions,
SearchConditionType, TokenType, TransactionStatus, TransactionType, UiTransactionEncoding, WebhookType,
};
use crate::types::{DisplayOptions, GetAssetOptions};
// use chrono::{DateTime, Utc};
Expand Down Expand Up @@ -288,6 +288,8 @@ pub struct AssetList {
pub items: Vec<Asset>,
#[serde(skip_serializing_if = "Option::is_none")]
pub errors: Option<Vec<AssetError>>,
#[serde(skip_serializing_if = "Option::is_none", rename = "nativeBalance")]
pub native_balance: Option<NativeBalance>,
}

#[derive(Serialize, Deserialize, Debug, Default)]
Expand Down
1 change: 1 addition & 0 deletions tests/rpc/test_get_assets_by_authority.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ async fn test_get_assets_by_authority_success() {
},
],
errors: None,
native_balance: None,
},
id: "1".to_string(),
};
Expand Down
1 change: 1 addition & 0 deletions tests/rpc/test_get_assets_by_creator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ async fn test_get_assets_by_creator_success() {
},
],
errors: None,
native_balance: None,
},
id: "1".to_string(),
};
Expand Down
1 change: 1 addition & 0 deletions tests/rpc/test_get_assets_by_group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ async fn test_get_assets_by_group_success() {
},
],
errors: None,
native_balance: None,
},
id: "1".to_string(),
};
Expand Down
1 change: 1 addition & 0 deletions tests/rpc/test_get_assets_by_owner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ async fn test_get_assets_by_owner_success() {
mpl_core_info: None,
}],
errors: None,
native_balance: None,
},
id: "1".to_string(),
};
Expand Down
1 change: 1 addition & 0 deletions tests/rpc/test_search_assets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ async fn test_search_assets_success() {
mpl_core_info: None,
}],
errors: None,
native_balance: None,
},
id: "1".to_string(),
};
Expand Down
4 changes: 4 additions & 0 deletions tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ mod utils {
mod test_make_keypairs;
}

mod types {
mod test_deserialize_asset_list;
}

mod rpc {
mod test_get_asset;
mod test_get_asset_batch;
Expand Down
108 changes: 108 additions & 0 deletions tests/types/asset_list_with_native_balance.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
{
"total": 1,
"limit": 1000,
"cursor": "eeHNoMJoFxNE9sGgyb3ZU4yJs65ekMX4mRUXWkVB3KV",
"items": [
{
"interface": "V1_NFT",
"id": "JDPM1gUHdke4pHuABnNv51sVGw2ZeuDbyPhKMFnxgGxn",
"content": {
"$schema": "https://schema.metaplex.com/nft1.0.json",
"json_uri": "https://www.hi-hi.vip/json/wifdrop6.json",
"files": [
{
"uri": "https://img.hi-hi.vip/json/img/wif.gif",
"cdn_uri": "https://cdn.helius-rpc.com/cdn-cgi/image//https://img.hi-hi.vip/json/img/wif.gif",
"mime": "image/gif"
}
],
"metadata": {
"attributes": [
{
"value": "https://wifdrop.com",
"trait_type": "Website"
},
{
"value": "True",
"trait_type": "Verified"
},
{
"value": "3,000+ WIF",
"trait_type": "Amount"
},
{
"value": "35 minutes!",
"trait_type": "Time Left"
}
],
"description": "Random Drop event! https://WIFDROP.com",
"name": "3000$ WIF Lucky Drop WIFDROP.com",
"symbol": "WFD",
"token_standard": "NonFungible"
},
"links": {
"external_url": "https://Wifdrop.com",
"image": "https://img.hi-hi.vip/json/img/wif.gif"
}
},
"authorities": [
{
"address": "G1GmNbuEwkmxvawJb2tRa1DAxScGPeXydmCVF2roaDEw",
"scopes": [
"full"
]
}
],
"compression": {
"eligible": false,
"compressed": true,
"data_hash": "38H9vrizmkU7s7eRr4nXWxJuTc2GivGSa8e5yw5fyQy8",
"creator_hash": "H6fmhtvYtsLtsXNtNBsniiSZXkV1J4yP35zgZVzcBb3b",
"asset_hash": "HEmRb8ED5EuyXTitH1bJcWgL6HDLHY95c728Am5A1JT5",
"tree": "3Grymv861hLiCa8cefye5UP5fkAirbtodvkjoe9dH2iV",
"seq": 20911,
"leaf_id": 20908
},
"grouping": [
{
"group_key": "collection",
"group_value": "HPc65ZxbkxdaWZrQBGKYhctCShJoSLWd7Dtkh2eicRwC"
}
],
"royalty": {
"royalty_model": "creators",
"target": null,
"percent": 0.0,
"basis_points": 0,
"primary_sale_happened": false,
"locked": false
},
"creators": [
{
"address": "H17XJzC1N5o15pwB9gMZ8RRHhmNVMbt4PXiHj53epf1n",
"share": 100,
"verified": true
}
],
"ownership": {
"frozen": false,
"delegated": true,
"delegate": "H17XJzC1N5o15pwB9gMZ8RRHhmNVMbt4PXiHj53epf1n",
"ownership_model": "single",
"owner": "HmBek5ZndbgLsWLUqRjXBauXjTupfxZrAGnuL9FJhsBP"
},
"supply": {
"print_max_supply": 0,
"print_current_supply": 0,
"edition_nonce": 0
},
"mutable": true,
"burnt": false
}
],
"nativeBalance": {
"lamports": 47770720,
"price_per_sol": 150.3314666748047,
"total_price": 7.1814424017114264
}
}
12 changes: 12 additions & 0 deletions tests/types/test_deserialize_asset_list.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
use helius::types::AssetList;

#[test]
fn des_asset_list_with_native_balance() {
let raw_json = include_str!("./asset_list_with_native_balance.json");
let deserialized: AssetList = serde_json::from_str(raw_json).unwrap();

let native_balance = deserialized.native_balance.unwrap();
assert_eq!(native_balance.lamports, 47770720);
assert_eq!(native_balance.price_per_sol, Some(150.3314666748047_f64));
assert_eq!(native_balance.total_price, Some(7.1814424017114264_f64));
}

0 comments on commit fcbd954

Please sign in to comment.