update tests to use the factory

This commit is contained in:
Andrea Franz 2020-02-21 20:01:15 +01:00
parent 29cdc8527f
commit fb4ba43469
No known key found for this signature in database
GPG Key ID: 4F0D2F2D9DE7F29D
1 changed files with 51 additions and 10 deletions

View File

@ -1,5 +1,7 @@
const EmbarkJS = artifacts.require('EmbarkJS');
const TestToken = artifacts.require('TestToken'); const TestToken = artifacts.require('TestToken');
const GiftBucket = artifacts.require('GiftBucket'); const _GiftBucket = artifacts.require('GiftBucket');
const GiftBucketFactory = artifacts.require('GiftBucketFactory');
const TOTAL_SUPPLY = 10000; const TOTAL_SUPPLY = 10000;
const GIFT_AMOUNT = 10; const GIFT_AMOUNT = 10;
@ -10,7 +12,6 @@ const EXPIRATION_TIME = NOW + 60 * 60 * 24; // in 24 hours
let shop, let shop,
user; user;
// For documentation please see https://framework.embarklabs.io/docs/contracts_testing.html
config({ config({
contracts: { contracts: {
deploy: { deploy: {
@ -19,7 +20,10 @@ config({
}, },
"GiftBucket": { "GiftBucket": {
args: ["$TestToken", EXPIRATION_TIME], args: ["$TestToken", EXPIRATION_TIME],
} },
"GiftBucketFactory": {
args: [],
},
} }
}, },
}, (_err, _accounts) => { }, (_err, _accounts) => {
@ -45,7 +49,7 @@ async function signRedeem(contractAddress, signer, message) {
]; ];
let redeem = [ let redeem = [
{ name: "keycard", type: "address" }, { name: "recipient", type: "address" },
{ name: "receiver", type: "address" }, { name: "receiver", type: "address" },
{ name: "code", type: "bytes32" }, { name: "code", type: "bytes32" },
]; ];
@ -106,8 +110,45 @@ if (assert.match === undefined) {
} }
contract("GiftBucket", function () { contract("GiftBucket", function () {
let GiftBucket;
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);
// it("deploy factory", async () => {
// // only to test gas
// await GiftBucketFactory.deploy([]);
// });
it("deploy bucket", async () => {
// only to test gas
await _GiftBucket.deploy([TestToken._address, EXPIRATION_TIME]);
});
it("deploy bucket via factory", async () => {
const create = GiftBucketFactory.methods.create(TestToken._address, EXPIRATION_TIME);
const gas = await create.estimateGas();
const receipt = await create.send({
from: shop,
gas: gas,
});
const bucketAddress = receipt.events.Created.returnValues.bucket;
const jsonInterface = _GiftBucket.options.jsonInterface;
GiftBucket = new EmbarkJS.Blockchain.Contract({
abi: jsonInterface,
address: bucketAddress,
});
// const deploy = await _GiftBucket.deploy({
// arguments: [TestToken._address, EXPIRATION_TIME],
// });
// gas = await deploy.estimateGas();
// await deploy.send({
// from: shop,
// gas: gas,
// });
});
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);
@ -197,7 +238,7 @@ contract("GiftBucket", function () {
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, /recipient already used/);
} }
}); });
@ -210,16 +251,16 @@ contract("GiftBucket", function () {
} }
}); });
async function testRedeem(receiver, keycard, signer, redeemCode) { async function testRedeem(receiver, recipient, signer, redeemCode) {
let initialBucketBalance = await TestToken.methods.balanceOf(GiftBucket._address).call(); let initialBucketBalance = await TestToken.methods.balanceOf(GiftBucket._address).call();
let initialUserBalance = await TestToken.methods.balanceOf(user).call(); let initialUserBalance = await TestToken.methods.balanceOf(user).call();
let initialRedeemableSupply = await GiftBucket.methods.redeemableSupply().call(); let initialRedeemableSupply = await GiftBucket.methods.redeemableSupply().call();
let gift = await GiftBucket.methods.gifts(keycard).call(); let gift = await GiftBucket.methods.gifts(recipient).call();
const amount = parseInt(gift.amount); const amount = parseInt(gift.amount);
const message = { const message = {
keycard: keycard, recipient: recipient,
receiver: receiver, receiver: receiver,
code: redeemCode, code: redeemCode,
}; };
@ -228,7 +269,7 @@ contract("GiftBucket", function () {
const redeem = GiftBucket.methods.redeem(message, sig); const redeem = GiftBucket.methods.redeem(message, sig);
const redeemGas = await redeem.estimateGas(); const redeemGas = await redeem.estimateGas();
await redeem.send({ await redeem.send({
from: keycard_1, from: receiver,
gas: redeemGas, gas: redeemGas,
}); });
@ -273,7 +314,7 @@ contract("GiftBucket", function () {
await testRedeem(user, keycard_1, keycard_2, REDEEM_CODE); await testRedeem(user, keycard_1, keycard_2, REDEEM_CODE);
assert.fail("redeem should have failed"); assert.fail("redeem should have failed");
} catch(e) { } catch(e) {
assert.match(e.message, /wrong keycard sig/); assert.match(e.message, /wrong recipient sig/);
} }
}); });