Skip to content

Commit

Permalink
feat: add callback when tx is submitted
Browse files Browse the repository at this point in the history
  • Loading branch information
SupernaviX committed Jan 15, 2025
1 parent 9231be8 commit 838c967
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 6 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions firefly-balius/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ repository = "https://github.com/blockfrost/firefly-cardano"

[dependencies]
balius-sdk = { git = "https://github.com/txpipe/balius.git", rev = "a72601f" }
serde = { version = "1", features = ["derive"] }
71 changes: 69 additions & 2 deletions firefly-balius/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
use balius_sdk::txbuilder::{
primitives::TransactionInput, BuildContext, BuildError, InputExpr, UtxoSource,
use std::marker::PhantomData;

use balius_sdk::{
txbuilder::{primitives::TransactionInput, BuildContext, BuildError, InputExpr, UtxoSource},
wit, Ack, Params, Worker, WorkerResult,
_internal::Handler,
};
use serde::Deserialize;

pub struct CoinSelectionInput(pub UtxoSource, pub u64);

Expand Down Expand Up @@ -43,3 +48,65 @@ impl InputExpr for CoinSelectionInput {
}
}
}

#[derive(Deserialize)]
pub struct SubmittedTx {
pub method: String,
pub hash: String,
}

pub struct SubmittedTxHandler<F, C>
where
F: Fn(C, SubmittedTx) -> WorkerResult<Ack> + 'static,
C: TryFrom<wit::Config>,
{
func: F,
phantom: PhantomData<C>,
}

impl<F, C> From<F> for SubmittedTxHandler<F, C>
where
F: Fn(C, SubmittedTx) -> WorkerResult<Ack> + 'static,
C: TryFrom<wit::Config>,
{
fn from(func: F) -> Self {
Self {
func,
phantom: PhantomData,
}
}
}

impl<F, C> Handler for SubmittedTxHandler<F, C>
where
F: Fn(C, SubmittedTx) -> WorkerResult<Ack> + Send + Sync + 'static,
C: TryFrom<wit::Config, Error = balius_sdk::Error> + Send + Sync + 'static,
{
fn handle(
&self,
config: wit::Config,
event: wit::Event,
) -> Result<wit::Response, wit::HandleError> {
let config: C = config.try_into()?;
let event: Params<SubmittedTx> = event.try_into()?;
let response = (self.func)(config, event.0)?;
Ok(response.try_into()?)
}
}

pub trait WorkerExt {
fn with_tx_submitted_handler<C, F>(self, func: F) -> Self
where
C: TryFrom<wit::Config, Error = balius_sdk::Error> + Send + Sync + 'static,
F: Fn(C, SubmittedTx) -> WorkerResult<Ack> + Send + Sync + 'static;
}

impl WorkerExt for Worker {
fn with_tx_submitted_handler<C, F>(self, func: F) -> Self
where
C: TryFrom<wit::Config, Error = balius_sdk::Error> + Send + Sync + 'static,
F: Fn(C, SubmittedTx) -> WorkerResult<Ack> + Send + Sync + 'static,
{
self.with_request_handler("__tx_submitted", SubmittedTxHandler::from(func))
}
}
11 changes: 11 additions & 0 deletions firefly-cardanoconnect/src/contracts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,17 @@ impl ContractManager {
}
}

pub async fn handle_submit(&self, contract: &str, method: &str, tx_id: &str) {
let params = serde_json::json!({
"method": method,
"hash": tx_id,
});
let runtime = self.get_contract_runtime(contract).await;
let mut lock = runtime.lock().await;

let _: Result<_, _> = lock.invoke("__tx_submitted", params).await;
}

pub async fn listen(&self, listener: &Listener) -> ContractListener {
let contracts = find_contract_names(&listener.filters);
let mut runtimes = vec![];
Expand Down
4 changes: 3 additions & 1 deletion firefly-cardanoconnect/src/operations/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,9 @@ impl OperationsManager {
}
};
if let Some(tx) = value {
op.tx_id = Some(self.submit_transaction(from, tx).await?);
let tx_id = self.submit_transaction(from, tx).await?;
op.tx_id = Some(tx_id.clone());
self.contracts.handle_submit(contract, method, &tx_id).await;
}

op.status = OperationStatus::Succeeded;
Expand Down
1 change: 1 addition & 0 deletions wasm/simple-tx/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 11 additions & 3 deletions wasm/simple-tx/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ use balius_sdk::{
AddressPattern, BuildError, FeeChangeReturn, OutputBuilder, TxBuilder, UtxoPattern,
UtxoSource,
},
Config, FnHandler, NewTx, Params, Worker, WorkerResult,
Ack, Config, FnHandler, NewTx, Params, Worker, WorkerResult,
};
use firefly_balius::CoinSelectionInput;
use firefly_balius::{CoinSelectionInput, SubmittedTx, WorkerExt};
use pallas_addresses::Address;
use serde::Deserialize;

Expand Down Expand Up @@ -42,7 +42,15 @@ fn send_ada(_: Config<()>, req: Params<TransferRequest>) -> WorkerResult<NewTx>
Ok(NewTx(Box::new(tx)))
}

fn handle_submit(_: Config<()>, tx: SubmittedTx) -> WorkerResult<Ack> {
// TODO: track this
let _ = tx;
Ok(Ack)
}

#[balius_sdk::main]
fn main() -> Worker {
Worker::new().with_request_handler("send_ada", FnHandler::from(send_ada))
Worker::new()
.with_request_handler("send_ada", FnHandler::from(send_ada))
.with_tx_submitted_handler(handle_submit)
}

0 comments on commit 838c967

Please sign in to comment.