21
21
import com .google .protobuf .InvalidProtocolBufferException ;
22
22
import lombok .extern .slf4j .Slf4j ;
23
23
import org .joda .time .DateTime ;
24
+ import org .tron .common .utils .ByteArray ;
24
25
import org .tron .core .capsule .AccountCapsule ;
25
26
import org .tron .core .capsule .AssetIssueCapsule ;
26
27
import org .tron .core .capsule .TransactionResultCapsule ;
@@ -57,13 +58,16 @@ public boolean execute(TransactionResultCapsule ret) throws ContractExeException
57
58
AssetIssueCapsule assetIssueCapsule =
58
59
this .dbManager .getAssetIssueStore ()
59
60
.get (participateAssetIssueContract .getAssetName ().toByteArray ());
60
- long exchangeAmount = cost / assetIssueCapsule .getTrxNum () * assetIssueCapsule .getNum ();
61
+ long exchangeAmount = cost * assetIssueCapsule .getNum () / assetIssueCapsule .getTrxNum ();
61
62
ownerAccount .addAssetAmount (assetIssueCapsule .getName (), exchangeAmount );
62
63
//add to to_address
63
64
byte [] toAddressBytes = participateAssetIssueContract .getToAddress ().toByteArray ();
64
65
AccountCapsule toAccount = this .dbManager .getAccountStore ().get (toAddressBytes );
65
66
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
+
67
71
//write to db
68
72
dbManager .getAccountStore ().put (ownerAddressBytes , ownerAccount );
69
73
dbManager .getAccountStore ().put (toAddressBytes , toAccount );
@@ -96,6 +100,11 @@ public boolean validate() throws ContractValidateException {
96
100
throw new ContractValidateException ("Trx Num must be positive!" );
97
101
}
98
102
103
+ if (participateAssetIssueContract .getOwnerAddress ()
104
+ .equals (participateAssetIssueContract .getToAddress ())) {
105
+ throw new ContractValidateException ("Cannot participate asset Issue yourself !" );
106
+ }
107
+
99
108
byte [] addressBytes = participateAssetIssueContract .getOwnerAddress ().toByteArray ();
100
109
//Whether the account exist
101
110
if (!this .dbManager .getAccountStore ().has (addressBytes )) {
@@ -106,33 +115,43 @@ public boolean validate() throws ContractValidateException {
106
115
long fee = calcFee ();
107
116
//Whether the balance is enough
108
117
if (ac .getBalance () < participateAssetIssueContract .getAmount () + fee ) {
109
- throw new ContractValidateException ();
118
+ throw new ContractValidateException ("No enough balance !" );
110
119
}
111
120
112
121
//Whether have the mapping
113
122
if (!this .dbManager .getAssetIssueStore ()
114
123
.has (participateAssetIssueContract .getAssetName ().toByteArray ())) {
115
- throw new ContractValidateException ();
124
+ throw new ContractValidateException ("No asset named " + ByteArray
125
+ .toStr (participateAssetIssueContract .getAssetName ().toByteArray ()));
116
126
}
117
-
118
- //Whether the exchange can be processed: to see if the exchange can be the exact int
119
- long cost = participateAssetIssueContract .getAmount ();
120
127
AssetIssueCapsule assetIssueCapsule =
121
128
this .dbManager .getAssetIssueStore ()
122
129
.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
+
123
138
DateTime now = DateTime .now ();
124
139
if (now .getMillis () >= assetIssueCapsule .getEndTime () || now .getMillis () < assetIssueCapsule
125
140
.getStartTime ()) {
126
141
throw new ContractValidateException ("No longer valid period!" );
127
142
}
128
143
int trxNum = assetIssueCapsule .getTrxNum ();
129
144
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 ) {
134
147
throw new ContractValidateException ("Can not process the exchange!" );
135
148
}
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
+
136
155
} catch (InvalidProtocolBufferException e ) {
137
156
throw new ContractValidateException ();
138
157
}
0 commit comments