Skip to content

Commit

Permalink
TransactionOutput to return both parsed output and raw tx response (#58)
Browse files Browse the repository at this point in the history
  • Loading branch information
UMR1352 authored and wulfraem committed Nov 20, 2024
1 parent 113993e commit 898d55c
Show file tree
Hide file tree
Showing 11 changed files with 142 additions and 77 deletions.
3 changes: 2 additions & 1 deletion examples/utils/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ where
let document = identity_client
.publish_did_document(unpublished)
.execute_with_gas(TEST_GAS_BUDGET, identity_client)
.await?;
.await?
.output;

Ok((document, verification_method_fragment))
}
Expand Down
6 changes: 3 additions & 3 deletions identity_sui_name_tbd/packages/identity_iota/Move.lock
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ latest-published-id = "0xbf2ba9e9383be1dc349b571cbc44006c6fa760f3b900e0be992232d
published-version = "1"

[env.localnet]
chain-id = "3e40a728"
original-published-id = "0x656cabb08dfe1f71e89a9682beb8f5f250042e92cdd8591ae7fc010fb811abf1"
latest-published-id = "0x656cabb08dfe1f71e89a9682beb8f5f250042e92cdd8591ae7fc010fb811abf1"
chain-id = "6cc84190"
original-published-id = "0xc7dac7b1a15253d593776e59e79647f76be64503f3428b34441b4818f8cb0551"
latest-published-id = "0xc7dac7b1a15253d593776e59e79647f76be64503f3428b34441b4818f8cb0551"
published-version = "1"
69 changes: 41 additions & 28 deletions identity_sui_name_tbd/src/assets/asset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::client::IdentityClient;
use crate::client::IotaKeySignature;
use crate::sui::move_calls;
use crate::transaction::Transaction;
use crate::transaction::TransactionOutput;
use crate::utils::MoveType;
use crate::Error;
use anyhow::anyhow;
Expand Down Expand Up @@ -382,7 +383,7 @@ where
self,
gas_budget: Option<u64>,
client: &IdentityClient<S>,
) -> Result<Self::Output, Error>
) -> Result<TransactionOutput<Self::Output>, Error>
where
S: Signer<IotaKeySignature> + Sync,
{
Expand All @@ -391,18 +392,21 @@ where
self.new_content.clone(),
client.package_id(),
)?;
let tx_status = client
.execute_transaction(tx, gas_budget)
.await?
let response = client.execute_transaction(tx, gas_budget).await?;
let tx_status = response
.effects
.as_ref()
.context("transaction had no effects")
.map(|effects| effects.into_status())
.map(|effects| effects.status())
.map_err(|e| Error::TransactionUnexpectedResponse(e.to_string()))?;

if let IotaExecutionStatus::Failure { error } = tx_status {
return Err(Error::TransactionUnexpectedResponse(error));
return Err(Error::TransactionUnexpectedResponse(error.clone()));
}

self.asset.inner = self.new_content;
Ok(())

Ok(TransactionOutput { output: (), response })
}
}

Expand All @@ -421,15 +425,15 @@ where
self,
gas_budget: Option<u64>,
client: &IdentityClient<S>,
) -> Result<Self::Output, Error>
) -> Result<TransactionOutput<Self::Output>, Error>
where
S: Signer<IotaKeySignature> + Sync,
{
let asset_ref = self.0.object_ref(client).await?;
let tx = move_calls::asset::delete::<T>(asset_ref, client.package_id())?;

client.execute_transaction(tx, gas_budget).await?;
Ok(())
let response = client.execute_transaction(tx, gas_budget).await?;
Ok(TransactionOutput { output: (), response })
}
}
/// A [`Transaction`] that creates a new [`AuthenticatedAsset`].
Expand All @@ -447,7 +451,7 @@ where
self,
gas_budget: Option<u64>,
client: &IdentityClient<S>,
) -> Result<Self::Output, Error>
) -> Result<TransactionOutput<Self::Output>, Error>
where
S: Signer<IotaKeySignature> + Sync,
{
Expand All @@ -459,17 +463,20 @@ where
} = self.0;
let tx = move_calls::asset::new(inner, mutable, transferable, deletable, client.package_id())?;

let created_asset_id = client
.execute_transaction(tx, gas_budget)
.await?
let response = client.execute_transaction(tx, gas_budget).await?;

let created_asset_id = response
.effects
.as_ref()
.ok_or_else(|| Error::TransactionUnexpectedResponse("could not find effects in transaction response".to_owned()))?
.created()
.first()
.ok_or_else(|| Error::TransactionUnexpectedResponse("no object was created in this transaction".to_owned()))?
.object_id();

AuthenticatedAsset::get_by_id(created_asset_id, client).await
AuthenticatedAsset::get_by_id(created_asset_id, client)
.await
.map(move |output| TransactionOutput { output, response })
}
}

