Blog

One deposit address per invoice with CREATE2

A fresh address for every invoice with no private key behind it: how CREATE2, EIP-1167 clones and a salt bound to the payout wallet make it work.

A payment gateway has to answer one question for every transfer it sees: which order is this for? The cleanest answer is a fresh address for every invoice. The hard part is doing that without a private key for each address sitting on a server. CREATE2 solves it: the address is known before anything exists on the chain, nobody holds a key for it, and the contract that later appears there can only send the money to one wallet.

This post shows how that works, with the formula, the code that computes the address off-chain, and a run on a local chain where a payment lands on an address that has no code yet.

Three ways to tell payments apart

One wallet, amount tailA key per invoice (HD wallet)A CREATE2 forwarder per invoice
How a payment is matchedBy an exact amount such as 10.001By the addressBy the address
Who can move the fundsThe merchantWhoever holds the derived keysNobody; the code sends them to the merchant
Keys on a serverNoYes, or the merchant moves every payment by handNo
Gas to move a token paymentNone, it is already in the walletNative coin must be sent to every address firstPaid once by whoever calls sweep
What goes wrongAn overpayment or a rounded amount matches no orderA leaked seed empties every open invoiceA payout wallet that does not exist on the network

The amount tail is simple and costs nothing, and Stablevoy uses it on Tron, where it pays out straight to the merchant's wallet. On EVM networks the forwarder gives what the other two cannot give together: a payment is matched by its address, whatever amount arrives, and no key for that address exists anywhere.

The CREATE2 formula

A contract deployed with CREATE2 (EIP-1014) lands at an address that depends only on three things: the deployer, a 32-byte salt and the hash of the code being deployed. Same inputs, same address, on any EVM network (the networks page lists the ones Stablevoy runs on).

The salt is keccak256 of the invoice salt, the merchant wallet and the fee; then 0xff, the factory address, the salt and the hash of the clone init code are hashed with keccak256, and the last 20 bytes are the deposit address
The salt commits to the payout wallet and the fee, so neither can change without moving the address.

The trick is what goes into the salt. Stablevoy's factory does not use the random invoice salt as it is. It hashes it together with the merchant's payout wallet and the fee rate:

function _salt(bytes32 invoiceSalt, address merchant, uint16 feeBps) private pure returns (bytes32) {
    return keccak256(abi.encode(invoiceSalt, merchant, feeBps));
}

The forwarder deployed at the address is told the merchant and the fee by the same call that deploys it. Change the wallet in that call and the factory deploys somewhere else: the funds already sitting at the original address stay there, still waiting for the right parameters. In the run below, the same invoice with another payout wallet gives a completely different address.

The code being deployed is not the full forwarder, but a 55-byte EIP-1167 clone that delegates every call to one forwarder implementation. Its init code hash is the same for every invoice, which keeps the formula cheap to compute and the deployment cheap to pay for.

Compute the address off-chain

Nothing in the formula needs the chain, so the address can be computed in a few lines with ethers.js:

import { AbiCoder, concat, getAddress, getCreate2Address, keccak256 } from 'ethers';

// The CREATE2 address of an invoice's deposit forwarder, computed off-chain (EIP-1014, EIP-1167 clone).
export function depositAddress({ factory, implementation, invoiceSalt, merchant, feeBps }) {
  // 1. The salt commits to the invoice, the payout wallet and the fee rate.
  const salt = keccak256(AbiCoder.defaultAbiCoder().encode(['bytes32', 'address', 'uint16'], [invoiceSalt, merchant, feeBps]));
  // 2. The init code is the 55-byte EIP-1167 clone of the forwarder implementation.
  const initCode = concat(['0x3d602d80600a3d3981f3363d3d373d3d3d363d73', getAddress(implementation), '0x5af43d82803e903d91602b57fd5bf3']);
  // 3. address = last 20 bytes of keccak256(0xff ++ factory ++ salt ++ keccak256(initCode))
  return getCreate2Address(getAddress(factory), salt, keccak256(initCode));
}

