avoid deploying contract manually in tests
This commit is contained in:
parent
745975dfa8
commit
ef49923d34
|
@ -1,6 +1,12 @@
|
||||||
const TestToken = artifacts.require('TestToken');
|
const TestToken = artifacts.require('TestToken');
|
||||||
const GiftsBatch = artifacts.require('GiftsBatch');
|
const GiftsBatch = artifacts.require('GiftsBatch');
|
||||||
|
|
||||||
|
const TOTAL_SUPPLY = 10000;
|
||||||
|
const GIFT_AMOUNT = 10;
|
||||||
|
const REDEEM_CODE = web3.utils.sha3("hello world");
|
||||||
|
const NOW = Math.round(new Date().getTime() / 1000);
|
||||||
|
const EXPIRATION_TIME = NOW + 60 * 60 * 24; // in 24 hours
|
||||||
|
|
||||||
let shop,
|
let shop,
|
||||||
user;
|
user;
|
||||||
|
|
||||||
|
@ -10,6 +16,9 @@ config({
|
||||||
deploy: {
|
deploy: {
|
||||||
"TestToken": {
|
"TestToken": {
|
||||||
args: [],
|
args: [],
|
||||||
|
},
|
||||||
|
"GiftsBatch": {
|
||||||
|
args: ["$TestToken", EXPIRATION_TIME],
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -20,12 +29,6 @@ config({
|
||||||
user = _accounts[3];
|
user = _accounts[3];
|
||||||
});
|
});
|
||||||
|
|
||||||
const TOTAL_SUPPLY = 10000;
|
|
||||||
const GIFT_AMOUNT = 10;
|
|
||||||
const REDEEM_CODE = web3.utils.sha3("hello world");
|
|
||||||
const NOW = Math.round(new Date().getTime() / 1000);
|
|
||||||
const EXPIRATION_TIME = NOW + 60 * 60 * 24; // in 24 hours
|
|
||||||
|
|
||||||
let sendMethod;
|
let sendMethod;
|
||||||
|
|
||||||
async function signRedeem(contractAddress, signer, message) {
|
async function signRedeem(contractAddress, signer, message) {
|
||||||
|
@ -97,206 +100,215 @@ function mineAt(timestamp) {
|
||||||
contract("GiftsBatch", function () {
|
contract("GiftsBatch", function () {
|
||||||
sendMethod = (web3.currentProvider.sendAsync) ? web3.currentProvider.sendAsync.bind(web3.currentProvider) : web3.currentProvider.send.bind(web3.currentProvider);
|
sendMethod = (web3.currentProvider.sendAsync) ? web3.currentProvider.sendAsync.bind(web3.currentProvider) : web3.currentProvider.send.bind(web3.currentProvider);
|
||||||
|
|
||||||
let giftsBatchInstance;
|
it("shop buys 100 tokens", async function () {
|
||||||
|
let supply = await TestToken.methods.totalSupply().call();
|
||||||
|
assert.equal(parseInt(supply), 0);
|
||||||
|
|
||||||
// it("shop buys 100 tokens", async function () {
|
await TestToken.methods.mint(TOTAL_SUPPLY).send({
|
||||||
// let supply = await TestToken.methods.totalSupply().call();
|
from: shop,
|
||||||
// assert.equal(parseInt(supply), 0);
|
});
|
||||||
|
|
||||||
// await TestToken.methods.mint(TOTAL_SUPPLY).send({
|
supply = await TestToken.methods.totalSupply().call();
|
||||||
// from: shop,
|
assert.equal(parseInt(supply), TOTAL_SUPPLY);
|
||||||
// });
|
|
||||||
|
|
||||||
// supply = await TestToken.methods.totalSupply().call();
|
let shopBalance = await TestToken.methods.balanceOf(shop).call();
|
||||||
// assert.equal(parseInt(supply), TOTAL_SUPPLY);
|
assert.equal(parseInt(shopBalance), TOTAL_SUPPLY);
|
||||||
|
|
||||||
// let shopBalance = await TestToken.methods.balanceOf(shop).call();
|
|
||||||
// assert.equal(parseInt(shopBalance), TOTAL_SUPPLY);
|
|
||||||
// });
|
|
||||||
|
|
||||||
it("deploy", async function() {
|
|
||||||
const giftsBatchInstance = await GiftsBatch.deploy([keycard_1, EXPIRATION_TIME]);
|
|
||||||
|
|
||||||
// const approve = TestToken.methods.approve(giftsBatchInstance._address, TOTAL_SUPPLY)
|
|
||||||
// const approveGas = await approve.estimateGas();
|
|
||||||
// await approve.send({
|
|
||||||
// from: shop,
|
|
||||||
// gas: approveGas,
|
|
||||||
// });
|
|
||||||
|
|
||||||
// console.log(giftsBatchInstance.methods)
|
|
||||||
// const addSupply = giftsBatchInstance.methods.addSupply(TOTAL_SUPPLY)
|
|
||||||
// const addSupplyGas = await addSupply.estimateGas();
|
|
||||||
// await addSupply.send({
|
|
||||||
// from: shop,
|
|
||||||
// gas: addSupplyGas,
|
|
||||||
// });
|
|
||||||
|
|
||||||
// let shopBalance = await TestToken.methods.balanceOf(shop).call();
|
|
||||||
// assert.equal(parseInt(shopBalance), 0);
|
|
||||||
|
|
||||||
// let factoryBalance = await TestToken.methods.balanceOf(giftsBatchInstance._address).call();
|
|
||||||
// assert.equal(parseInt(factoryBalance), TOTAL_SUPPLY);
|
|
||||||
|
|
||||||
// let totalSupply = await giftsBatchInstance.methods.totalSupply().call();
|
|
||||||
// assert.equal(parseInt(totalSupply), TOTAL_SUPPLY);
|
|
||||||
|
|
||||||
// let availableSupply = await giftsBatchInstance.methods.availableSupply().call();
|
|
||||||
// assert.equal(parseInt(availableSupply), TOTAL_SUPPLY);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// async function testCreateGift(keycard, amount) {
|
// it("deploy", async function() {
|
||||||
// const redeemCodeHash = web3.utils.sha3(REDEEM_CODE);
|
// const deploy = GiftsBatch.clone().deploy({ arguments: [keycard_1, EXPIRATION_TIME] });
|
||||||
// const createGift = giftsBatchInstance.methods.createGift(keycard, amount, redeemCodeHash);
|
// giftsBatchInstance = await deploy.send();
|
||||||
// const createGiftGas = await createGift.estimateGas();
|
|
||||||
// await createGift.send({
|
|
||||||
// from: shop,
|
|
||||||
// gas: createGiftGas,
|
|
||||||
// });
|
|
||||||
|
|
||||||
// let totalSupply = await giftsBatchInstance.methods.totalSupply().call();
|
|
||||||
// assert.equal(parseInt(totalSupply), TOTAL_SUPPLY);
|
|
||||||
|
|
||||||
// let availableSupply = await giftsBatchInstance.methods.availableSupply().call();
|
|
||||||
// assert.equal(parseInt(availableSupply), TOTAL_SUPPLY - amount);
|
|
||||||
// }
|
|
||||||
|
|
||||||
// it("createGift should fail amount is zero", async function() {
|
|
||||||
// try {
|
|
||||||
// await testCreateGift(keycard_1, 0);
|
|
||||||
// assert.fail("createGift should have failed");
|
|
||||||
// } catch(e) {
|
|
||||||
// assert.match(e.message, /invalid amount/);
|
|
||||||
// }
|
|
||||||
// });
|
// });
|
||||||
|
|
||||||
|
it("approve ERC20 transfer", async function() {
|
||||||
|
const approve = TestToken.methods.approve(GiftsBatch._address, TOTAL_SUPPLY)
|
||||||
|
const approveGas = await approve.estimateGas();
|
||||||
|
await approve.send({
|
||||||
|
from: shop,
|
||||||
|
gas: approveGas,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
// it("createGift fails if amount > totalSupply", async function() {
|
it("addSupply", async function() {
|
||||||
// try {
|
let factoryBalance = await TestToken.methods.balanceOf(GiftsBatch._address).call();
|
||||||
// await testCreateGift(keycard_1, TOTAL_SUPPLY + 1);
|
assert.equal(parseInt(factoryBalance), 0, `factory balance before is ${factoryBalance} instead of 0`);
|
||||||
// assert.fail("createGift should have failed");
|
|
||||||
// } catch(e) {
|
|
||||||
// assert.match(e.message, /low supply/);
|
|
||||||
// }
|
|
||||||
// });
|
|
||||||
|
|
||||||
// it("createGift", async function() {
|
let shopBalance = await TestToken.methods.balanceOf(shop).call();
|
||||||
// await testCreateGift(keycard_1, GIFT_AMOUNT);
|
assert.equal(parseInt(shopBalance), TOTAL_SUPPLY, `shop balance before is ${shopBalance} instead of ${TOTAL_SUPPLY}`);
|
||||||
// });
|
|
||||||
|
|
||||||
// it("createGift should fail if keycard has already been used", async function() {
|
const addSupply = GiftsBatch.methods.addSupply(TOTAL_SUPPLY)
|
||||||
// try {
|
const addSupplyGas = await addSupply.estimateGas();
|
||||||
// await testCreateGift(keycard_1, 1);
|
await addSupply.send({
|
||||||
// assert.fail("createGift should have failed");
|
from: shop,
|
||||||
// } catch(e) {
|
gas: addSupplyGas,
|
||||||
// assert.match(e.message, /keycard already used/);
|
});
|
||||||
// }
|
|
||||||
// });
|
|
||||||
|
|
||||||
// it("createGift amount > availableSupply", async function() {
|
factoryBalance = await TestToken.methods.balanceOf(GiftsBatch._address).call();
|
||||||
// try {
|
assert.equal(parseInt(factoryBalance), TOTAL_SUPPLY, `factory balance after is ${factoryBalance} instead of ${TOTAL_SUPPLY}`);
|
||||||
// await testCreateGift(keycard_2, TOTAL_SUPPLY - GIFT_AMOUNT + 1);
|
|
||||||
// assert.fail("createGift should have failed");
|
|
||||||
// } catch(e) {
|
|
||||||
// assert.match(e.message, /low supply/);
|
|
||||||
// }
|
|
||||||
// });
|
|
||||||
|
|
||||||
// async function testRedeem(redeemCode) {
|
shopBalance = await TestToken.methods.balanceOf(shop).call();
|
||||||
// let totalSupply = await giftsBatchInstance.methods.totalSupply().call();
|
assert.equal(parseInt(shopBalance), 0, `shop balance after is ${shopBalance} instead of 0`);
|
||||||
// assert.equal(parseInt(totalSupply), TOTAL_SUPPLY);
|
|
||||||
|
|
||||||
// let factoryBalance = await TestToken.methods.balanceOf(giftsBatchInstance._address).call();
|
|
||||||
// assert.equal(parseInt(factoryBalance), TOTAL_SUPPLY);
|
|
||||||
|
|
||||||
// let userBalance = await TestToken.methods.balanceOf(user).call();
|
let totalSupply = await GiftsBatch.methods.totalSupply().call();
|
||||||
// assert.equal(parseInt(userBalance), 0);
|
assert.equal(parseInt(totalSupply), TOTAL_SUPPLY, `total contract supply is ${totalSupply} instead of ${TOTAL_SUPPLY}`);
|
||||||
|
|
||||||
// // const gift = await giftsBatchInstance.methods.gifts(keycard_1).call();
|
let availableSupply = await GiftsBatch.methods.availableSupply().call();
|
||||||
// // const giftBlockNumber = gift.blockNumber;
|
assert.equal(parseInt(availableSupply), TOTAL_SUPPLY, `available contract supply is ${availableSupply} instead of ${TOTAL_SUPPLY}`);
|
||||||
// // const message = web3.utils.sha3(user);
|
});
|
||||||
// // const sig = await web3.eth.sign(message, keycard_1);
|
|
||||||
|
|
||||||
// const message = {
|
async function testCreateGift(keycard, amount) {
|
||||||
// keycard: keycard_1,
|
const redeemCodeHash = web3.utils.sha3(REDEEM_CODE);
|
||||||
// receiver: user,
|
const createGift = GiftsBatch.methods.createGift(keycard, amount, redeemCodeHash);
|
||||||
// code: redeemCode,
|
const createGiftGas = await createGift.estimateGas();
|
||||||
// };
|
await createGift.send({
|
||||||
|
from: shop,
|
||||||
|
gas: createGiftGas,
|
||||||
|
});
|
||||||
|
|
||||||
// const sig = await signRedeem(giftsBatchInstance._address, keycard_1, message);
|
let totalSupply = await GiftsBatch.methods.totalSupply().call();
|
||||||
|
assert.equal(parseInt(totalSupply), TOTAL_SUPPLY);
|
||||||
|
|
||||||
// const redeem = giftsBatchInstance.methods.redeem(message, sig);
|
let availableSupply = await GiftsBatch.methods.availableSupply().call();
|
||||||
// const redeemGas = await redeem.estimateGas();
|
assert.equal(parseInt(availableSupply), TOTAL_SUPPLY - amount);
|
||||||
// await redeem.send({
|
}
|
||||||
// from: user,
|
|
||||||
// gas: redeemGas,
|
|
||||||
// });
|
|
||||||
|
|
||||||
// factoryBalance = await TestToken.methods.balanceOf(giftsBatchInstance._address).call();
|
it("createGift should fail amount is zero", async function() {
|
||||||
// assert.equal(parseInt(factoryBalance), TOTAL_SUPPLY - GIFT_AMOUNT);
|
try {
|
||||||
|
await testCreateGift(keycard_1, 0);
|
||||||
|
assert.fail("createGift should have failed");
|
||||||
|
} catch(e) {
|
||||||
|
assert.match(e.message, /invalid amount/);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
// userBalance = await TestToken.methods.balanceOf(user).call();
|
|
||||||
// assert.equal(parseInt(userBalance), GIFT_AMOUNT);
|
|
||||||
|
|
||||||
// totalSupply = await giftsBatchInstance.methods.totalSupply().call();
|
it("createGift fails if amount > totalSupply", async function() {
|
||||||
// assert.equal(parseInt(totalSupply), TOTAL_SUPPLY - GIFT_AMOUNT);
|
try {
|
||||||
// }
|
await testCreateGift(keycard_1, TOTAL_SUPPLY + 1);
|
||||||
|
assert.fail("createGift should have failed");
|
||||||
|
} catch(e) {
|
||||||
|
assert.match(e.message, /low supply/);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
// it("cannot redeem after expiration date", async function() {
|
it("createGift", async function() {
|
||||||
// await mineAt(EXPIRATION_TIME);
|
await testCreateGift(keycard_1, GIFT_AMOUNT);
|
||||||
// try {
|
});
|
||||||
// await testRedeem(REDEEM_CODE);
|
|
||||||
// assert.fail("redeem should have failed");
|
|
||||||
// } catch(e) {
|
|
||||||
// assert.match(e.message, /expired/);
|
|
||||||
// }
|
|
||||||
// });
|
|
||||||
|
|
||||||
// it("cannot redeem with invalid code", async function() {
|
it("createGift should fail if keycard has already been used", async function() {
|
||||||
// await mineAt(NOW);
|
try {
|
||||||
// try {
|
await testCreateGift(keycard_1, 1);
|
||||||
// await testRedeem(web3.utils.sha3("bad-code"));
|
assert.fail("createGift should have failed");
|
||||||
// assert.fail("redeem should have failed");
|
} catch(e) {
|
||||||
// } catch(e) {
|
assert.match(e.message, /keycard already used/);
|
||||||
// assert.match(e.message, /invalid code/);
|
}
|
||||||
// }
|
});
|
||||||
// });
|
|
||||||
|
|
||||||
// it("can redeem before expiration date", async function() {
|
it("createGift amount > availableSupply", async function() {
|
||||||
// await mineAt(NOW);
|
try {
|
||||||
// await testRedeem(REDEEM_CODE);
|
await testCreateGift(keycard_2, TOTAL_SUPPLY - GIFT_AMOUNT + 1);
|
||||||
// });
|
assert.fail("createGift should have failed");
|
||||||
|
} catch(e) {
|
||||||
|
assert.match(e.message, /low supply/);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
// async function testKill() {
|
async function testRedeem(redeemCode) {
|
||||||
// let shopBalance = await TestToken.methods.balanceOf(shop).call();
|
let totalSupply = await GiftsBatch.methods.totalSupply().call();
|
||||||
// assert.equal(parseInt(shopBalance), 0);
|
assert.equal(parseInt(totalSupply), TOTAL_SUPPLY);
|
||||||
|
|
||||||
// let factoryBalance = await TestToken.methods.balanceOf(giftsBatchInstance._address).call();
|
let factoryBalance = await TestToken.methods.balanceOf(GiftsBatch._address).call();
|
||||||
// assert.equal(parseInt(factoryBalance), TOTAL_SUPPLY - GIFT_AMOUNT);
|
assert.equal(parseInt(factoryBalance), TOTAL_SUPPLY);
|
||||||
|
|
||||||
// await giftsBatchInstance.methods.kill().send({
|
let userBalance = await TestToken.methods.balanceOf(user).call();
|
||||||
// from: shop,
|
assert.equal(parseInt(userBalance), 0);
|
||||||
// });
|
|
||||||
|
|
||||||
// shopBalance = await TestToken.methods.balanceOf(shop).call();
|
// const gift = await GiftsBatch.methods.gifts(keycard_1).call();
|
||||||
// assert.equal(parseInt(shopBalance), TOTAL_SUPPLY - GIFT_AMOUNT);
|
// const giftBlockNumber = gift.blockNumber;
|
||||||
|
// const message = web3.utils.sha3(user);
|
||||||
|
// const sig = await web3.eth.sign(message, keycard_1);
|
||||||
|
|
||||||
// factoryBalance = await TestToken.methods.balanceOf(giftsBatchInstance._address).call();
|
const message = {
|
||||||
// assert.equal(parseInt(factoryBalance), 0);
|
keycard: keycard_1,
|
||||||
// }
|
receiver: user,
|
||||||
|
code: redeemCode,
|
||||||
|
};
|
||||||
|
|
||||||
// it("shop cannot kill contract before expirationTime", async function() {
|
const sig = await signRedeem(GiftsBatch._address, keycard_1, message);
|
||||||
// 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() {
|
const redeem = GiftsBatch.methods.redeem(message, sig);
|
||||||
// await mineAt(EXPIRATION_TIME);
|
const redeemGas = await redeem.estimateGas();
|
||||||
// await testKill();
|
await redeem.send({
|
||||||
// });
|
from: user,
|
||||||
|
gas: redeemGas,
|
||||||
|
});
|
||||||
|
|
||||||
|
factoryBalance = await TestToken.methods.balanceOf(GiftsBatch._address).call();
|
||||||
|
assert.equal(parseInt(factoryBalance), TOTAL_SUPPLY - GIFT_AMOUNT);
|
||||||
|
|
||||||
|
userBalance = await TestToken.methods.balanceOf(user).call();
|
||||||
|
assert.equal(parseInt(userBalance), GIFT_AMOUNT);
|
||||||
|
|
||||||
|
totalSupply = await GiftsBatch.methods.totalSupply().call();
|
||||||
|
assert.equal(parseInt(totalSupply), TOTAL_SUPPLY - GIFT_AMOUNT);
|
||||||
|
}
|
||||||
|
|
||||||
|
it("cannot redeem after expiration date", async function() {
|
||||||
|
await mineAt(EXPIRATION_TIME);
|
||||||
|
try {
|
||||||
|
await testRedeem(REDEEM_CODE);
|
||||||
|
assert.fail("redeem should have failed");
|
||||||
|
} catch(e) {
|
||||||
|
assert.match(e.message, /expired/);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
it("cannot redeem with invalid code", async function() {
|
||||||
|
await mineAt(NOW);
|
||||||
|
try {
|
||||||
|
await testRedeem(web3.utils.sha3("bad-code"));
|
||||||
|
assert.fail("redeem should have failed");
|
||||||
|
} catch(e) {
|
||||||
|
assert.match(e.message, /invalid code/);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
it("can redeem before expiration date", async function() {
|
||||||
|
await mineAt(NOW);
|
||||||
|
await testRedeem(REDEEM_CODE);
|
||||||
|
});
|
||||||
|
|
||||||
|
async function testKill() {
|
||||||
|
let shopBalance = await TestToken.methods.balanceOf(shop).call();
|
||||||
|
assert.equal(parseInt(shopBalance), 0);
|
||||||
|
|
||||||
|
let factoryBalance = await TestToken.methods.balanceOf(GiftsBatch._address).call();
|
||||||
|
assert.equal(parseInt(factoryBalance), TOTAL_SUPPLY - GIFT_AMOUNT);
|
||||||
|
|
||||||
|
await GiftsBatch.methods.kill().send({
|
||||||
|
from: shop,
|
||||||
|
});
|
||||||
|
|
||||||
|
shopBalance = await TestToken.methods.balanceOf(shop).call();
|
||||||
|
assert.equal(parseInt(shopBalance), TOTAL_SUPPLY - GIFT_AMOUNT);
|
||||||
|
|
||||||
|
factoryBalance = await TestToken.methods.balanceOf(GiftsBatch._address).call();
|
||||||
|
assert.equal(parseInt(factoryBalance), 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
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();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue