Skip to content

Commit

Permalink
feat: reference ledger in a contract
Browse files Browse the repository at this point in the history
  • Loading branch information
SupernaviX committed Nov 15, 2024
1 parent 328a7ac commit 7214418
Show file tree
Hide file tree
Showing 12 changed files with 935 additions and 269 deletions.
124 changes: 111 additions & 13 deletions Cargo.lock

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

5 changes: 3 additions & 2 deletions firefly-cardanoconnect/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ aide = { version = "0.13", features = ["axum"] }
anyhow = "1"
async-trait = "0.1"
axum = { version = "0.7", features = ["macros", "ws"] }
balius-runtime = { git = "https://github.com/txpipe/balius.git", rev = "09672d5" }
blockfrost = { version = "1", default-features = false, features = ["rustls-tls"] }
balius-runtime = { git = "https://github.com/SupernaviX/balius.git", rev = "e17cd93", features = ["utxorpc"] }
blockfrost = { git = "https://github.com/SupernaviX/blockfrost-rust.git", rev = "ffb40ae", default-features = false, features = ["rustls-tls"] }
clap = { version = "4", features = ["derive"] }
chrono = "0.4"
dashmap = "6"
futures = "0.3"
pallas-addresses = "0.31"
pallas-codec = "0.31"
pallas-crypto = "0.31"
pallas-primitives = "0.31"
Expand Down
50 changes: 38 additions & 12 deletions firefly-cardanoconnect/src/contracts.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
use std::path::PathBuf;
use std::{path::PathBuf, sync::Arc};

use anyhow::{bail, Result};
use balius_runtime::{Runtime, Store};
use balius_runtime::{
ledgers::Ledger,
Response, Runtime, Store,
};
use ledger::BlockfrostLedger;
use serde::Deserialize;
use serde_json::{json, Value};
use tokio::{fs, sync::RwLock};
use tokio::{
fs,
sync::{Mutex, RwLock},
};

mod ledger;

#[derive(Debug, Deserialize, Clone)]
#[serde(rename_all = "camelCase")]
Expand All @@ -19,9 +28,9 @@ pub struct ContractManager {
}

impl ContractManager {
pub async fn new(config: &ContractsConfig) -> Result<Self> {
pub async fn new(config: &ContractsConfig, blockfrost_key: Option<&str>) -> Result<Self> {
fs::create_dir_all(&config.components_path).await?;
let runtime = Self::new_runtime(config).await?;
let runtime = Self::new_runtime(config, blockfrost_key).await?;
Ok(Self {
runtime: Some(RwLock::new(runtime)),
})
Expand All @@ -31,20 +40,37 @@ impl ContractManager {
Self { runtime: None }
}

pub async fn invoke(&self, contract: &str, method: &str, params: Value) -> Result<Value> {
pub async fn invoke(
&self,
contract: &str,
method: &str,
params: Value,
) -> Result<Option<Vec<u8>>> {
let params = serde_json::to_vec(&params)?;
let Some(rt_lock) = &self.runtime else {
bail!("Contract manager not configured");
};

let runtime = rt_lock.read().await;
let result = runtime.handle_request(contract, method, params).await?;

Ok(result)
let response = runtime.handle_request(contract, method, params).await?;
match response {
Response::PartialTx(bytes) => Ok(Some(bytes)),
_ => Ok(None),
}
}

async fn new_runtime(config: &ContractsConfig) -> Result<Runtime> {
async fn new_runtime(
config: &ContractsConfig,
blockfrost_key: Option<&str>,
) -> Result<Runtime> {
let store = Store::open(&config.store_path, config.cache_size)?;
let mut runtime = Runtime::builder(store).build()?;
let mut runtime_builder = Runtime::builder(store);
if let Some(key) = blockfrost_key {
let ledger = BlockfrostLedger::new(key);
runtime_builder =
runtime_builder.with_ledger(Ledger::Custom(Arc::new(Mutex::new(ledger))))
}
let mut runtime = runtime_builder.build()?;
let mut entries = fs::read_dir(&config.components_path).await?;
while let Some(entry) = entries.next_entry().await? {
let extless = entry.path().with_extension("");
Expand All @@ -53,7 +79,7 @@ impl ContractManager {
};
let wasm_path = entry.path();

runtime.register_worker(id, wasm_path, json!({})).await?;
runtime.register_worker(id, wasm_path, json!(null)).await?;
}
Ok(runtime)
}
Expand Down
Loading

0 comments on commit 7214418

Please sign in to comment.