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
[RPC] Add endpoint for fetching CoinMetadata #6281
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@mysten/sui.js": patch | ||
--- | ||
|
||
Replace `getCoinDenominationInfo` with `getCoinMetadata` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,16 +7,20 @@ use jsonrpsee::core::RpcResult; | |
use jsonrpsee_core::server::rpc_module::RpcModule; | ||
use move_binary_format::normalized::{Module as NormalizedModule, Type}; | ||
use move_core_types::identifier::Identifier; | ||
use move_core_types::language_storage::StructTag; | ||
use std::collections::BTreeMap; | ||
use std::sync::Arc; | ||
use sui_types::coin::CoinMetadata; | ||
use sui_types::event::Event; | ||
use sui_types::gas_coin::GAS; | ||
use tap::TapFallible; | ||
|
||
use fastcrypto::encoding::Base64; | ||
use sui_core::authority::AuthorityState; | ||
use sui_json_rpc_types::{ | ||
GetObjectDataResponse, GetPastObjectDataResponse, MoveFunctionArgType, ObjectValueKind, Page, | ||
SuiMoveNormalizedFunction, SuiMoveNormalizedModule, SuiMoveNormalizedStruct, SuiObjectInfo, | ||
SuiTransactionEffects, SuiTransactionResponse, TransactionsPage, | ||
SuiCoinMetadata, SuiMoveNormalizedFunction, SuiMoveNormalizedModule, SuiMoveNormalizedStruct, | ||
SuiObjectInfo, SuiTransactionEffects, SuiTransactionResponse, TransactionsPage, | ||
}; | ||
use sui_open_rpc::Module; | ||
use sui_types::base_types::SequenceNumber; | ||
|
@@ -151,6 +155,78 @@ impl RpcFullNodeReadApiServer for FullNodeApi { | |
Ok(self.state.dry_exec_transaction(tx_data, txn_digest).await?) | ||
} | ||
|
||
async fn get_coin_metadata(&self, coin_type: String) -> RpcResult<SuiCoinMetadata> { | ||
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 looks quite expensive? We are pulling out the package object every time we need to get the coin's metadata. if this is frequently accessed, maybe we can consider creating a mapping in |
||
let coin_struct = coin_type.parse::<StructTag>().map_err(|e| anyhow!("{e}"))?; | ||
if GAS::is_gas(&coin_struct) { | ||
// TODO: We need to special case for `CoinMetadata<0x2::sui::SUI> because `get_transaction` | ||
// will fail for genesis transaction. However, instead of hardcoding the values here, We | ||
// can store the object id for `CoinMetadata<0x2::sui::SUI>` in the Sui System object | ||
return Ok(SuiCoinMetadata { | ||
id: None, | ||
decimals: 9, | ||
symbol: "SUI".to_string(), | ||
name: "Sui".to_string(), | ||
description: "".to_string(), | ||
icon_url: None, | ||
}); | ||
} | ||
let publish_txn_digest = self | ||
.state | ||
.get_object_read(&coin_struct.address.into()) | ||
.await | ||
.map_err(|e| anyhow!("{e}"))? | ||
.into_object() | ||
.map_err(|e| anyhow!("{e}"))? | ||
.previous_transaction; | ||
let (_, effects) = self.state.get_transaction(publish_txn_digest).await?; | ||
let event = effects | ||
.events | ||
.into_iter() | ||
.find(|e| { | ||
if let Event::NewObject { object_type, .. } = e { | ||
return object_type.parse::<StructTag>().map_or(false, |tag| { | ||
CoinMetadata::is_coin_metadata(&tag) | ||
&& tag.type_params.len() == 1 | ||
&& tag.type_params[0].to_canonical_string() | ||
== coin_struct.to_canonical_string() | ||
}); | ||
} | ||
false | ||
}) | ||
.ok_or(0) | ||
.map_err(|_| anyhow!("No NewObject event was emitted for CoinMetaData"))?; | ||
|
||
let metadata_object_id = event | ||
.object_id() | ||
.ok_or(0) | ||
.map_err(|_| anyhow!("No object id is found in NewObject event"))?; | ||
|
||
Ok(self | ||
.state | ||
.get_object_read(&metadata_object_id) | ||
.await | ||
.map_err(|e| { | ||
debug!(?metadata_object_id, "Failed to get object: {:?}", e); | ||
anyhow!("{e}") | ||
})? | ||
.into_object() | ||
.map_err(|e| { | ||
debug!( | ||
?metadata_object_id, | ||
"Failed to convert ObjectRead to Object: {:?}", e | ||
); | ||
anyhow!("{e}") | ||
})? | ||
.try_into() | ||
.map_err(|e| { | ||
debug!( | ||
?metadata_object_id, | ||
"Failed to convert object to CoinMetadata: {:?}", e | ||
); | ||
anyhow!("{e}") | ||
})?) | ||
} | ||
|
||
async fn get_normalized_move_modules_by_package( | ||
&self, | ||
package: ObjectID, | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
@666lcz what happens if this method isn’t available? This is currently hardcoded for SUI so I’m worried that this will break sui display for networks that don’t yet support this
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 catch! I just added the logic to return hardcoded value for SUI if the RPC version is prior to
0.17.0