Skip to content
18 changes: 8 additions & 10 deletions src/main/java/org/tron/core/actuator/ExchangeCreateActuator.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
import com.google.protobuf.Any;
import com.google.protobuf.ByteString;
import com.google.protobuf.InvalidProtocolBufferException;
import java.util.Arrays;
import lombok.extern.slf4j.Slf4j;
import org.tron.common.utils.StringUtil;
import org.tron.core.Wallet;
import org.tron.core.capsule.AccountCapsule;
import org.tron.core.capsule.ExchangeCapsule;
import org.tron.core.capsule.TransactionResultCapsule;
import org.tron.core.config.Parameter.ChainParameters;
import org.tron.core.db.Manager;
import org.tron.core.exception.ContractExeException;
import org.tron.core.exception.ContractValidateException;
Expand Down Expand Up @@ -39,13 +39,15 @@ public boolean execute(TransactionResultCapsule ret) throws ContractExeException

long newBalance = accountCapsule.getBalance() - calcFee();

if (firstTokenID == "_".getBytes()) {
accountCapsule.setBalance(newBalance);

if (Arrays.equals(firstTokenID, "_".getBytes())) {
accountCapsule.setBalance(newBalance - firstTokenBalance);
} else {
accountCapsule.reduceAssetAmount(firstTokenID, firstTokenBalance);
}

if (secondTokenID == "_".getBytes()) {
if (Arrays.equals(secondTokenID, "_".getBytes())) {
accountCapsule.setBalance(newBalance - secondTokenBalance);
} else {
accountCapsule.reduceAssetAmount(secondTokenID, secondTokenBalance);
Expand Down Expand Up @@ -119,7 +121,7 @@ public boolean validate() throws ContractValidateException {
long firstTokenBalance = contract.getFirstTokenBalance();
long secondTokenBalance = contract.getSecondTokenBalance();

if (firstTokenID == secondTokenID) {
if (Arrays.equals(firstTokenID, secondTokenID)) {
throw new ContractValidateException("cannot exchange same tokens");
}

Expand All @@ -132,7 +134,7 @@ public boolean validate() throws ContractValidateException {
throw new ContractValidateException("token balance must less than " + balanceLimit);
}

if (firstTokenID == "_".getBytes()) {
if (Arrays.equals(firstTokenID, "_".getBytes())) {
if (accountCapsule.getBalance() < (firstTokenBalance + calcFee())) {
throw new ContractValidateException("balance is not enough");
}
Expand All @@ -142,7 +144,7 @@ public boolean validate() throws ContractValidateException {
}
}

if (secondTokenID == "_".getBytes()) {
if (Arrays.equals(secondTokenID, "_".getBytes())) {
if (accountCapsule.getBalance() < (secondTokenBalance + calcFee())) {
throw new ContractValidateException("balance is not enough");
}
Expand All @@ -166,8 +168,4 @@ public long calcFee() {
return dbManager.getDynamicPropertiesStore().getExchangeCreateFee();
}

private boolean validKey(long idx) {
return idx >= 0 && idx < ChainParameters.values().length;
}

}
33 changes: 18 additions & 15 deletions src/main/java/org/tron/core/actuator/ExchangeInjectActuator.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.google.protobuf.Any;
import com.google.protobuf.ByteString;
import com.google.protobuf.InvalidProtocolBufferException;
import java.math.BigInteger;
import java.util.Arrays;
import lombok.extern.slf4j.Slf4j;
import org.tron.common.utils.ByteArray;
Expand All @@ -11,7 +12,6 @@
import org.tron.core.capsule.AccountCapsule;
import org.tron.core.capsule.ExchangeCapsule;
import org.tron.core.capsule.TransactionResultCapsule;
import org.tron.core.config.Parameter.ChainParameters;
import org.tron.core.db.Manager;
import org.tron.core.exception.ContractExeException;
import org.tron.core.exception.ContractValidateException;
Expand Down Expand Up @@ -65,13 +65,13 @@ public boolean execute(TransactionResultCapsule ret) throws ContractExeException

long newBalance = accountCapsule.getBalance() - calcFee();

if (tokenID == "_".getBytes()) {
if (Arrays.equals(tokenID, "_".getBytes())) {
accountCapsule.setBalance(newBalance - tokenQuant);
} else {
accountCapsule.reduceAssetAmount(tokenID, tokenQuant);
}

if (anotherTokenID == "_".getBytes()) {
if (Arrays.equals(anotherTokenID, "_".getBytes())) {
accountCapsule.setBalance(newBalance - anotherTokenQuant);
} else {
accountCapsule.reduceAssetAmount(anotherTokenID, anotherTokenQuant);
Expand Down Expand Up @@ -164,34 +164,41 @@ public boolean validate() throws ContractValidateException {
}

if (tokenQuant <= 0) {
throw new ContractValidateException("injected token balance must greater than zero");
throw new ContractValidateException("injected token quant must greater than zero");
}

BigInteger bigFirstTokenBalance = new BigInteger(String.valueOf(firstTokenBalance));
BigInteger bigSecondTokenBalance = new BigInteger(String.valueOf(secondTokenBalance));
BigInteger bigTokenQuant = new BigInteger(String.valueOf(tokenQuant));
long newTokenBalance, newAnotherTokenBalance;
if (Arrays.equals(tokenID, firstTokenID)) {
anotherTokenID = secondTokenID;
anotherTokenQuant = Math
.floorDiv(Math.multiplyExact(secondTokenBalance, tokenQuant), firstTokenBalance);
// anotherTokenQuant = Math
// .floorDiv(Math.multiplyExact(secondTokenBalance, tokenQuant), firstTokenBalance);
anotherTokenQuant = bigSecondTokenBalance.multiply(bigTokenQuant)
.divide(bigFirstTokenBalance).longValueExact();
newTokenBalance = firstTokenBalance + tokenQuant;
newAnotherTokenBalance = secondTokenBalance + anotherTokenQuant;
} else {
anotherTokenID = firstTokenID;
anotherTokenQuant = Math
.floorDiv(Math.multiplyExact(firstTokenBalance, tokenQuant), secondTokenBalance);
// anotherTokenQuant = Math
// .floorDiv(Math.multiplyExact(firstTokenBalance, tokenQuant), secondTokenBalance);
anotherTokenQuant = bigFirstTokenBalance.multiply(bigTokenQuant)
.divide(bigSecondTokenBalance).longValueExact();
newTokenBalance = secondTokenBalance + tokenQuant;
newAnotherTokenBalance = firstTokenBalance + anotherTokenQuant;
}

if (anotherTokenQuant <= 0) {
throw new ContractValidateException(" The calculated Token Quant must be larger than 0");
throw new ContractValidateException("the calculated token quant must be greater than 0");
}

long balanceLimit = dbManager.getDynamicPropertiesStore().getExchangeBalanceLimit();
if (newTokenBalance > balanceLimit || newAnotherTokenBalance > balanceLimit) {
throw new ContractValidateException("token balance must less than " + balanceLimit);
}

if (tokenID == "_".getBytes()) {
if (Arrays.equals(tokenID, "_".getBytes())) {
if (accountCapsule.getBalance() < (tokenQuant + calcFee())) {
throw new ContractValidateException("balance is not enough");
}
Expand All @@ -201,7 +208,7 @@ public boolean validate() throws ContractValidateException {
}
}

if (anotherTokenID == "_".getBytes()) {
if (Arrays.equals(anotherTokenID, "_".getBytes())) {
if (accountCapsule.getBalance() < (anotherTokenQuant + calcFee())) {
throw new ContractValidateException("balance is not enough");
}
Expand All @@ -225,8 +232,4 @@ public long calcFee() {
return 0;
}

private boolean validKey(long idx) {
return idx >= 0 && idx < ChainParameters.values().length;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import org.tron.core.capsule.AccountCapsule;
import org.tron.core.capsule.ExchangeCapsule;
import org.tron.core.capsule.TransactionResultCapsule;
import org.tron.core.config.Parameter.ChainParameters;
import org.tron.core.db.Manager;
import org.tron.core.exception.ContractExeException;
import org.tron.core.exception.ContractValidateException;
Expand Down Expand Up @@ -55,13 +54,13 @@ public boolean execute(TransactionResultCapsule ret) throws ContractExeException

long newBalance = accountCapsule.getBalance() - calcFee();

if (tokenID == "_".getBytes()) {
if (Arrays.equals(tokenID, "_".getBytes())) {
accountCapsule.setBalance(newBalance - tokenQuant);
} else {
accountCapsule.reduceAssetAmount(tokenID, tokenQuant);
}

if (anotherTokenID == "_".getBytes()) {
if (Arrays.equals(anotherTokenID, "_".getBytes())) {
accountCapsule.setBalance(newBalance + anotherTokenQuant);
} else {
accountCapsule.addAssetAmount(anotherTokenID, anotherTokenQuant);
Expand Down Expand Up @@ -145,14 +144,20 @@ public boolean validate() throws ContractValidateException {
throw new ContractValidateException("transaction token balance must greater than zero");
}

if (firstTokenBalance == 0 || secondTokenBalance == 0) {
throw new ContractValidateException("Token balance in exchange is equal with 0,"
+ "the exchange has been closed");
}

long balanceLimit = dbManager.getDynamicPropertiesStore().getExchangeBalanceLimit();
long tokenBalance = (tokenID == firstTokenID ? firstTokenBalance : secondTokenBalance);
long tokenBalance = (Arrays.equals(tokenID, firstTokenID) ? firstTokenBalance
: secondTokenBalance);
tokenBalance += tokenQuant;
if (tokenBalance > balanceLimit) {
throw new ContractValidateException("token balance must less than " + balanceLimit);
}

if (tokenID == "_".getBytes()) {
if (Arrays.equals(tokenID, "_".getBytes())) {
if (accountCapsule.getBalance() < (tokenQuant + calcFee())) {
throw new ContractValidateException("balance is not enough");
}
Expand Down Expand Up @@ -181,8 +186,4 @@ public long calcFee() {
return 0;
}

private boolean validKey(long idx) {
return idx >= 0 && idx < ChainParameters.values().length;
}

}
45 changes: 28 additions & 17 deletions src/main/java/org/tron/core/actuator/ExchangeWithdrawActuator.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.google.protobuf.Any;
import com.google.protobuf.ByteString;
import com.google.protobuf.InvalidProtocolBufferException;
import java.math.BigInteger;
import java.util.Arrays;
import lombok.extern.slf4j.Slf4j;
import org.tron.common.utils.ByteArray;
Expand All @@ -11,7 +12,6 @@
import org.tron.core.capsule.AccountCapsule;
import org.tron.core.capsule.ExchangeCapsule;
import org.tron.core.capsule.TransactionResultCapsule;
import org.tron.core.config.Parameter.ChainParameters;
import org.tron.core.db.Manager;
import org.tron.core.exception.ContractExeException;
import org.tron.core.exception.ContractValidateException;
Expand Down Expand Up @@ -49,29 +49,36 @@ public boolean execute(TransactionResultCapsule ret) throws ContractExeException
byte[] anotherTokenID;
long anotherTokenQuant;

BigInteger bigFirstTokenBalance = new BigInteger(String.valueOf(firstTokenBalance));
BigInteger bigSecondTokenBalance = new BigInteger(String.valueOf(secondTokenBalance));
BigInteger bigTokenQuant = new BigInteger(String.valueOf(tokenQuant));
if (Arrays.equals(tokenID, firstTokenID)) {
anotherTokenID = secondTokenID;
anotherTokenQuant = Math
.floorDiv(Math.multiplyExact(secondTokenBalance, tokenQuant), firstTokenBalance);
// anotherTokenQuant = Math
// .floorDiv(Math.multiplyExact(secondTokenBalance, tokenQuant), firstTokenBalance);
anotherTokenQuant = bigSecondTokenBalance.multiply(bigTokenQuant)
.divide(bigFirstTokenBalance).longValueExact();
exchangeCapsule.setBalance(firstTokenBalance - tokenQuant,
secondTokenBalance - anotherTokenQuant);
} else {
anotherTokenID = firstTokenID;
anotherTokenQuant = Math
.floorDiv(Math.multiplyExact(firstTokenBalance, tokenQuant), secondTokenBalance);
// anotherTokenQuant = Math
// .floorDiv(Math.multiplyExact(firstTokenBalance, tokenQuant), secondTokenBalance);
anotherTokenQuant = bigFirstTokenBalance.multiply(bigTokenQuant)
.divide(bigSecondTokenBalance).longValueExact();
exchangeCapsule.setBalance(firstTokenBalance - anotherTokenQuant,
secondTokenBalance - tokenQuant);
}

long newBalance = accountCapsule.getBalance() - calcFee();

if (tokenID == "_".getBytes()) {
if (Arrays.equals(tokenID, "_".getBytes())) {
accountCapsule.setBalance(newBalance + tokenQuant);
} else {
accountCapsule.addAssetAmount(tokenID, tokenQuant);
}

if (anotherTokenID == "_".getBytes()) {
if (Arrays.equals(anotherTokenID, "_".getBytes())) {
accountCapsule.setBalance(newBalance + anotherTokenQuant);
} else {
accountCapsule.addAssetAmount(anotherTokenID, anotherTokenQuant);
Expand Down Expand Up @@ -158,22 +165,30 @@ public boolean validate() throws ContractValidateException {
}

if (tokenQuant <= 0) {
throw new ContractValidateException("withdraw token balance must greater than zero");
throw new ContractValidateException("withdraw token quant must greater than zero");
}

if (firstTokenBalance == 0 || secondTokenBalance == 0) {
throw new ContractValidateException("Token balance in exchange is equal with 0,the exchange has been closed");
throw new ContractValidateException("Token balance in exchange is equal with 0,"
+ "the exchange has been closed");
}

BigInteger bigFirstTokenBalance = new BigInteger(String.valueOf(firstTokenBalance));
BigInteger bigSecondTokenBalance = new BigInteger(String.valueOf(secondTokenBalance));
BigInteger bigTokenQuant = new BigInteger(String.valueOf(tokenQuant));
if (Arrays.equals(tokenID, firstTokenID)) {
anotherTokenQuant = Math
.floorDiv(Math.multiplyExact(secondTokenBalance, tokenQuant), firstTokenBalance);
// anotherTokenQuant = Math
// .floorDiv(Math.multiplyExact(secondTokenBalance, tokenQuant), firstTokenBalance);
anotherTokenQuant = bigSecondTokenBalance.multiply(bigTokenQuant)
.divide(bigFirstTokenBalance).longValueExact();
if (firstTokenBalance < tokenQuant || secondTokenBalance < anotherTokenQuant) {
throw new ContractValidateException("exchange balance is not enough");
}
} else {
anotherTokenQuant = Math
.floorDiv(Math.multiplyExact(firstTokenBalance, tokenQuant), secondTokenBalance);
// anotherTokenQuant = Math
// .floorDiv(Math.multiplyExact(firstTokenBalance, tokenQuant), secondTokenBalance);
anotherTokenQuant = bigFirstTokenBalance.multiply(bigTokenQuant)
.divide(bigSecondTokenBalance).longValueExact();
if (secondTokenBalance < tokenQuant || firstTokenBalance < anotherTokenQuant) {
throw new ContractValidateException("exchange balance is not enough");
}
Expand All @@ -193,8 +208,4 @@ public long calcFee() {
return 0;
}

private boolean validKey(long idx) {
return idx >= 0 && idx < ChainParameters.values().length;
}

}
Loading