2424import org .tron .core .store .DynamicPropertiesStore ;
2525import org .tron .core .store .ExchangeStore ;
2626import org .tron .core .store .ExchangeV2Store ;
27- import org .tron .core .utils .TransactionUtil ;
2827import org .tron .protos .Protocol .Transaction .Contract .ContractType ;
2928import org .tron .protos .Protocol .Transaction .Result .code ;
3029import org .tron .protos .contract .ExchangeContract .ExchangeInjectContract ;
3130
3231@ Slf4j (topic = "actuator" )
33- public class ExchangeInjectActuator extends AbstractActuator {
32+ public class ExchangeInjectActuator extends AbstractExchangeActuator {
3433
3534 public ExchangeInjectActuator () {
3635 super (ContractType .ExchangeInjectContract , ExchangeInjectContract .class );
@@ -56,8 +55,8 @@ public boolean execute(Object object) throws ContractExeException {
5655 .get (exchangeInjectContract .getOwnerAddress ().toByteArray ());
5756
5857 ExchangeCapsule exchangeCapsule ;
59- exchangeCapsule = Commons .getExchangeStoreFinal (dynamicStore , exchangeStore , exchangeV2Store ).
60- get (ByteArray .fromLong (exchangeInjectContract .getExchangeId ()));
58+ exchangeCapsule = Commons .getExchangeStoreFinal (dynamicStore , exchangeStore , exchangeV2Store )
59+ . get (ByteArray .fromLong (exchangeInjectContract .getExchangeId ()));
6160 byte [] firstTokenID = exchangeCapsule .getFirstTokenId ();
6261 byte [] secondTokenID = exchangeCapsule .getSecondTokenId ();
6362 long firstTokenBalance = exchangeCapsule .getFirstTokenBalance ();
@@ -73,27 +72,27 @@ public boolean execute(Object object) throws ContractExeException {
7372 anotherTokenID = secondTokenID ;
7473 anotherTokenQuant = floorDiv (multiplyExact (
7574 secondTokenBalance , tokenQuant ), firstTokenBalance );
76- exchangeCapsule .setBalance (firstTokenBalance + tokenQuant ,
77- secondTokenBalance + anotherTokenQuant );
75+ exchangeCapsule .setBalance (addExact ( firstTokenBalance , tokenQuant ) ,
76+ addExact ( secondTokenBalance , anotherTokenQuant ) );
7877 } else {
7978 anotherTokenID = firstTokenID ;
8079 anotherTokenQuant = floorDiv (multiplyExact (
8180 firstTokenBalance , tokenQuant ), secondTokenBalance );
82- exchangeCapsule .setBalance (firstTokenBalance + anotherTokenQuant ,
83- secondTokenBalance + tokenQuant );
81+ exchangeCapsule .setBalance (addExact ( firstTokenBalance , anotherTokenQuant ) ,
82+ addExact ( secondTokenBalance , tokenQuant ) );
8483 }
8584
86- long newBalance = accountCapsule .getBalance () - calcFee ();
85+ long newBalance = subtractExact ( accountCapsule .getBalance (), calcFee () );
8786 accountCapsule .setBalance (newBalance );
8887
8988 if (Arrays .equals (tokenID , TRX_SYMBOL_BYTES )) {
90- accountCapsule .setBalance (newBalance - tokenQuant );
89+ accountCapsule .setBalance (subtractExact ( newBalance , tokenQuant ) );
9190 } else {
9291 accountCapsule .reduceAssetAmountV2 (tokenID , tokenQuant , dynamicStore , assetIssueStore );
9392 }
9493
9594 if (Arrays .equals (anotherTokenID , TRX_SYMBOL_BYTES )) {
96- accountCapsule .setBalance (newBalance - anotherTokenQuant );
95+ accountCapsule .setBalance (subtractExact ( newBalance , anotherTokenQuant ) );
9796 } else {
9897 accountCapsule
9998 .reduceAssetAmountV2 (anotherTokenID , anotherTokenQuant , dynamicStore , assetIssueStore );
@@ -105,7 +104,8 @@ public boolean execute(Object object) throws ContractExeException {
105104
106105 ret .setExchangeInjectAnotherAmount (anotherTokenQuant );
107106 ret .setStatus (fee , code .SUCESS );
108- } catch (ItemNotFoundException | InvalidProtocolBufferException e ) {
107+ } catch (ItemNotFoundException | InvalidProtocolBufferException
108+ | ArithmeticException e ) {
109109 logger .debug (e .getMessage (), e );
110110 ret .setStatus (fee , code .FAILED );
111111 throw new ContractExeException (e .getMessage ());
@@ -115,6 +115,14 @@ public boolean execute(Object object) throws ContractExeException {
115115
116116 @ Override
117117 public boolean validate () throws ContractValidateException {
118+ try {
119+ return doValidate ();
120+ } catch (ArithmeticException e ) {
121+ throw new ContractValidateException (e .getMessage ());
122+ }
123+ }
124+
125+ private boolean doValidate () throws ContractValidateException {
118126 if (this .any == null ) {
119127 throw new ContractValidateException (ActuatorConstant .CONTRACT_NOT_EXIST );
120128 }
@@ -156,8 +164,8 @@ public boolean validate() throws ContractValidateException {
156164
157165 ExchangeCapsule exchangeCapsule ;
158166 try {
159- exchangeCapsule = Commons .getExchangeStoreFinal (dynamicStore , exchangeStore , exchangeV2Store ).
160- get (ByteArray .fromLong (contract .getExchangeId ()));
167+ exchangeCapsule = Commons .getExchangeStoreFinal (dynamicStore , exchangeStore , exchangeV2Store )
168+ . get (ByteArray .fromLong (contract .getExchangeId ()));
161169
162170 } catch (ItemNotFoundException ex ) {
163171 throw new ContractValidateException ("Exchange[" + contract .getExchangeId () + ActuatorConstant
@@ -179,9 +187,9 @@ public boolean validate() throws ContractValidateException {
179187 byte [] anotherTokenID ;
180188 long anotherTokenQuant ;
181189
182- if (dynamicStore .getAllowSameTokenName () == 1 &&
183- !Arrays .equals (tokenID , TRX_SYMBOL_BYTES ) &&
184- !isNumber (tokenID )) {
190+ if (dynamicStore .getAllowSameTokenName () == 1
191+ && !Arrays .equals (tokenID , TRX_SYMBOL_BYTES )
192+ && !isNumber (tokenID )) {
185193 throw new ContractValidateException ("token id is not a valid number" );
186194 }
187195
@@ -208,14 +216,14 @@ public boolean validate() throws ContractValidateException {
208216 anotherTokenID = secondTokenID ;
209217 anotherTokenQuant = bigSecondTokenBalance .multiply (bigTokenQuant )
210218 .divide (bigFirstTokenBalance ).longValueExact ();
211- newTokenBalance = firstTokenBalance + tokenQuant ;
212- newAnotherTokenBalance = secondTokenBalance + anotherTokenQuant ;
219+ newTokenBalance = addExact ( firstTokenBalance , tokenQuant ) ;
220+ newAnotherTokenBalance = addExact ( secondTokenBalance , anotherTokenQuant ) ;
213221 } else {
214222 anotherTokenID = firstTokenID ;
215223 anotherTokenQuant = bigFirstTokenBalance .multiply (bigTokenQuant )
216224 .divide (bigSecondTokenBalance ).longValueExact ();
217- newTokenBalance = secondTokenBalance + tokenQuant ;
218- newAnotherTokenBalance = firstTokenBalance + anotherTokenQuant ;
225+ newTokenBalance = addExact ( secondTokenBalance , tokenQuant ) ;
226+ newAnotherTokenBalance = addExact ( firstTokenBalance , anotherTokenQuant ) ;
219227 }
220228
221229 if (anotherTokenQuant <= 0 ) {
@@ -228,7 +236,7 @@ public boolean validate() throws ContractValidateException {
228236 }
229237
230238 if (Arrays .equals (tokenID , TRX_SYMBOL_BYTES )) {
231- if (accountCapsule .getBalance () < (tokenQuant + calcFee ())) {
239+ if (accountCapsule .getBalance () < addExact (tokenQuant , calcFee ())) {
232240 throw new ContractValidateException ("balance is not enough" );
233241 }
234242 } else {
@@ -238,7 +246,7 @@ public boolean validate() throws ContractValidateException {
238246 }
239247
240248 if (Arrays .equals (anotherTokenID , TRX_SYMBOL_BYTES )) {
241- if (accountCapsule .getBalance () < (anotherTokenQuant + calcFee ())) {
249+ if (accountCapsule .getBalance () < addExact (anotherTokenQuant , calcFee ())) {
242250 throw new ContractValidateException ("balance is not enough" );
243251 }
244252 } else {
0 commit comments