diff --git a/src/main/java/org/whispersystems/bithub/BithubService.java b/src/main/java/org/whispersystems/bithub/BithubService.java index c826409..39c151b 100644 --- a/src/main/java/org/whispersystems/bithub/BithubService.java +++ b/src/main/java/org/whispersystems/bithub/BithubService.java @@ -62,12 +62,13 @@ public void run(BithubServerConfiguration config, Environment environment) String githubWebhookPwd = config.getGithubConfiguration().getWebhookConfiguration().getPassword(); List githubRepositories = config.getGithubConfiguration().getRepositories(); BigDecimal payoutRate = config.getBithubConfiguration().getPayoutRate(); + int btcPrecision = config.getBithubConfiguration().getBtcPrecision(); String organizationName = config.getOrganizationConfiguration().getName(); String donationUrl = config.getOrganizationConfiguration().getDonationUrl().toExternalForm(); GithubClient githubClient = new GithubClient(githubUser, githubToken); CoinbaseClient coinbaseClient = new CoinbaseClient(config.getCoinbaseConfiguration().getApiKey()); - CacheManager cacheManager = new CacheManager(coinbaseClient, githubClient, githubRepositories, payoutRate); + CacheManager cacheManager = new CacheManager(coinbaseClient, githubClient, githubRepositories, payoutRate, btcPrecision); environment.servlets().addFilter("CORS", CrossOriginFilter.class) .addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST), true, "/*"); diff --git a/src/main/java/org/whispersystems/bithub/config/BithubConfiguration.java b/src/main/java/org/whispersystems/bithub/config/BithubConfiguration.java index 0c6e337..4b72b89 100644 --- a/src/main/java/org/whispersystems/bithub/config/BithubConfiguration.java +++ b/src/main/java/org/whispersystems/bithub/config/BithubConfiguration.java @@ -30,8 +30,18 @@ public class BithubConfiguration { @NotEmpty private String payout = "0.02"; + // This is the number of decimal places shown when displaying amounts in BTC + // Change this if needed to whatever is appropriate for your payout amounts + @JsonProperty + @NotEmpty + private int btcPrecision = 4; + public BigDecimal getPayoutRate() { return new BigDecimal(payout); } + public int getBtcPrecision() { + return btcPrecision; + } + } diff --git a/src/main/java/org/whispersystems/bithub/entities/Transaction.java b/src/main/java/org/whispersystems/bithub/entities/Transaction.java index f88ea76..5ecf7ee 100644 --- a/src/main/java/org/whispersystems/bithub/entities/Transaction.java +++ b/src/main/java/org/whispersystems/bithub/entities/Transaction.java @@ -10,6 +10,9 @@ public class Transaction { @JsonProperty private String amount; + @JsonProperty + private String amountInBTC; + @JsonProperty private String commitUrl; @@ -24,11 +27,12 @@ public class Transaction { public Transaction() {} - public Transaction(String destination, String amount, String commitUrl, + public Transaction(String destination, String amount, String amountInBTC, String commitUrl, String commitSha, String timestamp, String description) { this.destination = destination; this.amount = amount; + this.amountInBTC = amountInBTC; this.commitUrl = commitUrl; this.commitSha = commitSha; this.timestamp = timestamp; @@ -43,6 +47,10 @@ public String getAmount() { return amount; } + public String getAmountInBTC() { + return amountInBTC; + } + public String getCommitUrl() { return commitUrl; } diff --git a/src/main/java/org/whispersystems/bithub/storage/CacheManager.java b/src/main/java/org/whispersystems/bithub/storage/CacheManager.java index a8a125b..0e95612 100644 --- a/src/main/java/org/whispersystems/bithub/storage/CacheManager.java +++ b/src/main/java/org/whispersystems/bithub/storage/CacheManager.java @@ -34,6 +34,7 @@ public class CacheManager implements Managed { private final CoinbaseClient coinbaseClient; private final GithubClient githubClient; private final BigDecimal payoutRate; + private final int btcPrecision; private final List repositories; private AtomicReference cachedPaymentStatus; @@ -43,18 +44,20 @@ public class CacheManager implements Managed { public CacheManager(CoinbaseClient coinbaseClient, GithubClient githubClient, List repositories, - BigDecimal payoutRate) + BigDecimal payoutRate, + int btcPrecision) { this.coinbaseClient = coinbaseClient; this.githubClient = githubClient; this.payoutRate = payoutRate; + this.btcPrecision = btcPrecision; this.repositories = repositories; } @Override public void start() throws Exception { this.cachedPaymentStatus = new AtomicReference<>(createCurrentPaymentForBalance(coinbaseClient)); - this.cachedTransactions = new AtomicReference<>(createRecentTransactions(coinbaseClient)); + this.cachedTransactions = new AtomicReference<>(createRecentTransactions(coinbaseClient, btcPrecision)); this.cachedRepositories = new AtomicReference<>(createRepositories(githubClient, repositories)); initializeUpdates(coinbaseClient, githubClient, repositories); @@ -86,7 +89,7 @@ public void initializeUpdates(final CoinbaseClient coinbaseClient, public void run() { try { CurrentPayment currentPayment = createCurrentPaymentForBalance(coinbaseClient); - List transactions = createRecentTransactions (coinbaseClient); + List transactions = createRecentTransactions (coinbaseClient, btcPrecision); List repositories = createRepositories(githubClient, repoConfigs); cachedPaymentStatus.set(currentPayment); @@ -126,7 +129,7 @@ private CurrentPayment createCurrentPaymentForBalance(CoinbaseClient coinbaseCli new Payment(paymentUsd.toPlainString())); } - private List createRecentTransactions(CoinbaseClient coinbaseClient) + private List createRecentTransactions(CoinbaseClient coinbaseClient, int precision) throws IOException { List recentTransactions = coinbaseClient.getRecentTransactions(); @@ -143,6 +146,7 @@ private List createRecentTransactions(CoinbaseClient coinbaseClient transactions.add(new Transaction(parser.parseDestinationFromMessage(), parser.parseAmountInDollars(exchangeRate), + parser.parseAmountInBTC(precision), url, sha, parser.parseTimestamp(), description)); diff --git a/src/main/java/org/whispersystems/bithub/storage/CoinbaseTransactionParser.java b/src/main/java/org/whispersystems/bithub/storage/CoinbaseTransactionParser.java index 5c81626..5a41704 100644 --- a/src/main/java/org/whispersystems/bithub/storage/CoinbaseTransactionParser.java +++ b/src/main/java/org/whispersystems/bithub/storage/CoinbaseTransactionParser.java @@ -24,6 +24,12 @@ public String parseAmountInDollars(BigDecimal exchangeRate) { .toPlainString(); } + public String parseAmountInBTC(int precision) { + return new BigDecimal(coinbaseTransaction.getAmount()).abs() + .setScale(precision, RoundingMode.CEILING) + .toPlainString(); + } + public String parseTimestamp() throws ParseException { String timestamp = coinbaseTransaction.getCreatedTime(); int offendingColon = timestamp.lastIndexOf(':'); diff --git a/src/main/resources/org/whispersystems/bithub/views/recent_transactions.mustache b/src/main/resources/org/whispersystems/bithub/views/recent_transactions.mustache index c500de0..6e72bf1 100644 --- a/src/main/resources/org/whispersystems/bithub/views/recent_transactions.mustache +++ b/src/main/resources/org/whispersystems/bithub/views/recent_transactions.mustache @@ -44,7 +44,7 @@ diff --git a/src/test/java/org/whispersystems/bithub/tests/controllers/StatusControllerTest.java b/src/test/java/org/whispersystems/bithub/tests/controllers/StatusControllerTest.java index 639ba32..3d44364 100644 --- a/src/test/java/org/whispersystems/bithub/tests/controllers/StatusControllerTest.java +++ b/src/test/java/org/whispersystems/bithub/tests/controllers/StatusControllerTest.java @@ -28,6 +28,7 @@ public class StatusControllerTest { private static final BigDecimal PAYOUT_RATE = new BigDecimal(0.02 ); private static final BigDecimal BALANCE = new BigDecimal(10.01); private static final BigDecimal EXCHANGE_RATE = new BigDecimal(1.0 ); + private static final int BTC_PRECISION = 4; private static final CoinbaseClient coinbaseClient = mock(CoinbaseClient.class); private static final GithubClient githubClient = mock(GithubClient.class ); @@ -43,7 +44,8 @@ public class StatusControllerTest { CacheManager coinbaseManager = new CacheManager(coinbaseClient, githubClient, new LinkedList(), - PAYOUT_RATE); + PAYOUT_RATE, + BTC_PRECISION); coinbaseManager.start(); resources = ResourceTestRule.builder() @@ -69,6 +71,7 @@ public class StatusControllerTest { // // assertThat(response.getStatus()).isEqualTo(200); // assertThat(response.getType()).isEqualTo(MediaType.TEXT_HTML_TYPE); +// assertThat(response.getEntity(String.class)).contains("
  • Sent $1.10 USD (1.1000 BTC)"); // } @Test @@ -78,6 +81,7 @@ public void testTransactionsJson() throws Exception { assertThat(response.getStatus()).isEqualTo(200); assertThat(response.getType()).isEqualTo(MediaType.APPLICATION_JSON_TYPE); + assertThat(response.getEntity(String.class)).contains("\"amount\":\"1.10\",\"amountInBTC\":\"1.1000\""); } }