org.bitcoinj.utils.ExchangeRate Java Examples

The following examples show how to use org.bitcoinj.utils.ExchangeRate. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: WalletTest.java    From green_android with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void sendRequestExchangeRate() throws Exception {
    receiveATransaction(wallet, myAddress);
    SendRequest sendRequest = SendRequest.to(myAddress, Coin.COIN);
    sendRequest.exchangeRate = new ExchangeRate(Fiat.parseFiat("EUR", "500"));
    wallet.completeTx(sendRequest);
    assertEquals(sendRequest.exchangeRate, sendRequest.tx.getExchangeRate());
}
 
Example #2
Source File: Price.java    From bisq with GNU Affero General Public License v3.0 5 votes vote down vote up
public Coin getAmountByVolume(Volume volume) {
    Monetary monetary = volume.getMonetary();
    if (monetary instanceof Fiat && this.monetary instanceof Fiat)
        return new ExchangeRate((Fiat) this.monetary).fiatToCoin((Fiat) monetary);
    else if (monetary instanceof Altcoin && this.monetary instanceof Altcoin)
        return new AltcoinExchangeRate((Altcoin) this.monetary).altcoinToCoin((Altcoin) monetary);
    else
        return Coin.ZERO;
}
 
Example #3
Source File: Price.java    From bisq with GNU Affero General Public License v3.0 5 votes vote down vote up
public Volume getVolumeByAmount(Coin amount) {
    if (monetary instanceof Fiat)
        return new Volume(new ExchangeRate((Fiat) monetary).coinToFiat(amount));
    else if (monetary instanceof Altcoin)
        return new Volume(new AltcoinExchangeRate((Altcoin) monetary).coinToAltcoin(amount));
    else
        throw new IllegalStateException("Monetary must be either of type Fiat or Altcoin");
}
 
Example #4
Source File: TradeStatistics2.java    From bisq with GNU Affero General Public License v3.0 5 votes vote down vote up
public Volume getTradeVolume() {
    if (getTradePrice().getMonetary() instanceof Altcoin) {
        return new Volume(new AltcoinExchangeRate((Altcoin) getTradePrice().getMonetary()).coinToAltcoin(getTradeAmount()));
    } else {
        Volume volume = new Volume(new ExchangeRate((Fiat) getTradePrice().getMonetary()).coinToFiat(getTradeAmount()));
        return OfferUtil.getRoundedFiatVolume(volume);
    }
}
 
Example #5
Source File: WalletTest.java    From GreenBits with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void sendRequestExchangeRate() throws Exception {
    receiveATransaction(wallet, myAddress);
    SendRequest sendRequest = SendRequest.to(myAddress, Coin.COIN);
    sendRequest.exchangeRate = new ExchangeRate(Fiat.parseFiat("EUR", "500"));
    wallet.completeTx(sendRequest);
    assertEquals(sendRequest.exchangeRate, sendRequest.tx.getExchangeRate());
}
 
Example #6
Source File: Price.java    From bisq-core with GNU Affero General Public License v3.0 5 votes vote down vote up
public Coin getAmountByVolume(Volume volume) {
    Monetary monetary = volume.getMonetary();
    if (monetary instanceof Fiat && this.monetary instanceof Fiat)
        return new ExchangeRate((Fiat) this.monetary).fiatToCoin((Fiat) monetary);
    else if (monetary instanceof Altcoin && this.monetary instanceof Altcoin)
        return new AltcoinExchangeRate((Altcoin) this.monetary).altcoinToCoin((Altcoin) monetary);
    else
        return Coin.ZERO;
}
 
Example #7
Source File: Price.java    From bisq-core with GNU Affero General Public License v3.0 5 votes vote down vote up
public Volume getVolumeByAmount(Coin amount) {
    if (monetary instanceof Fiat)
        return new Volume(new ExchangeRate((Fiat) monetary).coinToFiat(amount));
    else if (monetary instanceof Altcoin)
        return new Volume(new AltcoinExchangeRate((Altcoin) monetary).coinToAltcoin(amount));
    else
        throw new IllegalStateException("Monetary must be either of type Fiat or Altcoin");
}
 
Example #8
Source File: WalletTest.java    From bcm-android with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void sendRequestExchangeRate() throws Exception {
    receiveATransaction(wallet, myAddress);
    SendRequest sendRequest = SendRequest.to(myAddress, Coin.COIN);
    sendRequest.exchangeRate = new ExchangeRate(Fiat.parseFiat("EUR", "500"));
    wallet.completeTx(sendRequest);
    assertEquals(sendRequest.exchangeRate, sendRequest.tx.getExchangeRate());
}
 
Example #9
Source File: WalletProtobufSerializer.java    From green_android with GNU General Public License v3.0 4 votes vote down vote up
private static Protos.Transaction makeTxProto(WalletTransaction wtx) {
    Transaction tx = wtx.getTransaction();
    Protos.Transaction.Builder txBuilder = Protos.Transaction.newBuilder();

    txBuilder.setPool(getProtoPool(wtx))
             .setHash(hashToByteString(tx.getHash()))
             .setVersion((int) tx.getVersion());

    if (tx.getUpdateTime() != null) {
        txBuilder.setUpdatedAt(tx.getUpdateTime().getTime());
    }

    if (tx.getLockTime() > 0) {
        txBuilder.setLockTime((int)tx.getLockTime());
    }

    // Handle inputs.
    for (TransactionInput input : tx.getInputs()) {
        Protos.TransactionInput.Builder inputBuilder = Protos.TransactionInput.newBuilder()
            .setScriptBytes(ByteString.copyFrom(input.getScriptBytes()))
            .setTransactionOutPointHash(hashToByteString(input.getOutpoint().getHash()))
            .setTransactionOutPointIndex((int) input.getOutpoint().getIndex());
        if (input.hasSequence())
            inputBuilder.setSequence((int) input.getSequenceNumber());
        if (input.getValue() != null)
            inputBuilder.setValue(input.getValue().value);
        txBuilder.addTransactionInput(inputBuilder);
    }

    // Handle outputs.
    for (TransactionOutput output : tx.getOutputs()) {
        Protos.TransactionOutput.Builder outputBuilder = Protos.TransactionOutput.newBuilder()
            .setScriptBytes(ByteString.copyFrom(output.getScriptBytes()))
            .setValue(output.getValue().value);
        final TransactionInput spentBy = output.getSpentBy();
        if (spentBy != null) {
            Sha256Hash spendingHash = spentBy.getParentTransaction().getHash();
            int spentByTransactionIndex = spentBy.getParentTransaction().getInputs().indexOf(spentBy);
            outputBuilder.setSpentByTransactionHash(hashToByteString(spendingHash))
                         .setSpentByTransactionIndex(spentByTransactionIndex);
        }
        txBuilder.addTransactionOutput(outputBuilder);
    }

    // Handle which blocks tx was seen in.
    final Map<Sha256Hash, Integer> appearsInHashes = tx.getAppearsInHashes();
    if (appearsInHashes != null) {
        for (Map.Entry<Sha256Hash, Integer> entry : appearsInHashes.entrySet()) {
            txBuilder.addBlockHash(hashToByteString(entry.getKey()));
            txBuilder.addBlockRelativityOffsets(entry.getValue());
        }
    }

    if (tx.hasConfidence()) {
        TransactionConfidence confidence = tx.getConfidence();
        Protos.TransactionConfidence.Builder confidenceBuilder = Protos.TransactionConfidence.newBuilder();
        writeConfidence(txBuilder, confidence, confidenceBuilder);
    }

    Protos.Transaction.Purpose purpose;
    switch (tx.getPurpose()) {
        case UNKNOWN: purpose = Protos.Transaction.Purpose.UNKNOWN; break;
        case USER_PAYMENT: purpose = Protos.Transaction.Purpose.USER_PAYMENT; break;
        case KEY_ROTATION: purpose = Protos.Transaction.Purpose.KEY_ROTATION; break;
        case ASSURANCE_CONTRACT_CLAIM: purpose = Protos.Transaction.Purpose.ASSURANCE_CONTRACT_CLAIM; break;
        case ASSURANCE_CONTRACT_PLEDGE: purpose = Protos.Transaction.Purpose.ASSURANCE_CONTRACT_PLEDGE; break;
        case ASSURANCE_CONTRACT_STUB: purpose = Protos.Transaction.Purpose.ASSURANCE_CONTRACT_STUB; break;
        case RAISE_FEE: purpose = Protos.Transaction.Purpose.RAISE_FEE; break;
        default:
            throw new RuntimeException("New tx purpose serialization not implemented.");
    }
    txBuilder.setPurpose(purpose);

    ExchangeRate exchangeRate = tx.getExchangeRate();
    if (exchangeRate != null) {
        Protos.ExchangeRate.Builder exchangeRateBuilder = Protos.ExchangeRate.newBuilder()
                .setCoinValue(exchangeRate.coin.value).setFiatValue(exchangeRate.fiat.value)
                .setFiatCurrencyCode(exchangeRate.fiat.currencyCode);
        txBuilder.setExchangeRate(exchangeRateBuilder);
    }

    if (tx.getMemo() != null)
        txBuilder.setMemo(tx.getMemo());

    return txBuilder.build();
}
 
Example #10
Source File: Transaction.java    From green_android with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Getter for {@link #exchangeRate}.
 */
@Nullable
public ExchangeRate getExchangeRate() {
    return exchangeRate;
}
 
Example #11
Source File: Transaction.java    From green_android with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Setter for {@link #exchangeRate}.
 */
public void setExchangeRate(ExchangeRate exchangeRate) {
    this.exchangeRate = exchangeRate;
}
 
Example #12
Source File: Transaction.java    From bcm-android with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Getter for {@link #exchangeRate}.
 */
@Nullable
public ExchangeRate getExchangeRate() {
    return exchangeRate;
}
 
Example #13
Source File: GaService.java    From GreenBits with GNU General Public License v3.0 4 votes vote down vote up
public ExchangeRate getFiatRate() {
    final long rate = new BigDecimal(mFiatRate).movePointRight(Fiat.SMALLEST_UNIT_EXPONENT)
                                               .toBigInteger().longValue();
    return new ExchangeRate(Fiat.valueOf("???", rate));
}
 
Example #14
Source File: WalletProtobufSerializer.java    From GreenBits with GNU General Public License v3.0 4 votes vote down vote up
private static Protos.Transaction makeTxProto(WalletTransaction wtx) {
    Transaction tx = wtx.getTransaction();
    Protos.Transaction.Builder txBuilder = Protos.Transaction.newBuilder();

    txBuilder.setPool(getProtoPool(wtx))
             .setHash(hashToByteString(tx.getHash()))
             .setVersion((int) tx.getVersion());

    if (tx.getUpdateTime() != null) {
        txBuilder.setUpdatedAt(tx.getUpdateTime().getTime());
    }

    if (tx.getLockTime() > 0) {
        txBuilder.setLockTime((int)tx.getLockTime());
    }

    // Handle inputs.
    for (TransactionInput input : tx.getInputs()) {
        Protos.TransactionInput.Builder inputBuilder = Protos.TransactionInput.newBuilder()
            .setScriptBytes(ByteString.copyFrom(input.getScriptBytes()))
            .setTransactionOutPointHash(hashToByteString(input.getOutpoint().getHash()))
            .setTransactionOutPointIndex((int) input.getOutpoint().getIndex());
        if (input.hasSequence())
            inputBuilder.setSequence((int) input.getSequenceNumber());
        if (input.getValue() != null)
            inputBuilder.setValue(input.getValue().value);
        txBuilder.addTransactionInput(inputBuilder);
    }

    // Handle outputs.
    for (TransactionOutput output : tx.getOutputs()) {
        Protos.TransactionOutput.Builder outputBuilder = Protos.TransactionOutput.newBuilder()
            .setScriptBytes(ByteString.copyFrom(output.getScriptBytes()))
            .setValue(output.getValue().value);
        final TransactionInput spentBy = output.getSpentBy();
        if (spentBy != null) {
            Sha256Hash spendingHash = spentBy.getParentTransaction().getHash();
            int spentByTransactionIndex = spentBy.getParentTransaction().getInputs().indexOf(spentBy);
            outputBuilder.setSpentByTransactionHash(hashToByteString(spendingHash))
                         .setSpentByTransactionIndex(spentByTransactionIndex);
        }
        txBuilder.addTransactionOutput(outputBuilder);
    }

    // Handle which blocks tx was seen in.
    final Map<Sha256Hash, Integer> appearsInHashes = tx.getAppearsInHashes();
    if (appearsInHashes != null) {
        for (Map.Entry<Sha256Hash, Integer> entry : appearsInHashes.entrySet()) {
            txBuilder.addBlockHash(hashToByteString(entry.getKey()));
            txBuilder.addBlockRelativityOffsets(entry.getValue());
        }
    }

    if (tx.hasConfidence()) {
        TransactionConfidence confidence = tx.getConfidence();
        Protos.TransactionConfidence.Builder confidenceBuilder = Protos.TransactionConfidence.newBuilder();
        writeConfidence(txBuilder, confidence, confidenceBuilder);
    }

    Protos.Transaction.Purpose purpose;
    switch (tx.getPurpose()) {
        case UNKNOWN: purpose = Protos.Transaction.Purpose.UNKNOWN; break;
        case USER_PAYMENT: purpose = Protos.Transaction.Purpose.USER_PAYMENT; break;
        case KEY_ROTATION: purpose = Protos.Transaction.Purpose.KEY_ROTATION; break;
        case ASSURANCE_CONTRACT_CLAIM: purpose = Protos.Transaction.Purpose.ASSURANCE_CONTRACT_CLAIM; break;
        case ASSURANCE_CONTRACT_PLEDGE: purpose = Protos.Transaction.Purpose.ASSURANCE_CONTRACT_PLEDGE; break;
        case ASSURANCE_CONTRACT_STUB: purpose = Protos.Transaction.Purpose.ASSURANCE_CONTRACT_STUB; break;
        case RAISE_FEE: purpose = Protos.Transaction.Purpose.RAISE_FEE; break;
        default:
            throw new RuntimeException("New tx purpose serialization not implemented.");
    }
    txBuilder.setPurpose(purpose);

    ExchangeRate exchangeRate = tx.getExchangeRate();
    if (exchangeRate != null) {
        Protos.ExchangeRate.Builder exchangeRateBuilder = Protos.ExchangeRate.newBuilder()
                .setCoinValue(exchangeRate.coin.value).setFiatValue(exchangeRate.fiat.value)
                .setFiatCurrencyCode(exchangeRate.fiat.currencyCode);
        txBuilder.setExchangeRate(exchangeRateBuilder);
    }

    if (tx.getMemo() != null)
        txBuilder.setMemo(tx.getMemo());

    return txBuilder.build();
}
 
Example #15
Source File: Transaction.java    From GreenBits with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Getter for {@link #exchangeRate}.
 */
@Nullable
public ExchangeRate getExchangeRate() {
    return exchangeRate;
}
 
Example #16
Source File: Transaction.java    From GreenBits with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Setter for {@link #exchangeRate}.
 */
public void setExchangeRate(ExchangeRate exchangeRate) {
    this.exchangeRate = exchangeRate;
}
 
Example #17
Source File: TradeStatistics.java    From bisq-core with GNU Affero General Public License v3.0 4 votes vote down vote up
public Volume getTradeVolume() {
    if (getTradePrice().getMonetary() instanceof Altcoin)
        return new Volume(new AltcoinExchangeRate((Altcoin) getTradePrice().getMonetary()).coinToAltcoin(getTradeAmount()));
    else
        return new Volume(new ExchangeRate((Fiat) getTradePrice().getMonetary()).coinToFiat(getTradeAmount()));
}
 
Example #18
Source File: TradeStatistics2.java    From bisq-core with GNU Affero General Public License v3.0 4 votes vote down vote up
public Volume getTradeVolume() {
    if (getTradePrice().getMonetary() instanceof Altcoin)
        return new Volume(new AltcoinExchangeRate((Altcoin) getTradePrice().getMonetary()).coinToAltcoin(getTradeAmount()));
    else
        return new Volume(new ExchangeRate((Fiat) getTradePrice().getMonetary()).coinToFiat(getTradeAmount()));
}
 
Example #19
Source File: TradeStatistics.java    From bisq with GNU Affero General Public License v3.0 4 votes vote down vote up
public Volume getTradeVolume() {
    if (getTradePrice().getMonetary() instanceof Altcoin)
        return new Volume(new AltcoinExchangeRate((Altcoin) getTradePrice().getMonetary()).coinToAltcoin(getTradeAmount()));
    else
        return new Volume(new ExchangeRate((Fiat) getTradePrice().getMonetary()).coinToFiat(getTradeAmount()));
}
 
Example #20
Source File: Transaction.java    From bcm-android with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Setter for {@link #exchangeRate}.
 */
public void setExchangeRate(ExchangeRate exchangeRate) {
    this.exchangeRate = exchangeRate;
}