Search in sources :

Example 1 with Money

use of org.whispersystems.signalservice.api.payments.Money in project Signal-Android by WhisperSystems.

the class CreatePaymentViewModel method updateFiatAmount.

@NonNull
private InputState updateFiatAmount(@NonNull Context context, @NonNull InputState inputState, @NonNull AmountKeyboardGlyph glyph, @NonNull Currency currency) {
    String newFiatAmount = updateAmountString(context, inputState.getFiatAmount(), glyph, currency.getDefaultFractionDigits());
    FiatMoney newFiat = stringToFiatValueOrZero(newFiatAmount, currency);
    Money newMoney = OptionalUtil.flatMap(inputState.getExchangeRate(), e -> e.exchange(newFiat)).get();
    String newMoneyAmount;
    if (newFiatAmount.equals("0")) {
        newMoneyAmount = "0";
    } else {
        newMoneyAmount = newMoney.toString(FormatterOptions.builder().withoutUnit().build());
    }
    if (!withinMobileCoinBounds(newMoney.requireMobileCoin())) {
        return inputState;
    }
    return inputState.updateAmount(newMoneyAmount, newFiatAmount, newMoney, Optional.of(newFiat));
}
Also used : MutableLiveData(androidx.lifecycle.MutableLiveData) SignalStore(org.thoughtcrime.securesms.keyvalue.SignalStore) Context(android.content.Context) PayeeParcelable(org.thoughtcrime.securesms.payments.preferences.model.PayeeParcelable) CreatePaymentDetails(org.thoughtcrime.securesms.payments.CreatePaymentDetails) NonNull(androidx.annotation.NonNull) ViewModel(androidx.lifecycle.ViewModel) FiatMoney(org.signal.core.util.money.FiatMoney) Currency(java.util.Currency) Store(org.thoughtcrime.securesms.util.livedata.Store) FiatMoneyUtil(org.thoughtcrime.securesms.payments.FiatMoneyUtil) BigDecimal(java.math.BigDecimal) ProfileUtil(org.thoughtcrime.securesms.util.ProfileUtil) FormatterOptions(org.whispersystems.signalservice.api.payments.FormatterOptions) Recipient(org.thoughtcrime.securesms.recipients.Recipient) Transformations(androidx.lifecycle.Transformations) LiveData(androidx.lifecycle.LiveData) ViewModelProvider(androidx.lifecycle.ViewModelProvider) CurrencyExchange(org.thoughtcrime.securesms.payments.currency.CurrencyExchange) ApplicationDependencies(org.thoughtcrime.securesms.dependencies.ApplicationDependencies) IOException(java.io.IOException) OptionalUtil(org.whispersystems.signalservice.api.util.OptionalUtil) Optional(org.whispersystems.libsignal.util.guava.Optional) Objects(java.util.Objects) Log(org.signal.core.util.logging.Log) Nullable(androidx.annotation.Nullable) DefaultValueLiveData(org.thoughtcrime.securesms.util.DefaultValueLiveData) Balance(org.thoughtcrime.securesms.payments.Balance) LiveDataUtil(org.thoughtcrime.securesms.util.livedata.LiveDataUtil) Money(org.whispersystems.signalservice.api.payments.Money) FiatMoney(org.signal.core.util.money.FiatMoney) Money(org.whispersystems.signalservice.api.payments.Money) FiatMoney(org.signal.core.util.money.FiatMoney) NonNull(androidx.annotation.NonNull)

Example 2 with Money

use of org.whispersystems.signalservice.api.payments.Money in project Signal-Android by WhisperSystems.

the class BlockTransactionReconstructionTests method total_in_matches_total_out.

@Test
public void total_in_matches_total_out() {
    Money net = unspentTransactionOutputs.sum().subtract(spentTransactionOutputs.sum());
    TransactionReconstruction estimate = TransactionReconstruction.estimateBlockLevelActivity(spentTransactionOutputs.getMoney(), unspentTransactionOutputs.getMoney());
    Money transactionIn = Money.MobileCoin.sum(toValueList(estimate.received()));
    Money transactionOut = Money.MobileCoin.sum(toValueList(estimate.sent()));
    Money netTransactions = transactionIn.subtract(transactionOut);
    assertEquals(net, netTransactions);
}
Also used : Money(org.whispersystems.signalservice.api.payments.Money) Test(org.junit.Test)

