Skip to content

Commit

Permalink
Update aptos account list command to support fetching modules
Browse files Browse the repository at this point in the history
  • Loading branch information
movekevin authored and aptos-bot committed May 10, 2022
1 parent 5d91e05 commit b26c315
Show file tree
Hide file tree
Showing 3 changed files with 141 additions and 20 deletions.
87 changes: 85 additions & 2 deletions crates/aptos/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ Aptos is now set up for account 18B61497FD290B02BB0751F44381CADA1657C2B3AA6194A0
You can list the resources in an account from the command line. For example, see below for how to list the resources in the account you just created above:
```bash
$ aptos account list --account 18B61497FD290B02BB0751F44381CADA1657C2B3AA6194A00D9BC9A85FAD3B04
$ aptos account list --query resources --account 18B61497FD290B02BB0751F44381CADA1657C2B3AA6194A00D9BC9A85FAD3B04
```
Expand Down Expand Up @@ -281,7 +281,7 @@ $ aptos account list
Additionally, any place that takes an account can use the name of a profile:
```bash
$ ./aptos account list --account superuser
$ ./aptos account list --query resources --account superuser
{
"Result": [
{
Expand Down Expand Up @@ -327,6 +327,89 @@ $ ./aptos account list --account superuser
}
```
### Listing modules in an account
You can pass different types of queries to view different items under an account. Currently, 'resources' and
'modules' are supported but more query types are coming. For example, to fetch modules:
```bash
$ ./aptos account list --query modules --account superuser
{
"Result": [
{
"bytecode": "0xa11ceb0b050000000b01000a020a12031c2504410405452d0772e00108d202400692030a0a9c03150cb103650d96040400000101010201030104000506000006080001070700030e0401060100080001000009020300020f0404000110060100041107000003120709010603130a030106050806080105010802020c0a02000103040508020802070801010a0201060c010800010b0301090002070b030109000900074d657373616765054153434949064572726f7273054576656e74065369676e6572124d6573736167654368616e67654576656e740d4d657373616765486f6c64657206537472696e670b6765745f6d6573736167650b7365745f6d6573736167650c66726f6d5f6d6573736167650a746f5f6d657373616765076d657373616765156d6573736167655f6368616e67655f6576656e74730b4576656e7448616e646c650d6e6f745f7075626c697368656406737472696e670a616464726573735f6f66106e65775f6576656e745f68616e646c650a656d69745f6576656e747bd2d264eec4088a11c41a7acbcd8ab2d2c887fa4ea1a3ab0d0b4a405ddfb1560000000000000000000000000000000000000000000000000000000000000001030800000000000000000002020a08020b08020102020c08020d0b030108000001000101030b0a002901030607001102270b002b0110001402010200010105240b0111030c040e0011040c020a02290120030b05120e000b040e00380012012d0105230b022a010c050a051000140c030a050f010b030a04120038010b040b050f0015020100010100",
"abi": {
"address": "0x7bd2d264eec4088a11c41a7acbcd8ab2d2c887fa4ea1a3ab0d0b4a405ddfb156",
"name": "Message",
"friends": [],
"exposed_functions": [
{
"name": "get_message",
"visibility": "public",
"generic_type_params": [],
"params": [
"address"
],
"return": [
"0x1::ASCII::String"
]
},
{
"name": "set_message",
"visibility": "script",
"generic_type_params": [],
"params": [
"signer",
"vector"
],
"return": []
}
],
"structs": [
{
"name": "MessageChangeEvent",
"is_native": false,
"abilities": [
"drop",
"store"
],
"generic_type_params": [],
"fields": [
{
"name": "from_message",
"type": "0x1::ASCII::String"
},
{
"name": "to_message",
"type": "0x1::ASCII::String"
}
]
},
{
"name": "MessageHolder",
"is_native": false,
"abilities": [
"key"
],
"generic_type_params": [],
"fields": [
{
"name": "message",
"type": "0x1::ASCII::String"
},
{
"name": "message_change_events",
"type": "0x1::Event::EventHandle<0x7bd2d264eec4088a11c41a7acbcd8ab2d2c887fa4ea1a3ab0d0b4a405ddfb156::Message::MessageChangeEvent>"
}
]
}
]
}
}
]
}
```
### Transferring coins
The Aptos CLI is a simple wallet as well, and can transfer coins between accounts.
Expand Down
72 changes: 55 additions & 17 deletions crates/aptos/src/account/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,58 @@
use crate::common::types::{
CliCommand, CliConfig, CliError, CliTypedResult, ProfileOptions, RestOptions,
};
use aptos_rest_client::{types::Resource, Client};
use aptos_rest_client::Client;
use aptos_types::account_address::AccountAddress;
use async_trait::async_trait;
use clap::Parser;
use clap::{ArgEnum, Parser};
use serde_json::json;
use std::str::FromStr;

