Skip to content

Commit 11a092d

Browse files
committed
wip tx building code
1 parent b7fe298 commit 11a092d

File tree

4 files changed

+61
-23
lines changed

4 files changed

+61
-23
lines changed

chaincash_app/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use chaincash_offchain::{node::node_from_config, TransactionBuilder};
1+
use chaincash_offchain::{node::node_from_config, TransactionService};
22
use chaincash_server::{Server, ServerState};
33
use chaincash_store::{ChainCashStore, Update};
44
use config::{Environment, File};
@@ -64,12 +64,12 @@ impl ChainCashApp {
6464
});
6565

6666
let node = node_from_config(&self.config.node);
67-
let tx_builder = TransactionBuilder::new(node.clone());
67+
let tx_service = TransactionService::new(node.clone());
6868

6969
let state = ServerState {
7070
store,
7171
node,
72-
tx_builder,
72+
tx_service,
7373
};
7474

7575
Ok(Server::serve(listener, state).await?)

chaincash_offchain/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@ pub mod contracts;
22
pub mod node;
33
pub mod transactions;
44

5+
pub use ergo_node_interface::NanoErg;
56
pub use node::NodeInterface;
6-
pub use transactions::TransactionBuilder;
7+
pub use transactions::TransactionService;
Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,54 @@
11
pub mod reserves;
22

3-
use ergo_lib::ergo_chain_types::EcPoint;
3+
use crate::NanoErg;
4+
use ergo_lib::{ergo_chain_types::EcPoint, wallet::tx_builder::SUGGESTED_TX_FEE};
45
use ergo_node_interface::NodeInterface;
56

67
#[derive(Clone)]
7-
pub struct TransactionBuilder {
8+
pub struct TransactionService {
89
// TODO: inputs cache so we dont need to fetch after each tx build
910
node: NodeInterface,
11+
fee: NanoErg,
1012
}
1113

12-
impl TransactionBuilder {
14+
struct TxContext {
15+
current_height: u64,
16+
change_address: String,
17+
fee: NanoErg,
18+
}
19+
20+
impl TransactionService {
1321
pub fn new(node: NodeInterface) -> Self {
14-
Self { node }
22+
Self::with_fee(node, SUGGESTED_TX_FEE().as_u64().clone())
23+
}
24+
25+
pub fn with_fee(node: NodeInterface, fee: NanoErg) -> Self {
26+
Self { node, fee }
27+
}
28+
29+
// TODO: handle request failures
30+
// handle wallet uninitialized/locked
31+
// handle no change address
32+
fn get_ctx(&self) -> TxContext {
33+
let wallet_status = self.node.wallet_status().unwrap();
34+
35+
TxContext {
36+
current_height: self.node.current_block_height().unwrap(),
37+
change_address: wallet_status.change_address.unwrap(),
38+
fee: self.fee,
39+
}
1540
}
1641

17-
pub fn create_reserve(pk: EcPoint, amount: u64) {
18-
// fetch inputs (and cache?)
19-
todo!()
42+
pub fn create_reserve(&self, pk: EcPoint, amount: NanoErg) {
43+
// TODO: get utxos from a cache
44+
let ctx = self.get_ctx();
45+
let inputs = self
46+
.node
47+
.unspent_boxes_with_min_total(amount + ctx.fee)
48+
.unwrap();
49+
// transform inputs into box_selector
50+
// build reserve output ErgoboxCandidate
51+
// still want to use a separate function to actually build the tx so we can decouple from
52+
// node usage and make the building testable
2053
}
2154
}

chaincash_server/src/app.rs

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
//! ChainCash payment server creation and serving.
22
use axum::{routing::get, Router};
3-
use chaincash_offchain::{NodeInterface, TransactionBuilder};
3+
use chaincash_offchain::{NodeInterface, TransactionService};
44
use chaincash_store::ChainCashStore;
55
use tracing::info;
66

77
#[derive(Clone)]
88
pub struct ServerState {
99
pub store: ChainCashStore,
1010
pub node: NodeInterface,
11-
pub tx_builder: TransactionBuilder,
11+
pub tx_service: TransactionService,
1212
}
1313

1414
pub struct Server;
@@ -35,26 +35,30 @@ impl Server {
3535
}
3636

3737
#[cfg(test)]
38-
mod tests {
39-
use hyper::{Body, Request, StatusCode};
40-
use tower::ServiceExt;
41-
42-
use super::*;
43-
44-
fn make_state() -> ServerState {
45-
let node = NodeInterface::new("", "", "").unwrap();
38+
impl ServerState {
39+
pub fn for_test() -> Self {
40+
// node shouldn't be actually used in unit tests
41+
let node = NodeInterface::new("hello", "127.0.0.1", "9032").unwrap();
4642

4743
ServerState {
4844
store: ChainCashStore::open_in_memory().unwrap(),
4945
node: node.clone(),
50-
tx_builder: TransactionBuilder::new(node),
46+
tx_service: TransactionService::new(node),
5147
}
5248
}
49+
}
50+
51+
#[cfg(test)]
52+
mod tests {
53+
use hyper::{Body, Request, StatusCode};
54+
use tower::ServiceExt;
55+
56+
use super::*;
5357

5458
#[tokio::test]
5559
async fn test_healthcheck() {
5660
let response = Server::router()
57-
.with_state(make_state())
61+
.with_state(ServerState::for_test())
5862
.oneshot(Request::get("/healthcheck").body(Body::default()).unwrap())
5963
.await
6064
.unwrap();

0 commit comments

Comments
 (0)