Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make SC return tx_id #165

Merged
merged 2 commits into from
Jan 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -5,6 +5,7 @@ use checking::{
};
use clap::Parser;
use naumachia::scripts::context::PubKeyHash;
use naumachia::transaction::TxId;
use naumachia::{
smart_contract::{SmartContract, SmartContractTrait},
trireme_ledger_client::get_trireme_ledger_client_from_file,
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
@@ -1,6 +1,7 @@
use async_trait::async_trait;
use std::fmt::Debug;

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

/// Interface defining how to interact with your smart contract
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