-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGuarantorDeal.js
85 lines (64 loc) · 3.08 KB
/
GuarantorDeal.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
const { balance } = require('@openzeppelin/test-helpers');
const GuarantorDeal = artifacts.require("GuarantorDeal");
contract('GuarantorDeal', (accounts) => {
it('should create account', async () => {
const instance = await GuarantorDeal.deployed();
const receipt = await instance.create(accounts[1], accounts[2], {from: accounts[0], value: 100});
const dealId = receipt.logs[0].args.dealId.toNumber();
const deal = await instance.getDeal.call(dealId);
assert.equal(deal.buyer, accounts[0]);
assert.equal(deal.seller, accounts[1]);
assert.equal(deal.guarantor, accounts[2]);
assert.equal(deal.amount, 100);
});
it('should approve deal by guarantor', async() => {
const instance = await GuarantorDeal.deployed();
const receipt = await instance.create(accounts[1], accounts[2], {from: accounts[0], value: 200});
const dealId = receipt.logs[0].args.dealId.toNumber();
let deal = await instance.getDeal.call(dealId);
assert.equal(deal.buyer, accounts[0]);
assert.equal(deal.seller, accounts[1]);
assert.equal(deal.guarantor, accounts[2]);
assert.equal(deal.amount, 200);
assert.equal(deal.approved, false);
await instance.approve(dealId, {from: accounts[2]})
deal = await instance.getDeal.call(dealId);
assert.equal(deal.approved, true);
})
it('should not approve not by guarantor', async() => {
const instance = await GuarantorDeal.deployed();
const receipt = await instance.create(accounts[1], accounts[2], {from: accounts[0], value: 200});
const dealId = receipt.logs[0].args.dealId.toNumber();
let deal = await instance.getDeal.call(dealId);
assert.equal(deal.buyer, accounts[0]);
assert.equal(deal.seller, accounts[1]);
assert.equal(deal.guarantor, accounts[2]);
assert.equal(deal.amount, 200);
assert.equal(deal.approved, false);
let thrown = false;
await instance.approve(dealId, {from: accounts[3]})
.catch(() => thrown = true)
assert.equal(thrown, true, "method has to be reverted")
deal = await instance.getDeal.call(dealId);
assert.equal(deal.approved, false);
})
it('should withdraw funds after approve', async() => {
const instance = await GuarantorDeal.deployed();
const dealAmount = web3.utils.toWei('0.3', 'ether');
const receipt = await instance.create(accounts[1], accounts[2], {from: accounts[0], value: dealAmount});
const dealId = receipt.logs[0].args.dealId.toNumber();
let deal = await instance.getDeal.call(dealId);
assert.equal(deal.buyer, accounts[0]);
assert.equal(deal.seller, accounts[1]);
assert.equal(deal.guarantor, accounts[2]);
assert.equal(deal.amount, dealAmount);
assert.equal(deal.approved, false);
await instance.approve(dealId, {from: accounts[2]})
deal = await instance.getDeal.call(dealId);
assert.equal(deal.approved, true);
const tracker = await balance.tracker(accounts[1]);
await instance.withdraw(dealId, {from: accounts[1]});
const { delta, fees } = await tracker.deltaWithFees();
assert.equal(web3.utils.fromWei(delta.add(fees), 'ether'), '0.3');
})
});