Skip to content

Commit

Permalink
feat: add published events to if-write
Browse files Browse the repository at this point in the history
  • Loading branch information
willemneal committed Aug 2, 2024
1 parent af0f244 commit 4afe330
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 17 deletions.
8 changes: 4 additions & 4 deletions FULL_HELP_DOCS.md
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,7 @@ stellar contract invoke ... -- --help
###### **Options:**

* `--id <CONTRACT_ID>` — Contract ID to invoke
* `--is-view` — View the result simulating and do not sign and submit transaction. Deprecated use `--send=no`
* `--is-view` — View the result simulating and do not sign and submit transaction. Ieprecated use `--send=no`
* `--rpc-url <RPC_URL>` — RPC server endpoint
* `--network-passphrase <NETWORK_PASSPHRASE>` — Network passphrase to sign the transaction sent to the rpc server
* `--network <NETWORK>` — Name of network to use from config
Expand All @@ -501,11 +501,11 @@ stellar contract invoke ... -- --help

Possible values:
- `if-write`:
Send the transaction to the network if there are ledger writes or events published, otherwise output the simulation result and do not send to the network
Only send transaction if there are ledger writes or published events, otherwise return simulation result
- `no`:
Do not send the transaction to the network, and output the simulation result
Do not send transaction, return simulation result
- `yes`:
Send the transaction to the network, and output the transaction result
Always send transaction



Expand Down
39 changes: 26 additions & 13 deletions cmd/soroban-cli/src/commands/contract/invoke.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ use heck::ToKebabCase;

use soroban_env_host::{
xdr::{
self, AccountEntry, AccountEntryExt, AccountId, Hash, HostFunction, InvokeContractArgs,
InvokeHostFunctionOp, LedgerEntryData, Limits, Memo, MuxedAccount, Operation,
OperationBody, Preconditions, PublicKey, ScAddress, ScSpecEntry, ScSpecFunctionV0,
ScSpecTypeDef, ScVal, ScVec, SequenceNumber, String32, StringM, Thresholds, Transaction,
TransactionExt, Uint256, VecM, WriteXdr,
self, AccountEntry, AccountEntryExt, AccountId, ContractEvent, ContractEventType,
DiagnosticEvent, Hash, HostFunction, InvokeContractArgs, InvokeHostFunctionOp,
LedgerEntryData, Limits, Memo, MuxedAccount, Operation, OperationBody, Preconditions,
PublicKey, ScAddress, ScSpecEntry, ScSpecFunctionV0, ScSpecTypeDef, ScVal, ScVec,
SequenceNumber, String32, StringM, Thresholds, Transaction, TransactionExt, Uint256, VecM,
WriteXdr,
},
HostError,
};
Expand Down Expand Up @@ -45,7 +46,7 @@ pub struct Cmd {
// For testing only
#[arg(skip)]
pub wasm: Option<std::path::PathBuf>,
/// View the result simulating and do not sign and submit transaction. Deprecated use `--send=no`
/// View the result simulating and do not sign and submit transaction. Ieprecated use `--send=no`
#[arg(long, env = "STELLAR_INVOKE_VIEW")]
pub is_view: bool,
/// Function name as subcommand, then arguments for that function as `--arg-name value`
Expand Down Expand Up @@ -558,7 +559,7 @@ Note: The only types which aren't JSON are Bytes and BytesN, which are raw bytes

#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq, ValueEnum, Default)]
pub enum Send {
/// Only send transaction if there are ledger writes, otherwise return simulation result
/// Only send transaction if there are ledger writes or published events, otherwise return simulation result
#[default]
IfWrite,
/// Do not send transaction, return simulation result
Expand All @@ -570,14 +571,26 @@ pub enum Send {
impl Send {
pub fn should_send(self, sim_res: &SimulateTransactionResponse) -> Result<bool, Error> {
Ok(match self {
Send::IfWrite => !sim_res
.transaction_data()?
.resources
.footprint
.read_write
.is_empty(),
Send::IfWrite => has_write(sim_res)? || has_published_event(sim_res)?,
Send::No => false,
Send::Yes => true,
})
}
}

fn has_write(sim_res: &SimulateTransactionResponse) -> Result<bool, Error> {
Ok(!sim_res
.transaction_data()?
.resources
.footprint
.read_write
.is_empty())
}
fn has_published_event(sim_res: &SimulateTransactionResponse) -> Result<bool, Error> {
Ok(!sim_res.events()?.iter().any(
|DiagnosticEvent {
event: ContractEvent { type_, .. },
..
}| matches!(type_, ContractEventType::Contract),
))
}

0 comments on commit 4afe330

Please sign in to comment.