fix nft bucket tests

This commit is contained in:
Andrea Franz 2020-09-30 12:11:56 +02:00
parent 55c3a93008
commit 573b63adc9
No known key found for this signature in database
GPG Key ID: 4F0D2F2D9DE7F29D
3 changed files with 115 additions and 93 deletions

View File

@ -2,6 +2,7 @@ const Migrations = artifacts.require("Migrations");
const NFTBucketFactory = artifacts.require("NFTBucketFactory"); const NFTBucketFactory = artifacts.require("NFTBucketFactory");
const ERC20BucketFactory = artifacts.require("ERC20BucketFactory"); const ERC20BucketFactory = artifacts.require("ERC20BucketFactory");
const TestToken = artifacts.require("TestToken"); const TestToken = artifacts.require("TestToken");
const TestNFT = artifacts.require("TestNFT");
module.exports = function(deployer, network) { module.exports = function(deployer, network) {
deployer.deploy(Migrations); deployer.deploy(Migrations);
@ -10,5 +11,6 @@ module.exports = function(deployer, network) {
if (network === "development") { if (network === "development") {
deployer.deploy(TestToken, "Dev Test Token", "DTT", 18); deployer.deploy(TestToken, "Dev Test Token", "DTT", 18);
deployer.deploy(TestNFT);
} }
}; };

View File

@ -79,10 +79,10 @@ function mineAt(timestamp) {
}); });
} }
contract("ERC20 buckets", function () { contract("ERC20Bucket", function () {
let bucketInstance, let bucketInstance,
factoryInstance, factoryInstance,
testTokenInstance, tokenInstance,
shop, shop,
user, user,
relayer, relayer,
@ -98,7 +98,7 @@ contract("ERC20 buckets", function () {
keycard_2 = accounts[4]; keycard_2 = accounts[4];
const deployedTestToken = await TestToken.deployed(); const deployedTestToken = await TestToken.deployed();
testTokenInstance = new web3.eth.Contract(TestToken.abi, deployedTestToken.address); tokenInstance = new web3.eth.Contract(TestToken.abi, deployedTestToken.address);
}); });
it("deploy factory", async () => { it("deploy factory", async () => {
@ -117,7 +117,7 @@ contract("ERC20 buckets", function () {
const instance = new web3.eth.Contract(ERC20Bucket.abi); const instance = new web3.eth.Contract(ERC20Bucket.abi);
const deploy = instance.deploy({ const deploy = instance.deploy({
data: ERC20Bucket.bytecode, data: ERC20Bucket.bytecode,
arguments: [testTokenInstance.options.address, START_TIME, EXPIRATION_TIME, MAX_TX_DELAY_BLOCKS] arguments: [tokenInstance.options.address, START_TIME, EXPIRATION_TIME, MAX_TX_DELAY_BLOCKS]
}); });
const gas = await deploy.estimateGas(); const gas = await deploy.estimateGas();
const rec = await deploy.send({ const rec = await deploy.send({
@ -129,7 +129,7 @@ contract("ERC20 buckets", function () {
}); });
it("deploy bucket via factory", async () => { it("deploy bucket via factory", async () => {
const create = factoryInstance.methods.create(testTokenInstance._address, START_TIME, EXPIRATION_TIME, MAX_TX_DELAY_BLOCKS); const create = factoryInstance.methods.create(tokenInstance._address, START_TIME, EXPIRATION_TIME, MAX_TX_DELAY_BLOCKS);
const gas = await create.estimateGas(); const gas = await create.estimateGas();
const receipt = await create.send({ const receipt = await create.send({
from: shop, from: shop,
@ -143,38 +143,38 @@ contract("ERC20 buckets", function () {
}); });
it("shop buys 100 tokens", async function () { it("shop buys 100 tokens", async function () {
let supply = await testTokenInstance.methods.totalSupply().call(); let supply = await tokenInstance.methods.totalSupply().call();
assert.equal(parseInt(supply), 0); assert.equal(parseInt(supply), 0);
await testTokenInstance.methods.mint(TOTAL_SUPPLY).send({ await tokenInstance.methods.mint(TOTAL_SUPPLY).send({
from: shop, from: shop,
}); });
supply = await testTokenInstance.methods.totalSupply().call(); supply = await tokenInstance.methods.totalSupply().call();
assert.equal(parseInt(supply), TOTAL_SUPPLY); assert.equal(parseInt(supply), TOTAL_SUPPLY);
let shopBalance = await testTokenInstance.methods.balanceOf(shop).call(); let shopBalance = await tokenInstance.methods.balanceOf(shop).call();
assert.equal(parseInt(shopBalance), TOTAL_SUPPLY); assert.equal(parseInt(shopBalance), TOTAL_SUPPLY);
}); });
it("add supply", async function() { it("add supply", async function() {
let bucketBalance = await testTokenInstance.methods.balanceOf(bucketInstance.options.address).call(); let bucketBalance = await tokenInstance.methods.balanceOf(bucketInstance.options.address).call();
assert.equal(parseInt(bucketBalance), 0, `bucket balance before is ${bucketBalance} instead of 0`); assert.equal(parseInt(bucketBalance), 0, `bucket balance before is ${bucketBalance} instead of 0`);
let shopBalance = await testTokenInstance.methods.balanceOf(shop).call(); let shopBalance = await tokenInstance.methods.balanceOf(shop).call();
assert.equal(parseInt(shopBalance), TOTAL_SUPPLY, `shop balance before is ${shopBalance} instead of ${TOTAL_SUPPLY}`); assert.equal(parseInt(shopBalance), TOTAL_SUPPLY, `shop balance before is ${shopBalance} instead of ${TOTAL_SUPPLY}`);
const transfer = testTokenInstance.methods.transfer(bucketInstance.options.address, TOTAL_SUPPLY); const transfer = tokenInstance.methods.transfer(bucketInstance.options.address, TOTAL_SUPPLY);
const transferGas = await transfer.estimateGas(); const transferGas = await transfer.estimateGas();
await transfer.send({ await transfer.send({
from: shop, from: shop,
gas: transferGas, gas: transferGas,
}); });
bucketBalance = await testTokenInstance.methods.balanceOf(bucketInstance.options.address).call(); bucketBalance = await tokenInstance.methods.balanceOf(bucketInstance.options.address).call();
assert.equal(parseInt(bucketBalance), TOTAL_SUPPLY, `bucket balance after is ${bucketBalance} instead of ${TOTAL_SUPPLY}`); assert.equal(parseInt(bucketBalance), TOTAL_SUPPLY, `bucket balance after is ${bucketBalance} instead of ${TOTAL_SUPPLY}`);
shopBalance = await testTokenInstance.methods.balanceOf(shop).call(); shopBalance = await tokenInstance.methods.balanceOf(shop).call();
assert.equal(parseInt(shopBalance), 0, `shop balance after is ${shopBalance} instead of 0`); assert.equal(parseInt(shopBalance), 0, `shop balance after is ${shopBalance} instead of 0`);
let totalSupply = await bucketInstance.methods.totalSupply().call(); let totalSupply = await bucketInstance.methods.totalSupply().call();
@ -245,8 +245,8 @@ contract("ERC20 buckets", function () {
}); });
async function testRedeem(receiver, recipient, signer, relayer, redeemCode, blockNumber, blockHash) { async function testRedeem(receiver, recipient, signer, relayer, redeemCode, blockNumber, blockHash) {
let initialBucketBalance = await testTokenInstance.methods.balanceOf(bucketInstance.options.address).call(); let initialBucketBalance = await tokenInstance.methods.balanceOf(bucketInstance.options.address).call();
let initialUserBalance = await testTokenInstance.methods.balanceOf(user).call(); let initialUserBalance = await tokenInstance.methods.balanceOf(user).call();
let initialRedeemableSupply = await bucketInstance.methods.redeemableSupply().call(); let initialRedeemableSupply = await bucketInstance.methods.redeemableSupply().call();
let redeemable = await bucketInstance.methods.redeemables(recipient).call(); let redeemable = await bucketInstance.methods.redeemables(recipient).call();
@ -271,11 +271,11 @@ contract("ERC20 buckets", function () {
assert.equal(receipt.events.Redeemed.returnValues.data, redeemable.data); assert.equal(receipt.events.Redeemed.returnValues.data, redeemable.data);
let expectedBucketBalance = parseInt(initialBucketBalance) - amount; let expectedBucketBalance = parseInt(initialBucketBalance) - amount;
let bucketBalance = await testTokenInstance.methods.balanceOf(bucketInstance.options.address).call(); let bucketBalance = await tokenInstance.methods.balanceOf(bucketInstance.options.address).call();
assert.equal(parseInt(bucketBalance), expectedBucketBalance, `bucketBalance after redeem should be ${expectedBucketBalance} instead of ${bucketBalance}`); assert.equal(parseInt(bucketBalance), expectedBucketBalance, `bucketBalance after redeem should be ${expectedBucketBalance} instead of ${bucketBalance}`);
let expectedUserBalance = parseInt(initialUserBalance + amount); let expectedUserBalance = parseInt(initialUserBalance + amount);
userBalance = await testTokenInstance.methods.balanceOf(user).call(); userBalance = await tokenInstance.methods.balanceOf(user).call();
assert.equal(parseInt(userBalance), expectedUserBalance, `user`, `userBalance after redeem should be ${expectedUserBalance} instead of ${userBalance}`); assert.equal(parseInt(userBalance), expectedUserBalance, `user`, `userBalance after redeem should be ${expectedUserBalance} instead of ${userBalance}`);
let expectedRedeemableSupply = initialRedeemableSupply - amount; let expectedRedeemableSupply = initialRedeemableSupply - amount;
@ -369,18 +369,18 @@ contract("ERC20 buckets", function () {
}); });
async function testKill() { async function testKill() {
let initialShopBalance = parseInt(await testTokenInstance.methods.balanceOf(shop).call()); let initialShopBalance = parseInt(await tokenInstance.methods.balanceOf(shop).call());
let initialBucketBalance = parseInt(await testTokenInstance.methods.balanceOf(bucketInstance.options.address).call()); let initialBucketBalance = parseInt(await tokenInstance.methods.balanceOf(bucketInstance.options.address).call());
await bucketInstance.methods.kill().send({ await bucketInstance.methods.kill().send({
from: shop, from: shop,
}); });
let expectedShopBalance = initialShopBalance + initialBucketBalance; let expectedShopBalance = initialShopBalance + initialBucketBalance;
let shopBalance = await testTokenInstance.methods.balanceOf(shop).call(); let shopBalance = await tokenInstance.methods.balanceOf(shop).call();
assert.equal(parseInt(shopBalance), expectedShopBalance, `shop balance after kill is ${shopBalance} instead of ${expectedShopBalance}`); assert.equal(parseInt(shopBalance), expectedShopBalance, `shop balance after kill is ${shopBalance} instead of ${expectedShopBalance}`);
let bucketBalance = await testTokenInstance.methods.balanceOf(bucketInstance.options.address).call(); let bucketBalance = await tokenInstance.methods.balanceOf(bucketInstance.options.address).call();
assert.equal(parseInt(bucketBalance), 0, `bucketBalance after kill is ${bucketBalance} instead of 0`); assert.equal(parseInt(bucketBalance), 0, `bucketBalance after kill is ${bucketBalance} instead of 0`);
} }

View File

@ -1,5 +1,5 @@
const TestNFT = artifacts.require('TestNFT'); const TestNFT = artifacts.require('TestNFT');
const _NFTBucket = artifacts.require('NFTBucket'); const NFTBucket = artifacts.require('NFTBucket');
const NFTBucketFactory = artifacts.require('NFTBucketFactory'); const NFTBucketFactory = artifacts.require('NFTBucketFactory');
const TOTAL_SUPPLY = 10000; const TOTAL_SUPPLY = 10000;
@ -10,37 +10,6 @@ const START_TIME = NOW - 1;
const EXPIRATION_TIME = NOW + 60 * 60 * 24; // in 24 hours const EXPIRATION_TIME = NOW + 60 * 60 * 24; // in 24 hours
const MAX_TX_DELAY_BLOCKS = 10; const MAX_TX_DELAY_BLOCKS = 10;
let shop,
user,
relayer,
keycard_1,
keycard_2;
config({
contracts: {
deploy: {
"TestNFT": {
args: [],
},
"NFTBucket": {
args: ["$TestNFT", START_TIME, EXPIRATION_TIME, MAX_TX_DELAY_BLOCKS],
},
"NFTBucketFactory": {
args: [],
},
}
},
}, (_err, _accounts) => {
shop = _accounts[0];
user = _accounts[1];
relayer = _accounts[2];
keycard_1 = _accounts[3];
keycard_2 = _accounts[4];
keycard_3 = _accounts[5];
});
let sendMethod;
async function signRedeem(contractAddress, signer, message) { async function signRedeem(contractAddress, signer, message) {
const result = await web3.eth.net.getId(); const result = await web3.eth.net.getId();
let chainId = parseInt(result); let chainId = parseInt(result);
@ -79,7 +48,7 @@ async function signRedeem(contractAddress, signer, message) {
}; };
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
sendMethod({ web3.currentProvider.send({
jsonrpc: '2.0', jsonrpc: '2.0',
id: Date.now().toString().substring(9), id: Date.now().toString().substring(9),
method: "eth_signTypedData", method: "eth_signTypedData",
@ -96,7 +65,7 @@ async function signRedeem(contractAddress, signer, message) {
function mineAt(timestamp) { function mineAt(timestamp) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
sendMethod({ web3.currentProvider.send({
jsonrpc: '2.0', jsonrpc: '2.0',
method: "evm_mine", method: "evm_mine",
params: [timestamp], params: [timestamp],
@ -117,48 +86,67 @@ if (assert.match === undefined) {
} }
contract("NFTBucket", function () { contract("NFTBucket", function () {
let NFTBucket; let bucketInstance,
factoryInstance,
tokenInstance,
shop,
user,
relayer,
keycard_1,
keycard_2,
keycard_3;
sendMethod = (web3.currentProvider.sendAsync) ? web3.currentProvider.sendAsync.bind(web3.currentProvider) : web3.currentProvider.send.bind(web3.currentProvider); before(async () => {
const accounts = await web3.eth.getAccounts();
shop = accounts[0];
user = accounts[1];
relayer = accounts[2];
keycard_1 = accounts[3];
keycard_2 = accounts[4];
keycard_3 = accounts[5];
it("deploy factory", async () => { const deployedTestToken = await TestNFT.deployed();
// only to test gas tokenInstance = new web3.eth.Contract(TestNFT.abi, deployedTestToken.address);
const deploy = NFTBucketFactory.deploy({
arguments: []
}); });
it("deploy factory", async () => {
const contract = new web3.eth.Contract(NFTBucketFactory.abi);
const deploy = contract.deploy({ data: NFTBucketFactory.bytecode });
const gas = await deploy.estimateGas(); const gas = await deploy.estimateGas();
await deploy.send({ gas }) const rec = await deploy.send({
from: shop,
gas,
});
factoryInstance = new web3.eth.Contract(NFTBucketFactory.abi, rec.options.address);
}); });
it("deploy bucket", async () => { it("deploy bucket", async () => {
// only to test gas const instance = new web3.eth.Contract(NFTBucket.abi);
const deploy = _NFTBucket.deploy({ const deploy = instance.deploy({
arguments: [TestNFT._address, START_TIME, EXPIRATION_TIME, MAX_TX_DELAY_BLOCKS] data: NFTBucket.bytecode,
arguments: [tokenInstance.options.address, START_TIME, EXPIRATION_TIME, MAX_TX_DELAY_BLOCKS]
});
const gas = await deploy.estimateGas();
const rec = await deploy.send({
from: shop,
gas,
}); });
const gas = await deploy.estimateGas(); bucketInstance = new web3.eth.Contract(NFTBucket.abi, rec.options.address);
await deploy.send({ gas })
}); });
it("deploy bucket via factory", async () => { it("deploy bucket via factory", async () => {
const create = NFTBucketFactory.methods.create(TestNFT._address, START_TIME, EXPIRATION_TIME, MAX_TX_DELAY_BLOCKS); const create = factoryInstance.methods.create(tokenInstance._address, START_TIME, EXPIRATION_TIME, MAX_TX_DELAY_BLOCKS);
const gas = await create.estimateGas(); const gas = await create.estimateGas();
const receipt = await create.send({ const receipt = await create.send({
from: shop, from: shop,
gas: gas, gas: gas,
}); });
const bucketAddress = receipt.events.BucketCreated.returnValues.bucket;
const jsonInterface = _NFTBucket.options.jsonInterface;
NFTBucket = new EmbarkJS.Blockchain.Contract({
abi: jsonInterface,
address: bucketAddress,
});
}); });
it("return correct bucket type", async function () { it("return correct bucket type", async function () {
let bucketType = await NFTBucket.methods.bucketType().call(); let bucketType = await bucketInstance.methods.bucketType().call();
assert.equal(parseInt(bucketType), 721); assert.equal(parseInt(bucketType), 721);
}); });
@ -169,33 +157,55 @@ contract("NFTBucket", function () {
} }
async function checkRedeemable(recipient, tokenID) { async function checkRedeemable(recipient, tokenID) {
let redeemable = await NFTBucket.methods.redeemables(recipient).call(); let redeemable = await bucketInstance.methods.redeemables(recipient).call();
assert.equal(redeemable.recipient, recipient, "redeemable not found"); assert.equal(redeemable.recipient, recipient, "redeemable not found");
assert.equal(parseInt(redeemable.data), tokenID, "token ID does not match"); assert.equal(parseInt(redeemable.data), tokenID, "token ID does not match");
let tokenOwner = await TestNFT.methods.ownerOf(tokenID).call(); let tokenOwner = await tokenInstance.methods.ownerOf(tokenID).call();
assert.equal(tokenOwner, NFTBucket._address, "token owner is wrong"); assert.equal(tokenOwner, bucketInstance.options.address, "token owner is wrong");
} }
it("mint directly to redeemable", async function () { it("mint directly to redeemable", async function () {
await TestNFT.methods.mint(NFTBucket._address, 42, createRedeemableData(keycard_1)).send({ const mint = tokenInstance.methods.mint(bucketInstance.options.address, 42, createRedeemableData(keycard_1));
const gas = await mint.estimateGas();
await mint.send({
from: shop, from: shop,
gas,
}); });
await checkRedeemable(keycard_1, 42); await checkRedeemable(keycard_1, 42);
}); });
it("transfer token from shop", async function() { it("transfer token from shop", async function() {
await TestNFT.methods.mint(shop, 0xcafe).send({from: shop,}); const mint = tokenInstance.methods.mint(shop, 0xcafe)
await TestNFT.methods.safeTransferFrom(shop, NFTBucket._address, 0xcafe, createRedeemableData(keycard_2)).send({from: shop}); let gas = await mint.estimateGas();
await mint.send({
from: shop,
gas,
});
const transfer = tokenInstance.methods.safeTransferFrom(shop, bucketInstance.options.address, 0xcafe, createRedeemableData(keycard_2))
gas = await transfer.estimateGas();
await transfer.send({
from: shop,
gas,
});
await checkRedeemable(keycard_2, 0xcafe); await checkRedeemable(keycard_2, 0xcafe);
}); });
it("cannot create two redeemables for the same recipient", async function() { it("cannot create two redeemables for the same recipient", async function() {
await TestNFT.methods.mint(shop, 43).send({from: shop}); const mint = await tokenInstance.methods.mint(shop, 43)
const gas = await mint.estimateGas();
await mint.send({
from: shop,
gas,
});
try { try {
await TestNFT.methods.safeTransferFrom(shop, NFTBucket._address, 43, createRedeemableData(keycard_2)).send({from: shop}); const transfer = tokenInstance.methods.safeTransferFrom(shop, bucketInstance.options.address, 43, createRedeemableData(keycard_2))
await transfer.send({
from: shop,
gas,
});
assert.fail("transfer should have failed"); assert.fail("transfer should have failed");
} catch(e) { } catch(e) {
assert.match(e.message, /already used/); assert.match(e.message, /already used/);
@ -205,7 +215,12 @@ contract("NFTBucket", function () {
it("cannot create two redeemables for the same token", async function() { it("cannot create two redeemables for the same token", async function() {
try { try {
await NFTBucket.methods.onERC721Received(shop, shop, 0xcafe, createRedeemableData(keycard_3)).send({from: shop}); const received = bucketInstance.methods.onERC721Received(shop, shop, 0xcafe, createRedeemableData(keycard_3))
const gas = await received.estimateGas();
await send({
from: shop,
gas,
});
assert.fail("transfer should have failed"); assert.fail("transfer should have failed");
} catch(e) { } catch(e) {
assert.match(e.message, /only the NFT/); assert.match(e.message, /only the NFT/);
@ -214,7 +229,7 @@ contract("NFTBucket", function () {
}); });
async function testRedeem(receiver, recipient, signer, relayer, redeemCode, blockNumber, blockHash) { async function testRedeem(receiver, recipient, signer, relayer, redeemCode, blockNumber, blockHash) {
let redeemable = await NFTBucket.methods.redeemables(recipient).call(); let redeemable = await bucketInstance.methods.redeemables(recipient).call();
const tokenID = redeemable.data; const tokenID = redeemable.data;
const message = { const message = {
@ -224,8 +239,8 @@ contract("NFTBucket", function () {
code: redeemCode, code: redeemCode,
}; };
const sig = await signRedeem(NFTBucket._address, signer, message); const sig = await signRedeem(bucketInstance.options.address, signer, message);
const redeem = NFTBucket.methods.redeem(message, sig); const redeem = bucketInstance.methods.redeem(message, sig);
const redeemGas = await redeem.estimateGas(); const redeemGas = await redeem.estimateGas();
let receipt = await redeem.send({ let receipt = await redeem.send({
from: relayer, from: relayer,
@ -235,7 +250,7 @@ contract("NFTBucket", function () {
assert.equal(receipt.events.Redeemed.returnValues.recipient, recipient); assert.equal(receipt.events.Redeemed.returnValues.recipient, recipient);
assert.equal(receipt.events.Redeemed.returnValues.data, tokenID); assert.equal(receipt.events.Redeemed.returnValues.data, tokenID);
let tokenOwner = await TestNFT.methods.ownerOf(tokenID).call(); let tokenOwner = await tokenInstance.methods.ownerOf(tokenID).call();
assert.equal(tokenOwner, receiver, `Token owner is ${tokenOwner} instead of the expected ${receiver}`); assert.equal(tokenOwner, receiver, `Token owner is ${tokenOwner} instead of the expected ${receiver}`);
} }
@ -325,9 +340,14 @@ contract("NFTBucket", function () {
}); });
async function testKill() { async function testKill() {
assert(!await TestNFT.methods.isApprovedForAll(NFTBucket._address, shop).call(), `${shop} should not be the operator of bucket's tokens`); assert(!await tokenInstance.methods.isApprovedForAll(bucketInstance.options.address, shop).call(), `${shop} should not be the operator of bucket's tokens`);
await NFTBucket.methods.kill().send({from: shop}); const kill = bucketInstance.methods.kill();
assert(await TestNFT.methods.isApprovedForAll(NFTBucket._address, shop).call(), `${shop} should become the operator of the destroyed bucket's tokens`); const gas = await kill.estimateGas();
await kill.send({
from: shop,
gas,
});
assert(await tokenInstance.methods.isApprovedForAll(bucketInstance.options.address, shop).call(), `${shop} should become the operator of the destroyed bucket's tokens`);
} }
it("shop cannot kill contract before expirationTime", async function() { it("shop cannot kill contract before expirationTime", async function() {