@@ -112,7 +112,7 @@ impl IsPlutusData for CurrencySymbol {
112112 fn to_plutus_data ( & self ) -> PlutusData {
113113 match self {
114114 CurrencySymbol :: NativeToken ( policy_hash) => policy_hash. to_plutus_data ( ) ,
115- CurrencySymbol :: Ada => PlutusData :: Bytes ( Vec :: with_capacity ( 0 ) ) ,
115+ CurrencySymbol :: Ada => PlutusData :: Bytes ( Vec :: new ( ) ) ,
116116 }
117117 }
118118
@@ -262,23 +262,51 @@ impl Value {
262262 self . get_token_amount ( & CurrencySymbol :: Ada , & TokenName :: ada ( ) )
263263 }
264264
265- /// Insert a new token into the value, or replace the existing quantity.
266- pub fn insert_token ( & self , cs : & CurrencySymbol , tn : & TokenName , a : & BigInt ) -> Self {
267- let mut result_map = self . 0 . clone ( ) ;
265+ /// Insert ada into a value by inserting or replacing old value
266+ pub fn insert_ada_mut ( & mut self , amount : BigInt ) {
267+ self . 0 . insert (
268+ CurrencySymbol :: Ada ,
269+ BTreeMap :: from ( [ ( TokenName :: ada ( ) , amount) ] ) ,
270+ ) ;
271+ }
268272
269- result_map
270- . entry ( cs. clone ( ) )
271- . and_modify ( |tn_map| {
272- tn_map
273- . entry ( tn. clone ( ) )
274- . and_modify ( |old_a| {
275- old_a. clone_from ( a) ;
276- } )
277- . or_insert_with ( || a. clone ( ) ) ;
278- } )
279- . or_insert_with ( || singleton ( ( tn. clone ( ) , a. clone ( ) ) ) ) ;
273+ /// Create a new value by inserting a new token or replacing the existing quantity.
274+ pub fn insert_token (
275+ & self ,
276+ currency_symbol : & CurrencySymbol ,
277+ token_name : & TokenName ,
278+ amount : & BigInt ,
279+ ) -> Self {
280+ let mut result_map = self . clone ( ) ;
280281
281- Self ( result_map)
282+ result_map. insert_token_mut ( currency_symbol. clone ( ) , token_name. clone ( ) , amount. clone ( ) ) ;
283+
284+ result_map
285+ }
286+ /// Insert a new token into the value, or replace the existing quantity.
287+ pub fn insert_token_mut (
288+ & mut self ,
289+ currency_symbol : CurrencySymbol ,
290+ token_name : TokenName ,
291+ amount : BigInt ,
292+ ) {
293+ let tn_map = self . 0 . get_mut ( & currency_symbol) ;
294+
295+ match tn_map {
296+ None => {
297+ self . 0
298+ . insert ( currency_symbol, singleton ( ( token_name, amount) ) ) ;
299+ }
300+ Some ( tn_map) => {
301+ let val = tn_map. get_mut ( & token_name) ;
302+ match val {
303+ None => {
304+ tn_map. insert ( token_name, amount) ;
305+ }
306+ Some ( old_amount) => * old_amount = amount,
307+ }
308+ }
309+ }
282310 }
283311
284312 /// Return true if the value don't have any entries.
@@ -698,7 +726,7 @@ pub struct TokenName(pub LedgerBytes);
698726impl TokenName {
699727 /// Ada tokenname (empty bytestring)
700728 pub fn ada ( ) -> TokenName {
701- TokenName ( LedgerBytes ( Vec :: with_capacity ( 0 ) ) )
729+ TokenName ( LedgerBytes ( Vec :: new ( ) ) )
702730 }
703731
704732 pub fn is_empty ( & self ) -> bool {
0 commit comments