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

Refactor account balance for support balance API and add some test case #774

Merged
merged 5 commits into from
Sep 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ ethereum-types = "0.14.1"
ethers = { version = "2.0.7", features = ["legacy"] }
eyre = "0.6.8"
fastcrypto = { git = "https://github.com/rooch-network/fastcrypto", rev = "aa5f9f308b6598779820db8b673050c10cfcc3c1" }
futures = "0.3"
futures = "0.3.28"
hex = "0.4.3"
rustc-hex = "1.0"
rust-embed = "6.8.1"
Expand Down Expand Up @@ -165,7 +165,7 @@ smallvec = "1.6.1"
thiserror = "1.0.34"
tiny-keccak = { version = "2", features = ["keccak", "sha3"] }
tiny-bip39 = "1.0.0"
tokio = { version = "1", features = ["full"] }
tokio = { version = "1.28.1", features = ["full"] }
tonic = { version = "0.8", features = ["gzip"] }
tracing = "0.1"
tracing-subscriber = "0.3"
Expand Down Expand Up @@ -202,6 +202,7 @@ bitcoin-bech32 = "0.13.0"
bs58 = "0.5.0"
dirs-next = "2.0.0"
anstream = { version = "0.3" }
bigdecimal = { version = "0.3.0", features = ["serde"] }

