Function cap_sdk::insert

source · [−]
pub async fn insert(
    transaction: impl Into<IndefiniteEvent>
) -> Result<u64, InsertTransactionError>
Expand description

Inserts a transaction into the contract’s history.

Examples

Inserting an event from a builder.

See also: IndefiniteEventBuilder, [IntoDetails][crate::IntoDetails], [TryFromDetails][crate::TryFromDetails].

use cap_sdk_core::transaction::EventStatus;
pub struct TransactionDetails {
    foo: String,
    bar: u64,
}

let transaction_details = TransactionDetails {
    foo: String::from("foo"),
    bar: 42
};

// Construct the event which accompanies our details.
// `IndefiniteEventBuilder` allows any `IntoDetails` type
// to be used in a call to `IndefiniteEventBuilder::details`.
//
// This is useful when dealing with the `cap-standards` types,
// but also works with a dynamically-constructed details vec with
// the type signature of `Vec<(String, DetailValue)>`.
let event = IndefiniteEventBuilder::new()
    .caller(Principal::anonymous())
    .operation(String::from("transfer"))
    .status(EventStatus::Completed)
    .details(transaction_details)
    .build()
    .unwrap();


// Insert the transaction with `insert`. It takes any type
// that implements `Into<IndefiniteEvent>`, this includes
// types like `TypedIndefiniteEvent` as well as `Vec<(String, DetailValue)>`
insert(event).await.unwrap();