put trade price into trade and contract

This commit is contained in:
Manfred Karrer 2016-04-15 02:07:58 +02:00
parent 13a62b1342
commit bc07df2d7a
29 changed files with 139 additions and 51 deletions

View file

@ -43,8 +43,8 @@ public final class BuyerAsTakerTrade extends BuyerTrade implements TakerTrade {
// Constructor, initialization
///////////////////////////////////////////////////////////////////////////////////////////
public BuyerAsTakerTrade(Offer offer, Coin tradeAmount, NodeAddress tradingPeerNodeAddress, Storage<? extends TradableList> storage) {
super(offer, tradeAmount, tradingPeerNodeAddress, storage);
public BuyerAsTakerTrade(Offer offer, Coin tradeAmount, long tradePrice, NodeAddress tradingPeerNodeAddress, Storage<? extends TradableList> storage) {
super(offer, tradeAmount, tradePrice, tradingPeerNodeAddress, storage);
}
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {

View file

@ -38,8 +38,8 @@ public abstract class BuyerTrade extends Trade {
private static final Logger log = LoggerFactory.getLogger(BuyerAsOffererTrade.class);
BuyerTrade(Offer offer, Coin tradeAmount, NodeAddress tradingPeerNodeAddress, Storage<? extends TradableList> storage) {
super(offer, tradeAmount, tradingPeerNodeAddress, storage);
BuyerTrade(Offer offer, Coin tradeAmount, long tradePrice, NodeAddress tradingPeerNodeAddress, Storage<? extends TradableList> storage) {
super(offer, tradeAmount, tradePrice, tradingPeerNodeAddress, storage);
}
BuyerTrade(Offer offer, Storage<? extends TradableList> storage) {
@ -65,7 +65,7 @@ public abstract class BuyerTrade extends Trade {
log::warn);
}
}
@Override
public Coin getPayoutAmount() {
checkNotNull(getTradeAmount(), "Invalid state: getTradeAmount() = null");

View file

@ -25,6 +25,7 @@ import io.bitsquare.p2p.NodeAddress;
import io.bitsquare.payment.PaymentAccountContractData;
import io.bitsquare.trade.offer.Offer;
import org.bitcoinj.core.Coin;
import org.bitcoinj.utils.Fiat;
import javax.annotation.concurrent.Immutable;
import java.util.Arrays;
@ -40,6 +41,7 @@ public final class Contract implements Payload {
public final Offer offer;
private final long tradeAmount;
private final long tradePrice;
public final String takeOfferFeeTxID;
public final NodeAddress arbitratorNodeAddress;
private final boolean isBuyerOffererAndSellerTaker;
@ -64,6 +66,7 @@ public final class Contract implements Payload {
public Contract(Offer offer,
Coin tradeAmount,
Fiat tradePrice,
String takeOfferFeeTxID,
NodeAddress buyerNodeAddress,
NodeAddress sellerNodeAddress,
@ -80,6 +83,7 @@ public final class Contract implements Payload {
byte[] offererBtcPubKey,
byte[] takerBtcPubKey) {
this.offer = offer;
this.tradePrice = tradePrice.value;
this.buyerNodeAddress = buyerNodeAddress;
this.sellerNodeAddress = sellerNodeAddress;
this.tradeAmount = tradeAmount.value;
@ -154,6 +158,10 @@ public final class Contract implements Payload {
return Coin.valueOf(tradeAmount);
}
public Fiat getTradePrice() {
return Fiat.valueOf(offer.getCurrencyCode(), tradePrice);
}
public NodeAddress getBuyerNodeAddress() {
return buyerNodeAddress;
}
@ -171,6 +179,7 @@ public final class Contract implements Payload {
Contract contract = (Contract) o;
if (tradeAmount != contract.tradeAmount) return false;
if (tradePrice != contract.tradePrice) return false;
if (isBuyerOffererAndSellerTaker != contract.isBuyerOffererAndSellerTaker) return false;
if (offer != null ? !offer.equals(contract.offer) : contract.offer != null) return false;
if (takeOfferFeeTxID != null ? !takeOfferFeeTxID.equals(contract.takeOfferFeeTxID) : contract.takeOfferFeeTxID != null)
@ -206,6 +215,7 @@ public final class Contract implements Payload {
public int hashCode() {
int result = offer != null ? offer.hashCode() : 0;
result = 31 * result + (int) (tradeAmount ^ (tradeAmount >>> 32));
result = 31 * result + (int) (tradePrice ^ (tradePrice >>> 32));
result = 31 * result + (takeOfferFeeTxID != null ? takeOfferFeeTxID.hashCode() : 0);
result = 31 * result + (arbitratorNodeAddress != null ? arbitratorNodeAddress.hashCode() : 0);
result = 31 * result + (isBuyerOffererAndSellerTaker ? 1 : 0);
@ -229,6 +239,7 @@ public final class Contract implements Payload {
return "Contract{" +
"\n\toffer=" + offer +
"\n\ttradeAmount=" + tradeAmount +
"\n\ttradePrice=" + tradePrice +
"\n\ttakeOfferFeeTxID='" + takeOfferFeeTxID + '\'' +
"\n\tarbitratorAddress=" + arbitratorNodeAddress +
"\n\tisBuyerOffererAndSellerTaker=" + isBuyerOffererAndSellerTaker +

View file

@ -42,8 +42,8 @@ public final class SellerAsTakerTrade extends SellerTrade implements TakerTrade
// Constructor, initialization
///////////////////////////////////////////////////////////////////////////////////////////
public SellerAsTakerTrade(Offer offer, Coin tradeAmount, NodeAddress tradingPeerNodeAddress, Storage<? extends TradableList> storage) {
super(offer, tradeAmount, tradingPeerNodeAddress, storage);
public SellerAsTakerTrade(Offer offer, Coin tradeAmount, long tradePrice, NodeAddress tradingPeerNodeAddress, Storage<? extends TradableList> storage) {
super(offer, tradeAmount, tradePrice, tradingPeerNodeAddress, storage);
}
private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException {

View file

@ -37,8 +37,8 @@ public abstract class SellerTrade extends Trade {
private static final Logger log = LoggerFactory.getLogger(BuyerAsTakerTrade.class);
SellerTrade(Offer offer, Coin tradeAmount, NodeAddress tradingPeerNodeAddress, Storage<? extends TradableList> storage) {
super(offer, tradeAmount, tradingPeerNodeAddress, storage);
SellerTrade(Offer offer, Coin tradeAmount, long tradePrice, NodeAddress tradingPeerNodeAddress, Storage<? extends TradableList> storage) {
super(offer, tradeAmount, tradePrice, tradingPeerNodeAddress, storage);
}
SellerTrade(Offer offer, Storage<? extends TradableList> storage) {
@ -69,7 +69,7 @@ public abstract class SellerTrade extends Trade {
public Coin getPayoutAmount() {
return FeePolicy.getSecurityDeposit();
}
///////////////////////////////////////////////////////////////////////////////////////////
// Setter for Mutable objects
///////////////////////////////////////////////////////////////////////////////////////////

View file

@ -41,6 +41,7 @@ import javafx.beans.property.*;
import org.bitcoinj.core.Coin;
import org.bitcoinj.core.Transaction;
import org.bitcoinj.core.TransactionConfidence;
import org.bitcoinj.utils.ExchangeRate;
import org.bitcoinj.utils.Fiat;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
@ -167,6 +168,7 @@ public abstract class Trade implements Tradable, Model {
transient private ObjectProperty<Fiat> tradeVolumeProperty;
@Nullable
private String takeOfferFeeTxId;
private long tradePrice;
///////////////////////////////////////////////////////////////////////////////////////////
@ -189,11 +191,12 @@ public abstract class Trade implements Tradable, Model {
}
// taker
protected Trade(Offer offer, Coin tradeAmount, NodeAddress tradingPeerNodeAddress,
protected Trade(Offer offer, Coin tradeAmount, long tradePrice, NodeAddress tradingPeerNodeAddress,
Storage<? extends TradableList> storage) {
this(offer, storage);
this.tradeAmount = tradeAmount;
this.tradePrice = tradePrice;
this.tradingPeerNodeAddress = tradingPeerNodeAddress;
tradeAmountProperty.set(tradeAmount);
tradeVolumeProperty.set(getTradeVolume());
@ -381,8 +384,8 @@ public abstract class Trade implements Tradable, Model {
@Nullable
public Fiat getTradeVolume() {
if (tradeAmount != null)
return offer.getVolumeByAmount(tradeAmount);
if (tradeAmount != null && getTradePrice() != null)
return new ExchangeRate(getTradePrice()).coinToFiat(tradeAmount);
else
return null;
}
@ -452,6 +455,14 @@ public abstract class Trade implements Tradable, Model {
tradeVolumeProperty.set(getTradeVolume());
}
public void setTradePrice(long tradePrice) {
this.tradePrice = tradePrice;
}
public Fiat getTradePrice() {
return Fiat.valueOf(offer.getCurrencyCode(), tradePrice);
}
@Nullable
public Coin getTradeAmount() {
return tradeAmount;

View file

@ -113,7 +113,7 @@ public class TradeManager {
tradableListStorage = new Storage<>(storageDir);
trades = new TradableList<>(tradableListStorage, "PendingTrades");
trades.forEach(e -> e.getOffer().setPriceFeed(priceFeed));
p2PService.addDecryptedDirectMessageListener(new DecryptedDirectMessageListener() {
@Override
public void onDirectMessage(DecryptedMsgWithPubKey decryptedMsgWithPubKey, NodeAddress peerNodeAddress) {
@ -264,6 +264,7 @@ public class TradeManager {
// First we check if offer is still available then we create the trade with the protocol
public void onTakeOffer(Coin amount,
long tradePrice,
Coin fundsNeededForTrade,
Offer offer,
String paymentAccountId,
@ -273,11 +274,12 @@ public class TradeManager {
offer.checkOfferAvailability(model,
() -> {
if (offer.getState() == Offer.State.AVAILABLE)
createTrade(amount, fundsNeededForTrade, offer, paymentAccountId, useSavingsWallet, model, tradeResultHandler);
createTrade(amount, tradePrice, fundsNeededForTrade, offer, paymentAccountId, useSavingsWallet, model, tradeResultHandler);
});
}
private void createTrade(Coin amount,
long tradePrice,
Coin fundsNeededForTrade,
Offer offer,
String paymentAccountId,
@ -286,9 +288,9 @@ public class TradeManager {
TradeResultHandler tradeResultHandler) {
Trade trade;
if (offer.getDirection() == Offer.Direction.BUY)
trade = new SellerAsTakerTrade(offer, amount, model.getPeerNodeAddress(), tradableListStorage);
trade = new SellerAsTakerTrade(offer, amount, tradePrice, model.getPeerNodeAddress(), tradableListStorage);
else
trade = new BuyerAsTakerTrade(offer, amount, model.getPeerNodeAddress(), tradableListStorage);
trade = new BuyerAsTakerTrade(offer, amount, tradePrice, model.getPeerNodeAddress(), tradableListStorage);
trade.setTakerPaymentAccountId(paymentAccountId);

View file

@ -229,7 +229,7 @@ public final class Offer implements StoragePayload, RequiresOwnerIsOnlinePayload
public Fiat getVolumeByAmount(Coin amount) {
if (fiatPrice != 0 && amount != null && !amount.isZero())
return new ExchangeRate(Fiat.valueOf(currencyCode, fiatPrice)).coinToFiat(amount);
return new ExchangeRate(getPrice()).coinToFiat(amount);
else
return null;
}
@ -335,6 +335,13 @@ public final class Offer implements StoragePayload, RequiresOwnerIsOnlinePayload
double marketPriceAsDouble = marketPrice.getPrice(priceFeedType);
double factor = direction == Offer.Direction.BUY ? 1 - marketPriceMargin : 1 + marketPriceMargin;
double targetPrice = marketPriceAsDouble * factor;
// round
long factor1 = (long) Math.pow(10, 2);
targetPrice = targetPrice * factor1;
long tmp = Math.round(targetPrice);
targetPrice = (double) tmp / factor1;
try {
return Fiat.parseFiat(currencyCode, String.valueOf(targetPrice));
} catch (Exception e) {

View file

@ -35,6 +35,7 @@ public final class PayDepositRequest extends TradeMessage implements MailboxMess
private static final long serialVersionUID = Version.P2P_NETWORK_VERSION;
public final long tradeAmount;
public final long tradePrice;
public final byte[] takerMultiSigPubKey;
public final ArrayList<RawTransactionInput> rawTransactionInputs;
public final long changeOutputValue;
@ -52,6 +53,7 @@ public final class PayDepositRequest extends TradeMessage implements MailboxMess
public PayDepositRequest(NodeAddress senderNodeAddress,
String tradeId,
long tradeAmount,
long tradePrice,
ArrayList<RawTransactionInput> rawTransactionInputs,
long changeOutputValue,
String changeOutputAddress,
@ -66,6 +68,7 @@ public final class PayDepositRequest extends TradeMessage implements MailboxMess
super(tradeId);
this.senderNodeAddress = senderNodeAddress;
this.tradeAmount = tradeAmount;
this.tradePrice = tradePrice;
this.rawTransactionInputs = rawTransactionInputs;
this.changeOutputValue = changeOutputValue;
this.changeOutputAddress = changeOutputAddress;

View file

@ -60,6 +60,7 @@ public class CreateAndSignContract extends TradeTask {
Contract contract = new Contract(
processModel.getOffer(),
trade.getTradeAmount(),
trade.getTradePrice(),
trade.getTakeOfferFeeTxId(),
buyerNodeAddress,
sellerNodeAddress,

View file

@ -77,13 +77,14 @@ public class ProcessPayDepositRequest extends TradeTask {
failed("acceptedArbitratorNames size must be at least 1");
trade.setArbitratorNodeAddress(checkNotNull(payDepositRequest.arbitratorNodeAddress));
checkArgument(payDepositRequest.tradeAmount > 0);
trade.setTradePrice(payDepositRequest.tradePrice);
trade.setTradeAmount(Coin.valueOf(payDepositRequest.tradeAmount));
// update to the latest peer address of our peer if the payDepositRequest is correct
trade.setTradingPeerNodeAddress(processModel.getTempTradingPeerNodeAddress());
removeMailboxMessageAfterProcessing();
complete();
} catch (Throwable t) {
failed(t);

View file

@ -49,6 +49,7 @@ public class SendPayDepositRequest extends TradeTask {
processModel.getMyAddress(),
processModel.getId(),
trade.getTradeAmount().value,
trade.getTradePrice().value,
processModel.getRawTransactionInputs(),
processModel.getChangeOutputValue(),
processModel.getChangeOutputAddress(),

View file

@ -61,6 +61,7 @@ public class VerifyAndSignContract extends TradeTask {
Contract contract = new Contract(
processModel.getOffer(),
trade.getTradeAmount(),
trade.getTradePrice(),
trade.getTakeOfferFeeTxId(),
buyerNodeAddress,
sellerNodeAddress,