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.
We have started a discussion around a possible new wallet RPC to enable apps to have better awareness of this "just in time" funding.
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
- Pay gas only
- Transfer funds during execution only
- Pay gas and transfer funds during execution
- A ERC-4337 UserOperation is submitted to the bundler. The paymasterAndData field includes the MagicSpend address and the withdrawal request.
- Bundler (EOA) calls EntryPoint smart contract.
- Entrypoint first calls to
UserOperation.sender
, a smart contract wallet (SCW), to validate the user operation. - Entrypoint decrements the paymaster’s deposit in the Entrypoint. If the paymaster’s deposit is less than the gas cost, the transaction will revert.
- 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.
- Entrypoint calls to SCW with
UserOperation.calldata
- SCW does arbitrary operation, invoked by
UserOperation.calldata
. - Entrypoint makes post-op call to MagicSpend, with actual gas used in transaction.
- MagicSpend sends the SCW any withdraw.value minus actual gas used.
- Entrypoint refunds the paymaster if actual gas < estimated gas from (4.)
- Entrypoint pays bundler for tx gas
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.
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.
Network | Contract Address |
---|---|
Base | 0x011A61C07DbF256A68256B1cB51A5e246730aB92 |
Base Sepolia | 0x011A61C07DbF256A68256B1cB51A5e246730aB92 |
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