# Note: the BEGIN and END comments below are required for external tooling. Do not remove.
# BEGIN MOVE DEPENDENCIES
Expand Down
28 changes: 28 additions & 0 deletions crates/rooch-framework/doc/coin.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ This module provides the foundation for typesafe Coins.
- [Function `supply`](#0x3_coin_supply)
- [Function `is_same_coin`](#0x3_coin_is_same_coin)
- [Function `coin_store_handle`](#0x3_coin_coin_store_handle)
- [Function `coin_info_handle`](#0x3_coin_coin_info_handle)
- [Function `is_account_accept_coin`](#0x3_coin_is_account_accept_coin)
- [Function `can_auto_accept_coin`](#0x3_coin_can_auto_accept_coin)
- [Function `do_accept_coin`](#0x3_coin_do_accept_coin)
Expand Down Expand Up @@ -853,6 +854,33 @@ Return coin store handle for addr



</details>

<a name="0x3_coin_coin_info_handle"></a>

## Function `coin_info_handle`

Return coin info handle


<pre><code><b>public</b> <b>fun</b> <a href="coin.md#0x3_coin_coin_info_handle">coin_info_handle</a>(ctx: &<a href="_StorageContext">storage_context::StorageContext</a>): <a href="_ObjectID">object_id::ObjectID</a>
</code></pre>



<details>
<summary>Implementation</summary>


<pre><code><b>public</b> <b>fun</b> <a href="coin.md#0x3_coin_coin_info_handle">coin_info_handle</a>(ctx: &StorageContext): ObjectID {
// <a href="coin.md#0x3_coin">coin</a> info ensured via the Genesis transaction, so it should always exist
<b>let</b> coin_infos = <a href="_global_borrow">account_storage::global_borrow</a>&lt;<a href="coin.md#0x3_coin_CoinInfos">CoinInfos</a>&gt;(ctx, @rooch_framework);
*<a href="_handle">type_table::handle</a>(&coin_infos.coin_infos)
}
</code></pre>



</details>

<a name="0x3_coin_is_account_accept_coin"></a>
Expand Down
7 changes: 7 additions & 0 deletions crates/rooch-framework/sources/coin.move
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,13 @@ module rooch_framework::coin {
}
}

/// Return coin info handle
public fun coin_info_handle(ctx: &StorageContext): ObjectID {
// coin info ensured via the Genesis transaction, so it should always exist
let coin_infos = account_storage::global_borrow<CoinInfos>(ctx, @rooch_framework);
*type_table::handle(&coin_infos.coin_infos)
}

//
// Helper functions
//
Expand Down
101 changes: 101 additions & 0 deletions crates/rooch-open-rpc-spec/schemas/openrpc.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,46 @@
}
}
},
{
"name": "rooch_getBalances",
"description": "get account balances by AccountAddress",
"params": [
{
"name": "account_addr",
"required": true,
"schema": {
"$ref": "#/components/schemas/move_core_types::account_address::AccountAddress"
}
},
{
"name": "coin_type",
"schema": {
"$ref": "#/components/schemas/move_core_types::language_storage::StructTag"
}
},
{
"name": "cursor",
"schema": {
"$ref": "#/components/schemas/alloc::vec::Vec<u8>"
}
},
{
"name": "limit",
"schema": {
"type": "integer",
"format": "uint",
"minimum": 0.0
}
}
],
"result": {
"name": "ListBalanceInfoPageView",
"required": true,
"schema": {
"$ref": "#/components/schemas/PageView_for_Nullable_BalanceInfoView_and_alloc::vec::Vec<u8>"
}
}
},
{
"name": "rooch_getEvents",
"description": "Get the events by event filter",
Expand Down Expand Up @@ -563,6 +603,31 @@
}
}
},
"BalanceInfoView": {
"type": "object",
"required": [
"balance",
"coin_type",
"decimals",
"symbol"
],
"properties": {
"balance": {
"$ref": "#/components/schemas/move_core_types::u256::U256"
},
"coin_type": {
"$ref": "#/components/schemas/move_core_types::language_storage::StructTag"
},
"decimals": {
"type": "integer",
"format": "uint8",
"minimum": 0.0
},
"symbol": {
"type": "string"
}
}
},
"EventFilterView": {
"oneOf": [
{
Expand Down Expand Up @@ -1156,6 +1221,42 @@
}
}
},
"PageView_for_Nullable_BalanceInfoView_and_alloc::vec::Vec<u8>": {
"description": "`next_cursor` points to the last item in the page; Reading with `next_cursor` will start from the next item after `next_cursor` if `next_cursor` is `Some`, otherwise it will start from the first item.",
"type": "object",
"required": [
"data",
"has_next_page"
],
"properties": {
"data": {
"type": "array",
"items": {
"anyOf": [
{
"$ref": "#/components/schemas/BalanceInfoView"
},
{
"type": "null"
}
]
}
},
"has_next_page": {
"type": "boolean"
},
"next_cursor": {
"anyOf": [
{
"$ref": "#/components/schemas/alloc::vec::Vec<u8>"
},
{
"type": "null"
}
]
}
}
},
"PageView_for_Nullable_StateView_and_alloc::vec::Vec<u8>": {
"description": "`next_cursor` points to the last item in the page; Reading with `next_cursor` will start from the next item after `next_cursor` if `next_cursor` is `Some`, otherwise it will start from the first item.",
"type": "object",
Expand Down
1 change: 1 addition & 0 deletions crates/rooch-rpc-api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ schemars = { workspace = true }
serde_with = { workspace = true }
rand = { workspace = true }
fastcrypto = { workspace = true }
bigdecimal = { workspace = true }

move-core-types = { workspace = true }
move-resource-viewer = { workspace = true }
Expand Down
5 changes: 4 additions & 1 deletion crates/rooch-rpc-api/src/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ pub mod eth_api;
pub mod rooch_api;
pub mod wallet_api;

pub const MAX_RESULT_LIMIT: u64 = 50;
pub const DEFAULT_RESULT_LIMIT: u64 = 50;
pub const DEFAULT_RESULT_LIMIT_USIZE: usize = DEFAULT_RESULT_LIMIT as usize;

pub const MAX_RESULT_LIMIT: u64 = 200;
pub const MAX_RESULT_LIMIT_USIZE: usize = MAX_RESULT_LIMIT as usize;

// pub fn validate_limit(limit: Option<usize>, max: usize) -> Result<usize, anyhow::Error> {
Expand Down
19 changes: 15 additions & 4 deletions crates/rooch-rpc-api/src/api/rooch_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
// SPDX-License-Identifier: Apache-2.0

use crate::jsonrpc_types::{
AccessPathView, AnnotatedEventView, AnnotatedFunctionResultView, AnnotatedStateView,
EventFilterView, EventPageView, ExecuteTransactionResponseView, FunctionCallView, H256View,
ListAnnotatedStatesPageView, ListStatesPageView, StateView, StrView, StructTagView,
TransactionExecutionInfoView, TransactionInfoPageView, TransactionView,
AccessPathView, AccountAddressView, AnnotatedEventView, AnnotatedFunctionResultView,
AnnotatedStateView, EventFilterView, EventPageView, ExecuteTransactionResponseView,
FunctionCallView, H256View, ListAnnotatedStatesPageView, ListBalanceInfoPageView,
ListStatesPageView, StateView, StrView, StructTagView, TransactionExecutionInfoView,
TransactionInfoPageView, TransactionView,
};
use jsonrpsee::core::RpcResult;
use jsonrpsee::proc_macros::rpc;
Expand Down Expand Up @@ -104,4 +105,14 @@ pub trait RoochAPI {
&self,
tx_hashes: Vec<H256View>,
) -> RpcResult<Vec<Option<TransactionExecutionInfoView>>>;

/// get account balances by AccountAddress
#[method(name = "getBalances")]
async fn get_balances(
&self,
account_addr: AccountAddressView,
coin_type: Option<StructTagView>,
cursor: Option<StrView<Vec<u8>>>,
limit: Option<usize>,
) -> RpcResult<ListBalanceInfoPageView>;
}
28 changes: 22 additions & 6 deletions crates/rooch-rpc-api/src/jsonrpc_types/account_view.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
// Copyright (c) RoochNetwork
// SPDX-License-Identifier: Apache-2.0

use crate::jsonrpc_types::StructTagView;
use crate::jsonrpc_types::{StrView, StructTagView};
use move_core_types::u256::U256;
use rooch_types::account::{AccountInfo, BalanceInfo};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use std::ops::Div;

#[derive(Debug, Clone, Serialize, Deserialize)]
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
pub struct AccountInfoView {
pub sequence_number: u64,
pub balances: Vec<Option<BalanceInfoView>>,
Expand Down Expand Up @@ -50,17 +52,29 @@ impl From<AccountInfoView> for AccountInfo {
}
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
pub struct BalanceInfoView {
pub coin_type: StructTagView,
pub balance: U256,
pub symbol: String,
pub balance: StrView<U256>,
pub decimals: u8,
}

impl BalanceInfoView {
//TODO implements big decimal calculation for Decimal point display
pub fn get_balance_show(&self) -> String {
let balance = U256::div(self.balance.0, U256::from(10u32.pow(self.decimals as u32)));
balance.to_string()
}
}

impl From<BalanceInfo> for BalanceInfoView {
fn from(balance_info: BalanceInfo) -> Self {
BalanceInfoView {
coin_type: balance_info.coin_type.into(),
balance: balance_info.balance,
symbol: balance_info.symbol,
balance: balance_info.balance.into(),
decimals: balance_info.decimals,
}
}
}
Expand All @@ -69,7 +83,9 @@ impl From<BalanceInfoView> for BalanceInfo {
fn from(balance_info: BalanceInfoView) -> Self {
BalanceInfo {
coin_type: balance_info.coin_type.into(),
balance: balance_info.balance,
symbol: balance_info.symbol,
balance: balance_info.balance.into(),
decimals: balance_info.decimals,
}
}
}
Loading
Loading