Expand All @@ -491,7 +498,7 @@ where
self,
gas_budget: Option<u64>,
client: &IdentityClient<S>,
) -> Result<Self::Output, Error>
) -> Result<TransactionOutput<Self::Output>, Error>
where
S: Signer<IotaKeySignature> + Sync,
{
Expand All @@ -500,15 +507,16 @@ where
self.recipient,
client.package_id(),
)?;
for id in client
.execute_transaction(tx, gas_budget)
.await?

let tx_result = client.execute_transaction(tx, gas_budget).await?;
let created_obj_ids = tx_result
.effects
.as_ref()
.ok_or_else(|| Error::TransactionUnexpectedResponse("could not find effects in transaction response".to_owned()))?
.created()
.iter()
.map(|obj| obj.reference.object_id)
{
.map(|obj| obj.reference.object_id);
for id in created_obj_ids {
let object_type = client
.read_api()
.get_object_with_options(id, IotaObjectDataOptions::new().with_type())
Expand All @@ -519,7 +527,12 @@ where
.map_err(|e| Error::ObjectLookup(e.to_string()))?;

if object_type == TransferProposal::move_type(client.package_id()).to_string() {
return TransferProposal::get_by_id(id, client).await;
return TransferProposal::get_by_id(id, client)
.await
.map(move |proposal| TransactionOutput {
output: proposal,
response: tx_result,
});
}
}

Expand All @@ -540,7 +553,7 @@ impl Transaction for AcceptTransferTx {
self,
gas_budget: Option<u64>,
client: &IdentityClient<S>,
) -> Result<Self::Output, Error>
) -> Result<TransactionOutput<Self::Output>, Error>
where
S: Signer<IotaKeySignature> + Sync,
{
Expand Down Expand Up @@ -569,8 +582,8 @@ impl Transaction for AcceptTransferTx {
client.package_id(),
)?;

client.execute_transaction(tx, gas_budget).await?;
Ok(())
let response = client.execute_transaction(tx, gas_budget).await?;
Ok(TransactionOutput { output: (), response })
}
}

Expand All @@ -585,7 +598,7 @@ impl Transaction for ConcludeTransferTx {
self,
gas_budget: Option<u64>,
client: &IdentityClient<S>,
) -> Result<Self::Output, Error>
) -> Result<TransactionOutput<Self::Output>, Error>
where
S: Signer<IotaKeySignature> + Sync,
{
Expand All @@ -609,7 +622,7 @@ impl Transaction for ConcludeTransferTx {
client.package_id(),
)?;

client.execute_transaction(tx, gas_budget).await?;
Ok(())
let response = client.execute_transaction(tx, gas_budget).await?;
Ok(TransactionOutput { output: (), response })
}
}
3 changes: 2 additions & 1 deletion identity_sui_name_tbd/src/assets/public_available_vc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ impl PublicAvailableVC {
.deletable(true)
.finish()
.execute_with_opt_gas(gas_budget, client)
.await?;
.await?
.output;

Ok(Self { credential, asset })
}
Expand Down
15 changes: 11 additions & 4 deletions identity_sui_name_tbd/src/client/full_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ use crate::assets::AuthenticatedAssetBuilder;
use crate::migration::Identity;
use crate::migration::IdentityBuilder;
use crate::transaction::Transaction as TransactionT;
use crate::transaction::TransactionOutput;
use crate::utils::MoveType;
use crate::Error;

Expand Down Expand Up @@ -389,7 +390,7 @@ impl TransactionT for PublishDidTx {
self,
gas_budget: Option<u64>,
client: &IdentityClient<S>,
) -> Result<Self::Output, Error>
) -> Result<TransactionOutput<Self::Output>, Error>
where
S: Signer<IotaKeySignature> + Sync,
{
Expand All @@ -399,21 +400,27 @@ impl TransactionT for PublishDidTx {
.pack()
.map_err(|err| Error::DidDocSerialization(format!("could not pack DID document: {err}")))?;

let oci = client
let TransactionOutput {
output: identity,
response,
} = client
.create_identity(&packed)
.finish()
.execute_with_opt_gas(gas_budget, client)
.await?;

// replace placeholders in document
let did: IotaDID = IotaDID::new(&oci.id(), client.network());
let did: IotaDID = IotaDID::new(&identity.id(), client.network());
let metadata_document: StateMetadataDocument = self.0.into();
let document_without_placeholders = metadata_document.into_iota_document(&did).map_err(|err| {
Error::DidDocParsingFailed(format!(
"could not replace placeholders in published DID document {did}; {err}"
))
})?;

Ok(document_without_placeholders)
Ok(TransactionOutput {
output: document_without_placeholders,
response,
})
}
}
7 changes: 6 additions & 1 deletion identity_sui_name_tbd/src/migration/identity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ use crate::proposals::ProposalBuilder;
use crate::proposals::UpdateDidDocument;
use crate::sui::move_calls;
use crate::transaction::Transaction;
use crate::transaction::TransactionOutput;
use crate::utils::MoveType;
use crate::Error;