This is also what the Stablevoy payment page does in the buyer's browser before it shows an address, with one difference: the factory and implementation addresses are built into the page instead of taken from the server. An address that the factory would not create for this invoice, its payout wallet and its fee is never shown. The payout wallet itself comes from the store settings, where it changes only with the merchant's wallet signature.

Checked against the deployed factory with a read-only call, the function gives the same address the factory computes, and the factory gives the same address on Base and on Arbitrum:

base factory:     0x8CaCa524ed0C01c04C5bD1DB473e5FB7b0D7b2f7
arbitrum factory: 0x8CaCa524ed0C01c04C5bD1DB473e5FB7b0D7b2f7
off-chain:        0x8CaCa524ed0C01c04C5bD1DB473e5FB7b0D7b2f7

Pay first, deploy later

The part that surprises most people: the buyer pays an address where no contract exists yet. An address on an EVM chain is just a key in the state; it can hold a balance with no code behind it. The contract only appears when someone collects the money.

Three steps: the invoice address is computed with no transaction; the buyer pays it while it still has no code; anyone calls sweep, which deploys the 45-byte clone and splits the balance, 99.5 to the merchant and 0.5 to the fee recipient
One transaction deploys the forwarder and flushes it. Invoices that are never paid never cost gas.

Here is the whole cycle on a local Anvil chain: the factory deployed with a 2% fee cap, an invoice at 0.5%, 100 ETH paid before deployment, and the sweep called by an account that is neither the merchant nor the operator. The run uses the native coin to stay short; a token goes through sweep with the token address in exactly the same way.

computed off-chain  0x02794c60ECBB6b0ad1e112Eb6667Fd17fA1580C3
factory says        0x02794c60ECBB6b0ad1e112Eb6667Fd17fA1580C3
code at the address 0x
paid 100 ETH, balance 100.000000000000000000, still no code: 0x
after the sweep by a stranger:
  merchant      99.500000000000000000 ETH
  fee recipient 0.500000000000000000 ETH
  deposit addr  0.000000000000000000 ETH, code size 45 bytes
same invoice, other wallet: 0xb850ddf074A240327c3c17C8D2E4Da319Bd02D59
fee 201 bps: FeeAboveCap(201, 200)

The stranger paid the gas and received nothing. That is the point: sweeping is open to anyone because the caller has no say in where the money goes. Stablevoy normally sweeps and pays the gas itself, but if it stopped, the merchant, the buyer or anyone else could call the same function. The recovery guide walks through doing that by hand.

What the contracts refuse to do

The security of this design is mostly in what is missing:

  • Nobody can get to the address first. Only the factory can deploy there, since the deployer is part of the formula, and it binds the forwarder to the merchant in the same transaction that creates it.
  • No owner and no upgrade path. The factory has no admin functions. The fee recipient and the fee cap are immutable, set once in the constructor.
  • A capped fee. The factory refuses any fee above its cap, as the last line of the run shows. The fee rounds down, so the merchant gets any remainder.
  • A blocked fee never blocks the payout. If the fee transfer fails, for example because a stablecoin issuer froze the fee address, the whole amount goes to the merchant and no fee is taken.
  • The wrong token is not lost. The forwarder can send any token or the native coin it holds, whichever was sent, and always to the same merchant.
One condition stays with the merchant. The payout wallet must be a wallet the merchant controls on the network where the payment arrives. A regular wallet controls its address on every EVM network; a smart contract wallet exists only where it was deployed. That is why the dashboard checks the payout address on every network before it is used; the security page describes that check.

Questions

Can funds sent to the address before deployment be lost?

Not on a network where the factory exists. The address is fixed by the factory, the salt and the code, so the forwarder that collects them can only ever be deployed there, bound to the wallet in the salt.

Who pays for the deployment?

Whoever calls sweep. One transaction deploys the clone and moves the funds, and an invoice that is never paid never costs anything.

Is the address the same on every network?

Yes, when the factory has the same address on each network, as Stablevoy's does. A payment sent on the wrong supported network can still be swept, to the same wallet.

Can I check an address myself?

Yes. Call computeAddress on the factory with the invoice salt, your wallet and the fee from the webhook, or run the function above. The recovery guide walks through sweeping without Stablevoy.

More from the blog

Open the dashboard