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