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

Implements Account balance list cli #760

Merged
merged 5 commits into from
Sep 7, 2023
Merged

Implements Account balance list cli #760

merged 5 commits into from
Sep 7, 2023

Conversation

baichuan3
Copy link
Collaborator

@baichuan3 baichuan3 commented Sep 6, 2023

resolve #744

Problems
1, It is hard to support converting from AnnotatedMoveValueView to AnnotatedMoveValue, mainly because of it hard to support convert SpecificStruct to AnnotatedMoveStruct. As a result, parsing AnnotatedCoinStoreView from the RPC API is very tedious. relative of #758
2, The current implementation of AnnotatedMoveValueView may be flawed. Coin balance defines the U256 type, but the type obtained from AnnotatedMoveValueView is U64(StrView(10000), and the expected type is U256(StrView(10000). May return U128(StrView(value) type.
3, Type_table exposes the method of obtaining the handle, is it reasonable to expose the handle, and will this method have security problems?

// Returns table handle of `table`.
public fun handle(table: &TypeTable): &ObjectID {
      &table.handle
}
value: {Identifier("value"): U64(StrView(10000))} })

TODO
1, Coin balance needs to depend on CoinInfo to display the decimal point after processing decimals
2,Add test case for the balance command

Example

  • Publish coin examples
rooch move publish -p examples/coins --sender-account 0xe1176537c0175d336353dad12f7eb60c658ce526eeb3cd08409e6fd8c2dfa1d7  --named-addresses coins=0xe1176537c0175d336353dad12f7eb60c658ce526eeb3cd08409e6fd8c2dfa1d7
  • Call faucet to get test coin
rooch move run --function 0xe1176537c0175d336353dad12f7eb60c658ce526eeb3cd08409e6fd8c2dfa1d7::fixed_supply_coin::faucet  --sender-account 0xe1176537c0175d336353dad12f7eb60c658ce526eeb3cd08409e6fd8c2dfa1d7
  • Call Account balance cli
rooch account balance -a 0xe1176537c0175d336353dad12f7eb60c658ce526eeb3cd08409e6fd8c2dfa1d7

Result

                                              Coin Type                                                |             Balance
------------------------------------------------
0xe1176537c0175d336353dad12f7eb60c658ce526eeb3cd08409e6fd8c2dfa1d7::fixed_supply_coin::FSC |              10000
0x3::gas_coin::GasCoin |            9996919761
  • Call Account balance cli with coin-type
rooch account balance -a 0xe1176537c0175d336353dad12f7eb60c658ce526eeb3cd08409e6fd8c2dfa1d7 --coin-type "0xe1176537c0175d336353dad12f7eb60c658ce526eeb3cd08409e6fd8c2dfa1d7::fixed_supply_coin::FSC"

Result

Coin Type                                                |             Balance
------------------------------------------------
0xe1176537c0175d336353dad12f7eb60c658ce526eeb3cd08409e6fd8c2dfa1d7::fixed_supply_coin::FSC |              10000

@vercel
Copy link

vercel bot commented Sep 6, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

1 Ignored Deployment
Name Status Preview Comments Updated (UTC)
rooch ⬜️ Ignored (Inspect) Visit Preview Sep 7, 2023 7:10am

Comment on lines 61 to 72
let coin_store_handle_opt: Option<ObjectID> = client
.call_function(&ctx, call)?
.into_result()
.map(|values| {
let value = values.get(0).expect("Expected return value");
let result = MoveOption::<ObjectID>::from_bytes(&value.value)
.expect("Expected Option<ObjectID>");
result.into()
})?;
let coin_store_handle = coin_store_handle_opt
// .expect(format!("Failed to get coin store handle via {}", addr).as_str());
.unwrap_or_else(|| panic!("Failed to get coin store handle via {}", addr));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Directly call the coin_module::coin_store_handle?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried it before, but there are some problems.

After binding coin_mudule, await occurs here, with coin_mudule maybe used later.

the trait Sync is not implemented for dyn MoveFunctionCaller, future is not Send as this value is used across an await.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The client has already implemented the MoveFunctionCaller.

let session_key_module = client.as_module_binding::<SessionKeyModule>();
let session_key = session_key_module
.get_session_key(sender.into(), &session_auth_key)?
.ok_or_else(|| {
RoochError::ViewFunctionError(format!(
"Failed to get session key via {}",
session_auth_key
))
})?;

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The difference is whether MoveFunctionCaller is called by async/await.

You can open these two lines of comments, cargo build to see the specific error.

// let coin_module = client.as_module_binding::<CoinModule>();
// let coin_store_handle_opt = coin_module.coin_store_handle(addr)?;

/// Struct name as `<ADDRESS>::<MODULE_ID>::<STRUCT_NAME><TypeParam1?, TypeParam2?>`
/// Example: `0x123::counter::Counter`, `0x123::counter::Box<0x123::counter::Counter>`
#[clap(long = "coin_type")]
/// The block number or block hash for get state, if absent, use latest block state_root.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line of code comment is probably a copying error

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line of code comment is probably a copying error

fixed

address: Option<AccountAddressView>,

/// Struct name as `<ADDRESS>::<MODULE_ID>::<STRUCT_NAME><TypeParam1?, TypeParam2?>`
/// Example: `0x123::counter::Counter`, `0x123::counter::Box<0x123::counter::Counter>`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Give a coin struct_tag example

@jolestar
Copy link
Contributor

jolestar commented Sep 6, 2023

rooch account balance -a 0xe1176537c0175d336353dad12f7eb60c658ce526eeb3cd08409e6fd8c2dfa1d7 --coin_type "0x3::coin::CoinStore<0xe1176537c0175d336353dad12f7eb60c658ce526eeb3cd08409e6fd8c2dfa1d7::fixed_supply_coin::FSC>"

The coin-type arg should be 0xe1176537c0175d336353dad12f7eb60c658ce526eeb3cd08409e6fd8c2dfa1d7::fixed_supply_coin::FSC, remove the 0x3::coin::CoinStore?

and

Coin Type                                                |             Balance
------------------------------------------------
0x3::coin::Coin<0xe1176537c0175d336353dad12f7eb60c658ce526eeb3cd08409e6fd8c2dfa1d7::fixed_supply_coin::FSC> |              10000

We can remove the 0x3::coin::Coin from the result Coin Type.


/// Struct name as `<ADDRESS>::<MODULE_ID>::<STRUCT_NAME><TypeParam1?, TypeParam2?>`
/// Example: `0x123::counter::Counter`, `0x123::counter::Box<0x123::counter::Counter>`
#[clap(long = "coin_type")]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just use #[clap(long)]. The CLI option should use - instead of _.

@baichuan3
Copy link
Collaborator Author

rooch account balance -a 0xe1176537c0175d336353dad12f7eb60c658ce526eeb3cd08409e6fd8c2dfa1d7 --coin_type "0x3::coin::CoinStore<0xe1176537c0175d336353dad12f7eb60c658ce526eeb3cd08409e6fd8c2dfa1d7::fixed_supply_coin::FSC>"

The coin-type arg should be 0xe1176537c0175d336353dad12f7eb60c658ce526eeb3cd08409e6fd8c2dfa1d7::fixed_supply_coin::FSC, remove the 0x3::coin::CoinStore?

and

Coin Type                                                |             Balance
------------------------------------------------
0x3::coin::Coin<0xe1176537c0175d336353dad12f7eb60c658ce526eeb3cd08409e6fd8c2dfa1d7::fixed_supply_coin::FSC> |              10000

We can remove the 0x3::coin::Coin from the result Coin Type.

already remove

@@ -256,7 +256,7 @@ impl AnnotatedCoinStoreView {
}
}

pub fn get_coin_type(&self) -> StructTagView {
pub fn get_coin_type_(&self) -> StructTagView {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why need _?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This coin type contains 0x3::coin::Coin<>, corresponding to the type of filed_value, in order to distinguish the cointype in BalanceInfo.

let coin_type_ = filed_value.type_;

Ok(annotated_coin_store)
}

pub fn get_coin_type__(&self) -> StructTag {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there two _

@baichuan3
Copy link
Collaborator Author

I will merge the pr first

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[CLI] Add account balance command show user's coin balance
2 participants