Expand Down Expand Up @@ -441,7 +442,7 @@ impl<'a> Transaction for CreateIdentityTx<'a> {
self,
gas_budget: Option<u64>,
client: &IdentityClient<S>,
) -> Result<Self::Output, Error>
) -> Result<TransactionOutput<Self::Output>, Error>
where
S: Signer<IotaKeySignature> + Sync,
{
Expand Down Expand Up @@ -501,5 +502,9 @@ impl<'a> Transaction for CreateIdentityTx<'a> {
get_identity(client, new_identity_id)
.await
.and_then(|identity| identity.ok_or_else(|| Error::ObjectLookup(new_identity_id.to_string())))
.map(move |identity| TransactionOutput {
output: identity,
response,
})
}
}
36 changes: 22 additions & 14 deletions identity_sui_name_tbd/src/proposals/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ use crate::migration::OnChainIdentity;
use crate::migration::Proposal;
use crate::sui::move_calls;
use crate::transaction::Transaction;
use crate::transaction::TransactionOutput;
use crate::utils::MoveType;
use crate::Error;

Expand Down Expand Up @@ -149,7 +150,7 @@ where
self,
gas_budget: Option<u64>,
client: &IdentityClient<S>,
) -> Result<ProposalResult<Proposal<A>>, Error>
) -> Result<TransactionOutput<ProposalResult<Proposal<A>>>, Error>
where
S: Signer<IotaKeySignature> + Sync,
{
Expand Down Expand Up @@ -177,17 +178,17 @@ where
} else {
ptb.finish()
};
let tx_effects = client
.execute_transaction(tx, gas_budget)
.await?
let tx_result = client.execute_transaction(tx, gas_budget).await?;
let tx_effects = tx_result
.effects
.as_ref()
.ok_or_else(|| Error::TransactionUnexpectedResponse("missing effects".to_string()))?;
if let IotaExecutionStatus::Failure { error } = tx_effects.status() {
return Err(IotaSdkError::Data(error.clone()).into());
}

if chain_execute {
Proposal::<A>::parse_tx_effects(tx_effects).map(ProposalResult::Executed)
Proposal::<A>::parse_tx_effects(tx_effects.clone()).map(ProposalResult::Executed)
} else {
// 2 objects are created, one is the Bag's Field and the other is our Proposal. Proposal is not owned by the bag,
// but the field is.
Expand All @@ -204,6 +205,10 @@ where

client.get_object_by_id(proposal_id).await.map(ProposalResult::Pending)
}
.map(move |output| TransactionOutput {
output,
response: tx_result,
})
}
}

Expand All @@ -224,7 +229,7 @@ where
self,
gas_budget: Option<u64>,
client: &IdentityClient<S>,
) -> Result<Self::Output, Error>
) -> Result<TransactionOutput<Self::Output>, Error>
where
S: Signer<IotaKeySignature> + Sync,
{
Expand All @@ -233,15 +238,18 @@ where
let controller_cap = identity.get_controller_cap(client).await?;

let tx = proposal.make_execute_tx(identity_ref, controller_cap, client.package_id())?;
let tx_effects = client
.execute_transaction(tx, gas_budget)
.await?
let tx_result = client.execute_transaction(tx, gas_budget).await?;
let tx_effects = tx_result
.effects
.as_ref()
.ok_or_else(|| Error::TransactionUnexpectedResponse("missing effects".to_string()))?;
if let IotaExecutionStatus::Failure { error } = tx_effects.status() {
Err(IotaSdkError::Data(error.clone()).into())
} else {
Proposal::<A>::parse_tx_effects(tx_effects)
Proposal::<A>::parse_tx_effects(tx_effects.clone()).map(move |output| TransactionOutput {
output,
response: tx_result,
})
}
}
}
Expand All @@ -263,7 +271,7 @@ where
self,
gas_budget: Option<u64>,
client: &IdentityClient<S>,
) -> Result<Self::Output, Error>
) -> Result<TransactionOutput<Self::Output>, Error>
where
S: Signer<IotaKeySignature> + Sync,
{
Expand All @@ -278,15 +286,15 @@ where
)?;

let response = client.execute_transaction(tx, gas_budget).await?;
if let IotaExecutionStatus::Failure { error } = response.effects.expect("had effects").into_status() {
return Err(Error::TransactionUnexpectedResponse(error));
if let IotaExecutionStatus::Failure { error } = response.effects.as_ref().expect("had effects").status() {
return Err(Error::TransactionUnexpectedResponse(error.clone()));
}

let vp = identity
.controller_voting_power(controller_cap.0)
.expect("is identity's controller");
*proposal.votes_mut() = proposal.votes() + vp;

Ok(())
Ok(TransactionOutput { output: (), response })
}
}
Loading

0 comments on commit 898d55c

Please sign in to comment.