Skip to content

Commit

Permalink
AtomicSwapClient: Wait for bailin confirmations based on value of trade
Browse files Browse the repository at this point in the history
  • Loading branch information
mappum committed Mar 9, 2015
1 parent d79b6bb commit b1c353d
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions src/main/java/io/coinswap/swap/AtomicSwapClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -324,11 +324,19 @@ private void onBailin(final Transaction tx) {

log.info("Received other party's bailin via coin network: " + tx.toString());
final AtomicSwapClient parent = this;
int confirmDepth = 0;//currencies[b].getConfirmationDepth();
// NOTE: we currently aren't waiting for confirms, which is dangerous
// TODO: use deeper confirmations for high-value swaps

// TODO: calculate approx BTC value for pairs that don't have BTC, so we can pick a sensible confirmation depth
int confirmDepth;
if(swap.trade.coins[1].toLowerCase().equals("btc")) {
Coin btcValue = swap.trade.quantities[1];
confirmDepth = confirmDepthForValue(btcValue) * currencies[b].getConfirmationDepth();
} else {
confirmDepth = currencies[b].getConfirmationDepth();
}

if(tx.getConfidence().getDepthInBlocks() >= confirmDepth) {
// TODO: if this is an unconfirmed TX, make sure a sufficient number of peers have broadcast the TX to us,
// to ensure it's being accepted by the network
onBailinConfirm(tx);
} else {
tx.getConfidence().getDepthFuture(confirmDepth).addListener(
Expand All @@ -343,6 +351,16 @@ public void run() {
}
}

private int confirmDepthForValue(Coin approxBtcValue) {
// TODO: tweak these values to ensure they are configured safely
// TODO: user settings to override these values
if(approxBtcValue.isLessThan(Coin.parseCoin("0.5"))) return 0; // instant/unconfirmed for small trades
if(approxBtcValue.isLessThan(Coin.parseCoin("2.5"))) return 1;
if(approxBtcValue.isLessThan(Coin.parseCoin("5"))) return 2;
if(approxBtcValue.isLessThan(Coin.parseCoin("10"))) return 3;
return 4;
}

private void onBailinConfirm(Transaction tx) {
tx.setPurpose(Transaction.Purpose.ASSURANCE_CONTRACT_PLEDGE);

Expand Down

0 comments on commit b1c353d

Please sign in to comment.