switch to xmr atomic units as native units

This commit is contained in:
woodser 2023-03-05 14:09:14 -05:00
parent ab94b2d6fa
commit 9b4f8046b7
124 changed files with 1155 additions and 1283 deletions

View file

@ -31,11 +31,11 @@ import bisq.common.crypto.KeyRing;
import bisq.common.crypto.Sig;
import bisq.common.util.Utilities;
import org.bitcoinj.core.Coin;
import org.bitcoinj.core.ECKey;
import com.google.common.base.Charsets;
import java.math.BigInteger;
import java.security.KeyPair;
import java.time.Instant;
@ -354,7 +354,7 @@ public class SignedWitnessServiceTest {
when(keyRing.getSignatureKeyPair()).thenReturn(signerKeyPair);
AccountAgeWitness accountAgeWitness = new AccountAgeWitness(account1DataHash, accountCreationTime);
signedWitnessService.signAndPublishAccountAgeWitness(Coin.ZERO, accountAgeWitness, peerKeyPair.getPublic());
signedWitnessService.signAndPublishAccountAgeWitness(BigInteger.valueOf(0), accountAgeWitness, peerKeyPair.getPublic());
verify(p2pService, never()).addPersistableNetworkPayload(any(PersistableNetworkPayload.class), anyBoolean());
}

View file

@ -32,7 +32,7 @@ import bisq.core.support.dispute.DisputeResult;
import bisq.core.support.dispute.arbitration.TraderDataItem;
import bisq.core.support.dispute.arbitration.arbitrator.ArbitratorManager;
import bisq.core.trade.Contract;
import bisq.core.trade.HavenoUtils;
import bisq.network.p2p.P2PService;
import bisq.network.p2p.storage.persistence.AppendOnlyDataStoreService;
@ -44,7 +44,6 @@ import bisq.common.crypto.PubKeyRing;
import bisq.common.crypto.Sig;
import bisq.common.util.Utilities;
import org.bitcoinj.core.Coin;
import org.bitcoinj.core.ECKey;
import java.security.KeyPair;
@ -52,7 +51,6 @@ import java.security.PublicKey;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
@ -227,7 +225,7 @@ public class AccountAgeWitnessServiceTest {
when(chargeBackRisk.hasChargebackRisk(any(), any())).thenReturn(true);
when(contract.getPaymentMethodId()).thenReturn(PaymentMethod.SEPA_ID);
when(contract.getTradeAmount()).thenReturn(Coin.parseCoin("0.01"));
when(contract.getTradeAmount()).thenReturn(HavenoUtils.xmrToAtomicUnits(0.01));
when(contract.getBuyerPubKeyRing()).thenReturn(buyerPubKeyRing);
when(contract.getSellerPubKeyRing()).thenReturn(sellerPubKeyRing);
when(contract.getOfferPayload()).thenReturn(mock(OfferPayload.class));
@ -357,7 +355,7 @@ public class AccountAgeWitnessServiceTest {
signerKeyRing.getSignatureKeyPair().getPublic().getEncoded(),
witnessOwnerPubKey.getEncoded(),
time,
SignedWitnessService.MINIMUM_TRADE_AMOUNT_FOR_SIGNING.value);
SignedWitnessService.MINIMUM_TRADE_AMOUNT_FOR_SIGNING.longValueExact());
signedWitnessService.addToMap(signedWitness);
}

View file

