-
Notifications
You must be signed in to change notification settings - Fork 69
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
Add ConcurrentFungibleAssetSupply and ConcurrentFungibleAssetBalance #338
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,7 +9,7 @@ use crate::{ | |
coin_models::coin_utils::COIN_ADDR, default_models::move_resources::MoveResource, | ||
token_models::token_utils::URI_LENGTH, token_v2_models::v2_token_utils::ResourceReference, | ||
}, | ||
utils::util::{deserialize_from_string, truncate_str}, | ||
utils::util::{deserialize_from_string, truncate_str, Aggregator}, | ||
}; | ||
use anyhow::{Context, Result}; | ||
use aptos_protos::transaction::v1::WriteResource; | ||
|
@@ -187,6 +187,76 @@ impl FungibleAssetSupply { | |
} | ||
} | ||
|
||
#[derive(Serialize, Deserialize, Debug, Clone)] | ||
pub struct ConcurrentFungibleAssetSupply { | ||
pub current: Aggregator, | ||
} | ||
|
||
impl ConcurrentFungibleAssetSupply { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This doesn't seem to be used anywhere? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for the observation. I am now pre-processing and storing the ConcurrentFungibleAssetSupply in object_metadatas. I modified FungibleAssetMetadataModel to store the ConcurrentFungibleAssetSupply in supply_v2 and maximum_v2. |
||
pub fn from_write_resource( | ||
write_resource: &WriteResource, | ||
txn_version: i64, | ||
) -> anyhow::Result<Option<Self>> { | ||
let type_str: String = MoveResource::get_outer_type_from_write_resource(write_resource); | ||
if !V2FungibleAssetResource::is_resource_supported(type_str.as_str()) { | ||
return Ok(None); | ||
} | ||
let resource = MoveResource::from_write_resource( | ||
write_resource, | ||
0, // Placeholder, this isn't used anyway | ||
txn_version, | ||
0, // Placeholder, this isn't used anyway | ||
); | ||
|
||
if let V2FungibleAssetResource::ConcurrentFungibleAssetSupply(inner) = | ||
V2FungibleAssetResource::from_resource( | ||
&type_str, | ||
resource.data.as_ref().unwrap(), | ||
txn_version, | ||
)? | ||
{ | ||
Ok(Some(inner)) | ||
} else { | ||
Ok(None) | ||
} | ||
} | ||
} | ||
|
||
#[derive(Serialize, Deserialize, Debug, Clone)] | ||
pub struct ConcurrentFungibleAssetBalance { | ||
pub balance: Aggregator, | ||
} | ||
|
||
impl ConcurrentFungibleAssetBalance { | ||
pub fn from_write_resource( | ||
write_resource: &WriteResource, | ||
txn_version: i64, | ||
) -> anyhow::Result<Option<Self>> { | ||
let type_str: String = MoveResource::get_outer_type_from_write_resource(write_resource); | ||
if !V2FungibleAssetResource::is_resource_supported(type_str.as_str()) { | ||
return Ok(None); | ||
} | ||
let resource = MoveResource::from_write_resource( | ||
write_resource, | ||
0, // Placeholder, this isn't used anyway | ||
txn_version, | ||
0, // Placeholder, this isn't used anyway | ||
); | ||
|
||
if let V2FungibleAssetResource::ConcurrentFungibleAssetBalance(inner) = | ||
V2FungibleAssetResource::from_resource( | ||
&type_str, | ||
resource.data.as_ref().unwrap(), | ||
txn_version, | ||
)? | ||
{ | ||
Ok(Some(inner)) | ||
} else { | ||
Ok(None) | ||
} | ||
} | ||
} | ||
|
||
#[derive(Serialize, Deserialize, Debug, Clone)] | ||
pub struct DepositEvent { | ||
#[serde(deserialize_with = "deserialize_from_string")] | ||
|
@@ -209,14 +279,18 @@ pub enum V2FungibleAssetResource { | |
FungibleAssetMetadata(FungibleAssetMetadata), | ||
FungibleAssetStore(FungibleAssetStore), | ||
FungibleAssetSupply(FungibleAssetSupply), | ||
ConcurrentFungibleAssetSupply(ConcurrentFungibleAssetSupply), | ||
ConcurrentFungibleAssetBalance(ConcurrentFungibleAssetBalance), | ||
} | ||
|
||
impl V2FungibleAssetResource { | ||
pub fn is_resource_supported(data_type: &str) -> bool { | ||
[ | ||
format!("{}::fungible_asset::Supply", COIN_ADDR), | ||
format!("{}::fungible_asset::ConcurrentSupply", COIN_ADDR), | ||
format!("{}::fungible_asset::Metadata", COIN_ADDR), | ||
format!("{}::fungible_asset::FungibleStore", COIN_ADDR), | ||
format!("{}::fungible_asset::ConcurrentFungibleBalance", COIN_ADDR), | ||
] | ||
.contains(&data_type.to_string()) | ||
} | ||
|
@@ -231,6 +305,10 @@ impl V2FungibleAssetResource { | |
serde_json::from_value(data.clone()) | ||
.map(|inner| Some(Self::FungibleAssetSupply(inner))) | ||
}, | ||
x if x == format!("{}::fungible_asset::ConcurrentSupply", COIN_ADDR) => { | ||
serde_json::from_value(data.clone()) | ||
.map(|inner| Some(Self::ConcurrentFungibleAssetSupply(inner))) | ||
}, | ||
x if x == format!("{}::fungible_asset::Metadata", COIN_ADDR) => { | ||
serde_json::from_value(data.clone()) | ||
.map(|inner| Some(Self::FungibleAssetMetadata(inner))) | ||
|
@@ -239,6 +317,10 @@ impl V2FungibleAssetResource { | |
serde_json::from_value(data.clone()) | ||
.map(|inner| Some(Self::FungibleAssetStore(inner))) | ||
}, | ||
x if x == format!("{}::fungible_asset::ConcurrentFungibleBalance", COIN_ADDR) => { | ||
serde_json::from_value(data.clone()) | ||
.map(|inner| Some(Self::ConcurrentFungibleAssetBalance(inner))) | ||
}, | ||
_ => Ok(None), | ||
} | ||
.context(format!( | ||
|
@@ -312,4 +394,6 @@ mod tests { | |
panic!("Wrong type") | ||
} | ||
} | ||
|
||
// TODO: Add similar tests for ConcurrentFungibleAssetSupply. | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
like it.