Skip to content

Commit

Permalink
parseData() and packData() implemented #104
Browse files Browse the repository at this point in the history
  • Loading branch information
blackbeard002 committed Dec 22, 2023
1 parent a198bed commit 5cb9f6c
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 41 deletions.
17 changes: 16 additions & 1 deletion contracts/SwapFactory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,21 @@ abstract contract SwapFactory is ISwapFactory, ISwap, IErrors {

if (biding.length == 0 || asking.length == 0) revert InvalidAssetsLength();

return Swap(owner, allowed, expiry, biding, asking);
uint256 config=packData(allowed, expiry);

return Swap(owner,config, biding, asking);
}

function packData(
address allowed,
uint256 expiry
) public pure returns(uint256) {
return (uint256(uint160(allowed)) << 96) | uint256(expiry);
}

function parseData(
uint256 config
) public pure returns(address,uint256) {
return (address(uint160(config >> 96)),uint256(config & ((1 << 96) - 1)));
}
}
24 changes: 17 additions & 7 deletions contracts/Swaplace.sol
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ contract Swaplace is SwapFactory, ISwaplace, IERC165 {
function createSwap(Swap calldata swap) public returns (uint256) {
if (swap.owner != msg.sender) revert InvalidAddress(msg.sender);

if (swap.expiry < block.timestamp) revert InvalidExpiry(swap.expiry);
(address allowed , uint256 expiry) = parseData(swap.config);

if (expiry < block.timestamp) revert InvalidExpiry(expiry);

if (swap.biding.length == 0 || swap.asking.length == 0)
revert InvalidAssetsLength();
Expand All @@ -41,7 +43,7 @@ contract Swaplace is SwapFactory, ISwaplace, IERC165 {

_swaps[swapId] = swap;

emit SwapCreated(swapId, msg.sender, swap.allowed, swap.expiry);
emit SwapCreated(swapId, msg.sender, allowed, expiry);

return swapId;
}
Expand All @@ -52,12 +54,16 @@ contract Swaplace is SwapFactory, ISwaplace, IERC165 {
function acceptSwap(uint256 swapId) public returns (bool) {
Swap memory swap = _swaps[swapId];

if (swap.allowed != address(0) && swap.allowed != msg.sender)
(address allowed , uint256 expiry) = parseData(swap.config);

if (allowed != address(0) && allowed != msg.sender)
revert InvalidAddress(msg.sender);

if (swap.expiry < block.timestamp) revert InvalidExpiry(swap.expiry);
if (expiry < block.timestamp) revert InvalidExpiry(expiry);

expiry = 0;

_swaps[swapId].expiry = 0;
_swaps[swapId].config = packData(allowed,expiry);

Asset[] memory assets = swap.asking;

Expand Down Expand Up @@ -98,9 +104,13 @@ contract Swaplace is SwapFactory, ISwaplace, IERC165 {

if (swap.owner != msg.sender) revert InvalidAddress(msg.sender);

if (swap.expiry < block.timestamp) revert InvalidExpiry(swap.expiry);
(address allowed , uint256 expiry) = parseData(swap.config);

if (expiry < block.timestamp) revert InvalidExpiry(expiry);

expiry = 0;

_swaps[swapId].expiry = 0;
_swaps[swapId].config = packData(allowed,expiry);

emit SwapCanceled(swapId, msg.sender);
}
Expand Down
4 changes: 3 additions & 1 deletion contracts/echidna/TestSwapFactory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ contract TestFactory is SwapFactory {
make_asset_array(addr, amountOrId)
);

assert(swap.expiry > block.timestamp);
( , uint256 expiry) = parseData(swap.config);

assert(expiry > block.timestamp);
assert(swap.biding.length > 0);
assert(swap.asking.length > 0);
return swap;
Expand Down
64 changes: 32 additions & 32 deletions contracts/interfaces/ISwap.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,37 +5,37 @@ pragma solidity ^0.8.17;
* @dev Interface for the Swap Struct, used in the {Swaplace} implementation.
*/
interface ISwap {
/**
* @dev Assets can be ERC20 or ERC721.
*
* It is composed of:
* - `addr` of the asset.
* - `amountOrId` of the asset based on the standard.
*
* NOTE: `amountOrId` is the `amount` of ERC20 or the `tokenId` of ERC721.
*/
struct Asset {
address addr;
uint256 amountOrId;
}
/**
* @dev Assets can be ERC20 or ERC721.
*
* It is composed of:
* - `addr` of the asset.
* - `amountOrId` of the asset based on the standard.
*
* NOTE: `amountOrId` is the `amount` of ERC20 or the `tokenId` of ERC721.
*/
struct Asset {
address addr;
uint256 amountOrId;
}

/**
* @dev The Swap struct is the heart of Swaplace.
*
* It is composed of:
* - `owner` of the Swap.
* - `allowed` address to accept the Swap.
* - `expiry` date of the Swap.
* - `biding` assets that are being bided by the owner.
* - `asking` assets that are being asked by the owner.
*
* NOTE: When `allowed` address is the zero address, anyone can accept the Swap.
*/
struct Swap {
address owner;
address allowed;
uint256 expiry;
Asset[] biding;
Asset[] asking;
}
/**
* @dev The Swap struct is the heart of Swaplace.
*
* It is composed of:
* - `owner` of the Swap.
* - `config` holds two values 'allowed' and 'expiry',packed using bitwise.
* 'allowed' address to accept the Swap.
* 'expiry' date of the Swap.
* - `biding` assets that are being bided by the owner.
* - `asking` assets that are being asked by the owner.
*
* NOTE: When `allowed` address is the zero address, anyone can accept the Swap.
*/
struct Swap {
address owner;
uint256 config;
Asset[] biding;
Asset[] asking;
}
}
9 changes: 9 additions & 0 deletions contracts/interfaces/ISwapFactory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,13 @@ interface ISwapFactory {
ISwap.Asset[] memory assets,
ISwap.Asset[] memory asking
) external view returns (ISwap.Swap memory);

function packData(
address allowed,
uint256 expiry
) external pure returns(uint256);

function parseData(
uint256 config
) external pure returns (address,uint256);
}

0 comments on commit 5cb9f6c

Please sign in to comment.