This is a simple reference implementation of a way to bridge tokens using CoW Protocol.
It provides a simple way to bridge assets within the same transaction as the swap by using post-hooks.
The general idea is:
- A user creates an order where the
recipientis modified to a smart contract wallet they control (for example, a 1-1 Safe or a cow-shed). - The swap executes, and the user controlled wallet receives the buy amount (including the surplus).
- Within the same CoW settlement transaction, a post-hooks executes initiating the bridging process for the full amount.
- Eventually, the bridge will be completed. The recipient of the buy tokens in the target chain is typically the user that initiated the swap (and not the smart contract wallet used for bridging)
Each bridge provider creates their own helper contract. As a reference ApproveAndBridge abstract contract implements the common logic and defines some virtual functions to be overridden by the bridge provider helper contract.
The helper contract makes 2 assumptions:
- The contract assumes it is being called using a
delegatecallfrom the account that has the proceeds of the swap. - The balance of the account matches what the user intents to bridge. This is a simple way to handle CoW protocol's
surplus.- This can be achieved if the smart contract wallet is only used for bridging and is set as the
recipientin the order. - For example, the user sell 100 DAI for 100 USDC, but the solvers manage to give the user 101 USDC, then the contract will have exactly 101 USDC in its balance.
- This can be achieved if the smart contract wallet is only used for bridging and is set as the
The main logic is defined in approveAndBridge function, which:
- Get the balance of the bridged asset (includes
surplusfrom the swap) - Performs some validations (like the
minAmounttokens to bridge) - Sets the allowance, so the bridging protocol can pull the bridged amount.
- Initiate the bridging process. Typically, the assets will be transferred to the bridging protocol.
The bridge provider helper contract overrides the function:
bridge: Implements the specific logic for the bridge provider.bridgeApprovalTarget: Returns the address of the contract that should be approved to bridge the token.
This repository is provided solely as a reference and for informational purposes.
The smart contracts and related materials included here are not audited, may contain vulnerabilities, and must not be relied upon in production or for any security-critical use without independent review and testing.
Certain contracts and examples may be contributed by third parties. Inclusion in this repository does not constitute an endorsement, recommendation, or warranty by the maintainers. The maintainers make no representations or guarantees regarding correctness, security, or fitness for any particular purpose, and disclaim all liability for any loss or damage arising from the use of these materials.
forge buildforge testforge fmtforge snapshotanvilforge script script/Counter.s.sol:CounterScript --rpc-url <your_rpc_url> --private-key <your_private_key>cast <subcommand>forge --help
anvil --help
cast --help