/// Command to list resources owned by an address
#[derive(ArgEnum, Clone, Copy, Debug)]
enum ListQuery {
Modules,
Resources,
}

impl FromStr for ListQuery {
type Err = &'static str;

fn from_str(s: &str) -> Result<Self, Self::Err> {
match s.to_lowercase().as_str() {
"modules" => Ok(ListQuery::Modules),
"resources" => Ok(ListQuery::Resources),
_ => Err("Invalid query. Valid values are modules, resources"),
}
}
}

/// Command to list items owned by an address
///
#[derive(Debug, Parser)]
pub struct ListResources {
pub struct ListAccount {
#[clap(flatten)]
rest_options: RestOptions,

#[clap(flatten)]
profile_options: ProfileOptions,

/// Address of account you want to list resources for
/// Address of account you want to list resources/modules for
#[clap(long, parse(try_from_str=crate::common::types::load_account_arg))]
account: Option<AccountAddress>,

/// Type of items to list: resources, modules. (Defaults to 'resources').
/// TODO: add options like --tokens --nfts etc
#[clap(long, default_value = "resources")]
query: ListQuery,
}

#[async_trait]
impl CliCommand<Vec<serde_json::Value>> for ListResources {
impl CliCommand<Vec<serde_json::Value>> for ListAccount {
fn command_name(&self) -> &'static str {
"ListResources"
"ListAccount"
}

// TODO: Format this in a reasonable way while providing all information
// add options like --tokens --nfts etc
async fn execute(self) -> CliTypedResult<Vec<serde_json::Value>> {
let account = if let Some(account) = self.account {
account
Expand All @@ -46,14 +70,28 @@ impl CliCommand<Vec<serde_json::Value>> for ListResources {
};

let client = Client::new(self.rest_options.url(&self.profile_options.profile)?);
let response: Vec<Resource> = client
.get_account_resources(account)
.await
.map_err(|err| CliError::ApiError(err.to_string()))?
.into_inner();
Ok(response
.iter()
.map(|json| json.data.clone())
.collect::<Vec<serde_json::Value>>())
let map_err_func = |err: anyhow::Error| CliError::ApiError(err.to_string());
let response = match self.query {
ListQuery::Modules => client
.get_account_modules(account)
.await
.map_err(map_err_func)?
.into_inner()
.iter()
.cloned()
.map(|module| module.try_parse_abi().unwrap())
.map(|module| json!(module))
.collect::<Vec<serde_json::Value>>(),
ListQuery::Resources => client
.get_account_resources(account)
.await
.map_err(map_err_func)?
.into_inner()
.iter()
.map(|json| json.data.clone())
.collect::<Vec<serde_json::Value>>(),
};

Ok(response)
}
}
2 changes: 1 addition & 1 deletion crates/aptos/src/account/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub mod transfer;
#[derive(Debug, Subcommand)]
pub enum AccountTool {
Create(create::CreateAccount),
List(list::ListResources),
List(list::ListAccount),
Transfer(transfer::TransferCoins),
}

Expand Down

0 comments on commit b26c315

Please sign in to comment.