ERC20/ERC721
Last updated
Last updated
This guide aims to teach developers how to structure a swap for ERC20 and ERC721 tokens within the Swaplace project. We'll focus on creating swaps using theSwaplace
smart contract, which facilitates token swaps. The process involves defining the swap parameters, encoding configurations, and finally creating the swap.
Check the testsuit for this case
A swap in this context refers to an exchange of tokens between parties. For ERC721 tokens, these could represent unique digital items, while ERC20 tokens are fungible tokens representing a specific value. A swap can involve either a single token (1-1), multiple tokens of the same type (N-N), or a combination thereof.
Owner: The address initiating the swap.
Allowed: The address permitted to accept the swap. Setting this to the zero address allows anyone to accept.
Expiry: Timestamp indicating when the swap offer expires.
Recipient: Determines who receives any ETH involved in the swap. 0
means the acceptor, values 1
through 255
indicate the swap owner.
Value: Amount of ETH involved in the swap, with a maximum precision of 6 decimals.
Assets: Array of assets being offered in the swap.
Asking: Array of assets requested in exchange.
First, define the parameters for your swap. This includes specifying the addresses of the tokens involved, the amounts or IDs (for ERC721), and the configuration details like expiry and recipient.
Addresses of the tokens
biddingAddr
askingAddr
Amounts or IDs of the tokens
biddingAmountOrId
askingAmountOrId
Configuration object
config
These parameters are crucial for specifying what assets are being swapped and under what conditions.
Use the encodeConfig
function to encode the allowed address, expiry timestamp, recipient type, and value into a single uint256 value. This simplifies passing these parameters to the swap creation function.
Address of the recipient
Zero Address or a valid Address
currentTimestamp
a timestamp indicating when the swap becomes valid
The composeSwap
function takes the owner's address, the encoded configuration, and arrays of token addresses and amounts/IDs for both sides of the swap. This function constructs a structured representation of the swap that can be passed to the smart contract.
Finally, create the swap by calling the createSwap
function on the Swaplace
contract, passing in the composed swap structure. Ensure the transaction is sent from the owner's address.
You can check the full code