@ -4,8 +4,7 @@ import bisq.core.account.witness.AccountAgeWitness;
import bisq.core.support.dispute.arbitration.TraderDataItem;
import bisq.core.payment.payload.PaymentAccountPayload;
import org.bitcoinj.core.Coin;
import java.math.BigInteger;
import java.security.PublicKey;
import org.junit.Before;
@ -44,11 +43,11 @@ public class TraderDataItemTest {
public void setup() {
accountAgeWitness1 = new AccountAgeWitness(hash1, 123);
accountAgeWitness2 = new AccountAgeWitness(hash2, 124);
traderDataItem1 = new TraderDataItem(mock(PaymentAccountPayload.class), accountAgeWitness1, Coin.valueOf(546),
traderDataItem1 = new TraderDataItem(mock(PaymentAccountPayload.class), accountAgeWitness1, BigInteger.valueOf(546),
mock(PublicKey.class));
traderDataItem2 = new TraderDataItem(mock(PaymentAccountPayload.class), accountAgeWitness1, Coin.valueOf(547),
traderDataItem2 = new TraderDataItem(mock(PaymentAccountPayload.class), accountAgeWitness1, BigInteger.valueOf(547),
mock(PublicKey.class));
traderDataItem3 = new TraderDataItem(mock(PaymentAccountPayload.class), accountAgeWitness2, Coin.valueOf(548),
traderDataItem3 = new TraderDataItem(mock(PaymentAccountPayload.class), accountAgeWitness2, BigInteger.valueOf(548),
mock(PublicKey.class));
}

View file

@ -27,14 +27,16 @@ import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import java.math.BigInteger;
public class CoinUtilTest {
@Test
public void testGetFeePerBtc() {
assertEquals(Coin.parseCoin("1"), HavenoUtils.getFeePerXmr(Coin.parseCoin("1"), Coin.parseCoin("1")));
assertEquals(Coin.parseCoin("0.1"), HavenoUtils.getFeePerXmr(Coin.parseCoin("0.1"), Coin.parseCoin("1")));
assertEquals(Coin.parseCoin("0.01"), HavenoUtils.getFeePerXmr(Coin.parseCoin("0.1"), Coin.parseCoin("0.1")));
assertEquals(Coin.parseCoin("0.015"), HavenoUtils.getFeePerXmr(Coin.parseCoin("0.3"), Coin.parseCoin("0.05")));
assertEquals(HavenoUtils.xmrToAtomicUnits(1), HavenoUtils.getFeePerXmr(HavenoUtils.xmrToAtomicUnits(1), HavenoUtils.xmrToAtomicUnits(1)));
assertEquals(HavenoUtils.xmrToAtomicUnits(0.1), HavenoUtils.getFeePerXmr(HavenoUtils.xmrToAtomicUnits(0.1), HavenoUtils.xmrToAtomicUnits(1)));
assertEquals(HavenoUtils.xmrToAtomicUnits(0.01), HavenoUtils.getFeePerXmr(HavenoUtils.xmrToAtomicUnits(0.1), HavenoUtils.xmrToAtomicUnits(0.1)));
assertEquals(HavenoUtils.xmrToAtomicUnits(0.015), HavenoUtils.getFeePerXmr(HavenoUtils.xmrToAtomicUnits(0.3), HavenoUtils.xmrToAtomicUnits(0.05)));
}
@Test
@ -57,52 +59,52 @@ public class CoinUtilTest {
@Test
public void testGetAdjustedAmount() {
Coin result = CoinUtil.getAdjustedAmount(
Coin.valueOf(100_000),
BigInteger result = CoinUtil.getAdjustedAmount(
HavenoUtils.xmrToAtomicUnits(0.001),
Price.valueOf("USD", 1000_0000),
20_000_000,
HavenoUtils.xmrToAtomicUnits(0.2).longValueExact(),
1);
assertEquals(
"Minimum trade amount allowed should be adjusted to the smallest trade allowed.",
"0.001 BTC",
result.toFriendlyString()
"0.001 XMR",
HavenoUtils.formatToXmrWithCode(result)
);
try {
CoinUtil.getAdjustedAmount(
Coin.ZERO,
BigInteger.valueOf(0),
Price.valueOf("USD", 1000_0000),
20_000_000,
HavenoUtils.xmrToAtomicUnits(0.2).longValueExact(),
1);
fail("Expected IllegalArgumentException to be thrown when amount is too low.");
} catch (IllegalArgumentException iae) {
assertEquals(
"Unexpected exception message.",
"amount needs to be above minimum of 10k satoshis",
"amount needs to be above minimum of 0.0001 xmr",
iae.getMessage()
);
}
result = CoinUtil.getAdjustedAmount(
Coin.valueOf(1_000_000),
HavenoUtils.xmrToAtomicUnits(0.01),
Price.valueOf("USD", 1000_0000),
20_000_000,
HavenoUtils.xmrToAtomicUnits(0.2).longValueExact(),
1);
assertEquals(
"Minimum allowed trade amount should not be adjusted.",
"0.01 BTC",
result.toFriendlyString()
"0.01 XMR",
HavenoUtils.formatToXmrWithCode(result)
);
result = CoinUtil.getAdjustedAmount(
Coin.valueOf(100_000),
HavenoUtils.xmrToAtomicUnits(0.001),
Price.valueOf("USD", 1000_0000),
1_000_000,
HavenoUtils.xmrToAtomicUnits(0.1).longValueExact(),
1);
assertEquals(
"Minimum trade amount allowed should respect maxTradeLimit and factor, if possible.",
"0.001 BTC",
result.toFriendlyString()
"0.001 XMR",
HavenoUtils.formatToXmrWithCode(result)
);
// TODO(chirhonul): The following seems like it should raise an exception or otherwise fail.
@ -111,14 +113,14 @@ public class CoinUtilTest {
// 0.05 USD worth, which is below the factor of 1 USD, but does respect the maxTradeLimit.
// Basically the given constraints (maxTradeLimit vs factor) are impossible to both fulfill..
result = CoinUtil.getAdjustedAmount(
Coin.valueOf(100_000),
HavenoUtils.xmrToAtomicUnits(0.001),
Price.valueOf("USD", 1000_0000),
5_000,
HavenoUtils.xmrToAtomicUnits(0.00005).longValueExact(),
1);
assertEquals(
"Minimum trade amount allowed with low maxTradeLimit should still respect that limit, even if result does not respect the factor specified.",
"0.00005 BTC",
result.toFriendlyString()
"0.00005 XMR",
HavenoUtils.formatToXmrWithCode(result)
);
}
}