Example 3 with Money

use of org.whispersystems.signalservice.api.payments.Money in project Signal-Android by WhisperSystems.

the class BlockTransactionReconstructionTests method sum_of_all_transactions.

@Test
public void sum_of_all_transactions() {
    Money net = unspentTransactionOutputs.sum().subtract(spentTransactionOutputs.sum());
    TransactionReconstruction estimate = TransactionReconstruction.estimateBlockLevelActivity(spentTransactionOutputs.getMoney(), unspentTransactionOutputs.getMoney());
    Money netValueWithDirections = Money.MobileCoin.sum(toValueListWithDirection(estimate.getAllTransactions()));
    assertEquals(net, netValueWithDirections);
}
Also used : Money(org.whispersystems.signalservice.api.payments.Money) Test(org.junit.Test)

Example 4 with Money

use of org.whispersystems.signalservice.api.payments.Money in project Signal-Android by WhisperSystems.

the class MoneyView method init.

public void init(@NonNull Context context, @Nullable AttributeSet attrs) {
    FormatterOptions.Builder builder = FormatterOptions.builder(Locale.getDefault());
    TypedArray styledAttributes = context.obtainStyledAttributes(attrs, R.styleable.MoneyView, 0, 0);
    if (styledAttributes.getBoolean(R.styleable.MoneyView_always_show_sign, false)) {
        builder.alwaysPrefixWithSign();
    }
    formatterOptions = builder.withoutSpaceBeforeUnit().build();
    String value = styledAttributes.getString(R.styleable.MoneyView_money);
    if (value != null) {
        try {
            setMoney(Money.parse(value));
        } catch (Money.ParseException e) {
            throw new AssertionError("Invalid money format", e);
        }
    }
    styledAttributes.recycle();
}
Also used : Money(org.whispersystems.signalservice.api.payments.Money) FormatterOptions(org.whispersystems.signalservice.api.payments.FormatterOptions) TypedArray(android.content.res.TypedArray) SpannableString(android.text.SpannableString)

Example 5 with Money

use of org.whispersystems.signalservice.api.payments.Money in project Signal-Android by WhisperSystems.

the class CreatePaymentViewModel method clearAmount.

void clearAmount() {
    inputState.update(s -> {
        final Money money = Money.MobileCoin.ZERO;
        final Optional<FiatMoney> fiat = OptionalUtil.flatMap(s.getExchangeRate(), r -> r.exchange(money));
        return s.updateAmount("0", "0", Money.MobileCoin.ZERO, fiat);
    });
}
Also used : FiatMoney(org.signal.core.util.money.FiatMoney) Money(org.whispersystems.signalservice.api.payments.Money) FiatMoney(org.signal.core.util.money.FiatMoney)

Aggregations

Money (org.whispersystems.signalservice.api.payments.Money)5 Test (org.junit.Test)2 FiatMoney (org.signal.core.util.money.FiatMoney)2 FormatterOptions (org.whispersystems.signalservice.api.payments.FormatterOptions)2 Context (android.content.Context)1 TypedArray (android.content.res.TypedArray)1 SpannableString (android.text.SpannableString)1 NonNull (androidx.annotation.NonNull)1 Nullable (androidx.annotation.Nullable)1 LiveData (androidx.lifecycle.LiveData)1 MutableLiveData (androidx.lifecycle.MutableLiveData)1 Transformations (androidx.lifecycle.Transformations)1 ViewModel (androidx.lifecycle.ViewModel)1 ViewModelProvider (androidx.lifecycle.ViewModelProvider)1 IOException (java.io.IOException)1 BigDecimal (java.math.BigDecimal)1 Currency (java.util.Currency)1 Objects (java.util.Objects)1 Log (org.signal.core.util.logging.Log)1 ApplicationDependencies (org.thoughtcrime.securesms.dependencies.ApplicationDependencies)1