2323import java .util .Map ;
2424import lombok .extern .slf4j .Slf4j ;
2525import org .tron .common .utils .ByteArray ;
26+ import org .tron .core .db .Manager ;
2627import org .tron .protos .Contract .AccountCreateContract ;
2728import org .tron .protos .Contract .AccountUpdateContract ;
2829import org .tron .protos .Protocol .Account ;
@@ -350,6 +351,40 @@ public boolean reduceAssetAmount(byte[] key, long amount) {
350351 return false ;
351352 }
352353
354+ /**
355+ * reduce asset amount.
356+ */
357+ public boolean reduceAssetAmountV2 (byte [] key , long amount , Manager manager ) {
358+ if (manager .getDynamicPropertiesStore ().getAllowSameTokenName () == 0 ) {
359+ byte [] tokenName = key ;
360+ Map <String , Long > assetMap = this .account .getAssetMap ();
361+ AssetIssueCapsule assetIssueCapsule = manager .getAssetIssueStore ().get (tokenName );
362+ long tokenID = assetIssueCapsule .getId ();
363+ String nameKey = ByteArray .toStr (tokenName );
364+ Long currentAmount = assetMap .get (nameKey );
365+ if (amount > 0 && null != currentAmount && amount <= currentAmount ) {
366+ this .account = this .account .toBuilder ()
367+ .putAsset (nameKey , Math .subtractExact (currentAmount , amount ))
368+ .putAssetV2 (tokenID + "" , Math .subtractExact (currentAmount , amount ))
369+ .build ();
370+ return true ;
371+ }
372+ }
373+ if (manager .getDynamicPropertiesStore ().getAllowSameTokenName () == 1 ) {
374+ byte [] tokenID = key ;
375+ Map <String , Long > assetMapV2 = this .account .getAssetV2Map ();
376+ Long currentAmount = assetMapV2 .get (ByteArray .toLong (tokenID ) + "" );
377+ if (amount > 0 && null != currentAmount && amount <= currentAmount ) {
378+ this .account = this .account .toBuilder ()
379+ .putAssetV2 (tokenID + "" , Math .subtractExact (currentAmount , amount ))
380+ .build ();
381+ return true ;
382+ }
383+ }
384+
385+ return false ;
386+ }
387+
353388 /**
354389 * add asset amount.
355390 */
@@ -365,6 +400,39 @@ public boolean addAssetAmount(byte[] key, long amount) {
365400 return true ;
366401 }
367402
403+ /**
404+ * add asset amount.
405+ */
406+ public boolean addAssetAmountV2 (byte [] key , long amount , Manager manager ) {
407+ if (manager .getDynamicPropertiesStore ().getAllowSameTokenName () == 0 ) {
408+ byte [] tokenName = key ;
409+ Map <String , Long > assetMap = this .account .getAssetMap ();
410+ AssetIssueCapsule assetIssueCapsule = manager .getAssetIssueStore ().get (tokenName );
411+ long tokenID = assetIssueCapsule .getId ();
412+ String nameKey = ByteArray .toStr (tokenName );
413+ Long currentAmount = assetMap .get (nameKey );
414+ if (currentAmount == null ) {
415+ currentAmount = 0L ;
416+ }
417+ this .account = this .account .toBuilder ()
418+ .putAsset (nameKey , Math .addExact (currentAmount , amount ))
419+ .putAssetV2 (tokenID + "" , Math .addExact (currentAmount , amount ))
420+ .build ();
421+ }
422+ if (manager .getDynamicPropertiesStore ().getAllowSameTokenName () == 1 ) {
423+ byte [] tokenID = key ;
424+ Map <String , Long > assetMapV2 = this .account .getAssetV2Map ();
425+ Long currentAmount = assetMapV2 .get (ByteArray .toLong (tokenID ) + "" );
426+ if (currentAmount == null ) {
427+ currentAmount = 0L ;
428+ }
429+ this .account = this .account .toBuilder ()
430+ .putAssetV2 (ByteArray .toLong (tokenID ) + "" , Math .addExact (currentAmount , amount ))
431+ .build ();
432+ }
433+ return true ;
434+ }
435+
368436 /**
369437 * set account name
370438 */
0 commit comments