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 ERC20BucketFactory = artifacts.require("ERC20BucketFactory");
const TestToken = artifacts.require("TestToken");
const TestNFT = artifacts.require("TestNFT");
module.exports = function(deployer, network) {
deployer.deploy(Migrations);
@ -10,5 +11,6 @@ module.exports = function(deployer, network) {
if (network === "development") {
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,
factoryInstance,
testTokenInstance,
tokenInstance,
shop,
user,
relayer,
@ -98,7 +98,7 @@ contract("ERC20 buckets", function () {
keycard_2 = accounts[4];
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 () => {
@ -117,7 +117,7 @@ contract("ERC20 buckets", function () {
const instance = new web3.eth.Contract(ERC20Bucket.abi);
const deploy = instance.deploy({
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 rec = await deploy.send({
@ -129,7 +129,7 @@ contract("ERC20 buckets", function () {
});
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 receipt = await create.send({
from: shop,
@ -143,38 +143,38 @@ contract("ERC20 buckets", 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);
await testTokenInstance.methods.mint(TOTAL_SUPPLY).send({
await tokenInstance.methods.mint(TOTAL_SUPPLY).send({
from: shop,
});
supply = await testTokenInstance.methods.totalSupply().call();
supply = await tokenInstance.methods.totalSupply().call();
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);
});
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`);
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}`);
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();
await transfer.send({
from: shop,
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}`);
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`);
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) {
let initialBucketBalance = await testTokenInstance.methods.balanceOf(bucketInstance.options.address).call();
let initialUserBalance = await testTokenInstance.methods.balanceOf(user).call();
let initialBucketBalance = await tokenInstance.methods.balanceOf(bucketInstance.options.address).call();
let initialUserBalance = await tokenInstance.methods.balanceOf(user).call();
let initialRedeemableSupply = await bucketInstance.methods.redeemableSupply().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);
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}`);
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}`);
let expectedRedeemableSupply = initialRedeemableSupply - amount;
@ -369,18 +369,18 @@ contract("ERC20 buckets", function () {
});
async function testKill() {
let initialShopBalance = parseInt(await testTokenInstance.methods.balanceOf(shop).call());
let initialBucketBalance = parseInt(await testTokenInstance.methods.balanceOf(bucketInstance.options.address).call());
let initialShopBalance = parseInt(await tokenInstance.methods.balanceOf(shop).call());
let initialBucketBalance = parseInt(await tokenInstance.methods.balanceOf(bucketInstance.options.address).call());
await bucketInstance.methods.kill().send({
from: shop,
});
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}`);
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`);
}

View File

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