-
Notifications
You must be signed in to change notification settings - Fork 3.7k
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
[api] balance api #15755
[api] balance api #15755
Conversation
⏱️ 2h 49m total CI duration on this PR
🚨 2 jobs on the last run were significantly faster/slower than expected
|
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.
Please test with local testnet
api/src/accounts.rs
Outdated
pub fn balance(&self, asset_type: AssetType, accept_type: &AcceptType) -> BasicResultWith404<u64> { | ||
let (fa_metadata_address, mut balance) = match asset_type { | ||
AssetType::Coin(move_struct_tag) => { | ||
let coin_store_type_tag = StructTag::from_str(&format!("0x1::coin::CoinStore<{}>", move_struct_tag.to_string())).map_err(|err| { |
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.
Do you want to validate the move struct type first?
Ensure that we're not getting balances of say u8
?
Also, ensure it's limited in parsing
api/types/src/account.rs
Outdated
impl FromStr for AssetType { | ||
type Err = anyhow::Error; | ||
fn from_str(s: &str) -> Result<Self, Self::Err> { | ||
match Address::from_str(s) { | ||
Ok(address) => Ok(AssetType::FungibleAsset(address)), | ||
Err(_) => { | ||
match MoveStructTag::from_str(s) { | ||
Ok(tag) => Ok(AssetType::Coin(tag)), | ||
Err(e) => Err(e), | ||
} | ||
} | ||
} | ||
} | ||
} |
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.
Oh, I see, you do one then the other
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.
yes, I do FA first as it is shorter and we will migrate mostly eventually.
api/src/accounts.rs
Outdated
let primary_fungible_store_address = get_paired_fa_primary_store_address(self.address.into(), fa_metadata_address); | ||
if let Some(data_blob) = self.context.get_state_value_poem(&StateKey::resource_group(&primary_fungible_store_address, &ObjectGroupResource::struct_tag()), self.ledger_version, &self.latest_ledger_info)? { | ||
if let Some(object_group) = bcs::from_bytes::<ObjectGroupResource>(&data_blob).ok() { | ||
if let Some(fa_store) = object_group.group.get(&FungibleStoreResource::struct_tag()) { | ||
let fa_store_resource = bcs::from_bytes::<FungibleStoreResource>(fa_store).map_err(|err| { | ||
BasicErrorWith404::internal_with_code( | ||
err, | ||
AptosErrorCode::InternalError, | ||
&self.latest_ledger_info, | ||
) | ||
})?; | ||
if fa_store_resource.balance != 0 { | ||
balance += fa_store_resource.balance(); | ||
} else if let Some(concurrent_fa_balance) = object_group.group.get(&ConcurrentFungibleBalanceResource::struct_tag()) { | ||
// query potential concurrent fa balance | ||
let concurrent_fa_balance_resource = bcs::from_bytes::<ConcurrentFungibleBalanceResource>(concurrent_fa_balance).map_err(|err| { | ||
BasicErrorWith404::internal_with_code( | ||
err, | ||
AptosErrorCode::InternalError, | ||
&self.latest_ledger_info, | ||
) | ||
})?; | ||
balance += concurrent_fa_balance_resource.balance(); | ||
} |
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.
How's this compare in performance to the view function?
Also, if we're doing this... we probably want a supply query too
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.
good question. it does not need move vm so I guess it should be much better.
Anyway to quickly test it?
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.
I was just wondering if you had a comparison.
Otherwise, this should be much cheaper. I don't need you to prove it if you didn't compare.
d2a217b
to
38d30b2
Compare
coin_balance, | ||
) | ||
}, | ||
AssetType::FungibleAsset(fa_metadata_adddress) => (fa_metadata_adddress.into(), 0), |
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.
The variable fa_metadata_adddress
contains a typo (extra 'd'). Please rename to fa_metadata_address
to maintain consistent spelling throughout the codebase.
Spotted by Graphite Reviewer
Is this helpful? React 👍 or 👎 to let us know.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
38d30b2
to
55b8d69
Compare
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
55b8d69
to
544ab77
Compare
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
✅ Forge suite
|
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
✅ Forge suite
|
create endpoint
/accounts/{ADDRESS}/balance/{TYPE_TAG | ADDRESS}
to return the total balance of migrated coin or pure FA.