Skip to content

Commit

Permalink
added unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
0xrs committed Mar 9, 2021
1 parent 5ac7642 commit 0c89dd4
Show file tree
Hide file tree
Showing 7 changed files with 303 additions and 23 deletions.
Binary file added .DS_Store
Binary file not shown.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Token Basket Swaps

Docs WIP

```
npm i
npx hardhat test
```

![](docs/screenshot.png)
35 changes: 29 additions & 6 deletions contracts/MetaExchange.sol
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,28 @@ contract MetaExchange is VerifySignature {
public {

bytes32 orderHash = keccak256(abi.encodePacked(makerOrderSig, takerOrderSig));
//require(fills[orderHash] == false, "Order already filled or canceled by the maker");

//check if order already filled
require(fills[orderHash] == false, "Order already filled or canceled by the maker");
require(makerAddress != takerAddress, "Maker and taker should be different");
require(order.expiration > now, "Order already expired and no longer valid");
require(msg.sender != makerAddress, "Order cannot be executed by maker");

require(order.makerErc20Addresses.length == order.makerErc20Amounts.length,
"Invalid Order, Size of erc20 address array and amounts different");
require(order.makerErc721Addresses.length == order.makerErc721Ids.length,
"Invalid Order, Size of erc721 address array and amounts different");
require(order.makerErc1155Addresses.length == order.makerErc1155Ids.length
&& order.makerErc1155Addresses.length == order.makerErc1155Amounts.length,
"Invalid Order, Size of erc1155 address array and amounts different");

require(order.takerErc20Addresses.length == order.takerErc20Amounts.length,
"Invalid Order, Size of erc20 address array and amounts different");
require(order.takerErc721Addresses.length == order.takerErc721Ids.length,
"Invalid Order, Size of erc721 address array and amounts different");
require(order.takerErc1155Addresses.length == order.takerErc1155Ids.length
&& order.takerErc1155Addresses.length == order.takerErc1155Amounts.length,
"Invalid Order, Size of erc1155 address array and amounts different");

/* //check if order already filled
if (fills[orderHash]) {
emit Failed(3, makerAddress, takerAddress, orderHash, order.expiration, nonce);
return;
Expand All @@ -108,7 +127,7 @@ contract MetaExchange is VerifySignature {
if (msg.sender != takerAddress) {
emit Failed(4, makerAddress, takerAddress, orderHash, order.expiration, nonce);
return;
}
} */


require(verify(
Expand Down Expand Up @@ -179,7 +198,11 @@ contract MetaExchange is VerifySignature {
// Only the maker can cancel an order
if (msg.sender == makerAddress) {

// Check that order has not already been filled/cancelled
require(fills[orderHash] == false, "Order already filled or cancelled");
fills[orderHash] = true;
emit Canceled(makerAddress, takerAddress, orderHash, order.expiration, nonce);

/* // Check that order has not already been filled/cancelled
if (fills[orderHash] == false) {
// Cancel the order by considering it filled.
Expand All @@ -190,7 +213,7 @@ contract MetaExchange is VerifySignature {
} else {
emit Failed(5, makerAddress, takerAddress, orderHash, order.expiration, nonce);
}
} */
}
}

Expand Down
Binary file added docs/screeshot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 2 additions & 3 deletions scripts/sample-script.js → scripts/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
// When running the script with `hardhat run <script>` you'll find the Hardhat
// Runtime Environment's members available in the global scope.
const hre = require("hardhat");
const eutil = require('ethjs-util');
// const Web3 = require("Web3");
// const Wallet = ethers.Wallet;
// const utils = ethers.utils;
Expand Down Expand Up @@ -193,14 +192,14 @@ async function main() {
'makerErc721Addresses': makerErc721Addresses,
'makerErc721Ids': makerErc721Ids,
'makerErc1155Addresses': makerErc1155Addresses,
'makerErc1155Ids': makerErc721Ids,
'makerErc1155Ids': makerErc1155Ids,
'makerErc1155Amounts': makerErc1155Amounts,
'takerErc20Addresses': takerErc20Addresses,
'takerErc20Amounts': takerErc20Amounts,
'takerErc721Addresses': takerErc721Addresses,
'takerErc721Ids': takerErc721Ids,
'takerErc1155Addresses': takerErc1155Addresses,
'takerErc1155Ids': takerErc721Ids,
'takerErc1155Ids': takerErc1155Ids,
'takerErc1155Amounts': takerErc1155Amounts,
'expiration': expiration
}
Expand Down
14 changes: 0 additions & 14 deletions test/sample-test.js

This file was deleted.

262 changes: 262 additions & 0 deletions test/swap-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,262 @@
const { chai, expect, should } = require("chai");
const { BigNumber, Contract } = require('ethers');
const { ethers } = require('hardhat');

let owner
let addr1
let addrs
let owner_e
let addr1_e
let addrs_e
let f1
let f2
let nf1
let nf2
let nff1
let nff2
let metaExchange
let verifySignature

before(async() => {
console.log("===================Deploying Contracts, Setting Up=====================");

[owner, addr1, ...addrs] = await web3.eth.getAccounts();
[owner_e, addr1_e, ...addrs_e] = await ethers.getSigners();
const initial_mint = web3.utils.toWei('100000', 'ether');
const MetaExchange = await ethers.getContractFactory("MetaExchange");
metaExchange = await MetaExchange.deploy();
await metaExchange.deployed();
const VerifySignature = await ethers.getContractFactory("VerifySignature");
verifySignature = await VerifySignature.deploy();
await verifySignature.deployed();
const MockERC20 = await ethers.getContractFactory("MockERC20");
f1 = await MockERC20.deploy("F1", "F1");
await f1.deployed();

await f1.mint(owner, initial_mint);
await f1.mint(addr1, initial_mint);

f2 = await MockERC20.deploy("F2", "F2");
await f2.deployed();

await f2.mint(owner, initial_mint);
await f2.mint(addr1, initial_mint);

await f1.connect(owner_e).approve(metaExchange.address, await f1.balanceOf(owner));
await f1.connect(addr1_e).approve(metaExchange.address, await f1.balanceOf(addr1));

await f2.connect(owner_e).approve(metaExchange.address, await f2.balanceOf(owner));
await f2.connect(addr1_e).approve(metaExchange.address, await f2.balanceOf(addr1));

const MockERC721 = await ethers.getContractFactory("MockERC721");
nf1 = await MockERC721.deploy("NF1", "NF1");
await nf1.deployed();

await nf1.mint(owner, 1);
await nf1.mint(addr1, 2);

nf2 = await MockERC721.deploy("NF2", "NF2");
await nf2.deployed();

await nf2.mint(owner, 1);
await nf2.mint(addr1, 2);

//approvals
await nf1.connect(owner_e).approve(metaExchange.address, 1);
await nf1.connect(addr1_e).approve(metaExchange.address, 2);

await nf2.connect(owner_e).approve(metaExchange.address, 1);
await nf2.connect(addr1_e).approve(metaExchange.address, 2);

const MockERC1155 = await ethers.getContractFactory("MockERC1155");
nff1 = await MockERC1155.deploy("NFF1");
await nff1.deployed();

await nff1.mint(owner, 1, 10, "0x");
await nff1.mint(addr1, 2, 10, "0x");

nff2 = await MockERC1155.deploy("NFF2");
await nff2.deployed();

await nff2.mint(owner, 1, 10, "0x");
await nff2.mint(addr1, 2, 10, "0x");
//approvals
await nff1.connect(owner_e).setApprovalForAll(metaExchange.address, true);
await nff1.connect(addr1_e).setApprovalForAll(metaExchange.address, true);

await nff2.connect(owner_e).setApprovalForAll(metaExchange.address, true);
await nff2.connect(addr1_e).setApprovalForAll(metaExchange.address, true);

console.log("Maker Address: " + owner);
console.log("Taker Address: " + addr1);

console.log("Initial balance of ERC20 F1 for User1: " + web3.utils.fromWei((await f1.balanceOf(owner)).toString(), 'ether'));
console.log("Initial balance of ERC20 F1 for User2: " + web3.utils.fromWei((await f1.balanceOf(addr1)).toString(), 'ether'));
console.log("Initial balance of ERC20 F2 for User1: " + web3.utils.fromWei((await f2.balanceOf(owner)).toString(), 'ether'));
console.log("Initial balance of ERC20 F2 for User2: " + web3.utils.fromWei((await f2.balanceOf(addr1)).toString(), 'ether'));

console.log("Initial owner of ERC721 NFT1 ID1: " + (await nf1.ownerOf(1)));
console.log("Initial owner of ERC721 NFT2 ID2: " + (await nf2.ownerOf(2)));

console.log("Initial balance of ERC1155 NFT1 ID1 for User1: " + (await nff1.balanceOf(owner, 1)));
console.log("Initial balance of ERC1155 NFT1 ID2 for User1: " + (await nff1.balanceOf(owner, 2)));
console.log("Initial balance of ERC1155 NFT1 ID1 for User2: " + (await nff1.balanceOf(addr1, 1)));
console.log("Initial balance of ERC1155 NFT1 ID2 for User2: " + (await nff1.balanceOf(addr1, 2)));
console.log("Initial balance of ERC1155 NFT2 ID1 for User1: " + (await nff2.balanceOf(owner, 1)));
console.log("Initial balance of ERC1155 NFT2 ID2 for User1: " + (await nff2.balanceOf(owner, 2)));
console.log("Initial balance of ERC1155 NFT2 ID1 for User2: " + (await nff2.balanceOf(addr1, 1)));
console.log("Initial balance of ERC1155 NFT2 ID2 for User2: " + (await nff2.balanceOf(addr1, 2)));

console.log("=====================================================================\n\n")
})

describe("Do a transaction of basket of assets of different types", function() {

it("Should fail if transaction is made by maker", async() => {
let makerAddress = owner;
let takerAddress = addr1;
let makerErc20Addresses = [f1.address];
let makerErc20Amounts = [web3.utils.toWei('250', 'ether')];
let makerErc721Addresses = [nf1.address];
let makerErc721Ids = [1];
let makerErc1155Addresses = [nff1.address];
let makerErc1155Ids = [1];
let makerErc1155Amounts = [4];
let takerErc20Addresses = [f2.address];
let takerErc20Amounts = [web3.utils.toWei('750', 'ether')];
let takerErc721Addresses = [nf2.address];
let takerErc721Ids = [2];
let takerErc1155Addresses = [nff2.address];
let takerErc1155Ids = [2];
let takerErc1155Amounts = [2];

let expiration = new Date().getTime() + 60000;
let nonce = 1;

let makerArgs = [makerAddress, makerErc20Addresses,
makerErc20Amounts, makerErc721Addresses, makerErc721Ids,
makerErc1155Addresses, makerErc1155Ids, makerErc1155Amounts, expiration, nonce];
let takerArgs = [makerAddress, takerErc20Addresses,
takerErc20Amounts, takerErc721Addresses, takerErc721Ids,
takerErc1155Addresses, takerErc1155Ids, takerErc1155Amounts, expiration, nonce];

const makerMsgHash = await verifySignature.getMessageHash(...makerArgs);
const signedMakerMsg = await web3.eth.sign(makerMsgHash, owner, console.log);

const takerMsgHash = await verifySignature.getMessageHash(...takerArgs);
const signedTakerMsg = await web3.eth.sign(takerMsgHash, owner, console.log);

let order = {
'makerErc20Addresses': makerErc20Addresses,
'makerErc20Amounts': makerErc20Amounts,
'makerErc721Addresses': makerErc721Addresses,
'makerErc721Ids': makerErc721Ids,
'makerErc1155Addresses': makerErc1155Addresses,
'makerErc1155Ids': makerErc1155Ids,
'makerErc1155Amounts': makerErc1155Amounts,
'takerErc20Addresses': takerErc20Addresses,
'takerErc20Amounts': takerErc20Amounts,
'takerErc721Addresses': takerErc721Addresses,
'takerErc721Ids': takerErc721Ids,
'takerErc1155Addresses': takerErc1155Addresses,
'takerErc1155Ids': takerErc1155Ids,
'takerErc1155Amounts': takerErc1155Amounts,
'expiration': expiration
};

await expect(metaExchange.connect(owner_e).
fill(makerAddress, takerAddress, order, signedMakerMsg, signedTakerMsg, nonce)).to.be.revertedWith("Order cannot be executed by maker");
})

it("Should succeed if transaction is made by taker", async() => {
let makerAddress = owner;
let takerAddress = addr1;
let makerErc20Addresses = [f1.address];
let makerErc20Amounts = [web3.utils.toWei('250', 'ether')];
let makerErc721Addresses = [nf1.address];
let makerErc721Ids = [1];
let makerErc1155Addresses = [nff1.address];
let makerErc1155Ids = [1];
let makerErc1155Amounts = [4];
let takerErc20Addresses = [f2.address];
let takerErc20Amounts = [web3.utils.toWei('750', 'ether')];
let takerErc721Addresses = [nf2.address];
let takerErc721Ids = [2];
let takerErc1155Addresses = [nff2.address];
let takerErc1155Ids = [2];
let takerErc1155Amounts = [2];

let expiration = new Date().getTime() + 60000;
let nonce = 1;

let makerArgs = [makerAddress, makerErc20Addresses,
makerErc20Amounts, makerErc721Addresses, makerErc721Ids,
makerErc1155Addresses, makerErc1155Ids, makerErc1155Amounts, expiration, nonce];
let takerArgs = [makerAddress, takerErc20Addresses,
takerErc20Amounts, takerErc721Addresses, takerErc721Ids,
takerErc1155Addresses, takerErc1155Ids, takerErc1155Amounts, expiration, nonce];

const makerMsgHash = await verifySignature.getMessageHash(...makerArgs);
const signedMakerMsg = await web3.eth.sign(makerMsgHash, owner, console.log);

const takerMsgHash = await verifySignature.getMessageHash(...takerArgs);
const signedTakerMsg = await web3.eth.sign(takerMsgHash, owner, console.log);

let order = {
'makerErc20Addresses': makerErc20Addresses,
'makerErc20Amounts': makerErc20Amounts,
'makerErc721Addresses': makerErc721Addresses,
'makerErc721Ids': makerErc721Ids,
'makerErc1155Addresses': makerErc1155Addresses,
'makerErc1155Ids': makerErc1155Ids,
'makerErc1155Amounts': makerErc1155Amounts,
'takerErc20Addresses': takerErc20Addresses,
'takerErc20Amounts': takerErc20Amounts,
'takerErc721Addresses': takerErc721Addresses,
'takerErc721Ids': takerErc721Ids,
'takerErc1155Addresses': takerErc1155Addresses,
'takerErc1155Ids': takerErc1155Ids,
'takerErc1155Amounts': takerErc1155Amounts,
'expiration': expiration
};
//console.log(order);
await expect(metaExchange.connect(addr1_e).
fill(makerAddress, takerAddress, order, signedMakerMsg, signedTakerMsg, nonce)).to.not.be.reverted;
})

it("User balances should have updated values after swap", async() => {
expect(web3.utils.fromWei((await f1.balanceOf(owner)).toString())).to.equal('99750');
expect(web3.utils.fromWei((await f1.balanceOf(addr1)).toString())).to.equal('100250');
expect(web3.utils.fromWei((await f2.balanceOf(owner)).toString())).to.equal('100750');
expect(web3.utils.fromWei((await f2.balanceOf(addr1)).toString())).to.equal('99250');

expect(await nf1.ownerOf(1)).to.equal(addr1);
expect(await nf2.ownerOf(2)).to.equal(owner);

expect(await nff1.balanceOf(owner, 1)).to.equal(6);
expect(await nff1.balanceOf(owner, 2)).to.equal(0);
expect(await nff1.balanceOf(addr1, 1)).to.equal(4);
expect(await nff1.balanceOf(addr1, 2)).to.equal(10);
expect(await nff2.balanceOf(owner, 1)).to.equal(10);
expect(await nff2.balanceOf(owner, 2)).to.equal(2);
expect(await nff2.balanceOf(addr1, 1)).to.equal(0);
expect(await nff2.balanceOf(addr1, 2)).to.equal(8);

console.log("Final balance of ERC20 F1 for User1: " + web3.utils.fromWei((await f1.balanceOf(owner)).toString(), 'ether'));
console.log("Final balance of ERC20 F1 for User2: " + web3.utils.fromWei((await f1.balanceOf(addr1)).toString(), 'ether'));
console.log("Final balance of ERC20 F2 for User1: " + web3.utils.fromWei((await f2.balanceOf(owner)).toString(), 'ether'));
console.log("Final balance of ERC20 F2 for User2: " + web3.utils.fromWei((await f2.balanceOf(addr1)).toString(), 'ether'));

console.log("Final owner of ERC721 NFT1 ID1: " + (await nf1.ownerOf(1)));
console.log("Final owner of ERC721 NFT2 ID2: " + (await nf2.ownerOf(2)));

console.log("Final balance of ERC1155 NFT1 ID1 for User1: " + (await nff1.balanceOf(owner, 1)));
console.log("Final balance of ERC1155 NFT1 ID2 for User1: " + (await nff1.balanceOf(owner, 2)));
console.log("Final balance of ERC1155 NFT1 ID1 for User2: " + (await nff1.balanceOf(addr1, 1)));
console.log("Final balance of ERC1155 NFT1 ID2 for User2: " + (await nff1.balanceOf(addr1, 2)));
console.log("Final balance of ERC1155 NFT2 ID1 for User1: " + (await nff2.balanceOf(owner, 1)));
console.log("Final balance of ERC1155 NFT2 ID2 for User1: " + (await nff2.balanceOf(owner, 2)));
console.log("Final balance of ERC1155 NFT2 ID1 for User2: " + (await nff2.balanceOf(addr1, 1)));
console.log("Final balance of ERC1155 NFT2 ID2 for User2: " + (await nff2.balanceOf(addr1, 2)));
})
})

0 comments on commit 0c89dd4

Please sign in to comment.