Skip to content

Commit

Permalink
feat: implement u5c ledger (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
scarmuega authored Oct 6, 2024
1 parent d8f3eeb commit ce8189c
Show file tree
Hide file tree
Showing 9 changed files with 179 additions and 52 deletions.
23 changes: 4 additions & 19 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion balius-runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ redb = "2.1.3"
tracing = "0.1.40"
hex = "0.4.3"
itertools = "0.13.0"
utxorpc = { version = "0.6.0", optional = true }
async-trait = "0.1.83"
utxorpc = { version = "0.7.1", optional = true }

[dev-dependencies]
tokio = "1.40.0"
11 changes: 6 additions & 5 deletions balius-runtime/src/ledgers/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ use crate::wit::balius::app::ledger as wit;
#[derive(Clone)]
pub struct Ledger;

#[async_trait::async_trait]
impl wit::Host for Ledger {
async fn read_utxos(
impl Ledger {
pub async fn read_utxos(
&mut self,
_refs: Vec<wit::TxoRef>,
) -> Result<Vec<wit::Utxo>, wit::LedgerError> {
Expand All @@ -30,10 +29,12 @@ impl wit::Host for Ledger {
}])
}

async fn search_utxos(
pub async fn search_utxos(
&mut self,
_pattern: wit::UtxoPattern,
) -> Result<Vec<wit::Utxo>, wit::LedgerError> {
_start: Option<String>,
_max_items: u32,
) -> Result<wit::UtxoPage, wit::LedgerError> {
todo!()
}
}
8 changes: 5 additions & 3 deletions balius-runtime/src/ledgers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,14 @@ impl wit::Host for Ledger {
async fn search_utxos(
&mut self,
pattern: wit::UtxoPattern,
) -> Result<Vec<wit::Utxo>, wit::LedgerError> {
start: Option<String>,
max_items: u32,
) -> Result<wit::UtxoPage, wit::LedgerError> {
match self {
Ledger::Mock(ledger) => ledger.search_utxos(pattern).await,
Ledger::Mock(ledger) => ledger.search_utxos(pattern, start, max_items).await,

#[cfg(feature = "utxorpc")]
Ledger::U5C(ledger) => ledger.search_utxos(pattern).await,
Ledger::U5C(ledger) => ledger.search_utxos(pattern, start, max_items).await,
}
}
}
99 changes: 84 additions & 15 deletions balius-runtime/src/ledgers/u5c.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use std::sync::Arc;
use utxorpc::CardanoQueryClient;

use crate::wit::balius::app::ledger as wit;
Expand All @@ -9,14 +8,83 @@ impl From<utxorpc::Error> for crate::Error {
}
}

impl From<utxorpc::Error> for wit::LedgerError {
fn from(error: utxorpc::Error) -> Self {
wit::LedgerError::Upstream(error.to_string())
}
}

impl From<wit::TxoRef> for utxorpc::spec::query::TxoRef {
fn from(value: wit::TxoRef) -> Self {
utxorpc::spec::query::TxoRef {
hash: value.tx_hash.into(),
index: value.tx_index,
}
}
}

impl From<utxorpc::spec::query::TxoRef> for wit::TxoRef {
fn from(value: utxorpc::spec::query::TxoRef) -> Self {
wit::TxoRef {
tx_hash: value.hash.into(),
tx_index: value.index,
}
}
}

impl From<utxorpc::ChainUtxo<utxorpc::spec::cardano::TxOutput>> for wit::Utxo {
fn from(value: utxorpc::ChainUtxo<utxorpc::spec::cardano::TxOutput>) -> Self {
wit::Utxo {
body: value.native.into(),
ref_: value.txo_ref.unwrap_or_default().into(),
}
}
}

impl From<wit::AddressPattern> for utxorpc::spec::cardano::AddressPattern {
fn from(value: wit::AddressPattern) -> Self {
utxorpc::spec::cardano::AddressPattern {
exact_address: value.exact_address.into(),
..Default::default()
}
}
}

impl From<wit::AssetPattern> for utxorpc::spec::cardano::AssetPattern {
fn from(value: wit::AssetPattern) -> Self {
utxorpc::spec::cardano::AssetPattern {
policy_id: value.policy.into(),
asset_name: value.name.map(|n| n.into()).unwrap_or_default(),
}
}
}

impl From<wit::UtxoPattern> for utxorpc::spec::cardano::TxOutputPattern {
fn from(value: wit::UtxoPattern) -> Self {
utxorpc::spec::cardano::TxOutputPattern {
address: value.address.map(|a| a.into()),
asset: value.asset.map(|a| a.into()),
}
}
}

impl From<utxorpc::UtxoPage<utxorpc::Cardano>> for wit::UtxoPage {
fn from(value: utxorpc::UtxoPage<utxorpc::Cardano>) -> Self {
wit::UtxoPage {
utxos: value.items.into_iter().map(|u| u.into()).collect(),
next_token: value.next,
}
}
}

pub struct Config {
endpoint_url: String,
api_key: String,
pub endpoint_url: String,
pub api_key: String,
}

#[derive(Clone)]
pub struct Ledger {
queries: Arc<utxorpc::CardanoQueryClient>,
queries: utxorpc::CardanoQueryClient,
}

impl Ledger {
Expand All @@ -27,25 +95,26 @@ impl Ledger {
.build::<CardanoQueryClient>()
.await;

Ok(Self {
queries: Arc::new(queries),
})
Ok(Self { queries })
}
}

#[async_trait::async_trait]
impl crate::wit::balius::app::ledger::Host for Ledger {
async fn read_utxos(
pub async fn read_utxos(
&mut self,
refs: Vec<wit::TxoRef>,
) -> Result<Vec<wit::Utxo>, wit::LedgerError> {
todo!()
let refs = refs.into_iter().map(|r| r.into()).collect();
let utxos = self.queries.read_utxos(refs).await?;
Ok(utxos.into_iter().map(|u| u.into()).collect())
}

async fn search_utxos(
pub async fn search_utxos(
&mut self,
pattern: wit::UtxoPattern,
) -> Result<Vec<wit::Utxo>, wit::LedgerError> {
todo!()
start: Option<String>,
max_items: u32,
) -> Result<wit::UtxoPage, wit::LedgerError> {
let pattern = pattern.into();
let utxos = self.queries.match_utxos(pattern, start, max_items).await?;
Ok(utxos.into())
}
}
Binary file modified balius-runtime/tests/faucet.wasm
Binary file not shown.
58 changes: 58 additions & 0 deletions balius-runtime/tests/u5c.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#![cfg(test)]

use balius_runtime::{ledgers, Runtime, Store};
use serde_json::json;

#[tokio::test]
async fn faucet_claim() {
let store = Store::open("tests/balius.db", None).unwrap();

let ledger = ledgers::u5c::Ledger::new(ledgers::u5c::Config {
endpoint_url: "https://mainnet.utxorpc-v0.demeter.run".to_string(),
api_key: "dmtr_utxorpc1wgnnj0qcfj32zxsz2uc8d4g7uclm2s2w".to_string(),
})
.await
.unwrap();

let mut runtime = Runtime::builder(store)
.with_ledger(ledger.into())
.build()
.unwrap();

let config = json!({
"validator": {
"ref_txo": {
"transaction_id": "f7d3837715680f3a170e99cd202b726842d97f82c05af8fcd18053c64e33ec4f",
"index": 0
},
"hash": "ef7a1cebb2dc7de884ddf82f8fcbc91fe9750dcd8c12ec7643a99bbe",
"address": "addr1qx2fxv2umyhttkxyxp8x0dlpdt3k6cwng5pxj3jhsydzer3n0d3vllmyqwsx5wktcd8cc3sq835lu7drv2xwl2wywfgse35a3x"
}
});

runtime
.register_worker("faucet", "tests/faucet.wasm", config)
.await
.unwrap();

let req = json!({
"token": "54455354",
"quantity": 1,
"recipient": "addr1qx2fxv2umyhttkxyxp8x0dlpdt3k6cwng5pxj3jhsydzer3n0d3vllmyqwsx5wktcd8cc3sq835lu7drv2xwl2wywfgse35a3x",
"fuel": {
"Refs": [
{
"hash": "6ac91cdc14155f56c8819740ec334c1e3ff5f9055ba27909045e31ebd329b783",
"index": 0
}
]
}
});

let res = runtime
.handle_request("faucet", "claim", req)
.await
.unwrap();

println!("{:?}", res);
}
6 changes: 3 additions & 3 deletions balius-sdk/src/qol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ impl From<Error> for wit::HandleError {
code: 2,
message: "bad params".to_owned(),
},
Error::Ledger(e) => wit::HandleError {
code: e,
message: "ledger error".to_string(),
Error::Ledger(err) => wit::HandleError {
code: 4,
message: err.to_string(),
},
}
}
Expand Down
24 changes: 18 additions & 6 deletions wit/balius.wit
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,13 @@ interface kv {
}

interface ledger {
type ledger-error = u32;
type cbor = list<u8>;

variant ledger-error {
upstream(string),
internal(string),
not-found(txo-ref)
}

record txo-ref {
tx-hash: list<u8>,
Expand All @@ -23,20 +28,27 @@ interface ledger {
ref: txo-ref,
}

type address = list<u8>;
record address-pattern {
exact-address: list<u8>,
}

record token-pattern {
record asset-pattern {
policy: list<u8>,
name: option<list<u8>>,
}

record utxo-pattern {
address: option<address>,
token: option<token-pattern>,
address: option<address-pattern>,
asset: option<asset-pattern>,
}

record utxo-page {
utxos: list<utxo>,
next-token: option<string>,
}

read-utxos: func(refs: list<txo-ref>) -> result<list<utxo>, ledger-error>;
search-utxos: func(pattern: utxo-pattern) -> result<list<utxo>, ledger-error>;
search-utxos: func(pattern: utxo-pattern, start: option<string>, max-items: u32) -> result<utxo-page, ledger-error>;
}

interface submit {
Expand Down

0 comments on commit ce8189c

Please sign in to comment.