added some structs; added ToPlutusData
This commit is contained in:
parent
bc983d694a
commit
60d7c52c26
|
@ -11,6 +11,7 @@ dependencies = [
|
||||||
"hex",
|
"hex",
|
||||||
"pallas-addresses",
|
"pallas-addresses",
|
||||||
"pallas-codec",
|
"pallas-codec",
|
||||||
|
"pallas-crypto",
|
||||||
"pallas-primitives",
|
"pallas-primitives",
|
||||||
"pallas-traverse",
|
"pallas-traverse",
|
||||||
"serde",
|
"serde",
|
||||||
|
|
|
@ -17,6 +17,7 @@ hex = "0.4.3"
|
||||||
pallas-primitives = "0.13.2"
|
pallas-primitives = "0.13.2"
|
||||||
pallas-codec = "0.13.2"
|
pallas-codec = "0.13.2"
|
||||||
pallas-traverse = "0.13.2"
|
pallas-traverse = "0.13.2"
|
||||||
|
pallas-crypto = "0.13.2"
|
||||||
serde = { version = "1.0.144", features = ["derive"] }
|
serde = { version = "1.0.144", features = ["derive"] }
|
||||||
serde_json = "1.0.85"
|
serde_json = "1.0.85"
|
||||||
uplc = { path = '../uplc', version = "0.0.12" }
|
uplc = { path = '../uplc', version = "0.0.12" }
|
||||||
|
|
|
@ -1,6 +1,12 @@
|
||||||
use pallas_addresses::Address;
|
use pallas_addresses::{Address, PaymentKeyHash};
|
||||||
use pallas_codec::utils::{KeyValuePairs, MaybeIndefArray};
|
use pallas_codec::utils::{KeyValuePairs, MaybeIndefArray};
|
||||||
use pallas_primitives::babbage::{BigInt, Constr};
|
use pallas_crypto::hash::Hash;
|
||||||
|
use pallas_primitives::babbage::{
|
||||||
|
BigInt, Certificate, Constr, DatumHash, PolicyId, PostAlonzoTransactionOutput, Redeemer,
|
||||||
|
StakeCredential, TransactionInput, Value, Withdrawals,
|
||||||
|
};
|
||||||
|
use pallas_traverse::OutputRef;
|
||||||
|
use std::str::FromStr;
|
||||||
use uplc::PlutusData;
|
use uplc::PlutusData;
|
||||||
|
|
||||||
use crate::args::ResolvedInput;
|
use crate::args::ResolvedInput;
|
||||||
|
@ -9,33 +15,14 @@ pub fn get_tx_in_info(resolved_inputs: &[ResolvedInput]) -> anyhow::Result<Vec<P
|
||||||
let mut tx_in_info = Vec::new();
|
let mut tx_in_info = Vec::new();
|
||||||
|
|
||||||
for resolved_input in resolved_inputs {
|
for resolved_input in resolved_inputs {
|
||||||
let tx_out_ref = PlutusData::Constr(Constr {
|
let tx_out_ref = TransactionInput {
|
||||||
tag: 0,
|
transaction_id: Hash::from_str(resolved_input.input.tx_hash.as_str())?, // Not sure if this is the best approach?
|
||||||
any_constructor: None,
|
index: resolved_input.input.index,
|
||||||
fields: MaybeIndefArray::Indef(vec![
|
}
|
||||||
PlutusData::BoundedBytes(hex::decode(resolved_input.input.tx_hash.clone())?.into()),
|
.to_plutus_data();
|
||||||
PlutusData::BigInt(BigInt::Int(resolved_input.input.index.into())),
|
|
||||||
]),
|
|
||||||
});
|
|
||||||
|
|
||||||
let address = Address::from_bech32(&resolved_input.output.address)?;
|
let address = Address::from_bech32(&resolved_input.output.address)?;
|
||||||
|
|
||||||
let payment_tag = match address.typeid() % 2 {
|
|
||||||
0 => 0,
|
|
||||||
1 => 1,
|
|
||||||
_ => unreachable!(),
|
|
||||||
};
|
|
||||||
let stake_tag = match address.typeid() {
|
|
||||||
0 | 1 => Some(0),
|
|
||||||
2 | 3 => Some(1),
|
|
||||||
_ => None,
|
|
||||||
};
|
|
||||||
|
|
||||||
let (payment_part, stake_part) = match address {
|
|
||||||
Address::Shelley(s) => (s.payment().to_vec(), s.delegation().to_vec()),
|
|
||||||
_ => unreachable!(),
|
|
||||||
};
|
|
||||||
|
|
||||||
let lovelace = resolved_input.output.value.0;
|
let lovelace = resolved_input.output.value.0;
|
||||||
|
|
||||||
let mut assets = resolved_input.output.value.1.clone();
|
let mut assets = resolved_input.output.value.1.clone();
|
||||||
|
@ -50,6 +37,95 @@ pub fn get_tx_in_info(resolved_inputs: &[ResolvedInput]) -> anyhow::Result<Vec<P
|
||||||
any_constructor: None,
|
any_constructor: None,
|
||||||
fields: MaybeIndefArray::Indef(vec![
|
fields: MaybeIndefArray::Indef(vec![
|
||||||
// txOutAddress
|
// txOutAddress
|
||||||
|
address.to_plutus_data(),
|
||||||
|
// txOutValue
|
||||||
|
PlutusData::Map(KeyValuePairs::Def(
|
||||||
|
assets
|
||||||
|
.iter()
|
||||||
|
.map(|val| {
|
||||||
|
let currency_symbol =
|
||||||
|
PlutusData::BoundedBytes(hex::decode(val.0).unwrap().into());
|
||||||
|
let token_map = PlutusData::Map(KeyValuePairs::Def(
|
||||||
|
val.1
|
||||||
|
.iter()
|
||||||
|
.map(|token| {
|
||||||
|
(
|
||||||
|
PlutusData::BoundedBytes(
|
||||||
|
token.0.as_bytes().to_vec().into(),
|
||||||
|
),
|
||||||
|
PlutusData::BigInt(BigInt::Int((*token.1).into())),
|
||||||
|
)
|
||||||
|
})
|
||||||
|
.collect(),
|
||||||
|
));
|
||||||
|
(currency_symbol, token_map)
|
||||||
|
})
|
||||||
|
.collect(),
|
||||||
|
)),
|
||||||
|
]),
|
||||||
|
});
|
||||||
|
|
||||||
|
tx_in_info.push(PlutusData::Constr(Constr {
|
||||||
|
tag: 0,
|
||||||
|
any_constructor: None,
|
||||||
|
fields: MaybeIndefArray::Indef(vec![tx_out_ref, tx_out]),
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(tx_in_info)
|
||||||
|
}
|
||||||
|
|
||||||
|
//---- Time conversion: slot range => posix time range
|
||||||
|
|
||||||
|
type Slot = u64;
|
||||||
|
type PosixTime = u64; // in milliseconds
|
||||||
|
|
||||||
|
type SlotRange = (Slot, Slot);
|
||||||
|
type PosixTimeRange = (PosixTime, PosixTime);
|
||||||
|
|
||||||
|
struct SlotConfig {
|
||||||
|
slot_length: u64,
|
||||||
|
zero_time: PosixTime,
|
||||||
|
}
|
||||||
|
|
||||||
|
fn slot_to_begin_posix_time(slot: Slot, sc: &SlotConfig) -> PosixTime {
|
||||||
|
let ms_after_begin = slot * sc.slot_length;
|
||||||
|
sc.zero_time + ms_after_begin
|
||||||
|
}
|
||||||
|
|
||||||
|
fn slot_range_to_posix_time_range(slot_range: SlotRange, sc: &SlotConfig) -> PosixTimeRange {
|
||||||
|
(
|
||||||
|
slot_to_begin_posix_time(slot_range.0, sc),
|
||||||
|
slot_to_begin_posix_time(slot_range.1, sc),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------------
|
||||||
|
|
||||||
|
pub trait ToPlutusData {
|
||||||
|
fn to_plutus_data(&self) -> PlutusData;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ToPlutusData for Address {
|
||||||
|
fn to_plutus_data(&self) -> PlutusData {
|
||||||
|
//TOD: Byron address and reward address
|
||||||
|
|
||||||
|
let payment_tag = match self.typeid() % 2 {
|
||||||
|
0 => 0,
|
||||||
|
1 => 1,
|
||||||
|
_ => unreachable!(),
|
||||||
|
};
|
||||||
|
let stake_tag = match self.typeid() {
|
||||||
|
0 | 1 => Some(0),
|
||||||
|
2 | 3 => Some(1),
|
||||||
|
_ => None,
|
||||||
|
};
|
||||||
|
|
||||||
|
let (payment_part, stake_part) = match self {
|
||||||
|
Address::Shelley(s) => (s.payment().to_vec(), s.delegation().to_vec()),
|
||||||
|
_ => unreachable!(),
|
||||||
|
};
|
||||||
|
|
||||||
PlutusData::Constr(Constr {
|
PlutusData::Constr(Constr {
|
||||||
tag: 0,
|
tag: 0,
|
||||||
any_constructor: None,
|
any_constructor: None,
|
||||||
|
@ -88,40 +164,55 @@ pub fn get_tx_in_info(resolved_inputs: &[ResolvedInput]) -> anyhow::Result<Vec<P
|
||||||
}),
|
}),
|
||||||
}),
|
}),
|
||||||
]),
|
]),
|
||||||
}),
|
|
||||||
// txOutValue
|
|
||||||
PlutusData::Map(KeyValuePairs::Def(
|
|
||||||
assets
|
|
||||||
.iter()
|
|
||||||
.map(|val| {
|
|
||||||
let currency_symbol =
|
|
||||||
PlutusData::BoundedBytes(hex::decode(val.0).unwrap().into());
|
|
||||||
let token_map = PlutusData::Map(KeyValuePairs::Def(
|
|
||||||
val.1
|
|
||||||
.iter()
|
|
||||||
.map(|token| {
|
|
||||||
(
|
|
||||||
PlutusData::BoundedBytes(
|
|
||||||
token.0.as_bytes().to_vec().into(),
|
|
||||||
),
|
|
||||||
PlutusData::BigInt(BigInt::Int((*token.1).into())),
|
|
||||||
)
|
|
||||||
})
|
})
|
||||||
.collect(),
|
}
|
||||||
));
|
}
|
||||||
(currency_symbol, token_map)
|
|
||||||
})
|
|
||||||
.collect(),
|
|
||||||
)),
|
|
||||||
]),
|
|
||||||
});
|
|
||||||
|
|
||||||
tx_in_info.push(PlutusData::Constr(Constr {
|
impl ToPlutusData for TransactionInput {
|
||||||
|
fn to_plutus_data(&self) -> PlutusData {
|
||||||
|
PlutusData::Constr(Constr {
|
||||||
tag: 0,
|
tag: 0,
|
||||||
any_constructor: None,
|
any_constructor: None,
|
||||||
fields: MaybeIndefArray::Indef(vec![tx_out_ref, tx_out]),
|
fields: MaybeIndefArray::Indef(vec![
|
||||||
}));
|
PlutusData::BoundedBytes(hex::decode(self.transaction_id.clone()).unwrap().into()),
|
||||||
|
PlutusData::BigInt(BigInt::Int(self.index.into())),
|
||||||
|
]),
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
}
|
||||||
Ok(tx_in_info)
|
|
||||||
|
// impl ToPlutusData for LegacyTransactionOutput {
|
||||||
|
// fn to_plutus_data(&self) -> PlutusData {}
|
||||||
|
// }
|
||||||
|
|
||||||
|
// impl ToPlutusData for PostAlonzoTransactionOutput {
|
||||||
|
// fn to_plutus_data(&self) -> PlutusData {}
|
||||||
|
// }
|
||||||
|
|
||||||
|
pub struct TxInInfo {
|
||||||
|
out_ref: OutputRef,
|
||||||
|
resolved: PostAlonzoTransactionOutput,
|
||||||
|
}
|
||||||
|
|
||||||
|
// Plutus V2
|
||||||
|
pub enum ScriptPurpose {
|
||||||
|
Minting(PolicyId),
|
||||||
|
Spending(OutputRef),
|
||||||
|
Reward(StakeCredential),
|
||||||
|
Certifying(Certificate),
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct TxInfo {
|
||||||
|
inputs: Vec<TxInInfo>,
|
||||||
|
reference_inputs: Vec<TxInInfo>,
|
||||||
|
outputs: Vec<PostAlonzoTransactionOutput>,
|
||||||
|
fee: Value,
|
||||||
|
mint: Value,
|
||||||
|
dcert: Vec<Certificate>,
|
||||||
|
wdrl: Withdrawals,
|
||||||
|
valid_range: PosixTimeRange,
|
||||||
|
signatories: Vec<PaymentKeyHash>,
|
||||||
|
redeemers: KeyValuePairs<ScriptPurpose, Redeemer>,
|
||||||
|
data: KeyValuePairs<DatumHash, PlutusData>,
|
||||||
|
id: Hash<32>,
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue