Add payout fields to trade proto msgs, show optional trade/tx fees in swaps

This commit is contained in:
ghubstan 2022-01-23 17:03:43 -03:00
parent fadaaf9151
commit 0e020e8987
No known key found for this signature in database
GPG Key ID: E35592D6800A861E
4 changed files with 56 additions and 4 deletions

View File

@ -25,6 +25,9 @@ import bisq.common.Payload;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import static bisq.core.offer.OfferDirection.BUY;
import static bisq.core.offer.OfferDirection.SELL;
@EqualsAndHashCode
@Getter
public class BsqSwapTradeInfo implements Payload {
@ -41,6 +44,8 @@ public class BsqSwapTradeInfo implements Payload {
private final String takerBtcAddress;
private final long numConfirmations;
private final String errorMessage;
private final long payout;
private final long swapPeerPayout;
public BsqSwapTradeInfo(BsqSwapTradeInfoBuilder builder) {
this.txId = builder.getTxId();
@ -55,6 +60,8 @@ public class BsqSwapTradeInfo implements Payload {
this.takerBtcAddress = builder.getTakerBtcAddress();
this.numConfirmations = builder.getNumConfirmations();
this.errorMessage = builder.getErrorMessage();
this.payout = builder.getPayout();
this.swapPeerPayout = builder.getSwapPeerPayout();
}
public static BsqSwapTradeInfo toBsqSwapTradeInfo(BsqSwapTrade trade,
@ -66,12 +73,20 @@ public class BsqSwapTradeInfo implements Payload {
var makerBtcAddress = wasMyOffer ? protocolModel.getBtcAddress() : swapPeer.getBtcAddress();
var takerBsqAddress = wasMyOffer ? swapPeer.getBsqAddress() : protocolModel.getBsqAddress();
var takerBtcAddress = wasMyOffer ? swapPeer.getBtcAddress() : protocolModel.getBtcAddress();
// A BSQ Swap trade fee is paid in full by the BTC buyer (selling BSQ).
// The transferred BSQ (payout) is reduced by the fee of the peer.
var makerTradeFee = wasMyOffer && trade.getOffer().getDirection().equals(BUY)
? trade.getMakerFeeAsLong()
: 0L;
var takerTradeFee = !wasMyOffer && trade.getOffer().getDirection().equals(SELL)
? trade.getTakerFeeAsLong()
: 0L;
return new BsqSwapTradeInfoBuilder()
.withTxId(trade.getTxId())
.withBsqTradeAmount(trade.getBsqTradeAmount())
.withBtcTradeAmount(trade.getAmountAsLong())
.withBsqMakerTradeFee(trade.getMakerFeeAsLong())
.withBsqTakerTradeFee(trade.getTakerFeeAsLong())
.withBsqMakerTradeFee(makerTradeFee)
.withBsqTakerTradeFee(takerTradeFee)
.withTxFeePerVbyte(trade.getTxFeePerVbyte())
.withMakerBsqAddress(makerBsqAddress)
.withMakerBtcAddress(makerBtcAddress)
@ -79,6 +94,8 @@ public class BsqSwapTradeInfo implements Payload {
.withTakerBtcAddress(takerBtcAddress)
.withNumConfirmations(numConfirmations)
.withErrorMessage(trade.getErrorMessage())
.withPayout(protocolModel.getPayout())
.withSwapPeerPayout(protocolModel.getTradePeer().getPayout())
.build();
}
@ -101,6 +118,9 @@ public class BsqSwapTradeInfo implements Payload {
.setTakerBtcAddress(takerBtcAddress != null ? takerBtcAddress : "")
.setTakerBtcAddress(takerBtcAddress != null ? takerBtcAddress : "")
.setNumConfirmations(numConfirmations)
.setErrorMessage(errorMessage != null ? errorMessage : "")
.setPayout(payout)
.setSwapPeerPayout(swapPeerPayout)
.build();
}
@ -118,6 +138,8 @@ public class BsqSwapTradeInfo implements Payload {
.withTakerBtcAddress(proto.getTakerBtcAddress())
.withNumConfirmations(proto.getNumConfirmations())
.withErrorMessage(proto.getErrorMessage())
.withPayout(proto.getPayout())
.withSwapPeerPayout(proto.getSwapPeerPayout())
.build();
}
@ -136,6 +158,8 @@ public class BsqSwapTradeInfo implements Payload {
", takerBtcAddress='" + takerBtcAddress + '\'' +
", numConfirmations='" + numConfirmations + '\'' +
", errorMessage='" + errorMessage + '\'' +
", payout=" + payout +
", swapPeerPayout=" + swapPeerPayout +
'}';
}
}

View File

@ -32,6 +32,8 @@ import static bisq.core.api.model.BsqSwapTradeInfo.toBsqSwapTradeInfo;
import static bisq.core.api.model.OfferInfo.toMyOfferInfo;
import static bisq.core.api.model.OfferInfo.toOfferInfo;
import static bisq.core.api.model.PaymentAccountPayloadInfo.toPaymentAccountPayloadInfo;
import static bisq.core.offer.OfferDirection.BUY;
import static bisq.core.offer.OfferDirection.SELL;
import static java.util.Objects.requireNonNull;
@EqualsAndHashCode
@ -132,6 +134,18 @@ public class TradeInfo implements Payload {
int numConfirmations,
String closingStatus) {
OfferInfo offerInfo = isMyOffer ? toMyOfferInfo(bsqSwapTrade.getOffer()) : toOfferInfo(bsqSwapTrade.getOffer());
// A BSQ Swap miner tx fee is paid in full by the BTC seller (buying BSQ).
// The BTC buyer's payout = tradeamount minus his share of miner fee.
var iAmBtcSeller = (isMyOffer && bsqSwapTrade.getOffer().getDirection().equals(SELL))
|| (!isMyOffer && bsqSwapTrade.getOffer().getDirection().equals(BUY));
var txFeeInBtc = iAmBtcSeller
? bsqSwapTrade.getTxFee().value
: 0L;
// A BSQ Swap trade fee is paid in full by the BTC buyer (selling BSQ).
// The transferred BSQ (payout) is reduced by the peer's trade fee.
var takerFeeInBsq = !isMyOffer && bsqSwapTrade.getOffer().getDirection().equals(SELL)
? bsqSwapTrade.getTakerFeeAsLong()
: 0L;
TradeInfo tradeInfo = new TradeInfoV1Builder()
.withOffer(offerInfo)
.withTradeId(bsqSwapTrade.getId())
@ -139,8 +153,8 @@ public class TradeInfo implements Payload {
.withDate(bsqSwapTrade.getDate().getTime())
.withRole(role == null ? "" : role)
.withIsCurrencyForTakerFeeBtc(false) // BSQ Swap fees always paid in BSQ.
.withTxFeeAsLong(bsqSwapTrade.getBsqSwapProtocolModel().getTxFee())
.withTakerFeeAsLong(bsqSwapTrade.getTakerFeeAsLong())
.withTxFeeAsLong(txFeeInBtc)
.withTakerFeeAsLong(takerFeeInBsq)
// N/A for bsq-swaps: .withTakerFeeTxId(""), .withDepositTxId(""), .withPayoutTxId("")
.withTradeAmountAsLong(bsqSwapTrade.getAmountAsLong())
.withTradePrice(bsqSwapTrade.getPrice().getValue())

View File

@ -45,6 +45,8 @@ public final class BsqSwapTradeInfoBuilder {
private String takerBtcAddress;
private long numConfirmations;
private String errorMessage;
private long payout;
private long swapPeerPayout;
public BsqSwapTradeInfoBuilder withTxId(String txId) {
this.txId = txId;
@ -106,6 +108,16 @@ public final class BsqSwapTradeInfoBuilder {
return this;
}
public BsqSwapTradeInfoBuilder withPayout(long payout) {
this.payout = payout;
return this;
}
public BsqSwapTradeInfoBuilder withSwapPeerPayout(long swapPeerPayout) {
this.swapPeerPayout = swapPeerPayout;
return this;
}
public BsqSwapTradeInfo build() {
return new BsqSwapTradeInfo(this);
}

View File

@ -549,6 +549,8 @@ message BsqSwapTradeInfo {
string takerBtcAddress = 10;
uint64 numConfirmations = 11;
string errorMessage = 12;
uint64 payout = 13;
uint64 swapPeerPayout = 14;
}
message PaymentAccountPayloadInfo {