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

Set implicit account transition context inputs #1739

Merged
2 changes: 1 addition & 1 deletion sdk/src/types/block/context_input/block_issuance_credit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub struct BlockIssuanceCreditContextInput(AccountId);

impl BlockIssuanceCreditContextInput {
/// The context input kind of a [`BlockIssuanceCreditContextInput`].
pub const KIND: u8 = 1;
pub const KIND: u8 = 2;

/// Creates a new [`BlockIssuanceCreditContextInput`].
pub fn new(account_id: AccountId) -> Self {
Expand Down
2 changes: 1 addition & 1 deletion sdk/src/types/block/context_input/commitment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub struct CommitmentContextInput(SlotCommitmentId);

impl CommitmentContextInput {
/// The context input kind of a [`CommitmentContextInput`].
pub const KIND: u8 = 0;
pub const KIND: u8 = 1;

/// Creates a new [`CommitmentContextInput`].
pub fn new(commitment_id: SlotCommitmentId) -> Self {
Expand Down
34 changes: 14 additions & 20 deletions sdk/src/types/block/context_input/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,10 @@ mod tests {
use super::ContextInput;

#[test]
fn test_commitment() {
fn commitment() {
let commitment: ContextInput = serde_json::from_value(serde_json::json!(
{
"type": 0,
"type": 1,
"commitmentId": "0xedf5f572c58ddf4b4f9567d82bf96689cc68b730df796d822b4b9fb643f5efda4f9567d8"
}
))
Expand All @@ -107,15 +107,13 @@ mod tests {
}

#[test]
fn test_block_issuance_credit() {
let bic: ContextInput = serde_json::from_str(
r#"
fn block_issuance_credit() {
let bic: ContextInput = serde_json::from_value(serde_json::json!(
{
"type": 1,
"type": 2,
"accountId": "0x52fdfc072182654f163f5f0f9a621d729566c74d10037c4d7bbb0407d1e2c649"
}
"#,
)
))
.unwrap();
assert!(bic.is_block_issuance_credit());
assert_eq!(
Expand All @@ -124,27 +122,23 @@ mod tests {
);

// Test wrong type returns error.
let bic_deserialization_result: Result<ContextInput, _> = serde_json::from_str(
r#"
let bic_deserialization_result: Result<ContextInput, _> = serde_json::from_value(serde_json::json!(
{
"type": 2,
"type": 3,
"accountId": "0x52fdfc072182654f163f5f0f9a621d729566c74d10037c4d7bbb0407d1e2c649"
}
"#,
);
));
assert!(bic_deserialization_result.is_err());
}

#[test]
fn test_reward() {
let reward: ContextInput = serde_json::from_str(
r#"
fn reward() {
let reward: ContextInput = serde_json::from_value(serde_json::json!(
{
"type": 2,
"index": 10
"type": 3,
"index": 10
}
"#,
)
))
.unwrap();
assert!(reward.is_reward());
assert_eq!(reward.as_reward().index(), 10);
Expand Down
2 changes: 1 addition & 1 deletion sdk/src/types/block/context_input/reward.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub struct RewardContextInput(#[packable(unpack_error_with = Error::InvalidRewar

impl RewardContextInput {
/// The context input kind of a [`RewardContextInput`].
pub const KIND: u8 = 2;
pub const KIND: u8 = 3;

/// Creates a new [`RewardContextInput`].
pub fn new(index: u16) -> Result<Self, Error> {
Expand Down
15 changes: 12 additions & 3 deletions sdk/src/wallet/operations/transaction/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crate::{
client::{api::PreparedTransactionData, secret::SecretManage},
types::block::{
address::Address,
context_input::{BlockIssuanceCreditContextInput, CommitmentContextInput},
output::{
feature::{BlockIssuerFeature, BlockIssuerKey, BlockIssuerKeys, Ed25519BlockIssuerKey},
unlock_condition::AddressUnlockCondition,
Expand All @@ -29,7 +30,7 @@ where
pub async fn implicit_account_transition(
&self,
output_id: &OutputId,
key_source: Option<impl Into<BlockIssuerKeySource>>,
key_source: Option<impl Into<BlockIssuerKeySource> + Send>,
) -> Result<TransactionWithMetadata> {
let issuer_id = AccountId::from(output_id);

Expand All @@ -45,7 +46,7 @@ where
pub async fn prepare_implicit_account_transition(
&self,
output_id: &OutputId,
key_source: Option<impl Into<BlockIssuerKeySource>>,
key_source: Option<impl Into<BlockIssuerKeySource> + Send>,
) -> Result<PreparedTransactionData>
where
crate::wallet::Error: From<S::Error>,
Expand Down Expand Up @@ -83,7 +84,8 @@ where
}
};

let account = AccountOutput::build_with_amount(implicit_account.amount(), AccountId::from(output_id))
let account_id = AccountId::from(output_id);
let account = AccountOutput::build_with_amount(implicit_account.amount(), account_id)
.with_mana(implicit_account.mana())
.with_unlock_conditions([AddressUnlockCondition::from(Address::from(
*implicit_account
Expand All @@ -97,7 +99,14 @@ where
)?])
.finish_output()?;

// TODO https://github.com/iotaledger/iota-sdk/issues/1740
let issuance = self.client().get_issuance().await?;

let transaction_options = TransactionOptions {
context_inputs: Some(vec![
CommitmentContextInput::new(issuance.latest_commitment.id()).into(),
BlockIssuanceCreditContextInput::new(account_id).into(),
]),
custom_inputs: Some(vec![*output_id]),
..Default::default()
};
Expand Down
6 changes: 5 additions & 1 deletion sdk/src/wallet/operations/transaction/build_transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,13 @@ where
.with_inputs(inputs)
.with_outputs(selected_transaction_data.outputs);

// Optional add a tagged payload
if let Some(options) = options.into() {
// Optional add a tagged payload
builder = builder.with_payload(options.tagged_data_payload);

if let Some(context_inputs) = options.context_inputs {
builder = builder.with_context_inputs(context_inputs);
}
}

let transaction = builder.finish_with_params(&protocol_parameters)?;
Expand Down
6 changes: 5 additions & 1 deletion sdk/src/wallet/operations/transaction/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ use serde::{Deserialize, Serialize};

use crate::{
client::api::input_selection::Burn,
types::block::{address::Address, output::OutputId, payload::tagged_data::TaggedDataPayload},
types::block::{
address::Address, context_input::ContextInput, output::OutputId, payload::tagged_data::TaggedDataPayload,
},
};

/// Options for transactions
Expand All @@ -16,6 +18,8 @@ pub struct TransactionOptions {
pub remainder_value_strategy: RemainderValueStrategy,
#[serde(default)]
pub tagged_data_payload: Option<TaggedDataPayload>,
#[serde(default)]
pub context_inputs: Option<Vec<ContextInput>>,
// If custom inputs are provided only they are used. If also other additional inputs should be used,
// `mandatory_inputs` should be used instead.
#[serde(default)]
Expand Down
Loading