Skip to content

Commit

Permalink
Merge branch 'revision' into createSwapERC1155
Browse files Browse the repository at this point in the history
  • Loading branch information
0xneves committed May 19, 2024
2 parents 2bed24f + 7c7ca2c commit 7fb7743
Show file tree
Hide file tree
Showing 9 changed files with 347 additions and 19 deletions.
1 change: 1 addition & 0 deletions .env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ 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

# MAINNETS
ETH_RPC_URL="https://eth.llamarpc.com"
Expand Down
12 changes: 0 additions & 12 deletions contracts/echidna/TestSwapFactory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,4 @@ contract TestFactory is SwapFactory {
_asset
);
}

function echidna_revert_invalid_length() public view {
makeSwap(
address(0),
address(0),
uint32(block.timestamp + 100),
0,
0,
new Asset[](0),
new Asset[](0)
);
}
}
4 changes: 2 additions & 2 deletions contracts/mock/MockERC721.sol
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ contract MockERC721 is ERC721 {
function tokenURI(
uint256
) public view virtual override returns (string memory) {
return "ipfs://QmQJnHseE9VPw5qVxuEhxTiZ7avzgkCdFz69rg86UvTZdk/";
return "ipfs://QmWodCkovJk18U75g8Veg6rCnw7951vQvTjYfS7J3nMFma/";
}
}
}
2 changes: 1 addition & 1 deletion hardhat.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const config: HardhatUserConfig = {
* @dev Testnets
*/
kakarot: {
url: `${process.env.KAKAROT_RPC_URL}`,
url: `${process.env.KAKAROT_SEPOLIA_RPC_URL}`,
accounts: [`${DEPLOYER_PRIVATE_KEY}`],
},
sepolia: {
Expand Down
19 changes: 17 additions & 2 deletions scripts/approve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,18 @@ async function main() {
/// @dev This is the list of mock deployments addresses that were stored in the `.env` file.
const ERC20_ADDRESS = process.env.ERC20_ADDRESS || 0x0;
const ERC721_ADDRESS = process.env.ERC721_ADDRESS || 0x0;
const ERC1155_ADDRESS = process.env.ERC1155_ADDRESS || 0x0;
/// @dev The Swaplace address also needs to be instance to receive the approvals.
const SWAPLACE_ADDRESS = process.env.SWAPLACE_ADDRESS || 0x0;
/// @dev Will throw an error if any of the addresses were not set in the `.env` file.
if (!ERC20_ADDRESS || !ERC721_ADDRESS || !SWAPLACE_ADDRESS) {
if (
!ERC20_ADDRESS ||
!ERC721_ADDRESS ||
!SWAPLACE_ADDRESS ||
!ERC1155_ADDRESS
) {
throw new Error(
"Invalid ERC20, ERC721 or Swaplace address, please check if the addresse in the `.env` file is set up correctly.",
"Invalid ERC20, ERC721, ERC1155 or Swaplace address, please check if the addresse in the `.env` file is set up correctly.",
);
}

Expand All @@ -39,6 +45,7 @@ async function main() {
/// @dev The returned contract instance that will be deployed via the deploy function in utils.
let MockERC20: Contract;
let MockERC721: Contract;
let MockERC1155: Contract;

/// @dev will throw an error if any of the accounts was not set up correctly.
try {
Expand All @@ -63,6 +70,11 @@ async function main() {
ERC721_ADDRESS,
signers[0],
);
MockERC1155 = await ethers.getContractAt(
"MockERC1155",
ERC1155_ADDRESS,
signers[0],
);
} catch (error) {
throw new Error(
`Error deploying one of the Mock Contracts.
Expand All @@ -76,11 +88,13 @@ async function main() {
/// @dev Responses from the minting transactions.
let txErc20;
let txErc721;
let txErc1155;

/// @dev We are approving the signer address to spend the amount of tokens.
try {
txErc20 = await MockERC20.approve(SWAPLACE_ADDRESS, amount);
txErc721 = await MockERC721.approve(SWAPLACE_ADDRESS, tokenId);
txErc1155 = await MockERC1155.setApprovalForAll(SWAPLACE_ADDRESS, true);
} catch (error) {
throw new Error(
`Error while approving the tokens. Make sure that the approve function is
Expand All @@ -97,6 +111,7 @@ async function main() {
tokenId,
txErc721.hash,
);
console.log("\nERC1155 Approved all tokens \nAt Tx %s", txErc1155.hash);
}

main().catch((error) => {
Expand Down
11 changes: 11 additions & 0 deletions scripts/deployMock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ async function main() {
/// @dev The returned contract instance that will be deployed via the deploy function in utils.
let MockERC20: Contract;
let MockERC721: Contract;
let MockERC1155: Contract;

/// @dev will throw an error if any of the accounts was not set up correctly.
try {
Expand All @@ -27,6 +28,7 @@ async function main() {
// We are deploying both contracts to test the user flux with the entire functionality.
MockERC20 = await deploy("MockERC20", signers[0]);
MockERC721 = await deploy("MockERC721", signers[0]);
MockERC1155 = await deploy("MockERC1155", signers[0]);

// @dev Log Contract address and the Tx hash which can be searched on Etherscan (or any other block explorer).
console.log(
Expand All @@ -43,13 +45,22 @@ async function main() {
MockERC721.deployTransaction.hash,
);

console.log(
"\nContract %s \nDeployed to %s \nAt Tx %s\n",
"MockERC1155",
MockERC1155.address,
MockERC1155.deployTransaction.hash,
);

/// @dev Store the contract addresses in the .env file.
await storeEnv(MockERC20.address, "ERC20_ADDRESS", true);
await storeEnv(MockERC721.address, "ERC721_ADDRESS", true);
await storeEnv(MockERC1155.address, "ERC1155_ADDRESS", true);

/// @dev Awaits for the transaction to be mined.
await MockERC20.deployed();
await MockERC721.deployed();
await MockERC1155.deployed();
}

main().catch((error) => {
Expand Down
20 changes: 18 additions & 2 deletions scripts/mint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ async function main() {
/// @dev This is the list of mock deployments addresses that were stored in the `.env` file.
const ERC20_ADDRESS = process.env.ERC20_ADDRESS || 0x0;
const ERC721_ADDRESS = process.env.ERC721_ADDRESS || 0x0;
const ERC1155_ADDRESS = process.env.ERC1155_ADDRESS || 0x0;
/// @dev Will throw an error if any of the addresses were not set in the `.env` file.
if (!ERC20_ADDRESS || !ERC721_ADDRESS) {
if (!ERC20_ADDRESS || !ERC721_ADDRESS || !ERC1155_ADDRESS) {
throw new Error(
"Invalid ERC20 or ERC721 address, please check if the addresse in the `.env` file is set up correctly.",
"Invalid ERC20 or ERC721 or ERC1155 address, please check if the addresses in the `.env` file are set up correctly.",
);
}

Expand All @@ -25,6 +26,7 @@ async function main() {
/// @dev The returned contract instance that will be deployed via the deploy function in utils.
let MockERC20: Contract;
let MockERC721: Contract;
let MockERC1155: Contract;

/// @dev will throw an error if any of the accounts was not set up correctly.
try {
Expand All @@ -49,6 +51,11 @@ async function main() {
ERC721_ADDRESS,
signers[0],
);
MockERC1155 = await ethers.getContractAt(
"MockERC1155",
ERC1155_ADDRESS,
signers[0],
);
} catch (error) {
throw new Error(
`Error deploying one of the Mock Contracts.
Expand All @@ -69,6 +76,7 @@ async function main() {
/// @dev Responses from the minting transactions.
let txErc20;
let txErc721;
let txErc1155;

/// @dev Minting function will throw an error if the minting fails.
/// We are minting for the first signer of `hardhat.config.ts` 1000
Expand All @@ -77,6 +85,7 @@ async function main() {
try {
txErc20 = await MockERC20.mint(signers[0].address, amount);
txErc721 = await MockERC721.mint(signers[0].address, tokenId);
txErc1155 = await MockERC1155.mint(signers[0].address, tokenId, amount);
} catch (error) {
throw new Error(
`Error while minting tokens. Make sure that the minting function is
Expand All @@ -93,12 +102,19 @@ async function main() {
tokenId,
txErc721.hash,
);
console.log(
"\nERC1155 Minted %s tokens with ID #%s \nAt Tx %s",
amount,
tokenId,
txErc1155.hash,
);

await storeEnv(tokenId, "TOKEN_ID", false);

/// @dev Awaits for the transaction to be mined.
await txErc20.wait();
await txErc721.wait();
await txErc1155.wait();
}

main().catch((error) => {
Expand Down
113 changes: 113 additions & 0 deletions test/TestSwapFactory.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
composeSwap,
encodeConfig,
decodeConfig,
Swap,
} from "./utils/SwapFactory";
import { blocktimestamp, deploy } from "./utils/utils";

Expand All @@ -17,6 +18,7 @@ describe("Swaplace Factory", async function () {
let Swaplace: Contract;
let MockERC20: Contract;
let MockERC721: Contract;
let MockERC1155: Contract;

// The signers of the test
let deployer: SignerWithAddress;
Expand All @@ -30,6 +32,7 @@ describe("Swaplace Factory", async function () {
Swaplace = await deploy("Swaplace", deployer);
MockERC20 = await deploy("MockERC20", deployer);
MockERC721 = await deploy("MockERC721", deployer);
MockERC1155 = await deploy("MockERC1155", deployer);
});

it("Should be able to {makeAsset} for ERC20 and ERC721", async function () {
Expand Down Expand Up @@ -241,6 +244,116 @@ describe("Swaplace Factory", async function () {
expect(swap.asking[1]).to.be.equals(ERC721Asset);
});

it("Should be able to {makeSwap} with ERC1155 tokens", async function () {
const bidingAddr = [MockERC1155.address];
const tokenId = 1;
const amount = 3;
const amountAndId = await Swaplace.encodeAsset(tokenId, amount);
const bidingAmountOrId = [amountAndId];

const askingAddr = [MockERC721.address];
const askingAmountOrId = [50];

const ERC1155Asset: Asset = await makeAsset(
bidingAddr[0],
bidingAmountOrId[0],
);
const ERC721Asset: Asset = await makeAsset(
askingAddr[0],
askingAmountOrId[0],
);

const currentTimestamp = (await blocktimestamp()) + 1000000;
const config = await Swaplace.encodeConfig(
zeroAddress,
currentTimestamp,
0,
0,
);

const swap = await makeSwap(
owner.address,
config,
[ERC1155Asset],
[ERC721Asset],
);

const onChainSwap = await Swaplace.makeSwap(
owner.address,
zeroAddress,
currentTimestamp,
0,
0,
[ERC1155Asset],
[ERC721Asset],
);

const [allowed, expiry, recipient, value] = await Swaplace.decodeConfig(
swap.config,
);

const [onChainAllowed, onChainExpiry, onChainRecipient, onChainValue] =
await Swaplace.decodeConfig(onChainSwap.config);

expect(swap.owner).to.be.equals(onChainSwap.owner);
expect(expiry).to.be.equals(onChainExpiry);
expect(allowed).to.be.equals(onChainAllowed);
expect(recipient).to.be.equals(onChainRecipient);
expect(value).to.be.equals(onChainValue);
expect(swap.biding[0].addr).to.be.equals(onChainSwap.biding[0].addr);
expect(swap.biding[0].amountOrId).to.be.equals(
onChainSwap.biding[0].amountOrId,
);

expect(swap.asking[0].addr).to.be.equals(onChainSwap.asking[0].addr);
expect(swap.asking[0].amountOrId).to.be.equals(
onChainSwap.asking[0].amountOrId,
);
});

it("Should be able to {composeSwap} using ERC1155", async function () {
const bidingAddr = [MockERC1155.address];
const tokenId = 1;
const amount = 3;
const amountAndId = await Swaplace.encodeAsset(tokenId, amount);
const bidingAmountOrId = [amountAndId];

const askingAddr = [MockERC721.address];
const askingAmountOrId = [50];

const currentTimestamp = (await blocktimestamp()) + 1000000;
const config = await Swaplace.encodeConfig(
zeroAddress,
currentTimestamp,
0,
0,
);

const swap: Swap = await composeSwap(
owner.address,
config,
bidingAddr,
bidingAmountOrId,
askingAddr,
askingAmountOrId,
);

const [allowed, expiry, recipient, value] = await Swaplace.decodeConfig(
swap.config,
);

expect(swap.owner).to.be.equals(owner.address);
expect(allowed).to.be.equals(zeroAddress);
expect(expiry).to.be.equals(expiry);
expect(recipient).to.be.equals(0);
expect(value).to.be.equals(0);
expect(swap.biding[0].addr).to.be.equals(bidingAddr[0]);
expect(swap.biding[0].amountOrId).to.be.equals(bidingAmountOrId[0]);

expect(swap.asking[0].addr).to.be.equals(askingAddr[0]);
expect(swap.asking[0].amountOrId).to.be.equals(askingAmountOrId[0]);
});

it("Should be able to {composeSwap} using both ERC20, ERC721", async function () {
const currentTimestamp = (await blocktimestamp()) + 2000000;

Expand Down
Loading

0 comments on commit 7fb7743

Please sign in to comment.