Skip to content

Commit

Permalink
wip: transaction log almost working
Browse files Browse the repository at this point in the history
  • Loading branch information
cilki committed Jun 16, 2024
1 parent 7186ace commit 39323cf
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 32 deletions.
2 changes: 1 addition & 1 deletion assets/github.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 3 additions & 3 deletions src/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pub async fn index(State(state): State<AppState>) -> IndexTemplate {
IndexTemplate {
monero_enabled: cfg!(feature = "monero"),
#[cfg(feature = "monero")]
monero_balance: format!("{}", PrettyPrintFloat(monero_balance)),
monero_balance: format!("{:.5}", PrettyPrintFloat(monero_balance)),
#[cfg(feature = "monero")]
monero_block_height: state.monero.wallet.get_height().await.unwrap().get(),
#[cfg(feature = "monero")]
Expand All @@ -55,11 +55,11 @@ pub async fn index(State(state): State<AppState>) -> IndexTemplate {
.await
.unwrap()
.iter()
.filter_map(|transfer| repo.monero_transfer(transfer).ok())
.filter_map(|transfer| repo.find_monero_transaction(transfer).ok())
.collect(),
#[cfg(feature = "monero")]
monero_balance_usd: format!(
"{}",
"{:.2}",
PrettyPrintFloat(crate::currency::lookup("XMR").await.unwrap_or(0.0) * monero_balance)
),
..Default::default()
Expand Down
3 changes: 3 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ pub async fn serve(args: &ServeArgs) -> Result<ExitCode> {
#[cfg(feature = "monero")]
let app = app.route("/xmr/balance", get(crate::currency::monero::balance));

#[cfg(feature = "monero")]
let app = app.route("/xmr/payouts", get(crate::currency::monero::payouts));

// Refresh every hour
let every_hour = every(1)
.hour()
Expand Down
2 changes: 1 addition & 1 deletion src/currency/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use cached::proc_macro::once;
#[cfg(feature = "monero")]
pub mod monero;

#[derive(Clone, Debug)]
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Address {
BTC(String),
#[cfg(feature = "monero")]
Expand Down
16 changes: 14 additions & 2 deletions src/currency/monero.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use std::{
time::Duration,
};
use std::{str::FromStr, sync::Mutex};
use tracing::{debug, info};
use tracing::{debug, info, instrument};

#[derive(Clone, Debug)]
pub struct MoneroState {
Expand Down Expand Up @@ -119,7 +119,7 @@ impl MoneroState {
pub async fn get_balance(&self) -> Result<Amount> {
let balance = self.wallet.get_balance(self.account_index, None).await?;
debug!(balance = ?balance, "Current Monero wallet balance");
Ok(balance.unlocked_balance)
Ok(balance.balance)
}

/// Count outbound transfers to the given address.
Expand All @@ -146,6 +146,7 @@ impl MoneroState {
}

/// Get all outbound transfers.
#[instrument(skip_all, ret)]
pub async fn get_transfers(&self) -> Result<Vec<GotTransfer>> {
let transfers = self
.wallet
Expand Down Expand Up @@ -200,3 +201,14 @@ pub async fn balance(State(state): State<AppState>) -> impl IntoResponse {
),
)
}

/// Return an SVG badge with the total number of payouts.
pub async fn payouts(State(state): State<AppState>) -> impl IntoResponse {
(
[(header::CONTENT_TYPE, "image/svg+xml")],
crate::badge::generate(
"payouts",
&format!("{}", state.monero.get_transfers().await.unwrap().len()),
),
)
}
40 changes: 20 additions & 20 deletions src/repo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,22 @@ use anyhow::{bail, Result};
use base64::prelude::*;
use chrono::{DateTime, Utc};
use git2::{Commit, Oid, Repository, Sort};
use std::process::{Command};
use std::process::Command;
use tempfile::TempDir;
use tracing::{debug, info, instrument};

#[derive(Debug)]
pub struct Contributor {
pub address: Address,
pub last_payout: Option<DateTime<Utc>>,
/// Paid commits
pub commits: Vec<Oid>,
/// The user's GPG public key ID which must remain constant
pub key_id: String,

/// Paid commits
pub commits: Vec<Oid>,
pub last_payout: Option<DateTime<Utc>>,

/// The contributor's public name from git history
pub name: String,
}

impl Contributor {
Expand Down Expand Up @@ -108,33 +111,29 @@ impl TurbineRepo {
Ok(repo)
}

/// Find the contributor that created the given commit.
pub fn find_contributor<'a>(&'a self, commit_id: String) -> Result<&'a Contributor> {
let commit = self.container.find_commit(Oid::from_str(&commit_id)?)?;
let key_id = get_public_key_id(&commit)?;

Ok(self
#[cfg(feature = "monero")]
#[instrument(skip(self), ret)]
pub fn find_monero_transaction(
&self,
transfer: &monero_rpc::GotTransfer,
) -> Result<Transaction> {
if let Some(contributor) = self
.contributors
.iter()
.find(|contributor| contributor.key_id == key_id)
.unwrap())
}

#[cfg(feature = "monero")]
pub fn monero_transfer(&self, transfer: &monero_rpc::GotTransfer) -> Result<Transaction> {
if let Ok(_contributor) = self.find_contributor(transfer.payment_id.to_string()) {
.find(|contributor| contributor.address == Address::XMR(transfer.address.to_string()))
{
Ok(Transaction {
amount: format!("{}", transfer.amount.as_xmr()),
timestamp: transfer.timestamp.timestamp() as u64,
contributor_name: "test".into(),
contributor_name: contributor.name.clone(),
})
} else {
bail!("");
bail!("Transaction not found");
}
}

/// Verify a commit's GPG signature and return its key ID.
#[instrument(ret, level = "trace")]
#[instrument(skip(self), ret, level = "trace")]
fn verify_signature(&self, commit: &Commit) -> Result<String> {
// Receive the public key first
Command::new("gpg")
Expand Down Expand Up @@ -217,6 +216,7 @@ impl TurbineRepo {
last_payout: None,
key_id,
commits: Vec::new(),
name: commit.author().name().unwrap_or("<invalid>").to_string(),
};

info!(contributor = ?contributor, "Adding new contributor");
Expand Down
24 changes: 19 additions & 5 deletions templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@
max-height: 20px;
}

.address {
.wallet-address-container {
white-space: nowrap;
}

Expand All @@ -123,7 +123,7 @@
}

#wallet-address {
width: 80px;
width: 100%;
}

.summary-block {
Expand All @@ -140,10 +140,11 @@
height: 200px;
overflow-y: auto;
border: 1px solid rgb(62, 68, 70);
padding: 10px;
padding-top: 10px;
padding-left: 20px;
padding-right: 20px;
text-align: left;
background-color: rgb(24, 26, 27);
padding: 20px;
border-radius: 8px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
margin-left: 10px;
Expand Down Expand Up @@ -175,6 +176,18 @@
.tooltip:hover .tooltiptext {
visibility: visible;
}

a {
color: #007bff;
text-decoration: none;
transition: color 0.3s ease;
font-weight: bold;
}

a:hover {
color: #0056b3;
text-decoration: underline;
}
</style>
</head>

Expand All @@ -199,14 +212,15 @@
<p class="balance-zero">{{ monero_balance }} XMR</p>
{% endif %}
<p>Network: {{ monero_network }} ({{ monero_block_height }})</p>
<div class="address">
<div class="wallet-address-container">
<input type="text" id="wallet-address" value="{{ monero_wallet_address }}" readonly>
<button class="copy-button" onclick="copyAddress()">
<img class="svg-copy" src="/assets/copy.svg">
</button>
</div>
</div>
<div class="transaction-block">
<p>Transaction log</p>
<ul>
{% for transaction in monero_transactions %}
<li class="list-item">{{ transaction.amount }} XMR</li>
Expand Down

0 comments on commit 39323cf

Please sign in to comment.