Skip to main content

StructuredBlobData

Struct StructuredBlobData 

Source
pub struct StructuredBlobData<Action> {
    pub caller: Option<BlobIndex>,
    pub callees: Option<Vec<BlobIndex>>,
    pub parameters: Action,
}
Expand description

This struct allows to define cross-contract calls (aka contract composition). A contract A can “call” an other contract B by being it’s “caller”:

Blob for contract A has to be a StructuredBlobData with callees vec including the blob index of contract B.

Blob for contract B has to be a StructuredBlobData with caller = the blob index of contract A.

§When to use cross-contract calls ?

When a contract needs to do an operation on an other one. Like transfering funds from contract’s wallet to the user doing the transaction.

§Example: Bob Swap 2 USDC to 2 USDT

A swap contract can use transactions with 4 blobs:

┌─ Blob 0
│  Identity verification for user Bob
└─────
┌─ Blob 1 - Contract = "amm"
│  Swap action
│  callees = vec![2]
└─────
┌─ Blob 2 - Contract = "usdt"
│  Transfer action of 2 USDT to "Bob"
│  caller = 1
└─────
┌─ Blob 3 - Contract = "usdc"
│  Transfer action of 2 USDC to "amm"
└─────

Blob 2 will do various checks on the swap to ensure its validity (correct transfer amounts…)

As Blob 2 has a “caller”, the identity used by the contract will be “amm”, thus the transfer of USDT will be done FROM “amm” TO Bob

And as Blob 3 has no “caller”, the identity used by the contract will be the same as the transaction identity, i.e: Bob.

An alternative way that is more evm-like with an token approve would look like:

┌─ Blob 0
│  Identity verification for user Bob
└─────
┌─ Blob 1 - Contract = "usdc"
│  Approve action of 2 USDC for "amm"
└─────
┌─ Blob 2 - Contract = "amm"
│  Swap action
│  callees = vec![3, 4]
└─────
┌─ Blob 3 - Contract = "usdt"
│  Transfer action of 2 USDT to "Bob"
│  caller = 2
└─────
┌─ Blob 4 - Contract = "usdc"
│  TransferFrom action from "Bob" of 2 USDC to "amm"
│  caller = 2
└─────

As Blob 4 now has a “caller”, the identity used by the contract will be “amm” and not “Bob”. Note that here we are using a TransferFrom in blob 4, contract “amm” got the approval from Bob to initate a transfer on its behalf with blob 1.

You can find an example of this implementation in our amm contract

Fields§

§caller: Option<BlobIndex>§callees: Option<Vec<BlobIndex>>§parameters: Action

Trait Implementations§

Source§

impl<Action: BorshDeserialize> BorshDeserialize for StructuredBlobData<Action>

Source§

fn deserialize_reader<R: Read>(reader: &mut R) -> Result<Self>

Source§

fn deserialize(buf: &mut &[u8]) -> Result<Self, Error>

Deserializes this instance from a given slice of bytes. Updates the buffer to point at the remaining bytes.
Source§

fn try_from_slice(v: &[u8]) -> Result<Self, Error>

Deserialize this instance from a slice of bytes.
Source§

fn try_from_reader<R>(reader: &mut R) -> Result<Self, Error>
where R: Read,

Source§

impl BorshDeserialize for StructuredBlobData<DropEndOfReader>

Source§

fn deserialize_reader<R: Read>(reader: &mut R) -> Result<Self>

Source§

fn deserialize(buf: &mut &[u8]) -> Result<Self, Error>

Deserializes this instance from a given slice of bytes. Updates the buffer to point at the remaining bytes.
Source§

fn try_from_slice(v: &[u8]) -> Result<Self, Error>

Deserialize this instance from a slice of bytes.
Source§

fn try_from_reader<R>(reader: &mut R) -> Result<Self, Error>
where R: Read,

Source§

impl<Action> BorshSerialize for StructuredBlobData<Action>
where Action: BorshSerialize,

Source§

fn serialize<__W: Write>(&self, writer: &mut __W) -> Result<(), Error>

Source§

impl<Action: Debug> Debug for StructuredBlobData<Action>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<Action: BorshSerialize> From<StructuredBlobData<Action>> for BlobData

Source§

fn from(val: StructuredBlobData<Action>) -> Self

Converts to this type from the input type.
Source§

impl<Action: BorshDeserialize> TryFrom<BlobData> for StructuredBlobData<Action>

Source§

type Error = Error

The type returned in the event of a conversion error.
Source§

fn try_from(val: BlobData) -> Result<StructuredBlobData<Action>, Self::Error>

Performs the conversion.
Source§

impl TryFrom<BlobData> for StructuredBlobData<DropEndOfReader>

Source§

type Error = Error

The type returned in the event of a conversion error.
Source§

fn try_from( val: BlobData, ) -> Result<StructuredBlobData<DropEndOfReader>, Self::Error>

Performs the conversion.

Auto Trait Implementations§

§

impl<Action> Freeze for StructuredBlobData<Action>
where Action: Freeze,

§

impl<Action> RefUnwindSafe for StructuredBlobData<Action>
where Action: RefUnwindSafe,

§

impl<Action> Send for StructuredBlobData<Action>
where Action: Send,

§

impl<Action> Sync for StructuredBlobData<Action>
where Action: Sync,

§

impl<Action> Unpin for StructuredBlobData<Action>
where Action: Unpin,

§

impl<Action> UnsafeUnpin for StructuredBlobData<Action>
where Action: UnsafeUnpin,

§

impl<Action> UnwindSafe for StructuredBlobData<Action>
where Action: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.