Native Ether
This guide provides a step-by-step approach for developers looking to implement the swap mechanism for native Etherswithin the Swaplace project. The process involves defining the swap parameters, encoding configurations, and finally creating the swap.
Check the testsuit for this case here
Understanding Swap Parameters with Native Ethers
When dealing with native Ether in swaps, alongside ERC20, ERC721 and ERC1155 tokens, the key parameters remain similar but with added attention to the Ether value being transferred:
Owner: The Ethereum address initiating the swap.
Allowed: The address authorized to accept the swap. A zero address implies anyone can accept.
Expiry: A future timestamp indicating when the swap offer expires.
Recipient: Specifies who receives the Ether involved in the swap.
0denotes the acceptor, while values1through255refer to the swap owner.Value: The amount of Ether involved in the swap, with a precision of up to 6 decimals.
Assets: An array detailing the assets being offered in the swap, including token addresses, IDs, and amounts.
Asking: An array specifying what assets are requested in exchange.
Step-by-Step Implementation
1.Define Swap Parameters
Define the parameters for your swap, including the addresses of the tokens involved, their amounts, and configuration details like expiry and recipient.
2. Calculate Ether Value
Determine the amount of Ether to send with the swap. Use utility functions like ethers.utils.parseEther to convert Ether units to wei, the smallest unit of Ether.
3. Encode configuration
Encode the allowed address, expiry timestamp, recipient type, and Ether value into a single uint256 value using encodeConfig.
4. Compose the Swap
Compose the swap structure by calling the function called omposeSwap, passing in the owner's address, encoded configuration, and arrays detailing the bid and ask components.
5. Create the Swap with Ether
Finally, create the swap by calling the createSwap function on the Swaplace contract, passing in the composed swap structure and the Ether value as part of the transaction options.
You can check the full code here
Last updated