SWAP2 is a protocol for OTC NFT trading using ephemeral, CREATE2-deployed contracts with minimal gas overhead.
SWAP2 provides a least-privileges alternative to other trading protocols, circumventing the risks associated with approval-for-all trading with single-use but gas-efficient contracts. It was designed and implemented by divergence on behalf of brokerage firm, Fountain Digital, and audited by Spearbit.
- See SWAP2 Design to become familiar with the design and terminology.
- Install Task and run
task
to generate code. - Navigate in the order recommended below.
ET.sol
: general contracts for CREATE2-deployed contracts that "phone home" for mutable constructor arguments.TMPL/README.md
: explanation of generated code.TMPL/TMPLSwapperDeployer.tmpl.sol
: factory side ofET
, deploying swapper Instances with variable execution arguments (Fill vs Cancel).- Also includes all pre-deployment code, e.g. predicting counterfactual addresses.
TMPL/TMPLSwapperBase.tmpl.sol
: implementation of Swap logic; although a base contract, the entire implementation is here.<T>/<T>Swap.sol
: structs defining types of assets that can be traded; field types govern which overloaded library functions are called in the respective<T>SwapperBase
constructor.ConsiderationLib.sol
andERC721TransferLib.sol
: handling of fungible and non-fungible assets, respectively.SWAP2.sol
: merely glue of all<T>SwapperDeployer
s and<T>SwapperProposer
s generated fromTMPLSwapperDeployer.tmpl.sol
.
Files not included above will naturally be encountered as they are referenced by those explicitly listed.
Tip
All Foo*.gen.sol
files are identical to their TMPL*.tmpl.sol
equivalents, save for identifier substitution: s/TMPL/Foo/
.
These aren't checked in as they risk being out of sync; run task
instead.
SwapperTestBase.t.sol
: defines a fuzzableTestCase
struct, helpers, and virtual functions to be implemented by consideration-specific tests.- Note that no actual tests are implemented in here; it simply provides a general test harness.
- The virtual functions replace, for example,
vm.deal()
,address.balance
, etc.
{NativeToken,ERC20}Test.t.sol
: providers of virtual functions defined bySwapperTestBase
.ERC721ForXTest.sol
: abstract contract with suite of (typically end-to-end) tests, suitable for all<T>Swap
types.- Defines virtual functions to be provided for swap-specific instances.
<A>For<B>Test.t.sol
: compositions ofERC721ForXTest
with{NativeToken,ERC20}Test
with swap-specific implementations using<A>For<B>Swap
arguments.- All other
*.t.sol
files: unit tests not covered elsewhere.