Skip to content

Commit

Permalink
Merge pull request #237 from blockful-io/develop
Browse files Browse the repository at this point in the history
Merge 'develop' branch to the 'main'branch
  • Loading branch information
0xneves authored Jul 4, 2024
2 parents 1321fc8 + 026d8cc commit 92ca10e
Show file tree
Hide file tree
Showing 43 changed files with 2,586 additions and 1,163 deletions.
29 changes: 22 additions & 7 deletions .env.sample
Original file line number Diff line number Diff line change
@@ -1,22 +1,37 @@
# Swaplace deployed contracts addresses
SWAPLACE_ADDRESS=0xD8E3580C1b6f117c5b35DdD01dd9e50d9487501D
# Swaplace last deployed contracts address.
SWAPLACE_ADDRESS=0x000000000000000000000000000000000000000000
# Mocks last deployed contracts addresses.
ERC20_ADDRESS=0x000000000000000000000000000000000000000000
ERC721_ADDRESS=0x000000000000000000000000000000000000000000
ERC1155_ADDRESS=0x000000000000000000000000000000000000000000
# Amount of ERC20 tokens to be minted for signer address.
AMOUNT=1000
# Token ID of ERC721 to be minted for signer address.
TOKEN_ID=1
# The swap to be accepted by the acceptee.
SWAP_ID=1

# These are public known private keys and are here as an example.
# Funds in these accounts are at risk of being stolen.
# You should change the private keys to your own private keys.
# WARNING: Blockful advises to not, ever, use your wallet helding funds as the private keys in any kind of development.
# WARNING: funds in these account are at risk of being stolen.
DEPLOYER_PRIVATE_KEY=0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80

# Etherscan and similar services API keys
# Etherscan and similar services API keys used to verify contract addresses in block explorers.
ETHERSCAN_API_KEY=YourApiKeyToken

# EVM compatible chains and their RPC URLs
# TESTNET
# EVM compatible chains and their public RPC's
# They are not implemented in the code, but feel free to use and explore them.

# TESTNETS
GOERLI_RPC_URL=https://ethereum-goerli.publicnode.com
SEPOLIA_RPC_URL=https://ethereum-sepolia.publicnode.com
MUMBAI_RPC_URL=https://polygon-mumbai-bor.publicnode.com
FUJI_RPC_URL=https://avalanche-fuji-c-chain.publicnode.com
BNB_TESTNET_RPC_URL=https://bsc-testnet.publicnode.com
KAKAROT_SEPOLIA_RPC_URL=https://sepolia-rpc.kakarot.org

# MAINNET
# MAINNETS
ETH_RPC_URL="https://eth.llamarpc.com"
BNB_RPC_URL=https://binance.llamarpc.com
AVAX_RPC_URL=https://avalanche-mainnet.infura.io
Expand Down
60 changes: 60 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
## Running compile to generate the abi's and the type-chain types.
compile:
yarn compile

## Please specify which network following the available ones in hardhat.config.ts.
network=localhost

## Deploying Swaplace contract to the desired network.
swaplace:
yarn deploy:swaplace ${network}

## The mocks are essential to run the tests. We will be swapping token assets.
mocks:
yarn deploy:mocks ${network}

## Mock deployments are a requirement to run the mint script, which will fund the signer's wallet.
mint:
yarn mint ${network}

## Mock deployments are a requirement to run the mint script, which will fund the signer's wallet.
approve:
yarn approve ${network}

## Create a swap on the swaplace contract and save the swap id in the .env file.
swap:
yarn create-swap ${network}

## Accept a swap on the swaplace contract based on the swap id in the .env file.
accept:
yarn accept-swap ${network}

## Cancel a swap on the swaplace contract based on the swap id in the .env file.
cancel:
make compile
yarn cancel-swap ${network}

## Run the entire test suite.
## Running a local node by using `npx hardhat node` in a separated terminal is
## a requirement to run the tests in the localhost network.
test-suite-runner:
yarn clean
yarn compile
make swaplace
make mocks
make mint
make approve
make swap
make accept
make swap
make cancel

