Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add trade limit exceptions #3406

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -285,19 +285,6 @@ public long getWitnessSignAge(Offer offer, Date now) {
.orElse(-1L);
}

// Return -1 if not signed
public long getWitnessSignAge(Trade trade, Date now) {
TradingPeer tradingPeer = trade.getProcessModel().getTradingPeer();
if (tradingPeer.getPaymentAccountPayload() == null || tradingPeer.getPubKeyRing() == null) {
// unexpected
return -1;
}

return findWitness(tradingPeer.getPaymentAccountPayload(), tradingPeer.getPubKeyRing())
.map(witness -> getWitnessSignAge(witness, now))
.orElse(-1L);
}

public AccountAge getPeersAccountAgeCategory(long peersAccountAge) {
return getAccountAgeCategory(peersAccountAge);
}
Expand Down Expand Up @@ -376,15 +363,22 @@ private long getTradeLimit(Coin maxTradeLimit,
}

///////////////////////////////////////////////////////////////////////////////////////////
// Mature witness checks
// Trade limit exceptions
///////////////////////////////////////////////////////////////////////////////////////////

private boolean isImmature(AccountAgeWitness accountAgeWitness) {
return accountAgeWitness.getDate() > SAFE_ACCOUNT_AGE_DATE;
}

public boolean isMyAccountAgeImmature(PaymentAccount myPaymentAccount) {
return isImmature(getMyWitness(myPaymentAccount.getPaymentAccountPayload()));
public boolean myHasTradeLimitException(PaymentAccount myPaymentAccount) {
return hasTradeLimitException(getMyWitness(myPaymentAccount.getPaymentAccountPayload()));
}

// There are no trade limits on accounts that
// - are mature
// - were signed by an arbitrator
private boolean hasTradeLimitException(AccountAgeWitness accountAgeWitness) {
return !isImmature(accountAgeWitness) || signedWitnessService.isSignedByArbitrator(accountAgeWitness);
}

///////////////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -415,7 +409,7 @@ public long getMyTradeLimit(PaymentAccount paymentAccount, String currencyCode,

AccountAgeWitness accountAgeWitness = getMyWitness(paymentAccount.getPaymentAccountPayload());
Coin maxTradeLimit = paymentAccount.getPaymentMethod().getMaxTradeLimitAsCoin(currencyCode);
if (!isImmature(accountAgeWitness)) {
if (hasTradeLimitException(accountAgeWitness)) {
return maxTradeLimit.value;
}
final long accountSignAge = getWitnessSignAge(accountAgeWitness, new Date());
Expand Down Expand Up @@ -541,7 +535,7 @@ private boolean verifyPeersTradeLimit(Offer offer,
final String currencyCode = offer.getCurrencyCode();
final Coin defaultMaxTradeLimit = PaymentMethod.getPaymentMethodById(offer.getOfferPayload().getPaymentMethodId()).getMaxTradeLimitAsCoin(currencyCode);
long peersCurrentTradeLimit = defaultMaxTradeLimit.value;
if (isImmature(peersWitness)) {
if (!hasTradeLimitException(peersWitness)) {
final long accountSignAge = getWitnessSignAge(peersWitness, peersCurrentDate);
AccountAge accountAgeCategory = getPeersAccountAgeCategory(accountSignAge);
OfferPayload.Direction direction = offer.isMyOffer(keyRing) ?
Expand Down
12 changes: 6 additions & 6 deletions core/src/main/java/bisq/core/payment/PaymentAccounts.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ PaymentAccount getOldestPaymentAccountForOffer(Offer offer) {
}

private List<PaymentAccount> sortValidAccounts(Offer offer) {
Comparator<PaymentAccount> comparator = this::compareBySignAgeOrMatureAccount;
Comparator<PaymentAccount> comparator = this::compareByTradeLimit;
return accounts.stream()
.filter(account -> validator.apply(offer, account))
.sorted(comparator.reversed())
Expand Down Expand Up @@ -91,13 +91,13 @@ private void logAccounts(List<PaymentAccount> accounts) {
}
}

// Accounts created before
private int compareBySignAgeOrMatureAccount(PaymentAccount left, PaymentAccount right) {
// Accounts ranked by trade limit
private int compareByTradeLimit(PaymentAccount left, PaymentAccount right) {
// Mature accounts count as infinite sign age
if (!accountAgeWitnessService.isMyAccountAgeImmature(left)) {
return accountAgeWitnessService.isMyAccountAgeImmature(right) ? 1 : 0;
if (accountAgeWitnessService.myHasTradeLimitException(left)) {
return !accountAgeWitnessService.myHasTradeLimitException(right) ? 1 : 0;
}
if (!accountAgeWitnessService.isMyAccountAgeImmature(right)) {
if (accountAgeWitnessService.myHasTradeLimitException(right)) {
return -1;
}

Expand Down