upgrade embark version

This commit is contained in:
Andrea Franz 2020-02-19 10:33:38 +01:00
parent 55dbd113f9
commit 417a8cc85e
No known key found for this signature in database
GPG Key ID: 4F0D2F2D9DE7F29D
1 changed files with 118 additions and 123 deletions

View File

@ -1,5 +1,5 @@
const TestToken = require('Embark/contracts/TestToken'); const TestToken = artifacts.require('TestToken');
const GiftsBatch = require('Embark/contracts/GiftsBatch'); const GiftsBatch = artifacts.require('GiftsBatch');
let shop, let shop,
user; user;
@ -94,158 +94,153 @@ function mineAt(timestamp) {
}); });
} }
contract("TestToken", 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; let giftsBatchInstance;
it("shop buys 100 tokens", async function () { // it("shop buys 100 tokens", async function () {
let supply = await TestToken.methods.totalSupply().call(); // let supply = await TestToken.methods.totalSupply().call();
assert.equal(parseInt(supply), 0); // assert.equal(parseInt(supply), 0);
await TestToken.methods.mint(TOTAL_SUPPLY).send({ // await TestToken.methods.mint(TOTAL_SUPPLY).send({
from: shop, // from: shop,
}); // });
supply = await TestToken.methods.totalSupply().call(); // supply = await TestToken.methods.totalSupply().call();
assert.equal(parseInt(supply), TOTAL_SUPPLY); // assert.equal(parseInt(supply), TOTAL_SUPPLY);
let shopBalance = await TestToken.methods.balanceOf(shop).call(); // let shopBalance = await TestToken.methods.balanceOf(shop).call();
assert.equal(parseInt(shopBalance), TOTAL_SUPPLY); // assert.equal(parseInt(shopBalance), TOTAL_SUPPLY);
}); // });
it("deploy", async function() { it("deploy", async function() {
const deploy = GiftsBatch.deploy({ const giftsBatchInstance = await GiftsBatch.deploy([keycard_1, EXPIRATION_TIME]);
arguments: [TestToken.address, EXPIRATION_TIME],
});
giftsBatchInstance = await deploy.send({
gas: 900000,
});
const approve = TestToken.methods.approve(giftsBatchInstance._address, TOTAL_SUPPLY) // const approve = TestToken.methods.approve(giftsBatchInstance._address, TOTAL_SUPPLY)
const approveGas = await approve.estimateGas(); // const approveGas = await approve.estimateGas();
await approve.send({ // await approve.send({
from: shop, // from: shop,
gas: approveGas, // 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,
// });
const addSupply = giftsBatchInstance.methods.addSupply(TOTAL_SUPPLY) // let shopBalance = await TestToken.methods.balanceOf(shop).call();
const addSupplyGas = await addSupply.estimateGas(); // assert.equal(parseInt(shopBalance), 0);
await addSupply.send({
from: shop,
gas: addSupplyGas,
});
let shopBalance = await TestToken.methods.balanceOf(shop).call(); // let factoryBalance = await TestToken.methods.balanceOf(giftsBatchInstance._address).call();
assert.equal(parseInt(shopBalance), 0); // assert.equal(parseInt(factoryBalance), TOTAL_SUPPLY);
let factoryBalance = await TestToken.methods.balanceOf(giftsBatchInstance._address).call(); // let totalSupply = await giftsBatchInstance.methods.totalSupply().call();
assert.equal(parseInt(factoryBalance), TOTAL_SUPPLY); // assert.equal(parseInt(totalSupply), TOTAL_SUPPLY);
let totalSupply = await giftsBatchInstance.methods.totalSupply().call(); // let availableSupply = await giftsBatchInstance.methods.availableSupply().call();
assert.equal(parseInt(totalSupply), TOTAL_SUPPLY); // assert.equal(parseInt(availableSupply), TOTAL_SUPPLY);
let availableSupply = await giftsBatchInstance.methods.availableSupply().call();
assert.equal(parseInt(availableSupply), TOTAL_SUPPLY);
}); });
async function testCreateGift(keycard, amount) { // async function testCreateGift(keycard, amount) {
const redeemCodeHash = web3.utils.sha3(REDEEM_CODE); // const redeemCodeHash = web3.utils.sha3(REDEEM_CODE);
const createGift = giftsBatchInstance.methods.createGift(keycard, amount, redeemCodeHash); // const createGift = giftsBatchInstance.methods.createGift(keycard, amount, redeemCodeHash);
const createGiftGas = await createGift.estimateGas(); // const createGiftGas = await createGift.estimateGas();
await createGift.send({ // await createGift.send({
from: shop, // from: shop,
gas: createGiftGas, // gas: createGiftGas,
}); // });
let totalSupply = await giftsBatchInstance.methods.totalSupply().call(); // let totalSupply = await giftsBatchInstance.methods.totalSupply().call();
assert.equal(parseInt(totalSupply), TOTAL_SUPPLY); // assert.equal(parseInt(totalSupply), TOTAL_SUPPLY);
let availableSupply = await giftsBatchInstance.methods.availableSupply().call(); // let availableSupply = await giftsBatchInstance.methods.availableSupply().call();
assert.equal(parseInt(availableSupply), TOTAL_SUPPLY - amount); // assert.equal(parseInt(availableSupply), TOTAL_SUPPLY - amount);
} // }
it("createGift should fail amount is zero", async function() { // it("createGift should fail amount is zero", async function() {
try { // try {
await testCreateGift(keycard_1, 0); // await testCreateGift(keycard_1, 0);
assert.fail("createGift should have failed"); // assert.fail("createGift should have failed");
} catch(e) { // } catch(e) {
assert.match(e.message, /invalid amount/); // assert.match(e.message, /invalid amount/);
} // }
}); // });
it("createGift fails if amount > totalSupply", async function() { // it("createGift fails if amount > totalSupply", async function() {
try { // try {
await testCreateGift(keycard_1, TOTAL_SUPPLY + 1); // await testCreateGift(keycard_1, TOTAL_SUPPLY + 1);
assert.fail("createGift should have failed"); // assert.fail("createGift should have failed");
} catch(e) { // } catch(e) {
assert.match(e.message, /low supply/); // assert.match(e.message, /low supply/);
} // }
}); // });
it("createGift", async function() { // it("createGift", async function() {
await testCreateGift(keycard_1, GIFT_AMOUNT); // await testCreateGift(keycard_1, GIFT_AMOUNT);
}); // });
it("createGift should fail if keycard has already been used", async function() { // it("createGift should fail if keycard has already been used", async function() {
try { // try {
await testCreateGift(keycard_1, 1); // await testCreateGift(keycard_1, 1);
assert.fail("createGift should have failed"); // assert.fail("createGift should have failed");
} catch(e) { // } catch(e) {
assert.match(e.message, /keycard already used/); // assert.match(e.message, /keycard already used/);
} // }
}); // });
it("createGift amount > availableSupply", async function() { // it("createGift amount > availableSupply", async function() {
try { // try {
await testCreateGift(keycard_2, TOTAL_SUPPLY - GIFT_AMOUNT + 1); // await testCreateGift(keycard_2, TOTAL_SUPPLY - GIFT_AMOUNT + 1);
assert.fail("createGift should have failed"); // assert.fail("createGift should have failed");
} catch(e) { // } catch(e) {
assert.match(e.message, /low supply/); // assert.match(e.message, /low supply/);
} // }
}); // });
async function testRedeem(redeemCode) { // async function testRedeem(redeemCode) {
let totalSupply = await giftsBatchInstance.methods.totalSupply().call(); // let totalSupply = await giftsBatchInstance.methods.totalSupply().call();
assert.equal(parseInt(totalSupply), TOTAL_SUPPLY); // assert.equal(parseInt(totalSupply), TOTAL_SUPPLY);
let factoryBalance = await TestToken.methods.balanceOf(giftsBatchInstance._address).call(); // let factoryBalance = await TestToken.methods.balanceOf(giftsBatchInstance._address).call();
assert.equal(parseInt(factoryBalance), TOTAL_SUPPLY); // assert.equal(parseInt(factoryBalance), TOTAL_SUPPLY);
let userBalance = await TestToken.methods.balanceOf(user).call(); // let userBalance = await TestToken.methods.balanceOf(user).call();
assert.equal(parseInt(userBalance), 0); // assert.equal(parseInt(userBalance), 0);
// const gift = await giftsBatchInstance.methods.gifts(keycard_1).call(); // // const gift = await giftsBatchInstance.methods.gifts(keycard_1).call();
// const giftBlockNumber = gift.blockNumber; // // const giftBlockNumber = gift.blockNumber;
// const message = web3.utils.sha3(user); // // const message = web3.utils.sha3(user);
// const sig = await web3.eth.sign(message, keycard_1); // // const sig = await web3.eth.sign(message, keycard_1);
const message = { // const message = {
keycard: keycard_1, // keycard: keycard_1,
receiver: user, // receiver: user,
code: redeemCode, // code: redeemCode,
}; // };
const sig = await signRedeem(giftsBatchInstance._address, keycard_1, message); // const sig = await signRedeem(giftsBatchInstance._address, keycard_1, message);
const redeem = giftsBatchInstance.methods.redeem(message, sig); // const redeem = giftsBatchInstance.methods.redeem(message, sig);
const redeemGas = await redeem.estimateGas(); // const redeemGas = await redeem.estimateGas();
await redeem.send({ // await redeem.send({
from: user, // from: user,
gas: redeemGas, // gas: redeemGas,
}); // });
factoryBalance = await TestToken.methods.balanceOf(giftsBatchInstance._address).call(); // factoryBalance = await TestToken.methods.balanceOf(giftsBatchInstance._address).call();
assert.equal(parseInt(factoryBalance), TOTAL_SUPPLY - GIFT_AMOUNT); // assert.equal(parseInt(factoryBalance), TOTAL_SUPPLY - GIFT_AMOUNT);
userBalance = await TestToken.methods.balanceOf(user).call(); // userBalance = await TestToken.methods.balanceOf(user).call();
assert.equal(parseInt(userBalance), GIFT_AMOUNT); // assert.equal(parseInt(userBalance), GIFT_AMOUNT);
totalSupply = await giftsBatchInstance.methods.totalSupply().call(); // totalSupply = await giftsBatchInstance.methods.totalSupply().call();
assert.equal(parseInt(totalSupply), TOTAL_SUPPLY - GIFT_AMOUNT); // assert.equal(parseInt(totalSupply), TOTAL_SUPPLY - GIFT_AMOUNT);
} // }
// it("cannot redeem after expiration date", async function() { // it("cannot redeem after expiration date", async function() {
// await mineAt(EXPIRATION_TIME); // await mineAt(EXPIRATION_TIME);
@ -267,10 +262,10 @@ contract("TestToken", function () {
// } // }
// }); // });
it("can redeem before expiration date", async function() { // it("can redeem before expiration date", async function() {
await mineAt(NOW); // await mineAt(NOW);
await testRedeem(REDEEM_CODE); // await testRedeem(REDEEM_CODE);
}); // });
// async function testKill() { // async function testKill() {
// let shopBalance = await TestToken.methods.balanceOf(shop).call(); // let shopBalance = await TestToken.methods.balanceOf(shop).call();