summaryrefslogtreecommitdiff
path: root/eth-wire
diff options
context:
space:
mode:
authorAntoine A <>2022-03-07 18:12:04 +0100
committerAntoine A <>2022-03-07 18:12:04 +0100
commit06cbb24cbc316e7c9eb07480198af885f6ba492e (patch)
tree5973f687ee31c537c43ec58f46a43e6e1ada5412 /eth-wire
parenta02daa453da90f15ac1033e44ba3aaa54d38d1c3 (diff)
downloaddepolymerization-06cbb24cbc316e7c9eb07480198af885f6ba492e.tar.gz
depolymerization-06cbb24cbc316e7c9eb07480198af885f6ba492e.tar.bz2
depolymerization-06cbb24cbc316e7c9eb07480198af885f6ba492e.zip
Network based currency names
Diffstat (limited to 'eth-wire')
-rw-r--r--eth-wire/src/bin/eth-wire-utils.rs12
-rw-r--r--eth-wire/src/lib.rs37
-rw-r--r--eth-wire/src/loops/worker.rs8
-rw-r--r--eth-wire/src/main.rs2
-rw-r--r--eth-wire/src/sql.rs15
-rw-r--r--eth-wire/src/taler_util.rs16
6 files changed, 59 insertions, 31 deletions
diff --git a/eth-wire/src/bin/eth-wire-utils.rs b/eth-wire/src/bin/eth-wire-utils.rs
index b640a0d..7173807 100644
--- a/eth-wire/src/bin/eth-wire-utils.rs
+++ b/eth-wire/src/bin/eth-wire-utils.rs
@@ -104,7 +104,7 @@ enum Cmd {
fn main() {
init();
let args: Args = Args::parse();
- let taler_config = load_taler_config(args.config.as_deref());
+ let (taler_config, currency) = load_taler_config(args.config.as_deref());
let ipc_path = args.datadir.unwrap_or(taler_config.custom);
let mut rpc = Rpc::new(ipc_path).unwrap();
@@ -120,8 +120,9 @@ fn main() {
let to = H160::from_str(&to).unwrap();
rpc.unlock_account(&from, &passwd).ok();
for amount in amounts {
- let amount = Amount::from_str(&format!("ETH:{}{}", fmt, amount)).unwrap();
- let value = taler_to_eth(&amount).unwrap();
+ let amount =
+ Amount::from_str(&format!("{}:{}{}", currency.to_str(), fmt, amount)).unwrap();
+ let value = taler_to_eth(&amount, currency).unwrap();
rpc.deposit(from, to, value, rand_slice()).unwrap();
}
}
@@ -135,8 +136,9 @@ fn main() {
let to = H160::from_str(&to).unwrap();
rpc.unlock_account(&from, &passwd).ok();
for amount in amounts {
- let amount = Amount::from_str(&format!("ETH:{}{}", fmt, amount)).unwrap();
- let value = taler_to_eth(&amount).unwrap();
+ let amount =
+ Amount::from_str(&format!("{}:{}{}", currency.to_str(), fmt, amount)).unwrap();
+ let value = taler_to_eth(&amount, currency).unwrap();
rpc.send_transaction(&TransactionRequest {
from,
to,
diff --git a/eth-wire/src/lib.rs b/eth-wire/src/lib.rs
index 9a13378..b615acd 100644
--- a/eth-wire/src/lib.rs
+++ b/eth-wire/src/lib.rs
@@ -21,7 +21,14 @@ use std::{
sync::atomic::AtomicU32,
};
-use common::{api_common::Amount, config::TalerConfig, log::OrFail, postgres, url::Url};
+use common::{
+ api_common::Amount,
+ config::TalerConfig,
+ currency::{Currency, CurrencyEth},
+ log::{fail, OrFail},
+ postgres,
+ url::Url,
+};
use ethereum_types::{Address, H160, H256, U256, U64};
use metadata::{InMetadata, OutMetadata};
use rpc::{hex::Hex, Rpc, RpcClient, RpcStream, Transaction};
@@ -208,7 +215,7 @@ impl SyncState {
}
const DEFAULT_CONFIRMATION: u16 = 24;
-const DEFAULT_BOUNCE_FEE: &str = "ETH:0.00001";
+const DEFAULT_BOUNCE_FEE: &str = "0.00001";
pub struct WireState {
pub confirmation: AtomicU32,
@@ -221,11 +228,12 @@ pub struct WireState {
pub base_url: Url,
pub payto: Url,
pub db_config: postgres::Config,
+ pub currency: CurrencyEth,
}
impl WireState {
pub fn load_taler_config(file: Option<&Path>) -> Self {
- let taler_config = load_taler_config(file);
+ let (taler_config, currency) = load_taler_config(file);
let init_confirmation = taler_config.confirmation.unwrap_or(DEFAULT_CONFIRMATION) as u32;
let payto = taler_config.require_payto();
Self {
@@ -233,29 +241,38 @@ impl WireState {
max_confirmations: init_confirmation * 2,
address: eth_payto_addr(&payto).unwrap(),
ipc_path: taler_config.custom,
- bounce_fee: config_bounce_fee(&taler_config.bounce_fee),
+ bounce_fee: config_bounce_fee(&taler_config.bounce_fee, currency),
lifetime: taler_config.wire_lifetime,
bump_delay: taler_config.bump_delay,
base_url: taler_config.base_url,
db_config: taler_config.db_config,
payto,
+ currency,
}
}
}
// Load taler config with eth-wire specific config
-pub fn load_taler_config(file: Option<&Path>) -> TalerConfig<PathBuf> {
- TalerConfig::load_with_custom(file, |dep| {
+pub fn load_taler_config(file: Option<&Path>) -> (TalerConfig<PathBuf>, CurrencyEth) {
+ let config = TalerConfig::load_with_custom(file, |dep| {
common::config::path(dep, "IPC_PATH").unwrap_or_else(default_data_dir)
- })
+ });
+ let currency = match config.currency {
+ Currency::ETH(it) => it,
+ _ => fail(format!(
+ "currency {} is not supported by eth-wire",
+ config.currency.to_str()
+ )),
+ };
+ (config, currency)
}
// Parse ethereum value from config bounce fee
-fn config_bounce_fee(bounce_fee: &Option<String>) -> U256 {
+fn config_bounce_fee(bounce_fee: &Option<String>, currency: CurrencyEth) -> U256 {
let config = bounce_fee.as_deref().unwrap_or(DEFAULT_BOUNCE_FEE);
- Amount::from_str(config)
+ Amount::from_str(&format!("{}:{}", currency.to_str(), config))
.map_err(|s| s.to_string())
- .and_then(|a| taler_to_eth(&a))
+ .and_then(|a| taler_to_eth(&a, currency))
.or_fail(|a| {
format!(
"config value BOUNCE_FEE={} is not a valid ethereum amount: {}",
diff --git a/eth-wire/src/loops/worker.rs b/eth-wire/src/loops/worker.rs
index d2fd409..2fc9ca9 100644
--- a/eth-wire/src/loops/worker.rs
+++ b/eth-wire/src/loops/worker.rs
@@ -273,7 +273,7 @@ fn sync_chain_incoming_confirmed(
Ok(metadata) => match metadata {
InMetadata::Deposit { reserve_pub } => {
let date = SystemTime::now();
- let amount = eth_to_taler(&tx.value);
+ 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 (_date, amount, reserve_pub, debit_acc, credit_acc) VALUES ($1, $2, $3, $4, $5) ON CONFLICT (reserve_pub) DO NOTHING ", &[
&date, &amount.to_string(), &reserve_pub.as_ref(), &eth_payto_url(&credit_addr).as_ref(), &state.payto.as_ref()
@@ -306,7 +306,7 @@ fn sync_chain_outgoing(tx: &SyncTransaction, db: &mut Client, state: &WireState)
match OutMetadata::decode(&tx.input) {
Ok(metadata) => match metadata {
OutMetadata::Withdraw { wtid, .. } => {
- let amount = eth_to_taler(&tx.value);
+ let amount = eth_to_taler(&tx.value, state.currency);
let credit_addr = tx.to.unwrap();
// Get previous out tx
let row = db.query_opt(
@@ -422,7 +422,7 @@ fn withdraw(db: &mut Client, rpc: &mut Rpc, state: &WireState) -> LoopResult<boo
)?;
if let Some(row) = &row {
let id: i32 = row.get(0);
- let amount = sql_eth_amount(row, 1);
+ let amount = sql_eth_amount(row, 1, state.currency);
let wtid: [u8; 32] = sql_array(row, 2);
let addr = sql_addr(row, 3);
let url = sql_url(row, 4);
@@ -432,7 +432,7 @@ fn withdraw(db: &mut Client, rpc: &mut Rpc, state: &WireState) -> LoopResult<boo
"UPDATE tx_out SET status=$1, txid=$2, sent=now() WHERE id=$3",
&[&(WithdrawStatus::Sent as i16), &tx_id.as_ref(), &id],
)?;
- let amount = eth_to_taler(&amount);
+ let amount = eth_to_taler(&amount, state.currency);
info!(
">> {} {} in {} to {}",
amount,
diff --git a/eth-wire/src/main.rs b/eth-wire/src/main.rs
index 4cf11ca..85e9274 100644
--- a/eth-wire/src/main.rs
+++ b/eth-wire/src/main.rs
@@ -59,7 +59,7 @@ fn main() {
fn init(config: Option<PathBuf>, init: Init) {
// Parse taler config
- let taler_config = load_taler_config(config.as_deref());
+ let (taler_config, _) = load_taler_config(config.as_deref());
// Connect to database
let mut db = taler_config
.db_config
diff --git a/eth-wire/src/sql.rs b/eth-wire/src/sql.rs
index 5d2dac6..903e6e8 100644
--- a/eth-wire/src/sql.rs
+++ b/eth-wire/src/sql.rs
@@ -14,32 +14,37 @@
TALER; see the file COPYING. If not, see <http://www.gnu.org/licenses/>
*/
use common::{
+ currency::CurrencyEth,
+ log::OrFail,
postgres::Row,
sql::{sql_amount, sql_array, sql_url},
};
use eth_wire::taler_util::{eth_payto_addr, taler_to_eth};
use ethereum_types::{H160, H256, U256};
-pub fn sql_eth_amount(row: &Row, idx: usize) -> U256 {
+/// Ethereum amount from sql
+pub fn sql_eth_amount(row: &Row, idx: usize, currency: CurrencyEth) -> U256 {
let amount = sql_amount(row, idx);
- taler_to_eth(&amount).unwrap_or_else(|_| {
- panic!(
+ taler_to_eth(&amount, currency).or_fail(|_| {
+ format!(
"Database invariant: expected an ethereum amount got {}",
amount
)
})
}
+/// Ethereum address from sql
pub fn sql_addr(row: &Row, idx: usize) -> H160 {
let url = sql_url(row, idx);
- eth_payto_addr(&url).unwrap_or_else(|_| {
- panic!(
+ eth_payto_addr(&url).or_fail(|_| {
+ format!(
"Database invariant: expected an ethereum payto url got {}",
url
)
})
}
+/// Ethereum hash from sql
pub fn sql_hash(row: &Row, idx: usize) -> H256 {
let array: [u8; 32] = sql_array(row, idx);
H256::from_slice(&array)
diff --git a/eth-wire/src/taler_util.rs b/eth-wire/src/taler_util.rs
index b5db5df..c4ff9aa 100644
--- a/eth-wire/src/taler_util.rs
+++ b/eth-wire/src/taler_util.rs
@@ -15,8 +15,8 @@
*/
use std::str::FromStr;
+use common::{api_common::Amount, currency::CurrencyEth, url::Url};
use ethereum_types::{Address, U256};
-use common::{api_common::Amount, url::Url};
pub const WEI: u64 = 1_000_000_000_000_000_000;
pub const TRUNC: u64 = 10_000_000_000;
@@ -43,18 +43,22 @@ pub fn eth_payto_addr(url: &Url) -> Result<Address, String> {
}
/// Transform a eth amount into a taler amount
-pub fn eth_to_taler(amount: &U256) -> Amount {
+pub fn eth_to_taler(amount: &U256, currency: CurrencyEth) -> Amount {
return Amount::new(
- "ETH",
+ currency.to_str(),
(amount / WEI).as_u64(),
((amount % WEI) / TRUNC).as_u32(),
);
}
/// Transform a eth amount into a btc amount
-pub fn taler_to_eth(amount: &Amount) -> Result<U256, String> {
- if amount.currency != "ETH" {
- return Err(format!("expected ETH for {}", amount.currency));
+pub fn taler_to_eth(amount: &Amount, currency: CurrencyEth) -> Result<U256, String> {
+ if amount.currency != currency.to_str() {
+ return Err(format!(
+ "expected currency {} got {}",
+ currency.to_str(),
+ amount.currency
+ ));
}
Ok(U256::from(amount.value) * WEI + U256::from(amount.fraction) * TRUNC)