## Feed existing Swaplace contract with some transactions.
## Requires a deployed Swaplace contract and mocks.
transactions:
make mint
make approve
make swap
make accept
make swap
make cancel
93 changes: 74 additions & 19 deletions contracts/SwapFactory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,22 @@ import {ISwapFactory} from "./interfaces/ISwapFactory.sol";
* - The `allowed` address is the address that can accept the Swap. If the allowed
* address is the zero address, then anyone can accept the Swap.
* - The `expiry` date is the timestamp that the Swap will be available to accept.
* - The `recipient` is the address that will receive the ETH as type uint8. If the
* recipient is equals to 0, the acceptee will receive the ETH. If the recipient is
* between 1<>255 then the recipient will be the owner of the Swap.
* - The `value` is the amount of ETH that the recipient will receive with a maximum
* of 6 decimals (0.000001 ETH). The contract will fill the value up to 18 decimals.
* - The `biding` are the assets that the owner is offering.
* - The `asking` are the assets that the owner wants in exchange.
*
* The Swap struct uses an {Asset} struct to represent the asset. This struct is
* composed of:
*
* - The `address` of the asset. This address can be from an ERC20 or ERC721 contract.
* - The `address` of the token asset.
* - The `amount` or `id` of the asset. This amount can be the amount of ERC20 tokens
* or the ID of an ERC721 token.
*
* To use other standards, like ERC1155, you can wrap the ownership of the asset
* in an a trusted contract and Swap as an ERC721. This way, you can tokenize any
* on-chain execution and trade on Swaplace.
* or the NFT ID of an ERC721.
* - The `amount` and `id` can be encoded together in a single uint256, allowing the
* ERC1155 tokens to be swapped.
*/
abstract contract SwapFactory is ISwapFactory, ISwap, IErrors {
/**
Expand All @@ -43,39 +46,91 @@ abstract contract SwapFactory is ISwapFactory, ISwap, IErrors {
return Asset(addr, amountOrId);
}

/**
* @dev See {ISwapFactory-make1155Asset}.
*/
function make1155Asset(
address addr,
uint120 tokenId,
uint120 tokenAmount
) public pure virtual returns (Asset memory) {
return Asset(addr, encodeAsset(tokenId, tokenAmount));
}

/**
* @dev See {ISwapFactory-makeSwap}.
*/
function makeSwap(
address owner,
address allowed,
uint256 expiry,
uint32 expiry,
uint8 recipient,
uint56 value,
Asset[] memory biding,
Asset[] memory asking
) public view virtual returns (Swap memory) {
if (expiry < block.timestamp) revert InvalidExpiry(expiry);

if (biding.length == 0 || asking.length == 0) revert InvalidAssetsLength();
if (expiry < block.timestamp) revert InvalidExpiry();
uint256 config = encodeConfig(allowed, expiry, recipient, value);
return Swap(owner, config, biding, asking);
}

uint256 config = packData(allowed, expiry);
/**
* @dev See {ISwapFactory-encodeAsset}.
*/
function encodeAsset(
uint120 tokenId,
uint120 tokenAmount
) public pure returns (uint256 amountAndId) {
return
(uint256(type(uint16).max) << 240) |
(uint256(tokenId) << 120) |
uint256(tokenAmount);
}

return Swap(owner, config, biding, asking);
/**
* @dev See {ISwapFactory-decodeAsset}.
*/
function decodeAsset(
uint256 amountOrId
)
public
pure
returns (uint16 tokenType, uint256 tokenId, uint256 tokenAmount)
{
return (
uint16(amountOrId >> 240),
uint256(uint120(amountOrId >> 120)),
uint256(uint120(amountOrId))
);
}

/**
* @dev See {ISwapFactory-packData}.
* @dev See {ISwapFactory-encodeConfig}.
*/
function packData(
function encodeConfig(
address allowed,
uint256 expiry
uint32 expiry,
uint8 recipient,
uint56 value
) public pure returns (uint256) {
return (uint256(uint160(allowed)) << 96) | uint256(expiry);
return
(uint256(uint160(allowed)) << 96) |
(uint256(expiry) << 64) |
(uint256(recipient) << 56) |
uint256(value);
}

/**
* @dev See {ISwapFactory-parseData}.
* @dev See {ISwapFactory-decodeConfig}.
*/
function parseData(uint256 config) public pure returns (address, uint256) {
return (address(uint160(config >> 96)), uint256(config & ((1 << 96) - 1)));
function decodeConfig(
uint256 config
) public pure returns (address, uint32, uint8, uint56) {
return (
address(uint160(config >> 96)),
uint32(config >> 64),
uint8(config >> 56),
uint56(config)
);
}
}
Loading

0 comments on commit 92ca10e

Please sign in to comment.