@@ -22,8 +22,6 @@ use std::{
2222 sync:: Arc ,
2323} ;
2424
25- use itertools:: Itertools as _;
26-
2725use common:: {
2826 address:: Address ,
2927 chain:: {
@@ -44,6 +42,9 @@ use crate::storage::storage_api::{
4442
4543use super :: CURRENT_STORAGE_VERSION ;
4644
45+ use itertools:: Itertools as _;
46+ use num_bigint:: BigUint ;
47+
4748#[ derive( Debug , Clone , PartialEq , Eq ) ]
4849struct TokenTransactionOrderedByTxId ( TokenTransaction ) ;
4950
@@ -63,8 +64,9 @@ impl Ord for TokenTransactionOrderedByTxId {
6364struct ApiServerInMemoryStorage {
6465 block_table : BTreeMap < Id < Block > , BlockWithExtraData > ,
6566 block_aux_data_table : BTreeMap < Id < Block > , BlockAuxData > ,
66- address_balance_table : BTreeMap < String , BTreeMap < CoinOrTokenId , BTreeMap < BlockHeight , Amount > > > ,
67- address_locked_balance_table : BTreeMap < String , BTreeMap < ( CoinOrTokenId , BlockHeight ) , Amount > > ,
67+ address_balance_table :
68+ BTreeMap < String , BTreeMap < CoinOrTokenId , BTreeMap < BlockHeight , BigUint > > > ,
69+ address_locked_balance_table : BTreeMap < String , BTreeMap < ( CoinOrTokenId , BlockHeight ) , BigUint > > ,
6870 address_transactions_table : BTreeMap < String , BTreeMap < BlockHeight , Vec < Id < Transaction > > > > ,
6971 token_transactions_table :
7072 BTreeMap < TokenId , BTreeMap < BlockHeight , BTreeSet < TokenTransactionOrderedByTxId > > > ,
@@ -123,13 +125,13 @@ impl ApiServerInMemoryStorage {
123125 & self ,
124126 address : & str ,
125127 coin_or_token_id : CoinOrTokenId ,
126- ) -> Result < Option < Amount > , ApiServerStorageError > {
128+ ) -> Result < Option < BigUint > , ApiServerStorageError > {
127129 self . address_balance_table
128130 . get ( address)
129131 . and_then ( |by_coin_or_token| by_coin_or_token. get ( & coin_or_token_id) )
130132 . map_or_else (
131133 || Ok ( None ) ,
132- |by_height| Ok ( by_height. values ( ) . last ( ) . copied ( ) ) ,
134+ |by_height| Ok ( by_height. values ( ) . last ( ) . cloned ( ) ) ,
133135 )
134136 }
135137
@@ -155,7 +157,7 @@ impl ApiServerInMemoryStorage {
155157 (
156158 * coin_or_token_id,
157159 AmountWithDecimals {
158- amount : * by_height. values ( ) . last ( ) . expect ( "not empty" ) ,
160+ amount : by_height. values ( ) . last ( ) . expect ( "not empty" ) . clone ( ) ,
159161 decimals : number_of_decimals,
160162 } ,
161163 )
@@ -170,14 +172,14 @@ impl ApiServerInMemoryStorage {
170172 & self ,
171173 address : & str ,
172174 coin_or_token_id : CoinOrTokenId ,
173- ) -> Result < Option < Amount > , ApiServerStorageError > {
175+ ) -> Result < Option < BigUint > , ApiServerStorageError > {
174176 self . address_locked_balance_table . get ( address) . map_or_else (
175177 || Ok ( None ) ,
176178 |balance| {
177179 let range_begin = ( coin_or_token_id, BlockHeight :: zero ( ) ) ;
178180 let range_end = ( coin_or_token_id, BlockHeight :: max ( ) ) ;
179181 let range = balance. range ( range_begin..=range_end) ;
180- Ok ( range. last ( ) . map ( |( _, v) | * v ) )
182+ Ok ( range. last ( ) . map ( |( _, v) | v . clone ( ) ) )
181183 } ,
182184 )
183185 }
@@ -927,7 +929,7 @@ impl ApiServerInMemoryStorage {
927929 fn set_address_balance_at_height (
928930 & mut self ,
929931 address : & Address < Destination > ,
930- amount : Amount ,
932+ amount : BigUint ,
931933 coin_or_token_id : CoinOrTokenId ,
932934 block_height : BlockHeight ,
933935 ) -> Result < ( ) , ApiServerStorageError > {
@@ -937,8 +939,8 @@ impl ApiServerInMemoryStorage {
937939 . entry ( coin_or_token_id)
938940 . or_default ( )
939941 . entry ( block_height)
940- . and_modify ( |e| * e = amount)
941- . or_insert ( amount) ;
942+ . and_modify ( |e| * e = amount. clone ( ) )
943+ . or_insert ( amount. clone ( ) ) ;
942944
943945 self . update_nft_owner ( coin_or_token_id, amount, address, block_height) ;
944946
@@ -950,7 +952,7 @@ impl ApiServerInMemoryStorage {
950952 fn update_nft_owner (
951953 & mut self ,
952954 coin_or_token_id : CoinOrTokenId ,
953- amount : Amount ,
955+ amount : BigUint ,
954956 address : & Address < Destination > ,
955957 block_height : BlockHeight ,
956958 ) {
@@ -960,7 +962,7 @@ impl ApiServerInMemoryStorage {
960962
961963 if let Some ( by_height) = self . nft_token_issuances . get_mut ( & token_id) {
962964 let last = by_height. values ( ) . last ( ) . expect ( "not empty" ) ;
963- let owner = ( amount > Amount :: ZERO ) . then_some ( address. as_object ( ) . clone ( ) ) ;
965+ let owner = ( amount > BigUint :: ZERO ) . then_some ( address. as_object ( ) . clone ( ) ) ;
964966 let new = NftWithOwner {
965967 nft : last. nft . clone ( ) ,
966968 owner,
@@ -972,16 +974,16 @@ impl ApiServerInMemoryStorage {
972974 fn set_address_locked_balance_at_height (
973975 & mut self ,
974976 address : & Address < Destination > ,
975- amount : Amount ,
977+ amount : BigUint ,
976978 coin_or_token_id : CoinOrTokenId ,
977979 block_height : BlockHeight ,
978980 ) -> Result < ( ) , ApiServerStorageError > {
979981 self . address_locked_balance_table
980982 . entry ( address. to_string ( ) )
981983 . or_default ( )
982984 . entry ( ( coin_or_token_id, block_height) )
983- . and_modify ( |e| * e = amount)
984- . or_insert ( amount) ;
985+ . and_modify ( |e| * e = amount. clone ( ) )
986+ . or_insert ( amount. clone ( ) ) ;
985987
986988 self . update_nft_owner ( coin_or_token_id, amount, address, block_height) ;
987989
0 commit comments