Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update magic spend to magicSpend #24

Merged
merged 1 commit into from
Oct 30, 2024
Merged

Conversation

eric-ships
Copy link
Contributor

@eric-ships eric-ships commented Oct 29, 2024

What changes were made?

"Magic Spend "has been updated to be "MagicSpend".

How was this tested?

Locally - visually.

_Below the line is how the README now looks ✅ _


MagicSpend

MagicSpend is a contract that allows onchain accounts to present valid Withdraw Requests and receive funds. A Withdraw Request is defined as

struct WithdrawRequest {
    bytes signature;
    address asset;
    uint256 amount;
    uint256 nonce;
    uint48 expiry;
 }

Where signature is an EIP-191 compliant signature of the message

abi.encode(
  <MagicSpend contract address>,
  <UserOperation.sender and/or msg.sender of the withdraw call>,
  <chain ID>,
  withdrawRequest.asset,
  withdrawRequest.amount,
  withdrawRequest.nonce,
  withdrawRequest.expiry
)

MagicSpend is an ERC-4337 compliant paymaster (EntryPoint v0.6) and also enables withdraw requests with asset ETH (address(0)) to be used to pay transaction gas.

This contract is part of a broader MagicSpend product from Coinbase, which as a whole allows Coinbase users to seamlessly use their assets onchain.

Diagram of Coinbase user making use of MagicSpend

We have started a discussion around a possible new wallet RPC to enable apps to have better awareness of this "just in time" funding.

Detailed Flows

When the withdrawing account is an ERC-4337 compliant smart contract (like Smart Wallet), there are three different ways the MagicSpend smart contract can be used

  1. Pay gas only
  2. Transfer funds during execution only
  3. Pay gas and transfer funds during execution

Pay gas only

Pay gas only flow diagram
  1. A ERC-4337 UserOperation is submitted to the bundler. The paymasterAndData field includes the MagicSpend address and the withdrawal request.
  2. Bundler (EOA) calls EntryPoint smart contract.
  3. Entrypoint first calls to UserOperation.sender, a smart contract wallet (SCW), to validate the user operation.
  4. Entrypoint decrements the paymaster’s deposit in the Entrypoint. If the paymaster’s deposit is less than the gas cost, the transaction will revert.
  5. EntryPoint calls the MagicSpend contract to run validations on the withdrawal, including checking the signature and ensuring withdraw.value is greater than transaction max gas cost.
  6. Entrypoint calls to SCW with UserOperation.calldata
  7. SCW does arbitrary operation, invoked by UserOperation.calldata.
  8. Entrypoint makes post-op call to MagicSpend, with actual gas used in transaction.
  9. MagicSpend sends the SCW any withdraw.value minus actual gas used.
  10. Entrypoint refunds the paymaster if actual gas < estimated gas from (4.)
  11. Entrypoint pays bundler for tx gas

Transfer funds during execution only

Diagram of 'Transfer funds during execution only' flow

This is the simplest flow. The MagicSpend account is agnostic to any details of this transaction, even whether or not the caller is a SCW. It simply validates the withdraw and transfers funds if valid.

Pay gas and transfer funds during execution

Pay gas and transfer funds during execution

This flow is like "Pay gas only” with the addition of (7.) and (8.). Here, the SCW also requests funds during execution. In this flow, a user might be, for example, trying to mint an NFT and needs funds for the mint.

Deployments

Network Contract Address
Base 0x011A61C07DbF256A68256B1cB51A5e246730aB92
Base Sepolia 0x011A61C07DbF256A68256B1cB51A5e246730aB92

Developing

After cloning the repo, run the tests using Forge, from Foundry

forge test

You can run the echinda tests with this make command

make echidna-test

@cb-heimdall
Copy link
Collaborator

cb-heimdall commented Oct 29, 2024

✅ Heimdall Review Status

Requirement Status More Info
Reviews 1/1
Denominator calculation
Show calculation
1 if user is bot 0
1 if user is external 0
From .codeflow.yml 1
Additional review requirements
Show calculation
Max 0
0
From CODEOWNERS 0
Global minimum 0
Max 1
1
1 if commit is unverified 1
Sum 2

@cb-heimdall
Copy link
Collaborator

Review Error for stevieraykatz @ 2024-10-29 19:43:41 UTC
User must have write permissions to review

@@ -1,6 +1,6 @@
# MagicSpend

MagicSpend is a contract that allows onchain accounts to present valid Withdraw Requests and receive funds. A Withdraw Request is defined as
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Linter cleaned up the whitespace + added new lines where applicable. The only actual change should be "Magic Spend" becoming "MagicSpend".

@eric-ships eric-ships merged commit a58abb9 into main Oct 30, 2024
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants