Staged CLI for LiFiSwapEnforcer delegations on Base mainnet: create a signed delegation, save it locally, then execute swaps repeatedly against the same grant to test periodic budgets.
- Node.js 20+ with
npm install - Copy
.env.exampleto.envin this directory and setPRIVATE_KEYplus one RPC per chain you use (BASE_RPC_URL,ARC_RPC_URL,ETHEREUM_RPC_URL). Create, approve, and execute select the URL from the chain id. - Base ETH for gas (via relayer fee token) and input ERC-20 balance (e.g. USDC)
- Deployed contracts on Base:
DelegationManager:0xdb9B1e94B5b69Df7e401DDbedE43491141047dB3LiFiSwapEnforcer:0xD0e70cd777a527fB798e5EcA8800c5E3588041d4ChainlinkPriceRuleEnforcer:0x4dAEbF9C5813EFF2606acD41BA25e57841e7cb75
cd scripts/lifi-swap npm install cp .env.example .env # edit .env — set PRIVATE_KEY and the RPC for each chain (BASE_RPC_URL, ARC_RPC_URL, ETHEREUM_RPC_URL)
Read-only helpers for exploring the Li.FI Quote API. No PRIVATE_KEY required. Optionally set LIFI_API_KEY in .env for higher rate limits.
# Supported chains npm run lifi -- chains npm run lifi -- chains --chain-types EVM,SVM,UTXO --json # Tokens on a chain (resolves chain by name, key, or id) npm run lifi -- tokens --chain Base npm run lifi -- tokens --chain Base --symbol USDC npm run lifi -- tokens --chain Base --tags stablecoin # Bridges and exchanges npm run lifi -- tools npm run lifi -- tools --chains base,eth # Possible routes between chains npm run lifi -- connections --from-chain Base --to-chain Bitcoin --from-token USDC # Arc mainnet quote (uses LIFI_QUOTE_*_CHAIN and PRIVATE_KEY from .env) npm run quote:arc -- --amount 1000000 # Quote with human-readable chain/token names (amount in token atoms) npm run lifi -- quote \ --input-chain Arc \ --input-token USDC \ --output-chain Arc \ --output-token EURC \ --amount 1000000 npm run lifi -- quote \ --input-chain Base \ --input-token USDC \ --input-address 0x4a0C5B7c1262d5D76B26235F7D96E86C24de3532 \ --output-chain Bitcoin \ --output-token BTC \ --output-address bc1q9vpk73hpnvrv6mdsxrsc0as03ycywjr0qkja7h \ --amount 10000000 \ --allow-bridges near,layerswap \ --calldata npm run lifi -- quote \ --input-chain Base \ --input-token USDC \ --input-address 0x9fEad8B19C044C2f404dac38B925Ea16ADaa2954 \ --output-chain Ethereum \ --output-token Eth \ --output-address 0x9fEad8B19C044C2f404dac38B925Ea16ADaa2954 \ --amount 10000000 npm run lifi -- quote \ --input-chain Base \ --input-token USDC \ --input-address 0x4a0C5B7c1262d5D76B26235F7D96E86C24de3532 \ --output-chain Solana \ --output-token Sol \ --output-address HoJQwEF9VZVQREpwACNjMiQEkvZDwWYPHGuAdAHkX6dw \ --amount 10000000
Add --json to any command for raw API output. Quote also supports --slippage, --order FASTEST|CHEAPEST, --allow-bridges, --deny-bridges (comma-separated bridge keys from lifi tools), --calldata (print full transaction hex), and --verbose.
When no route is available, quote errors print a concise summary (route, filters, top filtered/failed reasons) instead of raw LiFi JSON. Use --raw-errors to dump the full API error payload for debugging.
Route flags use the same chain/token names as npm run lifi -- quote (no contract addresses in .env):
# Same-chain Base USDC → WETH npm run delegation -- create \ --id usdc-weth-2min \ --input-chain Base \ --input-token USDC \ --output-chain Base \ --output-token ETH \ --period-amount 1000000 \ --period-duration 120 # Cross-chain Base USDC → Ethereum ETH npm run delegation -- create \ --id usdc-eth-bridge-2min \ --input-chain Base \ --input-token USDC \ --output-chain Ethereum \ --output-token Eth \ --output-address 0x9fEad8B19C044C2f404dac38B925Ea16ADaa2954 \ --period-amount 1000000 \ --period-duration 120 npm run delegation -- create \ --id usdc-btc-bridge-2min \ --input-chain Base \ --input-token USDC \ --output-chain Bitcoin \ --output-token btc \ --output-address bc1q9vpk73hpnvrv6mdsxrsc0as03ycywjr0qkja7h \ --period-amount 10000000 \ --period-duration 120 npm run delegation -- list npm run delegation -- show usdc-weth-daily npm run delegation -- delete usdc-weth-daily # or: delete --all
Legacy: omit route flags and set LIFI_FROM_TOKEN, LIFI_TO_TOKEN, LIFI_TO_CHAIN in .env.
Saved files live in delegations/ (gitignored).
npm run approve -- usdc-weth-daily
Use delegation show to inspect the on-chain budget before running swaps.
# First swap: 0.5 USDC (within 1 USDC / period budget) npm run execute -- usdc-weth-daily --amount 500000 npm run delegation -- show usdc-weth-daily # Over budget: CLI warns locally, then submits to relayer — expect # relayer estimate or on-chain revert (LiFiSwapEnforcer:period-amount-exceeded) npm run execute -- usdc-weth-daily --amount 600000 # Within remaining budget npm run execute -- usdc-weth-daily --amount 500000
Dry-run (quote + relayer estimate, no send):
npm run execute -- usdc-weth-daily --amount 5000000 --dry-run
Before relayer submission, execute prints the LiFi quote request (API parameters) and a high-level quote summary (route, amounts, tool, steps) — same style as lifi quote — immediately after fetch, even if later validation (slippage, diamond, etc.) fails. Use --verbose for transaction-request details (to, value, calldata length).
Optional --allow-bridges / --deny-bridges (comma-separated bridge keys from lifi tools) limit which LiFi bridges are used at execute time only — not stored in delegation terms; no recreate needed when changing filters.
Optional flags override only the LiFi quote API parameters (toChain, toToken, toAddress). Signed quote metadata still comes from saved delegation terms, so calldata from the spoofed quote should fail LiFiSwapEnforcer on-chain verification. The CLI warns and still reaches the relayer (no local abort).
| Flag | Effect |
|---|---|
--spoof-output-chain <name|id> |
Wrong destination chain in LiFi quote fetch |
--spoof-output-address <addr> |
Wrong recipient in LiFi quote fetch |
--spoof-output-token <symbol|addr> |
Wrong output token (required when chain spoof changes token address unless metadata.outputSymbol is saved) |
Wrong recipient (same-chain delegation):
npm run execute -- usdc-weth-2min --dry-run \ --spoof-output-address 0x0000000000000000000000000000000000000001
Expected enforcer error: LiFiSwapEnforcer:calldata-recipient-mismatch
Wrong destination chain:
npm run execute -- usdc-weth-2min --dry-run \ --spoof-output-chain Ethereum --spoof-output-token Eth
Expected: LiFiSwapEnforcer:route-dest-chain-mismatch and/or LiFiSwapEnforcer:calldata-dest-chain-mismatch
delegation createresolves--input-chain/--output-chain/ tokens via the Li.FI chains and tokens API (no live quote), pinslifiDiamondfrom the source chain'sdiamondAddress(or--lifi-diamond/LIFI_DIAMOND), callsrelayer_getCapabilitieson the source chain, builds 284-byte enforcer terms, signs delegations to the relayertargetAddress, and saveschainId+relayerTargetAddressin the local file.approve/executere-fetch capabilities and fail fast iftargetAddresschanged since create (recreate with--force). They userelayer_estimate7710Transaction(withauthorizationListwhen needed) andrelayer_getFeeDatafor the fee floor before send.executeloads the saved delegation, fetches a fresh LiFi quote, signs an EIP-191 quote with the samePRIVATE_KEY(terms.quoteSigner), patches caveat args, and submits viarelayer_send7710Transactionwith the estimatecontextprice lock.- Periodic budget is tracked on-chain per
delegationHash; reuse the same saved file across executions.
The redemption wallet address comes from the 1Shot relayer at create time. If the relayer rotates it, approve and execute will error with instructions to recreate:
npm run delegation -- create --id usdc-weth-daily --force
RELAYER_URL in .env is only the JSON-RPC endpoint override, not the delegate address.
approve and execute use estimate-first submission: mock fee ≥ minFee from relayer_getFeeData, simulate via relayer_estimate7710Transaction, adjust fee from requiredPaymentAmount if needed, then send with signed context. Use --dry-run to see gasUsed and requiredPaymentAmount without submitting.
Experimental command to test pre-grant gas estimation: simulate DelegationManager.redeemDelegations on your RPC with bogus delegation signatures and a DelegatorEstimateShim injected at the user delegator via Geth state overrides, then compare eth_estimateGas to relayer_estimate7710Transaction (valid signatures, same fee+swap bundle).
npm run estimate-gas -- usdc-eth-bridge-2min --amount 1000000
Requires PRIVATE_KEY, BASE_RPC_URL (must support stateOverride on eth_call / eth_estimateGas), and RELAYER_URL. Uses the same LiFi quote + quote-signer flow as execute. Overrides are passed as viem’s StateOverride array ({ address, code?, stateDiff: [{ slot, value }] }), not a raw Geth JSON map.
What it prints
- Path A: relayer
gasUsedandrequiredPaymentAmount(valid swap + fee delegations). - Control:
eth_callwith bogus sig and ERC-20 overrides only → expected signature revert. - Path B:
eth_call+eth_estimateGaswith delegator shim + bogus sig → should succeed. - Comparison: relayer gas vs RPC gas and delta (%).
Flags: --skip-control, --skip-relayer, --skip-rpc, --skip-rpc-debug, --fee-atoms, plus the same bridge/chainlink flags as execute.
When the full PoC eth_call fails, the CLI runs fee-only and swap-only batch simulations (unless --skip-rpc-debug) and decodes revert data (Error(string), Panic, delegation-manager / shim custom errors) to pinpoint which leg failed.
Shim bytecode: src/poc/DelegatorEstimateShim.sol. After editing, run forge build and node scripts/lifi-swap/scripts/export-shim-bytecode.mjs to refresh src/poc/delegatorEstimateShimBytecode.ts. The export script patches the delegationManager immutable via Foundry immutableReferences (do not global-replace zero addresses in bytecode).
Caveats: RPC gas simulates direct redeemDelegations; the relayer includes its own submission/wrapper path. The shim’s executeFromExecutor is minimal—not byte-identical to EIP7702StatelessDeleGator. Use this to measure correlation, not as a production fee API.
Stack ChainlinkPriceRuleEnforcer with LiFi for buy-the-dip, take-profit, and absolute price triggers. Use a separate create command — plain delegation create stays LiFi-only.
Deployed on Base, Ethereum, and Arc at 0x4dAEbF9C5813EFF2606acD41BA25e57841e7cb75
Set in .env (see .env.example):
CHAINLINK_PRICE_FEED— default Base ETH/USDCHAINLINK_MAX_STALE_SECONDS— default 120CHAINLINK_MIN_GAP_SECONDS— default 300
Rule kind, window, threshold, and trigger price are CLI flags, not env vars.
# DIP — buy the dip (10% drop vs 24h high) npm run delegation -- create-chainlink \ --id dip-usdc-weth \ --rule-kind dip \ --window-seconds 86400 \ --threshold-bps 1000 \ --trigger-price 0 \ --period-amount 1000000 \ --period-duration 86400 # RISE — take profit (10% rise vs 12h low) npm run delegation -- create-chainlink \ --id rise-weth-usdc-1000bps-12h \ --rule-kind rise \ --window-seconds 12400 \ --threshold-bps 1000 \ --trigger-price 0 \ --period-amount 1000000 # ABSOLUTE_GTE — sell when ETH/USD >= $4000 (8-decimal feed: 4000e8) npm run delegation -- create-chainlink \ --id tp-gte \ --rule-kind absolute_gte \ --window-seconds 0 \ --threshold-bps 0 \ --trigger-price 400000000000 # ABSOLUTE_LTE — buy when ETH/USD <= $3000 npm run delegation -- create-chainlink \ --id buy-lte \ --rule-kind absolute_lte \ --window-seconds 0 \ --threshold-bps 0 \ --trigger-price 300000000000
npm run chainlink -- check dip-usdc-weth npm run chainlink -- check dip-usdc-weth --reference-round-id 12345
Output includes round ids: Current (latest feed round), Window newest/oldest (relative rules — bounds of the valid reference time window), and Reference (auto-picked dip/rise round encoded in args).
Same as LiFi-only execute; Chainlink args are resolved and patched automatically. By default, execute always reaches the relayer — failed Chainlink pre-checks warn locally but still submit so on-chain enforcers are the source of truth.
npm run approve -- dip-usdc-weth npm run execute -- dip-usdc-weth --amount 500000 --dry-run npm run execute -- dip-usdc-weth --amount 500000
Execute flags for Chainlink delegations:
--reference-round-id <id>— override reference round (relative rules)--require-chainlink— abort locally if Chainlink pre-check fails (strict mode)--skip-chainlink— skip Chainlink resolve/patch (args stay empty; enforcer will revert)
This CLI validates on-chain enforcer behavior via the relayer. Do not add client-side aborts in execute that duplicate enforcer checks (Chainlink, LiFi budget, etc.) — warn and proceed. Use optional --require-* flags for strict local gates. chainlink check is informational only.
- Loosen
--threshold-bpsor widen--window-secondsfor smoke tests - Use
chainlink checkbefore first execute to see current vs reference price - For absolute rules, set
--trigger-pricerelative to current feed price at create time
See also: ChainlinkPriceRuleEnforcer-Wallet-Integration.md, ChainlinkPriceRuleEnforcer-App-Integration.md
Use a normal 0x --output-address on the destination chain. Terms encode the recipient as a padded EVM address (bytes32), matching the enforcer’s EvmBridge path.
npm run delegation -- create \ --id usdc-eth-bridge-2min \ --input-chain Base \ --input-token USDC \ --output-chain Ethereum \ --output-token Eth \ --output-address 0x9fEad8B19C044C2f404dac38B925Ea16ADaa2954 \ --period-amount 1000000 \ --period-duration 120 npm run approve -- usdc-eth-bridge-2min npm run execute -- usdc-eth-bridge-2min --amount 1000000 --dry-run
If you created an EVM bridge delegation before this encoding fix, recreate with --force so terms.outputRecipient is padded (not the non-EVM string hash).
Use route flags plus --output-address for the destination recipient (bech32, Solana pubkey, etc.).
Solana: --output-address must be a 32-byte base58 pubkey. It is not defaulted to the EVM signer. Pubkeys, SPL mints, and native SOL are the raw 32-byte base58 decode, not a hash of the base58 text. Native SOL (11111…) encodes to bytes32(0), the same sentinel as native ETH. Create stores that value. Native-token outputs still need real support: the enforcer does not pin a native destination asset in calldata, and the NEAR facet reverts when destinationAsset is zero. LayerSwap is the route that can deliver native SOL today; pin it with --allow-bridges layerswap.
The unrestricted Base USDC → Solana USDC quote uses tool lifiIntents (route kind LiFiIntentsNonEvm, 6). Mayan is MayanNonEvm (4), Allbridge is AllbridgeNonEvm (5), and near stays NearBtc (2) with the current facet selectors. The Mayan path also requires refundRecipient to be the delegator. lifi quote will still return a relay / relaydepository route (often FASTEST); execute errors on those because the Solana recipient is not in the calldata.
npm run lifi -- quote \ --input-chain Base \ --input-token USDC \ --output-chain Solana \ --output-token USDC \ --output-address HoJQwEF9VZVQREpwACNjMiQEkvZDwWYPHGuAdAHkX6dw \ --amount 10000000 npm run delegation -- create \ --id usdc-sol-usdc \ --input-chain Base --input-token USDC \ --output-chain Solana --output-token USDC \ --output-address HoJQwEF9VZVQREpwACNjMiQEkvZDwWYPHGuAdAHkX6dw \ --period-amount 10000000 --period-duration 120
Bitcoin (bc1q P2WPKH): bech32 addresses are encoded to LiFi’s binary bytes32 (witness program as 5-bit words), not keccak256 of the ASCII string. If you created a BTC bridge delegation before this fix, recreate with --force (e.g. usdc-btc-bridge-2min).
npm run delegation -- create \ --id usdc-btc-bridge-2min \ --input-chain Base --input-token USDC \ --output-chain Bitcoin --output-token BTC \ --output-address bc1q9vpk73hpnvrv6mdsxrsc0as03ycywjr0qkja7h \ --period-amount 10000000 --period-duration 120 --force
Expected in saved JSON: terms.outputRecipient = 0x050c01161e111701130c030c1a1b0d10060310180f1d100f110418040e12030f for that bc1q address.
Example create + execute:
npm run delegation -- create \ --id usdc-btc \ --input-chain Base \ --input-token USDC \ --output-chain Bitcoin \ --output-token BTC \ --output-address bc1q... \ --period-amount 1000000 \ --period-duration 86400 npm run execute -- usdc-btc --amount 500000 --dry-run # Limit to enforcer-supported BTC bridges (Near + LayerSwap; excludes Relay) npm run execute -- usdc-btc --amount 10000000 \ --allow-bridges near,layerswap --dry-run # Test one bridge at a time npm run execute -- usdc-btc --amount 10000000 --allow-bridges layerswap --dry-run
Optional overrides if auto-encoding differs from LiFi quote tooling: LIFI_OUTPUT_ASSET_ID, LIFI_OUTPUT_RECIPIENT_BYTES32 in .env.
Note: Source-chain afterHook does not verify delivery on non-EVM destination chains — monitor bridge status via LiFi tooling.
The LiFiSwapEnforcer treats address(0) / bytes32(0) as a native ETH sentinel. Use token symbols Eth / native on the relevant chain via route flags:
# USDC → native ETH (same-chain Base)
npm run delegation -- create \
--input-chain Base --input-token USDC \
--output-chain Base --output-token Eth \
--period-amount 1000000 --period-duration 1200Native ETH input skips the approve delegation (no ERC-20 approval). Execution must carry value == inputAmount when input is native.