Skip to content

Commit

Permalink
adding plugins and 1st test
Browse files Browse the repository at this point in the history
  • Loading branch information
felipeasf committed Sep 13, 2023
1 parent ae66e6b commit 514da32
Show file tree
Hide file tree
Showing 6 changed files with 562 additions and 2 deletions.
2 changes: 2 additions & 0 deletions packages/marketplace/hardhat.config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import {HardhatUserConfig} from 'hardhat/config';

import "@nomiclabs/hardhat-truffle5";

const config: HardhatUserConfig = {
// solidity compiler version may be updated for new packages as required
// to ensure packages use up-to-date dependencies
Expand Down
8 changes: 6 additions & 2 deletions packages/marketplace/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,18 @@
"@sandbox-smart-contracts/dependency-metatx": "^0.0.2"
},
"devDependencies": {
"@daonomic/tests-common": "^0.2.2",
"@nomiclabs/hardhat-truffle5": "^2.0.7",
"@nomiclabs/hardhat-web3": "^2.0.0",
"@openzeppelin/contracts": "4.9.3",
"@openzeppelin/contracts-upgradeable": "4.9.3",
"hardhat": "^2.17.2",
"ts-node": "^10.9.1",
"typescript": "^4.0.5"
"typescript": "^4.0.5",
"web3": "^1.10.2"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"test": "hardhat test"
},
"author": "",
"license": "ISC"
Expand Down
216 changes: 216 additions & 0 deletions packages/marketplace/test/AssetMatcher.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,216 @@
const AssetMatcher = artifacts.require('AssetMatcher.sol');
const TestAssetMatcher = artifacts.require('TestAssetMatcher.sol');

const {expectThrow} = require('@daonomic/tests-common');

const order = require('./utils/order.js');
const {
enc,
ETH,
ERC20,
ERC721,
ERC1155,
BUNDLE,
id,
} = require('./utils/assets.js');

