Example Use Cases

Scenario 1: Mutually Beneficial Token Swap

Consider two users, Alice and Bob, who wish to execute a mutually beneficial token swap. Alice wants to exchange 1 Ether for an equivalent amount in USDC, while Bob looks to receive 1 Ether in exchange for his USDC. This operation can be atomically executed through a single bundle of user operations (userOps) within a single on-chain Ethereum transaction, ensuring both parties achieve their desired outcome.

Bundle Description

  • userOp-1: Transfers 1 Ether from Alice to Bob.

  • userOp-2: Transfers USDC from Bob to Alice, equivalent to 1 Ether's current market value.

[Alice] --1 ETH--> [Bob]
                    |
                    v
[Bob] --USDC(eq. 1 ETH)--> [Alice]

validatePostExecution Functionality

For this scenario, there are 2 validatePostExecution functions safeguarding the token exchanges adhere to the agreed terms, validating the final state post-execution.

We will demonstrate 1 validation function validating Aliceโ€™s received the agreed token amount.

// In practice, the actual validation logic would be encoded in a 
// ERC-6900, ERC-7579 plugin  
function validatePostExecution(UserOperation calldata userOp) external {
  // Example validation for a token swap from Ether to USDC
  require(userOp.callData == expectedCallData, "Unexpected operation");
  uint256 usdcReceived = IERC20(USDC_ADDRESS).balanceOf(ALICE_ADDRESS);
  // from a DEX i.e. Uniswap
  uint256 expectedUsdc = getExpectedUsdcForEther(1 ether);
  require(usdcReceived >= expectedUsdc, "Received less USDC than expected");
}

USDC_ADDRESS is a placeholder for the actual Ethereum address of the USDC token contract. getExpectedUsdcForEther is a hypothetical function that calculates the expected amount of USDC Alice should receive for 1 Ether, based on the current exchange rate.

Scenario 2: Guardian-Validated Transaction

In another scenario, Bob wishes to swap 100 USDC for MATIC on Uniswap but requires approval from his guardian, Alice, for transactions matching a threshold of 100 USDC.

Bundle Description

  • userOp-A: Transfers 100 USDC from Bob to Uniswap for swapping USDC to MATIC.

  • userOp-B: Alice, as Bob's guardian, signs userOp-A, approving the transaction.

[Bob's Account] --100 USDC--> [Uniswap] --Swap USDC to MATIC--> [Bob's Account]
       |
       +-- [Guardian Signature from Alice]

validatePostExecution Implementation

The validatePostExecution function in Bob's smart account contract would validate Alice's signature and ensure the transaction meets the criteria for guardian approval.

function validatePostExecution(UserOperation calldata userOp) external {
    // Alice's signature follows an aggregated scheme or appended to Bob's signature  
    require(userOp.signature == AliceSignature, "Guardian signature missing or invalid");
    // Further validation to ensure the swap was successful and met Bob's expectations
    uint256 maticReceived = IERC20(MATIC_ADDRESS).balanceOf(BOB_ADDRESS);
    require(maticReceived >= expectedMatic, "Received less MATIC than expected");
}

MATIC_ADDRESS and BOB_ADDRESS are placeholders for the actual Ethereum addresses of the MATIC token contract and Bob's account, respectively. expectedSignature represents Alice's expected signature as Bob's guardian and expectedMatic is the amount of MATIC Bob expects to receive from the swap.

Last updated