Skip to content

Commit fe4ecd4

Browse files
SeungMin Leesgkim126
authored andcommitted
Implement the trait AccountView
1 parent fbf6525 commit fe4ecd4

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed

basic_module/account/src/core.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,11 @@ pub trait AccountManager {
3737

3838
fn increment_sequence(&self, account_id: &Public);
3939
}
40+
41+
pub trait AccountView {
42+
fn is_active(&self, account_id: &Public) -> bool;
43+
44+
fn get_balance(&self, account_id: &Public) -> u64;
45+
46+
fn get_sequence(&self, account_id: &Public) -> u64;
47+
}

basic_module/account/src/impls.rs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414
// You should have received a copy of the GNU Affero General Public License
1515
// along with this program. If not, see <https://www.gnu.org/licenses/>.
1616

17-
use crate::core::{AccountManager, CheckTxHandler, TransactionExecutor};
17+
use crate::core::{AccountManager, AccountView, CheckTxHandler, TransactionExecutor};
1818
use crate::error::Error;
19-
use crate::internal::{add_balance, get_account, get_sequence, sub_balance};
19+
use crate::internal::{add_balance, get_account, get_balance, get_sequence, sub_balance};
2020
use crate::types::{Action, SignedTransaction};
2121
use crate::{check, get_context};
2222
use ckey::Ed25519Public as Public;
@@ -92,3 +92,20 @@ impl AccountManager for Manager {
9292
context.set(account_id, account.to_vec());
9393
}
9494
}
95+
96+
#[allow(dead_code)]
97+
pub struct View {}
98+
99+
impl AccountView for View {
100+
fn is_active(&self, account_id: &Public) -> bool {
101+
get_balance(account_id) != 0 || get_sequence(account_id) != 0
102+
}
103+
104+
fn get_balance(&self, account_id: &Public) -> u64 {
105+
get_balance(account_id)
106+
}
107+
108+
fn get_sequence(&self, account_id: &Public) -> u64 {
109+
get_sequence(account_id)
110+
}
111+
}

basic_module/account/src/internal.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ pub fn get_sequence(account_id: &Public) -> u64 {
4848
get_account(account_id).sequence
4949
}
5050

51+
pub fn get_balance(account_id: &Public) -> u64 {
52+
get_account(account_id).balance
53+
}
54+
5155
pub fn get_account(account_id: &Public) -> Account {
5256
get_context().get(account_id).map(|account| account.into()).unwrap_or_default()
5357
}

0 commit comments

Comments
 (0)