contract('AssetMatcher', (accounts) => {
let testing;

before(async function () {
testing = await AssetMatcher.new();
});

it('setAssetMatcher works', async function () {
const encoded = enc(accounts[5]);
await expectThrow(
testing.matchAssets(
order.AssetType(ERC20, encoded),
order.AssetType(id('BLA'), encoded)
)
);
const testMatcher = await TestAssetMatcher.new();
await testing.setAssetMatcher(id('BLA'), testMatcher.address);
const result = await testing.matchAssets(
order.AssetType(ERC20, encoded),
order.AssetType(id('BLA'), encoded)
);
assert.equal(result[0], ERC20);
assert.equal(result[1], encoded);
});

describe('ETH', function () {
it('should extract ETH type if both are ETHs', async function () {
const result = await testing.matchAssets(
order.AssetType(ETH, '0x'),
order.AssetType(ETH, '0x')
);
assert.equal(result[0], ETH);
});

it('should extract nothing if one is not ETH', async function () {
const result = await testing.matchAssets(
order.AssetType(ETH, '0x'),
order.AssetType(ERC20, '0x')
);
assert.equal(result[0], 0);
});
});

describe('ERC20', function () {
it('should extract ERC20 type if both are and addresses equal', async function () {
const encoded = enc(accounts[5]);
const result = await testing.matchAssets(
order.AssetType(ERC20, encoded),
order.AssetType(ERC20, encoded)
);
assert.equal(result[0], ERC20);
assert.equal(result[1], encoded);
});

it("should extract nothing if erc20 don't match", async function () {
const result = await testing.matchAssets(
order.AssetType(ERC20, enc(accounts[1])),
order.AssetType(ERC20, enc(accounts[2]))
);
assert.equal(result[0], 0);
});

it('should extract nothing if other type is not ERC20', async function () {
const result = await testing.matchAssets(
order.AssetType(ERC20, enc(accounts[1])),
order.AssetType(ETH, '0x')
);
assert.equal(result[0], 0);
});
});

describe('ERC721', function () {
it('should extract ERC721 type if both are equal', async function () {
const encoded = enc(accounts[5], 100);
const result = await testing.matchAssets(
order.AssetType(ERC721, encoded),
order.AssetType(ERC721, encoded)
);
assert.equal(result[0], ERC721);
assert.equal(result[1], encoded);
});

it("should extract nothing if tokenIds don't match", async function () {
const result = await testing.matchAssets(
order.AssetType(ERC721, enc(accounts[5], 100)),
order.AssetType(ERC721, enc(accounts[5], 101))
);
assert.equal(result[0], 0);
});

it("should extract nothing if addresses don't match", async function () {
const result = await testing.matchAssets(
order.AssetType(ERC721, enc(accounts[4], 100)),
order.AssetType(ERC721, enc(accounts[5], 100))
);
assert.equal(result[0], 0);
});

it('should extract nothing if other type is not ERC721', async function () {
const result = await testing.matchAssets(
order.AssetType(ERC721, enc(accounts[5], 100)),
order.AssetType(ETH, '0x')
);
assert.equal(result[0], 0);
});
});

describe('ERC1155', function () {
it('should extract ERC1155 type if both are equal', async function () {
const encoded = enc(accounts[5], 100);
const result = await testing.matchAssets(
order.AssetType(ERC1155, encoded),
order.AssetType(ERC1155, encoded)
);
assert.equal(result[0], ERC1155);
assert.equal(result[1], encoded);
});

it("should extract nothing if tokenIds don't match", async function () {
const result = await testing.matchAssets(
order.AssetType(ERC1155, enc(accounts[5], 100)),
order.AssetType(ERC1155, enc(accounts[5], 101))
);
assert.equal(result[0], 0);
});

it("should extract nothing if addresses don't match", async function () {
const result = await testing.matchAssets(
order.AssetType(ERC1155, enc(accounts[4], 100)),
order.AssetType(ERC1155, enc(accounts[5], 100))
);
assert.equal(result[0], 0);
});

it('should extract nothing if other type is not erc1155', async function () {
const encoded = enc(accounts[5], 100);
const result = await testing.matchAssets(
order.AssetType(ERC1155, encoded),
order.AssetType(ERC721, encoded)
);
assert.equal(result[0], 0);
});
});

describe('BUNDLE', function () {
it('should extract BUNDLE type if both are equal', async function () {
const encoded = enc(accounts[5], 100);
const result = await testing.matchAssets(
order.AssetType(BUNDLE, encoded),
order.AssetType(BUNDLE, encoded)
);
assert.equal(result[0], BUNDLE);
assert.equal(result[1], encoded);
});

it("should extract nothing if tokenIds don't match", async function () {
const result = await testing.matchAssets(
order.AssetType(BUNDLE, enc(accounts[5], 100)),
order.AssetType(BUNDLE, enc(accounts[5], 101))
);
assert.equal(result[0], 0);
});

it("should extract nothing if addresses don't match", async function () {
const result = await testing.matchAssets(
order.AssetType(BUNDLE, enc(accounts[4], 100)),
order.AssetType(BUNDLE, enc(accounts[5], 100))
);
assert.equal(result[0], 0);
});

it('should extract nothing if other type is not a BUNDLE', async function () {
const encoded = enc(accounts[5], 100);
const result = await testing.matchAssets(
order.AssetType(BUNDLE, encoded),
order.AssetType(ERC721, encoded)
);
assert.equal(result[0], 0);
});
});

describe('generic', function () {
it('should extract left type if asset types are equal', async function () {
const result = await testing.matchAssets(
order.AssetType('0x00112233', '0x1122'),
order.AssetType('0x00112233', '0x1122')
);
assert.equal(result[0], '0x00112233');
assert.equal(result[1], '0x1122');
});

it('should extract nothing single byte differs', async function () {
const result = await testing.matchAssets(
order.AssetType('0x00112233', '0x1122'),
order.AssetType('0x00112233', '0x1111')
);
assert.equal(result[0], 0);
});
});
});
78 changes: 78 additions & 0 deletions packages/marketplace/test/utils/EIP712.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// TODO: This is the same as the root folder scripts... fix it
const DOMAIN_TYPE = [
{
type: 'string',
name: 'name',
},
{
type: 'string',
name: 'version',
},
{
type: 'uint256',
name: 'chainId',
},
{
type: 'address',
name: 'verifyingContract',
},
];

module.exports = {
createTypeData: function (domainData, primaryType, message, types) {
return {
types: Object.assign(
{
EIP712Domain: DOMAIN_TYPE,
},
types
),
domain: domainData,
primaryType: primaryType,
message: message,
};
},

signTypedData: function (web3, from, data) {
return new Promise((resolve, reject) => {
function cb(err, result) {
if (result.error) {
return reject(result.error);
}
if (err) {
return reject(err);
}

const sig = result.result;
const sig0 = sig.substring(2);
const r = '0x' + sig0.substring(0, 64);
const s = '0x' + sig0.substring(64, 128);
const v = parseInt(sig0.substring(128, 130), 16);

resolve({
data,
sig,
v,
r,
s,
});
}

let send = web3.currentProvider.sendAsync;
if (!send) send = web3.currentProvider.send;
send.bind(web3.currentProvider)(
{
jsonrpc: '2.0',
method: web3.currentProvider.isMetaMask
? 'eth_signTypedData_v3'
: 'eth_signTypedData',
params: web3.currentProvider.isMetaMask
? [from, JSON.stringify(data)]
: [from, data],
id: new Date().getTime(),
},
cb
);
});
},
};
Loading

0 comments on commit 514da32

Please sign in to comment.