Skip to content

Commit

Permalink
Make SC return tx_id
Browse files Browse the repository at this point in the history
  • Loading branch information
MitchTurner committed Jan 13, 2024
1 parent 32928f0 commit 262edbf
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 27 deletions.
18 changes: 11 additions & 7 deletions sample-dApps/always-succeeds-contract/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,21 @@ async fn main() {
let contract = SmartContract::new(logic, ledger_client);

match args.action {
ActionParams::Lock { amount } => contract
.hit_endpoint(AlwaysSucceedsEndpoints::Lock {
amount: (amount * 1_000_000.) as u64,
})
.await
.unwrap(),
ActionParams::Lock { amount } => {
let tx_id = contract
.hit_endpoint(AlwaysSucceedsEndpoints::Lock {
amount: (amount * 1_000_000.) as u64,
})
.await
.unwrap();
println!("TxId: {:?}", tx_id);
},
ActionParams::Claim { tx_hash, index } => {
let tx_hash_bytes = hex::decode(tx_hash).unwrap();
let output_id = OutputId::new(tx_hash_bytes, index);
let endpoint = AlwaysSucceedsEndpoints::Claim { output_id };
contract.hit_endpoint(endpoint).await.unwrap()
let tx_id = contract.hit_endpoint(endpoint).await.unwrap();
println!("TxId: {:?}", tx_id);
}
ActionParams::List { count } => {
let res = contract
Expand Down
2 changes: 1 addition & 1 deletion sample-dApps/checking_account/checking/aiken.lock
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ requirements = []
source = "github"

[etags]
"aiken-lang/stdlib@main" = [{ secs_since_epoch = 1704665601, nanos_since_epoch = 297637899 }, "cf946239d3dd481ed41f20e56bf24910b5229ea35aa171a708edc2a47fc20a7b"]
"aiken-lang/stdlib@main" = [{ secs_since_epoch = 1705105664, nanos_since_epoch = 813120107 }, "cf946239d3dd481ed41f20e56bf24910b5229ea35aa171a708edc2a47fc20a7b"]
11 changes: 8 additions & 3 deletions sample-dApps/checking_account/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use naumachia::{
smart_contract::{SmartContract, SmartContractTrait},
trireme_ledger_client::get_trireme_ledger_client_from_file,
};
use naumachia::transaction::TxId;

#[derive(Parser, Debug)]
#[clap(author, version, about, long_about = None)]
Expand Down Expand Up @@ -41,7 +42,7 @@ async fn main() -> Result<()> {
Ok(())
}

async fn hit_endpoint(endpoint: CheckingAccountEndpoints) -> Result<()> {
async fn hit_endpoint(endpoint: CheckingAccountEndpoints) -> Result<TxId> {
let logic = CheckingAccountLogic;
let ledger_client = get_trireme_ledger_client_from_file().await?;
let contract = SmartContract::new(logic, ledger_client);
Expand All @@ -61,7 +62,9 @@ async fn run_lookup(lookup: CheckingAccountLookups) -> Result<CheckingAccountLoo
async fn init_checking_account_impl(starting_ada: f64) -> Result<()> {
let starting_lovelace = (starting_ada * 1_000_000.0) as u64; // TODO: Panic
let endpoint = CheckingAccountEndpoints::InitAccount { starting_lovelace };
hit_endpoint(endpoint).await
let tx_id = hit_endpoint(endpoint).await?;
println!("TxId: {:?}", tx_id);
Ok(())
}

async fn my_account_impl() -> Result<()> {
Expand Down Expand Up @@ -115,5 +118,7 @@ async fn add_puller_impl() -> Result<()> {
period,
next_pull,
};
hit_endpoint(endpoint).await
let tx_id = hit_endpoint(endpoint).await?;
println!("TxId: {:?}", tx_id);
Ok(())
}
5 changes: 3 additions & 2 deletions sample-dApps/free-minting-contract/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@ async fn main() {
let ledger_client = get_trireme_ledger_client_from_file().await.unwrap();
let contract = SmartContract::new(logic, ledger_client);

match args.action {
let tx_id = match args.action {
ActionParams::Mint { amount } => contract
.hit_endpoint(FreeMintingEndpoints::Mint { amount })
.await
.unwrap(),
}
};
println!("TxId: {:?}", tx_id);
}
5 changes: 3 additions & 2 deletions sample-dApps/mint_nft/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ async fn main() {
let ledger_client = get_trireme_ledger_client_from_file().await.unwrap();
let contract = SmartContract::new(logic, ledger_client);

match args.action {
let tx_id = match args.action {
ActionParams::Mint => contract.hit_endpoint(MintNFTEndpoints::Mint).await.unwrap(),
}
};
println!("TxId: {:?}", tx_id);
}
19 changes: 11 additions & 8 deletions sample-dApps/time-locked-contract/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,22 @@ async fn main() {
let contract = SmartContract::new(logic, ledger_client);

match args.action {
ActionParams::Lock { amount, after_secs } => contract
.hit_endpoint(TimeLockedEndpoints::Lock {
amount: (amount * 1_000_000.) as u64,
after_secs,
})
.await
.unwrap(),
ActionParams::Lock { amount, after_secs } => {
let tx_id = contract
.hit_endpoint(TimeLockedEndpoints::Lock {
amount: (amount * 1_000_000.) as u64,
after_secs,
})
.await
.unwrap();
println!("TxId: {:?}", tx_id);
},
ActionParams::Claim { tx_hash, index } => {
let tx_hash_bytes = hex::decode(tx_hash).unwrap();
let output_id = OutputId::new(tx_hash_bytes, index);
let endpoint = TimeLockedEndpoints::Claim { output_id };
match contract.hit_endpoint(endpoint).await {
Ok(_) => println!("Claimed output :)"),
Ok(tx_id) => println!("Claimed output :) with tx_id: {:?}", tx_id),
Err(e) => println!("Error claiming output: {:?}", e),
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/smart_contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use async_trait::async_trait;
use std::fmt::Debug;

use crate::{error::Result, ledger_client::LedgerClient, logic::SCLogic};
use crate::transaction::TxId;

/// Interface defining how to interact with your smart contract
#[async_trait]
Expand All @@ -16,7 +17,7 @@ pub trait SmartContractTrait {
type LookupResponse;

/// Method for hitting specific endpoint
async fn hit_endpoint(&self, endpoint: Self::Endpoint) -> Result<()>;
async fn hit_endpoint(&self, endpoint: Self::Endpoint) -> Result<TxId>;
/// Method for querying specific data
async fn lookup(&self, lookup: Self::Lookup) -> Result<Self::LookupResponse>;
}
Expand Down Expand Up @@ -67,12 +68,11 @@ where
type Lookup = Logic::Lookups;
type LookupResponse = Logic::LookupResponses;

async fn hit_endpoint(&self, endpoint: Logic::Endpoints) -> Result<()> {
async fn hit_endpoint(&self, endpoint: Logic::Endpoints) -> Result<TxId> {
let tx_actions = Logic::handle_endpoint(endpoint, &self.ledger_client).await?;
let tx = tx_actions.to_unbuilt_tx()?;
let tx_id = self.ledger_client.issue(tx).await?;
println!("Transaction Submitted: {:?}", &tx_id);
Ok(())
Ok(tx_id)
}

async fn lookup(&self, lookup: Self::Lookup) -> Result<Self::LookupResponse> {
Expand Down

0 comments on commit 262edbf

Please sign in to comment.