Skip to content

Commit

Permalink
trade fees are adjustable and persisted in offer payload
Browse files Browse the repository at this point in the history
  • Loading branch information
woodser committed Apr 7, 2024
1 parent 59fbd80 commit 7d76604
Show file tree
Hide file tree
Showing 73 changed files with 540 additions and 543 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ protected void validate() {
protected final Function<TradeInfo, Long> toTradeFeeBtc = (t) -> {
var isMyOffer = t.getOffer().getIsMyOffer();
if (isMyOffer) {
return t.getOffer().getMakerFee();
return t.getMakerFee();
} else {
return t.getTakerFee();
}
Expand All @@ -198,7 +198,7 @@ protected void validate() {
protected final Function<TradeInfo, Long> toMyMakerOrTakerFee = (t) -> {
return isTaker.test(t)
? t.getTakerFee()
: t.getOffer().getMakerFee();
: t.getMakerFee();
};

protected final Function<TradeInfo, String> toOfferType = (t) -> {
Expand Down
3 changes: 0 additions & 3 deletions core/src/main/java/haveno/core/api/CoreTradesService.java
Original file line number Diff line number Diff line change
Expand Up @@ -143,18 +143,15 @@ void takeOffer(Offer offer,
}

// synchronize access to take offer model // TODO (woodser): to avoid synchronizing, don't use stateful model
BigInteger takerFee;
BigInteger fundsNeededForTrade;
synchronized (takeOfferModel) {
takeOfferModel.initModel(offer, paymentAccount, amount, useSavingsWallet);
takerFee = takeOfferModel.getTakerFee();
fundsNeededForTrade = takeOfferModel.getFundsNeededForTrade();
log.debug("Initiating take {} offer, {}", offer.isBuyOffer() ? "buy" : "sell", takeOfferModel);
}

// take offer
tradeManager.onTakeOffer(amount,
takerFee,
fundsNeededForTrade,
offer,
paymentAccountId,
Expand Down
30 changes: 20 additions & 10 deletions core/src/main/java/haveno/core/api/model/OfferInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,9 @@ public class OfferInfo implements Payload {
private final long minAmount;
private final String volume;
private final String minVolume;
private final long makerFee;
@Nullable
private final double makerFeePct;
private final double takerFeePct;
private final double penaltyFeePct;
private final double buyerSecurityDepositPct;
private final double sellerSecurityDepositPct;
private final String triggerPrice;
Expand Down Expand Up @@ -86,11 +87,13 @@ public OfferInfo(OfferInfoBuilder builder) {
this.marketPriceMarginPct = builder.getMarketPriceMarginPct();
this.amount = builder.getAmount();
this.minAmount = builder.getMinAmount();
this.volume = builder.getVolume();
this.minVolume = builder.getMinVolume();
this.makerFee = builder.getMakerFee();
this.makerFeePct = builder.getMakerFeePct();
this.takerFeePct = builder.getTakerFeePct();
this.penaltyFeePct = builder.getPenaltyFeePct();
this.buyerSecurityDepositPct = builder.getBuyerSecurityDepositPct();
this.sellerSecurityDepositPct = builder.getSellerSecurityDepositPct();
this.volume = builder.getVolume();
this.minVolume = builder.getMinVolume();
this.triggerPrice = builder.getTriggerPrice();
this.paymentAccountId = builder.getPaymentAccountId();
this.paymentMethodId = builder.getPaymentMethodId();
Expand Down Expand Up @@ -155,11 +158,14 @@ private static OfferInfoBuilder getBuilder(Offer offer) {
.withMarketPriceMarginPct(marketPriceMarginAsPctLiteral)
.withAmount(offer.getAmount().longValueExact())
.withMinAmount(offer.getMinAmount().longValueExact())
.withVolume(roundedVolume)
.withMinVolume(roundedMinVolume)
.withMakerFee(offer.getMakerFee().longValueExact())
.withMakerFeePct(offer.getMakerFeePct())
.withTakerFeePct(offer.getTakerFeePct())
.withPenaltyFeePct(offer.getPenaltyFeePct())
.withSellerSecurityDepositPct(offer.getSellerSecurityDepositPct())
.withBuyerSecurityDepositPct(offer.getBuyerSecurityDepositPct())
.withSellerSecurityDepositPct(offer.getSellerSecurityDepositPct())
.withVolume(roundedVolume)
.withMinVolume(roundedMinVolume)
.withPaymentAccountId(offer.getMakerPaymentAccountId())
.withPaymentMethodId(offer.getPaymentMethod().getId())
.withPaymentMethodShortName(offer.getPaymentMethod().getShortName())
Expand Down Expand Up @@ -190,7 +196,9 @@ public haveno.proto.grpc.OfferInfo toProtoMessage() {
.setMinAmount(minAmount)
.setVolume(volume)
.setMinVolume(minVolume)
.setMakerFee(makerFee)
.setMakerFeePct(makerFeePct)
.setTakerFeePct(takerFeePct)
.setPenaltyFeePct(penaltyFeePct)
.setBuyerSecurityDepositPct(buyerSecurityDepositPct)
.setSellerSecurityDepositPct(sellerSecurityDepositPct)
.setTriggerPrice(triggerPrice == null ? "0" : triggerPrice)
Expand Down Expand Up @@ -225,7 +233,9 @@ public static OfferInfo fromProto(haveno.proto.grpc.OfferInfo proto) {
.withMinAmount(proto.getMinAmount())
.withVolume(proto.getVolume())
.withMinVolume(proto.getMinVolume())
.withMakerFee(proto.getMakerFee())
.withMakerFeePct(proto.getMakerFeePct())
.withTakerFeePct(proto.getTakerFeePct())
.withPenaltyFeePct(proto.getPenaltyFeePct())
.withBuyerSecurityDepositPct(proto.getBuyerSecurityDepositPct())
.withSellerSecurityDepositPct(proto.getSellerSecurityDepositPct())
.withTriggerPrice(proto.getTriggerPrice())
Expand Down
18 changes: 12 additions & 6 deletions core/src/main/java/haveno/core/api/model/TradeInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,12 @@ public class TradeInfo implements Payload {
private final String shortId;
private final long date;
private final String role;
private final long takerFee;
private final String makerDepositTxId;
private final String takerDepositTxId;
private final String payoutTxId;
private final long amount;
private final long makerFee;
private final long takerFee;
private final long buyerSecurityDeposit;
private final long sellerSecurityDeposit;
private final long buyerDepositTxFee;
Expand Down Expand Up @@ -104,11 +105,12 @@ public TradeInfo(TradeInfoV1Builder builder) {
this.shortId = builder.getShortId();
this.date = builder.getDate();
this.role = builder.getRole();
this.takerFee = builder.getTakerFee();
this.makerDepositTxId = builder.getMakerDepositTxId();
this.takerDepositTxId = builder.getTakerDepositTxId();
this.payoutTxId = builder.getPayoutTxId();
this.amount = builder.getAmount();
this.makerFee = builder.getMakerFee();
this.takerFee = builder.getTakerFee();
this.buyerSecurityDeposit = builder.getBuyerSecurityDeposit();
this.sellerSecurityDeposit = builder.getSellerSecurityDeposit();
this.buyerDepositTxFee = builder.getBuyerDepositTxFee();
Expand Down Expand Up @@ -166,11 +168,12 @@ public static TradeInfo toTradeInfo(Trade trade, String role) {
.withShortId(trade.getShortId())
.withDate(trade.getDate().getTime())
.withRole(role == null ? "" : role)
.withTakerFee(trade.getTakerFee().longValueExact())
.withMakerDepositTxId(trade.getMaker().getDepositTxHash())
.withTakerDepositTxId(trade.getTaker().getDepositTxHash())
.withPayoutTxId(trade.getPayoutTxId())
.withAmount(trade.getAmount().longValueExact())
.withMakerFee(trade.getMakerFee().longValueExact())
.withTakerFee(trade.getTakerFee().longValueExact())
.withBuyerSecurityDeposit(trade.getBuyer().getSecurityDeposit() == null ? -1 : trade.getBuyer().getSecurityDeposit().longValueExact())
.withSellerSecurityDeposit(trade.getSeller().getSecurityDeposit() == null ? -1 : trade.getSeller().getSecurityDeposit().longValueExact())
.withBuyerDepositTxFee(trade.getBuyer().getDepositTxFee() == null ? -1 : trade.getBuyer().getDepositTxFee().longValueExact())
Expand Down Expand Up @@ -216,11 +219,12 @@ public haveno.proto.grpc.TradeInfo toProtoMessage() {
.setShortId(shortId)
.setDate(date)
.setRole(role)
.setTakerFee(takerFee)
.setMakerDepositTxId(makerDepositTxId == null ? "" : makerDepositTxId)
.setTakerDepositTxId(takerDepositTxId == null ? "" : takerDepositTxId)
.setPayoutTxId(payoutTxId == null ? "" : payoutTxId)
.setAmount(amount)
.setMakerFee(makerFee)
.setTakerFee(takerFee)
.setBuyerSecurityDeposit(buyerSecurityDeposit)
.setSellerSecurityDeposit(sellerSecurityDeposit)
.setBuyerDepositTxFee(buyerDepositTxFee)
Expand Down Expand Up @@ -259,11 +263,12 @@ public static TradeInfo fromProto(haveno.proto.grpc.TradeInfo proto) {
.withShortId(proto.getShortId())
.withDate(proto.getDate())
.withRole(proto.getRole())
.withTakerFee(proto.getTakerFee())
.withMakerDepositTxId(proto.getMakerDepositTxId())
.withTakerDepositTxId(proto.getTakerDepositTxId())
.withPayoutTxId(proto.getPayoutTxId())
.withAmount(proto.getAmount())
.withMakerFee(proto.getMakerFee())
.withTakerFee(proto.getTakerFee())
.withBuyerSecurityDeposit(proto.getBuyerSecurityDeposit())
.withSellerSecurityDeposit(proto.getSellerSecurityDeposit())
.withBuyerDepositTxFee(proto.getBuyerDepositTxFee())
Expand Down Expand Up @@ -302,11 +307,12 @@ public String toString() {
", shortId='" + shortId + '\'' + "\n" +
", date='" + date + '\'' + "\n" +
", role='" + role + '\'' + "\n" +
", takerFee='" + takerFee + '\'' + "\n" +
", makerDepositTxId='" + makerDepositTxId + '\'' + "\n" +
", takerDepositTxId='" + takerDepositTxId + '\'' + "\n" +
", payoutTxId='" + payoutTxId + '\'' + "\n" +
", amount='" + amount + '\'' + "\n" +
", makerFee='" + makerFee + '\'' + "\n" +
", takerFee='" + takerFee + '\'' + "\n" +
", buyerSecurityDeposit='" + buyerSecurityDeposit + '\'' + "\n" +
", sellerSecurityDeposit='" + sellerSecurityDeposit + '\'' + "\n" +
", buyerDepositTxFee='" + buyerDepositTxFee + '\'' + "\n" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ public final class OfferInfoBuilder {
private long minAmount;
private String volume;
private String minVolume;
private long makerFee;
private double makerFeePct;
private double takerFeePct;
private double penaltyFeePct;
private double buyerSecurityDepositPct;
private double sellerSecurityDepositPct;
private String triggerPrice;
Expand Down Expand Up @@ -97,18 +99,18 @@ public OfferInfoBuilder withMinAmount(long minAmount) {
return this;
}

public OfferInfoBuilder withVolume(String volume) {
this.volume = volume;
public OfferInfoBuilder withMakerFeePct(double makerFeePct) {
this.makerFeePct = makerFeePct;
return this;
}

public OfferInfoBuilder withMinVolume(String minVolume) {
this.minVolume = minVolume;
public OfferInfoBuilder withTakerFeePct(double takerFeePct) {
this.takerFeePct = takerFeePct;
return this;
}

public OfferInfoBuilder withMakerFee(long makerFee) {
this.makerFee = makerFee;
public OfferInfoBuilder withPenaltyFeePct(double penaltyFeePct) {
this.penaltyFeePct = penaltyFeePct;
return this;
}

Expand All @@ -122,6 +124,16 @@ public OfferInfoBuilder withSellerSecurityDepositPct(double sellerSecurityDeposi
return this;
}

public OfferInfoBuilder withVolume(String volume) {
this.volume = volume;
return this;
}

public OfferInfoBuilder withMinVolume(String minVolume) {
this.minVolume = minVolume;
return this;
}

public OfferInfoBuilder withTriggerPrice(String triggerPrice) {
this.triggerPrice = triggerPrice;
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public final class TradeInfoV1Builder {
private String role;
private boolean isCurrencyForTakerFeeBtc;
private long totalTxFee;
private long makerFee;
private long takerFee;
private long buyerSecurityDeposit;
private long sellerSecurityDeposit;
Expand Down Expand Up @@ -108,6 +109,11 @@ public TradeInfoV1Builder withTotalTxFee(long totalTxFee) {
return this;
}

public TradeInfoV1Builder withMakerFee(long makerFee) {
this.makerFee = makerFee;
return this;
}

public TradeInfoV1Builder withTakerFee(long takerFee) {
this.takerFee = takerFee;
return this;
Expand Down
12 changes: 6 additions & 6 deletions core/src/main/java/haveno/core/offer/CreateOfferService.java
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,6 @@ public Offer createAndGetOffer(String offerId,
List<String> acceptedCountryCodes = PaymentAccountUtil.getAcceptedCountryCodes(paymentAccount);
String bankId = PaymentAccountUtil.getBankId(paymentAccount);
List<String> acceptedBanks = PaymentAccountUtil.getAcceptedBanks(paymentAccount);
BigInteger makerFee = HavenoUtils.getMakerFee(amount);
long maxTradePeriod = paymentAccount.getMaxTradePeriod();

// reserved for future use cases
Expand All @@ -178,8 +177,7 @@ public Offer createAndGetOffer(String offerId,
offerUtil.validateOfferData(
securityDepositAsDouble,
paymentAccount,
currencyCode,
makerFee);
currencyCode);

OfferPayload offerPayload = new OfferPayload(offerId,
creationTime,
Expand All @@ -191,6 +189,11 @@ public Offer createAndGetOffer(String offerId,
useMarketBasedPriceValue,
amountAsLong,
minAmountAsLong,
HavenoUtils.MAKER_FEE_PCT,
HavenoUtils.TAKER_FEE_PCT,
HavenoUtils.PENALTY_FEE_PCT,
securityDepositAsDouble,
securityDepositAsDouble,
baseCurrencyCode,
counterCurrencyCode,
paymentAccount.getPaymentMethod().getId(),
Expand All @@ -201,9 +204,6 @@ public Offer createAndGetOffer(String offerId,
acceptedBanks,
Version.VERSION,
xmrWalletService.getWallet().getHeight(),
makerFee.longValueExact(),
securityDepositAsDouble,
securityDepositAsDouble,
maxTradeLimit,
maxTradePeriod,
useAutoClose,
Expand Down
27 changes: 24 additions & 3 deletions core/src/main/java/haveno/core/offer/Offer.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import haveno.core.payment.payload.PaymentMethod;
import haveno.core.provider.price.MarketPrice;
import haveno.core.provider.price.PriceFeedService;
import haveno.core.trade.HavenoUtils;
import haveno.core.util.VolumeUtil;
import haveno.network.p2p.NodeAddress;
import javafx.beans.property.ObjectProperty;
Expand Down Expand Up @@ -285,12 +286,12 @@ public void setErrorMessage(String errorMessage) {
public BigInteger getReserveAmount() {
BigInteger reserveAmount = getDirection() == OfferDirection.BUY ? getMaxBuyerSecurityDeposit() : getMaxSellerSecurityDeposit();
if (getDirection() == OfferDirection.SELL) reserveAmount = reserveAmount.add(getAmount());
reserveAmount = reserveAmount.add(getMakerFee());
reserveAmount = reserveAmount.add(getMaxMakerFee());
return reserveAmount;
}

public BigInteger getMakerFee() {
return BigInteger.valueOf(offerPayload.getMakerFee());
public BigInteger getMaxMakerFee() {
return offerPayload.getMaxMakerFee();
}

public BigInteger getMaxBuyerSecurityDeposit() {
Expand All @@ -301,6 +302,26 @@ public BigInteger getMaxSellerSecurityDeposit() {
return offerPayload.getMaxSellerSecurityDeposit();
}

public double getMakerFeePct() {
return offerPayload.getMakerFeePct();
}

public double getTakerFeePct() {
return offerPayload.getTakerFeePct();
}

public double getPenaltyFeePct() {
return offerPayload.getPenaltyFeePct();
}

public BigInteger getMakerFee(BigInteger tradeAmount) {
return HavenoUtils.multiply(tradeAmount, getMakerFeePct());
}

public BigInteger getTakerFee(BigInteger tradeAmount) {
return HavenoUtils.multiply(tradeAmount, getTakerFeePct());
}

public double getBuyerSecurityDepositPct() {
return offerPayload.getBuyerSecurityDepositPct();
}
Expand Down
Loading

0 comments on commit 7d76604

Please sign in to comment.