Skip to content
This repository has been archived by the owner on Feb 3, 2022. It is now read-only.

Add BTC amounts to transactions list view #40

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/main/java/org/whispersystems/bithub/BithubService.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,13 @@ public void run(BithubServerConfiguration config, Environment environment)
String githubWebhookPwd = config.getGithubConfiguration().getWebhookConfiguration().getPassword();
List<RepositoryConfiguration> 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, "/*");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ public class Transaction {
@JsonProperty
private String amount;

@JsonProperty
private String amountInBTC;

@JsonProperty
private String commitUrl;

Expand All @@ -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;
Expand All @@ -43,6 +47,10 @@ public String getAmount() {
return amount;
}

public String getAmountInBTC() {
return amountInBTC;
}

public String getCommitUrl() {
return commitUrl;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<RepositoryConfiguration> repositories;

private AtomicReference<CurrentPayment> cachedPaymentStatus;
Expand All @@ -43,18 +44,20 @@ public class CacheManager implements Managed {
public CacheManager(CoinbaseClient coinbaseClient,
GithubClient githubClient,
List<RepositoryConfiguration> 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);
Expand Down Expand Up @@ -86,7 +89,7 @@ public void initializeUpdates(final CoinbaseClient coinbaseClient,
public void run() {
try {
CurrentPayment currentPayment = createCurrentPaymentForBalance(coinbaseClient);
List<Transaction> transactions = createRecentTransactions (coinbaseClient);
List<Transaction> transactions = createRecentTransactions (coinbaseClient, btcPrecision);
List<Repository> repositories = createRepositories(githubClient, repoConfigs);

cachedPaymentStatus.set(currentPayment);
Expand Down Expand Up @@ -126,7 +129,7 @@ private CurrentPayment createCurrentPaymentForBalance(CoinbaseClient coinbaseCli
new Payment(paymentUsd.toPlainString()));
}

private List<Transaction> createRecentTransactions(CoinbaseClient coinbaseClient)
private List<Transaction> createRecentTransactions(CoinbaseClient coinbaseClient, int precision)
throws IOException
{
List<CoinbaseTransaction> recentTransactions = coinbaseClient.getRecentTransactions();
Expand All @@ -143,6 +146,7 @@ private List<Transaction> createRecentTransactions(CoinbaseClient coinbaseClient

transactions.add(new Transaction(parser.parseDestinationFromMessage(),
parser.parseAmountInDollars(exchangeRate),
parser.parseAmountInBTC(precision),
url, sha, parser.parseTimestamp(),
description));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(':');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@

<ul>
{{#transactions}}
<li>Sent ${{amount}} USD to <a href="https://github.com/{{destination}}" target="_blank">{{destination}}</a> for <a href="{{commitUrl}}" target="_blank"><sha>{{commitSha}}</sha></a> {{timestamp}}.</li>
<li>Sent ${{amount}} USD to <a href="https://github.com/{{destination}}" target="_blank">{{destination}}</a> for <a href="{{commitUrl}}" target="_blank"><sha>{{commitSha}}</sha></a> {{timestamp}}.</li>
{{/transactions}}
</ul>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand All @@ -43,7 +44,8 @@ public class StatusControllerTest {

CacheManager coinbaseManager = new CacheManager(coinbaseClient, githubClient,
new LinkedList<RepositoryConfiguration>(),
PAYOUT_RATE);
PAYOUT_RATE,
BTC_PRECISION);
coinbaseManager.start();

resources = ResourceTestRule.builder()
Expand All @@ -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("<li>Sent $1.10 USD (1.1000 BTC)");
// }

@Test
Expand All @@ -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\"");
}

}