depolymerization

wire gateway for Bitcoin/Ethereum
Log | Files | Refs | Submodules | README | LICENSE

commit f3855206f93b91d89941dc091b2c013bdff3b0c6
parent e7ae6758859b2b80a6e7fb8d97cc0fedd0ce998d
Author: Antoine A <>
Date:   Fri,  6 Dec 2024 16:19:03 +0100

update utils

Diffstat:
MCargo.lock | 20++++++++++----------
MCargo.toml | 7++++++-
Mbtc-wire/src/loops/worker.rs | 4++--
Mbtc-wire/src/taler_utils.rs | 2+-
Mcommon/Cargo.toml | 2+-
Meth-wire/src/loops/worker.rs | 4++--
Meth-wire/src/taler_util.rs | 2+-
Minstrumentation/src/gateway.rs | 2+-
Mwire-gateway/Cargo.toml | 7+------
Mwire-gateway/src/main.rs | 61++++++++++++++++++++++++++++++++-----------------------------
10 files changed, 57 insertions(+), 54 deletions(-)

diff --git a/Cargo.lock b/Cargo.lock @@ -392,9 +392,9 @@ dependencies = [ [[package]] name = "clap" -version = "4.5.22" +version = "4.5.23" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "69371e34337c4c984bbe322360c2547210bf632eb2814bbe78a6e87a2935bd2b" +checksum = "3135e7ec2ef7b10c6ed8950f0f792ed96ee093fa088608f1c76e569722700c84" dependencies = [ "clap_builder", "clap_derive", @@ -402,9 +402,9 @@ dependencies = [ [[package]] name = "clap_builder" -version = "4.5.22" +version = "4.5.23" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6e24c1b4099818523236a8ca881d2b45db98dadfb4625cf6608c12069fcbbde1" +checksum = "30582fc632330df2bd26877bde0c1f4470d57c582bbc070376afcd04d8cb4838" dependencies = [ "anstream", "anstyle", @@ -426,9 +426,9 @@ dependencies = [ [[package]] name = "clap_lex" -version = "0.7.3" +version = "0.7.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "afb84c814227b90d6895e01398aee0d8033c00e7466aca416fb6a8e0eb19d8a7" +checksum = "f46ad14479a25103f283c0f10005961cf086d8dc42205bb44c46ac563475dca6" [[package]] name = "clap_mangen" @@ -2869,7 +2869,7 @@ dependencies = [ [[package]] name = "taler-api" version = "0.1.0" -source = "git+https://git.taler.net/taler-rust.git/#d8e5f800f6adcb43822c779d71f8187003841217" +source = "git+https://git.taler.net/taler-rust.git/#0ff83d96b82649f06ee6c3a48090c4f9c646d088" dependencies = [ "axum", "dashmap", @@ -2890,7 +2890,7 @@ dependencies = [ [[package]] name = "taler-common" version = "0.1.0" -source = "git+https://git.taler.net/taler-rust.git/#d8e5f800f6adcb43822c779d71f8187003841217" +source = "git+https://git.taler.net/taler-rust.git/#0ff83d96b82649f06ee6c3a48090c4f9c646d088" dependencies = [ "base32", "fastrand", @@ -3112,9 +3112,9 @@ dependencies = [ [[package]] name = "tokio-stream" -version = "0.1.16" +version = "0.1.17" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4f4e6ce100d0eb49a2734f8c0812bcd324cf357d21810932c5df6b96ef2b86f1" +checksum = "eca58d7bba4a75707817a2c44174253f9236b2d5fbd055602e9d5c07c139a047" dependencies = [ "futures-core", "pin-project-lite", diff --git a/Cargo.toml b/Cargo.toml @@ -18,7 +18,12 @@ serde_json = "1.0" serde = { version = "1.0", features = ["derive"] } tokio = { version = "1.42", features = ["macros"] } axum = "0.7.9" -sqlx = "0.8" +sqlx = { version = "0.8", features = [ + "postgres", + "runtime-tokio-rustls", + "tls-rustls-ring", + "time", +], default-features = false } url = { version = "2.2", features = ["serde"] } taler-common = { git = "https://git.taler.net/taler-rust.git/" } taler-api = { git = "https://git.taler.net/taler-rust.git/" } diff --git a/btc-wire/src/loops/worker.rs b/btc-wire/src/loops/worker.rs @@ -335,7 +335,7 @@ fn sync_chain_incoming_confirmed( let credit_addr = full.details[0].address.clone().unwrap().assume_checked(); let amount = btc_to_taler(&full.amount, state.currency); let nb = db.execute("INSERT INTO tx_in (received, amount, reserve_pub, debit_acc, credit_acc) VALUES ($1, ($2, $3)::taler_amount, $4, $5, $6) ON CONFLICT (reserve_pub) DO NOTHING ", &[ - &((full.time * 1000000) as i64), &(amount.value as i64), &(amount.fraction as i32), &reserve_pub.as_slice(), &btc_payto_url(&debit_addr).as_ref(), &btc_payto_url(&credit_addr).as_ref() + &((full.time * 1000000) as i64), &(amount.decimal.value as i64), &(amount.decimal.fraction as i32), &reserve_pub.as_slice(), &btc_payto_url(&debit_addr).as_ref(), &btc_payto_url(&credit_addr).as_ref() ])?; if nb > 0 { info!( @@ -439,7 +439,7 @@ fn sync_chain_debit( let debit_addr = sender_address(rpc, full)?; let nb = db.execute( "INSERT INTO tx_out (created, amount, wtid, debit_acc, credit_acc, exchange_url, status, txid, request_uid) VALUES ($1, ($2, $3)::taler_amount, $4, $5, $6, $7, $8, $9, $10) ON CONFLICT (wtid) DO NOTHING", - &[&((full.time*1000000) as i64), &(amount.value as i64), &(amount.fraction as i32), &wtid.as_slice(), &btc_payto_url(&debit_addr).as_ref(), &btc_payto_url(&credit_addr).as_ref(), &state.base_url.as_ref(), &(DebitStatus::Sent as i16), &id.as_byte_array().as_slice(), &None::<&[u8]>], + &[&((full.time*1000000) as i64), &(amount.decimal.value as i64), &(amount.decimal.fraction as i32), &wtid.as_slice(), &btc_payto_url(&debit_addr).as_ref(), &btc_payto_url(&credit_addr).as_ref(), &state.base_url.as_ref(), &(DebitStatus::Sent as i16), &id.as_byte_array().as_slice(), &None::<&[u8]>], )?; if nb > 0 { warn!( diff --git a/btc-wire/src/taler_utils.rs b/btc-wire/src/taler_utils.rs @@ -60,6 +60,6 @@ pub fn taler_to_btc(amount: &Amount, currency: CurrencyBtc) -> Result<BtcAmount, )); } - let sat = amount.value * 100_000_000 + amount.fraction as u64; + let sat = amount.decimal.value * 100_000_000 + amount.decimal.fraction as u64; Ok(BtcAmount::from_sat(sat)) } diff --git a/common/Cargo.toml b/common/Cargo.toml @@ -36,4 +36,4 @@ uri-pack = { path = "../uri-pack" } # Exponential backoff generator exponential-backoff = "1.2.0" taler-common.workspace = true -sqlx = "*" +sqlx.workspace = true diff --git a/eth-wire/src/loops/worker.rs b/eth-wire/src/loops/worker.rs @@ -279,7 +279,7 @@ fn sync_chain_incoming_confirmed( let amount = eth_to_taler(&tx.value, state.currency); let credit_addr = tx.from.expect("Not coinbase"); let nb = db.execute("INSERT INTO tx_in (received, amount, reserve_pub, debit_acc, credit_acc) VALUES ($1, ($2, $3)::taler_amount, $4, $5, $6) ON CONFLICT (reserve_pub) DO NOTHING ", &[ - &Timestamp::now().as_sql_micros(), &(amount.value as i64), &(amount.fraction as i32), &reserve_pub.as_ref(), &eth_payto_url(&credit_addr).as_ref(), &state.payto.as_ref() + &Timestamp::now().as_sql_micros(), &(amount.decimal.value as i64), &(amount.decimal.fraction as i32), &reserve_pub.as_ref(), &eth_payto_url(&credit_addr).as_ref(), &state.payto.as_ref() ])?; if nb > 0 { info!( @@ -353,7 +353,7 @@ fn sync_chain_outgoing(tx: &SyncTransaction, db: &mut Client, state: &WireState) // Else add to database let nb = db.execute( "INSERT INTO tx_out (created, amount, wtid, debit_acc, credit_acc, exchange_url, status, txid, request_uid) VALUES ($1, ($2, $3)::taler_amount, $4, $5, $6, $7, $8, $9, $10) ON CONFLICT (wtid) DO NOTHING", - &[&Timestamp::now().as_sql_micros(), &(amount.value as i64), &(amount.fraction as i32), &wtid.as_ref(), &eth_payto_url(&state.address).as_ref(), &eth_payto_url(&credit_addr).as_ref(), &state.base_url.as_ref(), &(DebitStatus::Sent as i16), &tx.hash.as_ref(), &None::<&[u8]>], + &[&Timestamp::now().as_sql_micros(), &(amount.decimal.value as i64), &(amount.decimal.fraction as i32), &wtid.as_ref(), &eth_payto_url(&state.address).as_ref(), &eth_payto_url(&credit_addr).as_ref(), &state.base_url.as_ref(), &(DebitStatus::Sent as i16), &tx.hash.as_ref(), &None::<&[u8]>], )?; if nb > 0 { warn!( diff --git a/eth-wire/src/taler_util.rs b/eth-wire/src/taler_util.rs @@ -61,5 +61,5 @@ pub fn taler_to_eth(amount: &Amount, currency: CurrencyEth) -> Result<U256, Stri )); } - Ok(U256::from(amount.value) * WEI + U256::from(amount.fraction) * TRUNC) + Ok(U256::from(amount.decimal.value) * WEI + U256::from(amount.decimal.fraction) * TRUNC) } diff --git a/instrumentation/src/gateway.rs b/instrumentation/src/gateway.rs @@ -164,7 +164,7 @@ pub fn api(ctx: TestCtx) { 200 ); // Collision - request.amount.fraction += 42; + request.amount.decimal.fraction += 42; assert_eq!( http_code(ureq::post(&format!("{}transfer", ctx.gateway_url)).send_json(&request)), 409 diff --git a/wire-gateway/Cargo.toml b/wire-gateway/Cargo.toml @@ -14,12 +14,7 @@ tokio = { workspace = true, features = ["net", "macros", "rt-multi-thread"] } # Error macros thiserror.workspace = true # Async postgres client -sqlx = { version = "0.8", features = [ - "postgres", - "runtime-tokio-rustls", - "tls-rustls-ring", - "time", -] } +sqlx.workspace = true # Socket activation listenfd = "1.0.0" # Common lib diff --git a/wire-gateway/src/main.rs b/wire-gateway/src/main.rs @@ -43,7 +43,10 @@ use std::{ time::Duration, }; use taler_api::{ - db::{query_helper::SqlQueryHelper, type_helper::SqlTypeHelper}, + db::{ + query_helper::{page, SqlQueryHelper}, + type_helper::SqlTypeHelper, + }, error::{failure, failure_status, ApiResult}, wire_gateway_api, WireGatewayImpl, }; @@ -111,9 +114,8 @@ impl WireGatewayImpl for ServerState { let r = sqlx::query( "INSERT INTO tx_out (created, amount, wtid, debit_acc, credit_acc, exchange_url, request_uid) VALUES ($1, ($2, $3)::taler_amount, $4, $5, $6, $7, $8) RETURNING id" ) - .bind(Timestamp::now().as_sql_micros()) - .bind(req.amount.value as i64) - .bind(req.amount.fraction as i32) + .bind_timestamp(&Timestamp::now()) + .bind_amount(&req.amount) .bind(req.wtid.as_slice()) .bind(self.payto.as_str()) .bind(req.credit_account.as_str()) @@ -142,22 +144,18 @@ impl WireGatewayImpl for ServerState { } async fn outgoing_history(&self, params: History) -> ApiResult<OutgoingHistory> { - let outgoing_transactions = QueryBuilder::new( - "SELECT id, created, (amount).val, (amount).frac, wtid, credit_acc, exchange_url FROM tx_out WHERE", - ).page("id", &params.page) - .build() - .try_map(|r| { - Ok(OutgoingBankTransaction { - row_id: r.try_get_safeu64(0)?, - date: r.try_get_timestamp(1)?, - amount: r.try_get_amount_i(2, self.currency())?, - wtid: r.try_get_base32(4)?, - credit_account: r.try_get_url(5)?, - exchange_base_url: r.try_get_url(6)?, - }) + let outgoing_transactions = page(&self.pool, "id", &params.page, || QueryBuilder::new( + "SELECT id, created, (amount).val, (amount).frac, wtid, credit_acc, exchange_url FROM tx_out WHERE" + ), |r| { + Ok(OutgoingBankTransaction { + row_id: r.try_get_safeu64(0)?, + date: r.try_get_timestamp(1)?, + amount: r.try_get_amount_i(2, self.currency())?, + wtid: r.try_get_base32(4)?, + credit_account: r.try_get_url(5)?, + exchange_base_url: r.try_get_url(6)?, }) - .fetch_all(&self.pool) - .await?; + }).await?; Ok(OutgoingHistory { debit_account: self.payto.clone(), outgoing_transactions, @@ -165,10 +163,16 @@ impl WireGatewayImpl for ServerState { } async fn incoming_history(&self, params: History) -> ApiResult<IncomingHistory> { - let incoming_transactions = QueryBuilder::new("SELECT id, received, (amount).val, (amount).frac, reserve_pub, debit_acc FROM tx_in WHERE") - .page("id", &params.page) - .build() - .try_map(|r| { + let incoming_transactions = page( + &self.pool, + "id", + &params.page, + || { + QueryBuilder::new( + "SELECT id, received, (amount).val, (amount).frac, reserve_pub, debit_acc FROM tx_in WHERE" + ) + }, + |r| { Ok(IncomingBankTransaction::IncomingReserveTransaction { row_id: r.try_get_safeu64(0)?, date: r.try_get_timestamp(1)?, @@ -176,9 +180,9 @@ impl WireGatewayImpl for ServerState { reserve_pub: r.try_get_base32(4)?, debit_account: r.try_get_url(5)?, }) - }) - .fetch_all(&self.pool) - .await?; + }, + ) + .await?; Ok(IncomingHistory { credit_account: self.payto.clone(), incoming_transactions, @@ -191,9 +195,8 @@ impl WireGatewayImpl for ServerState { ) -> ApiResult<AddIncomingResponse> { let timestamp = Timestamp::now(); let r = sqlx::query("INSERT INTO tx_in (received, amount, reserve_pub, debit_acc, credit_acc) VALUES ($1, ($2, $3)::taler_amount, $4, $5, $6) RETURNING id") - .bind(Timestamp::now().as_sql_micros()) - .bind(req.amount.value as i64) - .bind(req.amount.fraction as i32) + .bind_timestamp(&Timestamp::now()) + .bind_amount(&req.amount) .bind(req.reserve_pub.as_slice()) .bind(req.debit_account.as_str()) .bind("payto://bitcoin/bcrt1qgkgxkjj27g3f7s87mcvjjsghay7gh34cx39prj")