2121import com .google .protobuf .InvalidProtocolBufferException ;
2222import lombok .extern .slf4j .Slf4j ;
2323import org .joda .time .DateTime ;
24+ import org .tron .common .utils .ByteArray ;
2425import org .tron .core .capsule .AccountCapsule ;
2526import org .tron .core .capsule .AssetIssueCapsule ;
2627import org .tron .core .capsule .TransactionResultCapsule ;
@@ -57,13 +58,16 @@ public boolean execute(TransactionResultCapsule ret) throws ContractExeException
5758 AssetIssueCapsule assetIssueCapsule =
5859 this .dbManager .getAssetIssueStore ()
5960 .get (participateAssetIssueContract .getAssetName ().toByteArray ());
60- long exchangeAmount = cost / assetIssueCapsule .getTrxNum () * assetIssueCapsule .getNum ();
61+ long exchangeAmount = cost * assetIssueCapsule .getNum () / assetIssueCapsule .getTrxNum ();
6162 ownerAccount .addAssetAmount (assetIssueCapsule .getName (), exchangeAmount );
6263 //add to to_address
6364 byte [] toAddressBytes = participateAssetIssueContract .getToAddress ().toByteArray ();
6465 AccountCapsule toAccount = this .dbManager .getAccountStore ().get (toAddressBytes );
6566 toAccount .setBalance (toAccount .getBalance () + cost );
66- toAccount .reduceAssetAmount (assetIssueCapsule .getName (), exchangeAmount );
67+ if (!toAccount .reduceAssetAmount (assetIssueCapsule .getName (), exchangeAmount )) {
68+ throw new ContractExeException ("reduceAssetAmount failed !" );
69+ }
70+
6771 //write to db
6872 dbManager .getAccountStore ().put (ownerAddressBytes , ownerAccount );
6973 dbManager .getAccountStore ().put (toAddressBytes , toAccount );
@@ -96,6 +100,11 @@ public boolean validate() throws ContractValidateException {
96100 throw new ContractValidateException ("Trx Num must be positive!" );
97101 }
98102
103+ if (participateAssetIssueContract .getOwnerAddress ()
104+ .equals (participateAssetIssueContract .getToAddress ())) {
105+ throw new ContractValidateException ("Cannot participate asset Issue yourself !" );
106+ }
107+
99108 byte [] addressBytes = participateAssetIssueContract .getOwnerAddress ().toByteArray ();
100109 //Whether the account exist
101110 if (!this .dbManager .getAccountStore ().has (addressBytes )) {
@@ -106,33 +115,43 @@ public boolean validate() throws ContractValidateException {
106115 long fee = calcFee ();
107116 //Whether the balance is enough
108117 if (ac .getBalance () < participateAssetIssueContract .getAmount () + fee ) {
109- throw new ContractValidateException ();
118+ throw new ContractValidateException ("No enough balance !" );
110119 }
111120
112121 //Whether have the mapping
113122 if (!this .dbManager .getAssetIssueStore ()
114123 .has (participateAssetIssueContract .getAssetName ().toByteArray ())) {
115- throw new ContractValidateException ();
124+ throw new ContractValidateException ("No asset named " + ByteArray
125+ .toStr (participateAssetIssueContract .getAssetName ().toByteArray ()));
116126 }
117-
118- //Whether the exchange can be processed: to see if the exchange can be the exact int
119- long cost = participateAssetIssueContract .getAmount ();
120127 AssetIssueCapsule assetIssueCapsule =
121128 this .dbManager .getAssetIssueStore ()
122129 .get (participateAssetIssueContract .getAssetName ().toByteArray ());
130+ if (!participateAssetIssueContract .getToAddress ()
131+ .equals (assetIssueCapsule .getOwnerAddress ())) {
132+ throw new ContractValidateException ("The asset is not issued by " + ByteArray
133+ .toHexString (participateAssetIssueContract .getToAddress ().toByteArray ()));
134+ }
135+ //Whether the exchange can be processed: to see if the exchange can be the exact int
136+ long cost = participateAssetIssueContract .getAmount ();
137+
123138 DateTime now = DateTime .now ();
124139 if (now .getMillis () >= assetIssueCapsule .getEndTime () || now .getMillis () < assetIssueCapsule
125140 .getStartTime ()) {
126141 throw new ContractValidateException ("No longer valid period!" );
127142 }
128143 int trxNum = assetIssueCapsule .getTrxNum ();
129144 int num = assetIssueCapsule .getNum ();
130- long exchangeAmount = cost / trxNum * num ;
131- float preciseExchangeAmount = (float ) cost / (float ) trxNum * (float ) num ;
132- if (preciseExchangeAmount - exchangeAmount >= 0.000001f
133- || preciseExchangeAmount - exchangeAmount <= -0.000001f ) {
145+ long exchangeAmount = cost * num / trxNum ;
146+ if (exchangeAmount == 0 ) {
134147 throw new ContractValidateException ("Can not process the exchange!" );
135148 }
149+ AccountCapsule toAccount = this .dbManager .getAccountStore ()
150+ .get (participateAssetIssueContract .getToAddress ().toByteArray ());
151+ if (!toAccount .assetBalanceEnough (assetIssueCapsule .getName (), exchangeAmount )) {
152+ throw new ContractValidateException ("Asset balance is not enough !" );
153+ }
154+
136155 } catch (InvalidProtocolBufferException e ) {
137156 throw new ContractValidateException ();
138157 }
0 commit comments