forked from Qortal/qortal
Improved refund/refundAll HTLC code, to handle cases where there have been multiple purchase attempts for the same AT.
This commit is contained in:
parent
b0486f44bb
commit
eb569304ba
@ -8,11 +8,10 @@ import io.swagger.v3.oas.annotations.responses.ApiResponse;
|
|||||||
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
|
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
|
||||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
import java.util.concurrent.locks.ReentrantLock;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
import javax.ws.rs.*;
|
import javax.ws.rs.*;
|
||||||
@ -25,7 +24,6 @@ import org.bitcoinj.core.*;
|
|||||||
import org.bitcoinj.script.Script;
|
import org.bitcoinj.script.Script;
|
||||||
import org.qortal.api.*;
|
import org.qortal.api.*;
|
||||||
import org.qortal.api.model.CrossChainBitcoinyHTLCStatus;
|
import org.qortal.api.model.CrossChainBitcoinyHTLCStatus;
|
||||||
import org.qortal.controller.Controller;
|
|
||||||
import org.qortal.crosschain.*;
|
import org.qortal.crosschain.*;
|
||||||
import org.qortal.crypto.Crypto;
|
import org.qortal.crypto.Crypto;
|
||||||
import org.qortal.data.at.ATData;
|
import org.qortal.data.at.ATData;
|
||||||
@ -586,7 +584,13 @@ public class CrossChainHtlcResource {
|
|||||||
}
|
}
|
||||||
|
|
||||||
List<TradeBotData> allTradeBotData = repository.getCrossChainRepository().getAllTradeBotData();
|
List<TradeBotData> allTradeBotData = repository.getCrossChainRepository().getAllTradeBotData();
|
||||||
TradeBotData tradeBotData = allTradeBotData.stream().filter(tradeBotDataItem -> tradeBotDataItem.getAtAddress().equals(atAddress)).findFirst().orElse(null);
|
List<TradeBotData> tradeBotDataList = allTradeBotData.stream().filter(tradeBotDataItem -> tradeBotDataItem.getAtAddress().equals(atAddress)).collect(Collectors.toList());
|
||||||
|
if (tradeBotDataList == null || tradeBotDataList.isEmpty())
|
||||||
|
throw ApiExceptionFactory.INSTANCE.createException(request, ApiError.INVALID_CRITERIA);
|
||||||
|
|
||||||
|
// Loop through all matching entries for this AT address, as there might be more than one
|
||||||
|
for (TradeBotData tradeBotData : tradeBotDataList) {
|
||||||
|
|
||||||
if (tradeBotData == null)
|
if (tradeBotData == null)
|
||||||
throw ApiExceptionFactory.INSTANCE.createException(request, ApiError.INVALID_CRITERIA);
|
throw ApiExceptionFactory.INSTANCE.createException(request, ApiError.INVALID_CRITERIA);
|
||||||
|
|
||||||
@ -595,12 +599,12 @@ public class CrossChainHtlcResource {
|
|||||||
|
|
||||||
// We can't refund P2SH-A until lockTime-A has passed
|
// We can't refund P2SH-A until lockTime-A has passed
|
||||||
if (NTP.getTime() <= lockTime * 1000L)
|
if (NTP.getTime() <= lockTime * 1000L)
|
||||||
throw ApiExceptionFactory.INSTANCE.createException(request, ApiError.FOREIGN_BLOCKCHAIN_TOO_SOON);
|
continue;
|
||||||
|
|
||||||
// We can't refund P2SH-A until median block time has passed lockTime-A (see BIP113)
|
// We can't refund P2SH-A until median block time has passed lockTime-A (see BIP113)
|
||||||
int medianBlockTime = bitcoiny.getMedianBlockTime();
|
int medianBlockTime = bitcoiny.getMedianBlockTime();
|
||||||
if (medianBlockTime <= lockTime)
|
if (medianBlockTime <= lockTime)
|
||||||
throw ApiExceptionFactory.INSTANCE.createException(request, ApiError.FOREIGN_BLOCKCHAIN_TOO_SOON);
|
continue;
|
||||||
|
|
||||||
// Fee for redeem/refund is subtracted from P2SH-A balance.
|
// Fee for redeem/refund is subtracted from P2SH-A balance.
|
||||||
long feeTimestamp = calcFeeTimestamp(lockTime, crossChainTradeData.tradeTimeout);
|
long feeTimestamp = calcFeeTimestamp(lockTime, crossChainTradeData.tradeTimeout);
|
||||||
@ -615,8 +619,7 @@ public class CrossChainHtlcResource {
|
|||||||
redeemScriptA = PirateChainHTLC.buildScript(tradeBotData.getTradeForeignPublicKey(), lockTime, crossChainTradeData.creatorForeignPKH, tradeBotData.getHashOfSecret());
|
redeemScriptA = PirateChainHTLC.buildScript(tradeBotData.getTradeForeignPublicKey(), lockTime, crossChainTradeData.creatorForeignPKH, tradeBotData.getHashOfSecret());
|
||||||
p2shAddressA = PirateChain.getInstance().deriveP2shAddressBPrefix(redeemScriptA);
|
p2shAddressA = PirateChain.getInstance().deriveP2shAddressBPrefix(redeemScriptA);
|
||||||
htlcStatusA = PirateChainHTLC.determineHtlcStatus(bitcoiny.getBlockchainProvider(), p2shAddressA, minimumAmountA);
|
htlcStatusA = PirateChainHTLC.determineHtlcStatus(bitcoiny.getBlockchainProvider(), p2shAddressA, minimumAmountA);
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
redeemScriptA = BitcoinyHTLC.buildScript(tradeBotData.getTradeForeignPublicKeyHash(), lockTime, crossChainTradeData.creatorForeignPKH, tradeBotData.getHashOfSecret());
|
redeemScriptA = BitcoinyHTLC.buildScript(tradeBotData.getTradeForeignPublicKeyHash(), lockTime, crossChainTradeData.creatorForeignPKH, tradeBotData.getHashOfSecret());
|
||||||
p2shAddressA = bitcoiny.deriveP2shAddress(redeemScriptA);
|
p2shAddressA = bitcoiny.deriveP2shAddress(redeemScriptA);
|
||||||
htlcStatusA = BitcoinyHTLC.determineHtlcStatus(bitcoiny.getBlockchainProvider(), p2shAddressA, minimumAmountA);
|
htlcStatusA = BitcoinyHTLC.determineHtlcStatus(bitcoiny.getBlockchainProvider(), p2shAddressA, minimumAmountA);
|
||||||
@ -627,16 +630,16 @@ public class CrossChainHtlcResource {
|
|||||||
case UNFUNDED:
|
case UNFUNDED:
|
||||||
case FUNDING_IN_PROGRESS:
|
case FUNDING_IN_PROGRESS:
|
||||||
// Still waiting for P2SH-A to be funded...
|
// Still waiting for P2SH-A to be funded...
|
||||||
throw ApiExceptionFactory.INSTANCE.createException(request, ApiError.FOREIGN_BLOCKCHAIN_TOO_SOON);
|
continue;
|
||||||
|
|
||||||
case REDEEM_IN_PROGRESS:
|
case REDEEM_IN_PROGRESS:
|
||||||
case REDEEMED:
|
case REDEEMED:
|
||||||
case REFUND_IN_PROGRESS:
|
case REFUND_IN_PROGRESS:
|
||||||
case REFUNDED:
|
case REFUNDED:
|
||||||
// Too late!
|
// Too late!
|
||||||
return false;
|
continue;
|
||||||
|
|
||||||
case FUNDED:{
|
case FUNDED: {
|
||||||
Coin refundAmount = Coin.valueOf(crossChainTradeData.expectedForeignAmount);
|
Coin refundAmount = Coin.valueOf(crossChainTradeData.expectedForeignAmount);
|
||||||
|
|
||||||
if (Objects.equals(bitcoiny.getCurrencyCode(), "ARRR")) {
|
if (Objects.equals(bitcoiny.getCurrencyCode(), "ARRR")) {
|
||||||
@ -659,8 +662,7 @@ public class CrossChainHtlcResource {
|
|||||||
String txid = PirateChain.getInstance().refundP2sh(p2shAddressT3,
|
String txid = PirateChain.getInstance().refundP2sh(p2shAddressT3,
|
||||||
receiveAddress, refundAmount.value, redeemScript58, fundingTxid58, lockTime, privateKey58);
|
receiveAddress, refundAmount.value, redeemScript58, fundingTxid58, lockTime, privateKey58);
|
||||||
LOGGER.info("Refund txid: {}", txid);
|
LOGGER.info("Refund txid: {}", txid);
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
// ElectrumX coins
|
// ElectrumX coins
|
||||||
|
|
||||||
ECKey refundKey = ECKey.fromPrivate(tradeBotData.getTradePrivateKey());
|
ECKey refundKey = ECKey.fromPrivate(tradeBotData.getTradePrivateKey());
|
||||||
@ -680,6 +682,7 @@ public class CrossChainHtlcResource {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
} catch (DataException e) {
|
} catch (DataException e) {
|
||||||
throw ApiExceptionFactory.INSTANCE.createException(request, ApiError.REPOSITORY_ISSUE, e);
|
throw ApiExceptionFactory.INSTANCE.createException(request, ApiError.REPOSITORY_ISSUE, e);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user