2020-04-10 10:14:50 +00:00
|
|
|
const EmbarkJS = artifacts.require('EmbarkJS');
|
|
|
|
const TestNFT = artifacts.require('TestNFT');
|
|
|
|
const _NFTBucket = artifacts.require('NFTBucket');
|
|
|
|
const NFTBucketFactory = artifacts.require('NFTBucketFactory');
|
|
|
|
|
|
|
|
const TOTAL_SUPPLY = 10000;
|
|
|
|
const GIFT_AMOUNT = 10;
|
|
|
|
const REDEEM_CODE = web3.utils.sha3("hello world");
|
|
|
|
const NOW = Math.round(new Date().getTime() / 1000);
|
2020-04-23 12:51:05 +00:00
|
|
|
const START_TIME = NOW - 1;
|
2020-04-10 10:14:50 +00:00
|
|
|
const EXPIRATION_TIME = NOW + 60 * 60 * 24; // in 24 hours
|
|
|
|
|
|
|
|
let shop,
|
|
|
|
user,
|
|
|
|
relayer,
|
|
|
|
keycard_1,
|
|
|
|
keycard_2;
|
|
|
|
|
|
|
|
config({
|
|
|
|
contracts: {
|
|
|
|
deploy: {
|
|
|
|
"TestNFT": {
|
|
|
|
args: [],
|
|
|
|
},
|
|
|
|
"NFTBucket": {
|
2020-04-23 12:51:05 +00:00
|
|
|
args: ["$TestNFT", START_TIME, EXPIRATION_TIME],
|
2020-04-10 10:14:50 +00:00
|
|
|
},
|
|
|
|
"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);
|
|
|
|
//FIXME: in tests, getChainID in the contract returns 1 so we hardcode it here to 1.
|
|
|
|
chainId = 1;
|
|
|
|
|
|
|
|
const domain = [
|
|
|
|
{ name: "name", type: "string" },
|
|
|
|
{ name: "version", type: "string" },
|
|
|
|
{ name: "chainId", type: "uint256" },
|
|
|
|
{ name: "verifyingContract", type: "address" }
|
|
|
|
];
|
|
|
|
|
|
|
|
const redeem = [
|
|
|
|
{ name: "blockNumber", type: "uint256" },
|
|
|
|
{ name: "blockHash", type: "bytes32" },
|
|
|
|
{ name: "receiver", type: "address" },
|
|
|
|
{ name: "code", type: "bytes32" },
|
|
|
|
];
|
|
|
|
|
|
|
|
const domainData = {
|
2020-04-28 09:36:34 +00:00
|
|
|
name: "KeycardNFTBucket",
|
2020-04-10 10:14:50 +00:00
|
|
|
version: "1",
|
|
|
|
chainId: chainId,
|
|
|
|
verifyingContract: contractAddress
|
|
|
|
};
|
|
|
|
|
|
|
|
const data = {
|
|
|
|
types: {
|
|
|
|
EIP712Domain: domain,
|
|
|
|
Redeem: redeem,
|
|
|
|
},
|
|
|
|
primaryType: "Redeem",
|
|
|
|
domain: domainData,
|
|
|
|
message: message
|
|
|
|
};
|
|
|
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
sendMethod({
|
|
|
|
jsonrpc: '2.0',
|
|
|
|
id: Date.now().toString().substring(9),
|
|
|
|
method: "eth_signTypedData",
|
|
|
|
params: [signer, data],
|
|
|
|
from: signer
|
|
|
|
}, (error, res) => {
|
|
|
|
if (error) {
|
|
|
|
return reject(error);
|
|
|
|
}
|
|
|
|
resolve(res.result);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function mineAt(timestamp) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
sendMethod({
|
|
|
|
jsonrpc: '2.0',
|
|
|
|
method: "evm_mine",
|
|
|
|
params: [timestamp],
|
|
|
|
id: Date.now().toString().substring(9)
|
|
|
|
}, (error, res) => {
|
|
|
|
if (error) {
|
|
|
|
return reject(error);
|
|
|
|
}
|
|
|
|
resolve(res.result);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (assert.match === undefined) {
|
|
|
|
assert.match = (message, pattern) => {
|
|
|
|
assert(pattern.test(message), `${message} doesn't match ${pattern}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
contract("NFTBucket", function () {
|
|
|
|
let NFTBucket;
|
|
|
|
|
|
|
|
sendMethod = (web3.currentProvider.sendAsync) ? web3.currentProvider.sendAsync.bind(web3.currentProvider) : web3.currentProvider.send.bind(web3.currentProvider);
|
|
|
|
|
|
|
|
it("deploy factory", async () => {
|
|
|
|
// only to test gas
|
|
|
|
const deploy = NFTBucketFactory.deploy({
|
|
|
|
arguments: []
|
|
|
|
});
|
|
|
|
|
|
|
|
const gas = await deploy.estimateGas();
|
|
|
|
await deploy.send({ gas })
|
|
|
|
});
|
|
|
|
|
|
|
|
it("deploy bucket", async () => {
|
|
|
|
// only to test gas
|
|
|
|
const deploy = _NFTBucket.deploy({
|
2020-04-23 12:51:05 +00:00
|
|
|
arguments: [TestNFT._address, START_TIME, EXPIRATION_TIME]
|
2020-04-10 10:14:50 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
const gas = await deploy.estimateGas();
|
|
|
|
await deploy.send({ gas })
|
|
|
|
});
|
|
|
|
|
|
|
|
it("deploy bucket via factory", async () => {
|
2020-04-23 12:51:05 +00:00
|
|
|
const create = NFTBucketFactory.methods.create(TestNFT._address, START_TIME, EXPIRATION_TIME);
|
2020-04-10 10:14:50 +00:00
|
|
|
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,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-04-28 09:36:34 +00:00
|
|
|
function createRedeemableData(recipient) {
|
2020-04-10 10:14:50 +00:00
|
|
|
const redeemCodeHash = web3.utils.sha3(REDEEM_CODE);
|
|
|
|
return recipient + redeemCodeHash.replace("0x", "");
|
|
|
|
}
|
|
|
|
|
2020-04-28 09:36:34 +00:00
|
|
|
async function checkRedeemable(recipient, tokenID) {
|
|
|
|
let redeemable = await NFTBucket.methods.redeemables(recipient).call();
|
|
|
|
assert.equal(redeemable.recipient, recipient, "redeemable not found");
|
|
|
|
assert.equal(parseInt(redeemable.data), tokenID, "token ID does not match");
|
2020-04-10 10:14:50 +00:00
|
|
|
let tokenOwner = await TestNFT.methods.ownerOf(tokenID).call();
|
|
|
|
assert.equal(tokenOwner, NFTBucket._address, "token owner is wrong");
|
|
|
|
}
|
|
|
|
|
2020-04-28 09:36:34 +00:00
|
|
|
it("mint directly to redeemable", async function () {
|
|
|
|
await TestNFT.methods.mint(NFTBucket._address, 42, createRedeemableData(keycard_1)).send({
|
2020-04-10 10:14:50 +00:00
|
|
|
from: shop,
|
|
|
|
});
|
|
|
|
|
2020-04-28 09:36:34 +00:00
|
|
|
await checkRedeemable(keycard_1, 42);
|
2020-04-10 10:14:50 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("transfer token from shop", async function() {
|
|
|
|
await TestNFT.methods.mint(shop, 0xcafe).send({from: shop,});
|
2020-04-28 09:36:34 +00:00
|
|
|
await TestNFT.methods.safeTransferFrom(shop, NFTBucket._address, 0xcafe, createRedeemableData(keycard_2)).send({from: shop});
|
2020-04-10 10:14:50 +00:00
|
|
|
|
2020-04-28 09:36:34 +00:00
|
|
|
await checkRedeemable(keycard_2, 0xcafe);
|
2020-04-10 10:14:50 +00:00
|
|
|
});
|
|
|
|
|
2020-04-28 09:36:34 +00:00
|
|
|
it("cannot create two redeemables for the same recipient", async function() {
|
2020-04-10 10:14:50 +00:00
|
|
|
await TestNFT.methods.mint(shop, 43).send({from: shop});
|
|
|
|
|
|
|
|
try {
|
2020-04-28 09:36:34 +00:00
|
|
|
await TestNFT.methods.safeTransferFrom(shop, NFTBucket._address, 43, createRedeemableData(keycard_2)).send({from: shop});
|
2020-04-10 10:14:50 +00:00
|
|
|
assert.fail("transfer should have failed");
|
|
|
|
} catch(e) {
|
|
|
|
assert.match(e.message, /already used/);
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2020-04-28 09:36:34 +00:00
|
|
|
it("cannot create two redeemables for the same token", async function() {
|
2020-04-13 08:46:09 +00:00
|
|
|
try {
|
2020-04-28 09:36:34 +00:00
|
|
|
await NFTBucket.methods.onERC721Received(shop, shop, 0xcafe, createRedeemableData(keycard_3)).send({from: shop});
|
2020-04-13 08:46:09 +00:00
|
|
|
assert.fail("transfer should have failed");
|
|
|
|
} catch(e) {
|
|
|
|
assert.match(e.message, /only the NFT/);
|
|
|
|
}
|
|
|
|
|
2020-04-23 11:42:08 +00:00
|
|
|
});
|
2020-04-13 08:46:09 +00:00
|
|
|
|
2020-04-10 10:14:50 +00:00
|
|
|
async function testRedeem(receiver, recipient, signer, relayer, redeemCode, blockNumber, blockHash) {
|
2020-04-28 09:36:34 +00:00
|
|
|
let redeemable = await NFTBucket.methods.redeemables(recipient).call();
|
|
|
|
const tokenID = redeemable.data;
|
2020-04-10 10:14:50 +00:00
|
|
|
|
|
|
|
const message = {
|
|
|
|
blockNumber: blockNumber,
|
|
|
|
blockHash: blockHash,
|
|
|
|
receiver: receiver,
|
|
|
|
code: redeemCode,
|
|
|
|
};
|
|
|
|
|
|
|
|
const sig = await signRedeem(NFTBucket._address, signer, message);
|
|
|
|
const redeem = NFTBucket.methods.redeem(message, sig);
|
|
|
|
const redeemGas = await redeem.estimateGas();
|
|
|
|
await redeem.send({
|
|
|
|
from: relayer,
|
|
|
|
gas: redeemGas,
|
|
|
|
});
|
|
|
|
|
|
|
|
let tokenOwner = await TestNFT.methods.ownerOf(tokenID).call();
|
|
|
|
assert.equal(tokenOwner, receiver, `Token owner is ${tokenOwner} instead of the expected ${receiver}`);
|
|
|
|
}
|
|
|
|
|
2020-04-23 12:51:05 +00:00
|
|
|
it("cannot redeem before the start date", async function() {
|
|
|
|
const block = await web3.eth.getBlock("latest");
|
|
|
|
await mineAt(START_TIME);
|
|
|
|
|
|
|
|
try {
|
|
|
|
await testRedeem(user, keycard_1, keycard_1, relayer, REDEEM_CODE, block.number, block.hash);
|
|
|
|
assert.fail("redeem should have failed");
|
|
|
|
} catch(e) {
|
|
|
|
assert.match(e.message, /not yet started/);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-04-10 10:14:50 +00:00
|
|
|
it("cannot redeem after expiration date", async function() {
|
|
|
|
const block = await web3.eth.getBlock("latest");
|
|
|
|
await mineAt(EXPIRATION_TIME);
|
|
|
|
|
|
|
|
try {
|
|
|
|
await testRedeem(user, keycard_1, keycard_1, relayer, REDEEM_CODE, block.number, block.hash);
|
|
|
|
assert.fail("redeem should have failed");
|
|
|
|
} catch(e) {
|
|
|
|
assert.match(e.message, /expired/);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
it("cannot redeem with invalid code", async function() {
|
|
|
|
const block = await web3.eth.getBlock("latest");
|
|
|
|
await mineAt(NOW);
|
|
|
|
try {
|
|
|
|
await testRedeem(user, keycard_1, keycard_1, relayer, web3.utils.sha3("bad-code"), block.number, block.hash);
|
|
|
|
assert.fail("redeem should have failed");
|
|
|
|
} catch(e) {
|
|
|
|
assert.match(e.message, /invalid code/);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
it("cannot redeem with invalid recipient", async function() {
|
|
|
|
const block = await web3.eth.getBlock("latest");
|
|
|
|
await mineAt(NOW);
|
|
|
|
try {
|
|
|
|
await testRedeem(user, keycard_1, keycard_3, relayer, REDEEM_CODE, block.number, block.hash);
|
|
|
|
assert.fail("redeem should have failed");
|
|
|
|
} catch(e) {
|
|
|
|
assert.match(e.message, /not found/);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
it("cannot redeem with a block in the future", async function() {
|
|
|
|
const block = await web3.eth.getBlock("latest");
|
|
|
|
await mineAt(NOW);
|
|
|
|
try {
|
|
|
|
await testRedeem(user, keycard_1, keycard_1, relayer, REDEEM_CODE, (block.number + 2), "0x0000000000000000000000000000000000000000000000000000000000000000");
|
|
|
|
} catch (e) {
|
|
|
|
assert.match(e.message, /future/);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
it("cannot redeem with an old block", async function() {
|
|
|
|
const currentBlock = await web3.eth.getBlock("latest");
|
|
|
|
const block = await web3.eth.getBlock(currentBlock.number - 10);
|
|
|
|
|
|
|
|
await mineAt(NOW);
|
|
|
|
try {
|
|
|
|
await testRedeem(user, keycard_1, keycard_1, relayer, REDEEM_CODE, block.number, block.hash);
|
|
|
|
} catch (e) {
|
|
|
|
assert.match(e.message, /too old/);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
it("cannot redeem with an invalid hash", async function() {
|
|
|
|
const block = await web3.eth.getBlock("latest");
|
|
|
|
|
|
|
|
await mineAt(NOW);
|
|
|
|
try {
|
|
|
|
await testRedeem(user, keycard_1, keycard_1, relayer, REDEEM_CODE, block.number, "0x0000000000000000000000000000000000000000000000000000000000000000");
|
|
|
|
} catch (e) {
|
|
|
|
assert.match(e.message, /invalid block hash/);
|
|
|
|
}
|
2020-04-23 11:42:08 +00:00
|
|
|
});
|
2020-04-10 10:14:50 +00:00
|
|
|
|
|
|
|
it("can redeem before expiration date", async function() {
|
|
|
|
const block = await web3.eth.getBlock("latest");
|
|
|
|
await mineAt(NOW);
|
|
|
|
await testRedeem(user, keycard_1, keycard_1, relayer, REDEEM_CODE, block.number, block.hash);
|
|
|
|
});
|
|
|
|
|
|
|
|
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`);
|
|
|
|
}
|
|
|
|
|
|
|
|
it("shop cannot kill contract before expirationTime", async function() {
|
|
|
|
await mineAt(NOW);
|
|
|
|
try {
|
|
|
|
await testKill();
|
|
|
|
assert.fail("redeem should have failed");
|
|
|
|
} catch(e) {
|
|
|
|
assert.match(e.message, /not expired yet/);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
it("shop can kill contract after expirationTime", async function() {
|
|
|
|
await mineAt(EXPIRATION_TIME);
|
|
|
|
await testKill();
|
2020-04-23 11:42:08 +00:00
|
|
|
await mineAt(NOW);
|
2020-04-10 10:14:50 +00:00
|
|
|
});
|
|
|
|
});
|