Skip to content

Commit 3ca1e3b

Browse files
authored
refactor: use TII format for invoke command (#49)
1 parent c2778be commit 3ca1e3b

File tree

13 files changed

+142
-920
lines changed

13 files changed

+142
-920
lines changed

Cargo.lock

Lines changed: 60 additions & 306 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,15 @@ keywords = ["cardano", "blockchain", "wallet"]
1414
categories = ["command-line-utilities", "blockchain", "cardano", "wallet"]
1515

1616
[dependencies]
17-
tx3-lang = "0.13.0"
18-
tx3-cardano = "0.13.0"
19-
tx3-sdk = "^0"
20-
21-
# tx3-lang = { git = "https://github.com/tx3-lang/tx3.git" }
22-
# tx3-cardano = { git = "https://github.com/tx3-lang/tx3.git" }
17+
# tx3-sdk = "^0"
2318
# tx3-sdk = { git = "https://github.com/tx3-lang/rust-sdk.git" }
24-
25-
# tx3-lang = { path = "../../tx3-lang/tx3/crates/tx3-lang" }
26-
# tx3-cardano = { path = "../../tx3-lang/tx3/crates/tx3-cardano" }
27-
# tx3-sdk = { path = "../../tx3-lang/rust-sdk/sdk" }
19+
tx3-sdk = { path = "../../tx3-lang/rust-sdk/sdk" }
2820

2921
# utxorpc = { git = "https://github.com/utxorpc/rust-sdk" }
3022
utxorpc = "0.12.0"
3123
# utxorpc = { path = "../../utxorpc/rust-sdk" }
3224

33-
bech32 = "0.9.1"
25+
bech32 = "0.11.1"
3426
bip39 = { version = "2.0.0", features = ["rand_core"] }
3527
chrono = { version = "0.4.39", features = ["serde"] }
3628
clap = { version = "4.5.29", features = ["derive", "env"] }

src/tx/common.rs

Lines changed: 40 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,20 @@ use std::{
66
collections::{BTreeMap, HashMap},
77
path::Path,
88
};
9-
use tx3_lang::{ArgValue, ProtoTx, Protocol, UtxoRef};
10-
use tx3_sdk::trp::{self, TxEnvelope};
9+
10+
use tx3_sdk::{
11+
tii::Invocation,
12+
trp::{self, TxEnvelope},
13+
WellKnownType,
14+
};
15+
use tx3_sdk::{ArgValue, UtxoRef};
1116

1217
use crate::provider::types::Provider;
1318

1419
pub fn load_args(
1520
inline_args: Option<&str>,
1621
file_args: Option<&Path>,
17-
params: &BTreeMap<String, tx3_lang::ir::Type>,
22+
params: &BTreeMap<String, WellKnownType>,
1823
) -> Result<HashMap<String, ArgValue>> {
1924
let json_string = match (inline_args, file_args) {
2025
(Some(inline_args), None) => inline_args.to_string(),
@@ -33,38 +38,40 @@ pub fn load_args(
3338

3439
for (key, ty) in params {
3540
if let Some(value) = value.remove(key) {
36-
let arg_value = tx3_sdk::trp::args::from_json(value, ty)?;
41+
let arg_value = tx3_sdk::interop::json::from_json(value, ty)?;
3742
args.insert(key.clone(), arg_value);
3843
}
3944
}
4045

4146
Ok(args)
4247
}
4348

44-
pub fn load_prototx(tx3_file: &Path, tx3_template: Option<String>) -> Result<ProtoTx> {
45-
let protocol = Protocol::from_file(tx3_file)
46-
.load()
47-
.context("parsing tx3 file")?;
49+
pub fn prepare_invocation(tii_file: &Path, tx: Option<String>) -> Result<Invocation> {
50+
let protocol = tx3_sdk::tii::Protocol::from_file(tii_file).context("parsing tii file")?;
4851

49-
let txs: Vec<String> = protocol.txs().map(|x| x.name.value.to_string()).collect();
50-
51-
let template = match tx3_template {
52+
let template = match tx {
5253
Some(template) => template,
5354
None => {
54-
let template = if txs.len() == 1 {
55-
txs.first().unwrap().clone()
55+
let template = if protocol.txs().len() == 1 {
56+
protocol.txs().keys().next().unwrap().clone()
5657
} else {
57-
inquire::Select::new("What transaction do you want to build?", txs).prompt()?
58+
let keys = protocol
59+
.txs()
60+
.keys()
61+
.map(|x| x.to_string())
62+
.collect::<Vec<String>>();
63+
64+
inquire::Select::new("What transaction do you want to build?", keys).prompt()?
5865
};
5966
template
6067
}
6168
};
6269

63-
Ok(protocol.new_tx(&template)?)
70+
Ok(protocol.invoke(&template, None)?)
6471
}
6572

6673
pub fn inquire_args(
67-
params: &BTreeMap<String, tx3_lang::ir::Type>,
74+
params: &BTreeMap<String, WellKnownType>,
6875
ctx: &crate::Context,
6976
provider: &Provider,
7077
) -> Result<HashMap<String, ArgValue>> {
@@ -74,7 +81,7 @@ pub fn inquire_args(
7481
let text_key = format!("{key}:");
7582

7683
match value {
77-
tx3_lang::ir::Type::Address => {
84+
WellKnownType::Address => {
7885
let custom_address = String::from("custom address");
7986
let mut options = ctx
8087
.store
@@ -104,7 +111,7 @@ pub fn inquire_args(
104111

105112
argvalues.insert(key.clone(), trp::ArgValue::Address(address.to_vec()));
106113
}
107-
tx3_lang::ir::Type::Int => {
114+
WellKnownType::Int => {
108115
let value = inquire::Text::new(&text_key)
109116
.with_help_message("Enter an integer value")
110117
.prompt()?
@@ -113,7 +120,7 @@ pub fn inquire_args(
113120

114121
argvalues.insert(key.clone(), trp::ArgValue::Int(value.into()));
115122
}
116-
tx3_lang::ir::Type::UtxoRef => {
123+
WellKnownType::UtxoRef => {
117124
let value = inquire::Text::new(&text_key)
118125
.with_help_message("Enter the utxo reference as hash#idx")
119126
.prompt()
@@ -130,12 +137,12 @@ pub fn inquire_args(
130137
let utxo_ref = UtxoRef::new(hash.as_slice(), idx);
131138
argvalues.insert(key.clone(), trp::ArgValue::UtxoRef(utxo_ref));
132139
}
133-
tx3_lang::ir::Type::Bool => {
140+
WellKnownType::Bool => {
134141
let value = inquire::Confirm::new(&text_key).prompt()?;
135142

136143
argvalues.insert(key.clone(), trp::ArgValue::Bool(value));
137144
}
138-
tx3_lang::ir::Type::Bytes => {
145+
WellKnownType::Bytes => {
139146
let value = inquire::Text::new(&text_key)
140147
.with_help_message("Enter the bytes as hex string")
141148
.prompt()?;
@@ -145,37 +152,37 @@ pub fn inquire_args(
145152
argvalues.insert(key.clone(), trp::ArgValue::Bytes(value));
146153
}
147154

148-
tx3_lang::ir::Type::Undefined => {
155+
WellKnownType::Undefined => {
149156
return Err(anyhow::anyhow!(
150157
"tx3 arg {key} is of type Undefined, not supported yet"
151158
));
152159
}
153-
tx3_lang::ir::Type::Unit => {
160+
WellKnownType::Unit => {
154161
return Err(anyhow::anyhow!(
155162
"tx3 arg {key} is of type Unit, not supported yet",
156163
));
157164
}
158-
tx3_lang::ir::Type::Utxo => {
165+
WellKnownType::Utxo => {
159166
return Err(anyhow::anyhow!(
160167
"tx3 arg {key} is of type Utxo, not supported yet"
161168
));
162169
}
163-
tx3_lang::ir::Type::AnyAsset => {
170+
WellKnownType::AnyAsset => {
164171
return Err(anyhow::anyhow!(
165172
"tx3 arg {key} is of type AnyAsset, not supported yet"
166173
));
167174
}
168-
tx3_lang::ir::Type::List => {
175+
WellKnownType::List => {
169176
return Err(anyhow::anyhow!(
170177
"tx3 arg {key} is of type List, not supported yet",
171178
));
172179
}
173-
tx3_lang::ir::Type::Custom(x) => {
180+
WellKnownType::Custom(x) => {
174181
return Err(anyhow::anyhow!(
175182
"tx3 arg {key} is a custom type {x}, not supported yet"
176183
));
177184
}
178-
tx3_lang::ir::Type::Map => {
185+
WellKnownType::Map => {
179186
return Err(anyhow::anyhow!(
180187
"tx3 arg {key} is of type Map, not supported yet",
181188
));
@@ -187,12 +194,14 @@ pub fn inquire_args(
187194
}
188195

189196
pub fn define_args(
190-
params: &BTreeMap<String, tx3_lang::ir::Type>,
197+
invocation: &mut Invocation,
191198
inline_args: Option<&str>,
192199
file_args: Option<&Path>,
193200
ctx: &crate::Context,
194201
provider: &Provider,
195202
) -> Result<HashMap<String, ArgValue>> {
203+
let params = invocation.define_params()?;
204+
196205
let mut remaining_params = params.clone();
197206

198207
let mut loaded_args = super::common::load_args(inline_args, file_args, &remaining_params)?;
@@ -211,19 +220,8 @@ pub fn define_args(
211220
Ok(loaded_args)
212221
}
213222

214-
pub async fn resolve_tx(
215-
prototx: &ProtoTx,
216-
args: HashMap<String, ArgValue>,
217-
provider: &Provider,
218-
) -> Result<TxEnvelope> {
219-
let request = tx3_sdk::trp::ProtoTxRequest {
220-
tir: tx3_sdk::trp::TirInfo {
221-
version: tx3_lang::ir::IR_VERSION.to_string(),
222-
encoding: "hex".to_string(),
223-
bytecode: hex::encode(prototx.ir_bytes()),
224-
},
225-
args,
226-
};
223+
pub async fn resolve_tx(invocation: Invocation, provider: &Provider) -> Result<TxEnvelope> {
224+
let request = invocation.into_trp_request()?;
227225

228226
provider.trp_resolve(request).await
229227
}

src/tx/construct/add_input.rs

Lines changed: 0 additions & 33 deletions
This file was deleted.

src/tx/construct/add_output.rs

Lines changed: 0 additions & 33 deletions
This file was deleted.

src/tx/construct/build.rs

Lines changed: 0 additions & 31 deletions
This file was deleted.

0 commit comments

Comments
 (0)