-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extensive documentation + minor refactoring for readability (#20)
* doc: comments for `{ERC20,Native}Consideration.sol` * chore: rename `ERC721SwapperLib` to `ERC721TransferLib` * chore/doc: move `ERC721TransferLib` structs inside library + document it * doc: `SWAP2.sol` except for contract-level comments * doc: improve `SwapperBase` comments * doc+refactor: document virtual `SwapperDeployerBase_platformFeeConfig()` and move `ISwapperEvents` inheritance to actual deployers * doc: `Action` type and `ActionMessageLib` library * doc+refactor: document contract artifacts and change their type to `bytes3` * doc: structs used for `<T>Swap` fields * doc: expand on `propose()` security * doc: post-execution invariant guarantees with native-token consideration * doc: full documentation of `For{ERC20,Native}Swapper.sol.tmpl` * doc: complete documentation of `Swapper{Base,Deployer}` templates
- Loading branch information
Showing
24 changed files
with
322 additions
and
120 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,11 @@ | ||
// SPDX-License-Identifier: UNLICENSED | ||
pragma solidity 0.8.25; | ||
|
||
import {ERC721Token} from "../ERC721SwapperLib.sol"; | ||
import {ERC721TransferLib} from "../ERC721TransferLib.sol"; | ||
import {Consideration, PayableParties} from "../TypesAndConstants.sol"; | ||
|
||
struct ERC721ForNativeSwap { | ||
PayableParties parties; | ||
ERC721Token offer; | ||
ERC721TransferLib.ERC721Token offer; | ||
Consideration consideration; | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// SPDX-License-Identifier: UNLICENSED | ||
// Copyright 2024 Divergence Tech Ltd. | ||
pragma solidity ^0.8.24; | ||
|
||
import {IERC721} from "@openzeppelin/contracts/interfaces/IERC721.sol"; | ||
import {Parties} from "./TypesAndConstants.sol"; | ||
|
||
/** | ||
* @dev Transfers one or more ERC721 tokens between parties. | ||
* @dev Note that all `_transfer(<T>, Parties)` functions have effectively the same signature, allowing them to be | ||
* called without explicit knowledge of the <T> type as the constructor will select the respective function. This is | ||
* exploited by the `TMPL/SwapperBase.sol.tmpl` template. | ||
*/ | ||
library ERC721TransferLib { | ||
/// @dev Represents a single ERC721 token. | ||
struct ERC721Token { | ||
IERC721 addr; | ||
uint256 id; | ||
} | ||
|
||
/// @dev Transfers the token from `parties.seller` to `parties.buyer`. | ||
function _transfer(ERC721Token memory token, Parties memory parties) internal { | ||
token.addr.transferFrom(parties.seller, parties.buyer, token.id); | ||
} | ||
|
||
/// @dev Represents multiple ERC721 tokens within the same contract. | ||
struct MultiERC721Token { | ||
IERC721 addr; | ||
uint256[] ids; // MUST be distinct | ||
} | ||
|
||
/** | ||
* @dev Transfers all tokens from `parties.seller` to `parties.buyer`. | ||
* @param tokens An _array_ of MultiERC721Token structs, representing any number of tokens. {addr,id} pairs MUST be | ||
* distinct across the entire array. | ||
*/ | ||
function _transfer(MultiERC721Token[] memory tokens, Parties memory parties) internal { | ||
for (uint256 i = 0; i < tokens.length; ++i) { | ||
IERC721 t = tokens[i].addr; | ||
for (uint256 j = 0; j < tokens[i].ids.length; ++j) { | ||
t.transferFrom(parties.seller, parties.buyer, tokens[i].ids[j]); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,11 @@ | ||
// SPDX-License-Identifier: UNLICENSED | ||
pragma solidity 0.8.25; | ||
|
||
import {ISwapperEvents} from "./TypesAndConstants.sol"; | ||
|
||
/// @dev Abstract base contract for all <T>SwapperDeployer implementations. | ||
abstract contract SwapperDeployerBase is ISwapperEvents { | ||
abstract contract SwapperDeployerBase { | ||
/** | ||
* @return recipient Address to which platform fees MUST be sent by swapper contracts. | ||
* @return basisPoints One-hundredths of a percentage point of swap consideration that MUST be sent to `recipient`. | ||
*/ | ||
function _platformFeeConfig() internal view virtual returns (address payable recipient, uint16 basisPoints); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,29 @@ | ||
// SPDX-License-Identifier: UNLICENSED | ||
// Copyright 2024 Divergence Tech Ltd. | ||
pragma solidity 0.8.25; | ||
/** | ||
* GENERATED CODE - DO NOT EDIT | ||
*/ | ||
|
||
import {TMPLSwap as Swap} from "./TMPLSwap.sol"; | ||
import {TMPLSwap} from "./TMPLSwap.sol"; | ||
import {TMPLSwapperBase} from "./TMPLSwapperBase.gen.sol"; | ||
|
||
import {ERC20Consideration} from "../ERC20Consideration.sol"; | ||
|
||
/// @notice Executes the swap with consideration denominated in the ERC20 denoted by the swap's currency field. | ||
contract TMPLSwapper is TMPLSwapperBase, ERC20Consideration { | ||
constructor(Swap memory swap) TMPLSwapperBase(swap) {} | ||
constructor(TMPLSwap memory swap) TMPLSwapperBase(swap) {} | ||
|
||
function _disburseFunds(Swap memory swap, address payable feeRecipient, uint256 fee) internal override { | ||
/** | ||
* @dev Propagates arguments, unchanged, to ERC20Consideration._disburseFunds(), acting only to modify the function | ||
* signature to accept a `TMPLSwap`. | ||
*/ | ||
function _disburseFunds(TMPLSwap memory swap, address payable feeRecipient, uint256 fee) internal override { | ||
ERC20Consideration._disburseFunds(swap.parties, swap.consideration, swap.currency, feeRecipient, fee); | ||
} | ||
|
||
function _postExecutionInvariantsMet(Swap memory) internal pure override returns (bool) { | ||
// Will be removed by the compiler, but explicitly stating that there are no checks. | ||
/// @dev Always returns true, explicitly stating that there are no checks to be performed. | ||
function _postExecutionInvariantsMet(TMPLSwap memory) internal pure override returns (bool) { | ||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,39 @@ | ||
// SPDX-License-Identifier: UNLICENSED | ||
// Copyright 2024 Divergence Tech Ltd. | ||
pragma solidity 0.8.25; | ||
/** | ||
* GENERATED CODE - DO NOT EDIT | ||
*/ | ||
|
||
import {TMPLSwap as Swap} from "./TMPLSwap.sol"; | ||
import {TMPLSwap} from "./TMPLSwap.sol"; | ||
import {TMPLSwapperBase} from "./TMPLSwapperBase.gen.sol"; | ||
|
||
import {NativeTokenConsideration} from "../NativeTokenConsideration.sol"; | ||
import {SwapperBase} from "../SwapperBase.sol"; | ||
import {PayableParties} from "../TypesAndConstants.sol"; | ||
|
||
/// @notice Executes the swap with consideration denominated in the chain's native toiken. | ||
contract TMPLSwapper is TMPLSwapperBase, NativeTokenConsideration { | ||
constructor(Swap memory swap) payable TMPLSwapperBase(swap) {} | ||
constructor(TMPLSwap memory swap) payable TMPLSwapperBase(swap) {} | ||
|
||
function _disburseFunds(Swap memory swap, address payable feeRecipient, uint256 fee) internal override { | ||
/** | ||
* @dev Propagates arguments, unchanged, to NativeTokenConsideration._disburseFunds(), acting only to modify the | ||
* function signature to accept a `TMPLSwap`. | ||
*/ | ||
function _disburseFunds(TMPLSwap memory swap, address payable feeRecipient, uint256 fee) internal override { | ||
NativeTokenConsideration._disburseFunds(swap.parties, swap.consideration, feeRecipient, fee); | ||
} | ||
|
||
/// @dev Required override; propagates `p`, unchanged, to NativeTokenConsideration._cancel(). | ||
function _cancel(PayableParties memory p) internal override(SwapperBase, NativeTokenConsideration) { | ||
NativeTokenConsideration._cancel(p); | ||
} | ||
|
||
function _postExecutionInvariantsMet(Swap memory) internal view override returns (bool) { | ||
/** | ||
* @dev Returns NativeTokenConsideration._postExecutionInvariantsMet(), acting only to modify the function signature | ||
* to accept a `TMPLSwap`. | ||
*/ | ||
function _postExecutionInvariantsMet(TMPLSwap memory) internal view override returns (bool) { | ||
return NativeTokenConsideration._postExecutionInvariantsMet(); | ||
} | ||
} |
Oops, something went wrong.