/
refundAction.ts
223 lines (188 loc) · 9.1 KB
/
refundAction.ts
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
import {expect} from 'chai';
import 'mocha';
import requestArtifacts from 'requestnetworkartifacts';
import RequestNetwork from '../../../src/requestNetwork';
import * as utils from '../../utils';
const WEB3 = require('web3');
const BN = WEB3.utils.BN;
const addressRequestEthereum = requestArtifacts('private', 'last-RequestEthereum').networks.private.address;
const addressRequestCore = requestArtifacts('private', 'last-RequestCore').networks.private.address;
let rn: any;
let web3: any;
let defaultAccount: string;
let payer: string;
let payee: string;
let otherGuy: string;
let coreVersion: any;
let currentNumRequest: any;
let requestId: any;
describe('refundAction', () => {
const arbitraryAmount = 100000000;
rn = new RequestNetwork('http://localhost:8545', 10000000000, false);
web3 = rn.requestEthereumService.web3Single.web3;
beforeEach(async () => {
const accounts = await web3.eth.getAccounts();
defaultAccount = accounts[0].toLowerCase();
payer = accounts[2].toLowerCase();
payee = accounts[3].toLowerCase();
otherGuy = accounts[4].toLowerCase();
currentNumRequest = await rn.requestCoreService.getCurrentNumRequest();
const req = await rn.requestEthereumService.createRequestAsPayee(
payer,
arbitraryAmount,
'',
'',
[],
{from: payee});
requestId = req.request.requestId;
});
it('payBack request', async () => {
await rn.requestEthereumService.accept(
requestId,
{from: payer});
const result0 = await rn.requestEthereumService.paymentAction(
requestId,
arbitraryAmount,
0,
{from: payer});
const result = await rn.requestEthereumService.refundAction(
requestId,
arbitraryAmount,
{from: payee})
.on('broadcasted', (data: any) => {
expect(data.transaction, 'data.transaction.hash is wrong').to.have.property('hash');
});
utils.expectEqualsBN(result.request.expectedAmount, arbitraryAmount, 'expectedAmount is wrong');
utils.expectEqualsBN(result.request.balance, 0, 'balance is wrong');
expect(result.request.creator.toLowerCase(), 'creator is wrong').to.equal(payee);
expect(result.request.extension, 'extension is wrong').to.be.undefined;
expect(result.request.payee.toLowerCase(), 'payee is wrong').to.equal(payee);
expect(result.request.payer.toLowerCase(), 'payer is wrong').to.equal(payer);
expect(result.request.requestId, 'requestId is wrong').to.equal(utils.getRequestId(addressRequestCore, ++currentNumRequest));
expect(result.request.state, 'state is wrong').to.equal(1);
expect(result.request.currencyContract.address.toLowerCase(), 'currencyContract is wrong').to.equal(addressRequestEthereum);
expect(result.transaction, 'result.transaction.hash is wrong').to.have.property('hash');
});
it('payBack request not fully', async () => {
await rn.requestEthereumService.accept(
requestId,
{from: payer});
await rn.requestEthereumService.paymentAction(
requestId,
arbitraryAmount,
0,
{from: payer});
const result = await rn.requestEthereumService.refundAction(
requestId,
10,
{from: payee})
.on('broadcasted', (data: any) => {
expect(data.transaction, 'data.transaction.hash is wrong').to.have.property('hash');
});
utils.expectEqualsBN(result.request.expectedAmount, arbitraryAmount, 'expectedAmount is wrong');
utils.expectEqualsBN(result.request.balance, arbitraryAmount-10, 'balance is wrong');
expect(result.request.creator.toLowerCase(), 'creator is wrong').to.equal(payee);
expect(result.request.extension, 'extension is wrong').to.be.undefined;
expect(result.request.payee.toLowerCase(), 'payee is wrong').to.equal(payee);
expect(result.request.payer.toLowerCase(), 'payer is wrong').to.equal(payer);
expect(result.request.requestId, 'requestId is wrong').to.equal(utils.getRequestId(addressRequestCore, ++currentNumRequest));
expect(result.request.state, 'state is wrong').to.equal(1);
expect(result.request.currencyContract.address.toLowerCase(), 'currencyContract is wrong').to.equal(addressRequestEthereum);
expect(result.transaction, 'result.transaction.hash is wrong').to.have.property('hash');
});
it('payback request with not valid requestId', async () => {
const result = await rn.requestEthereumService.accept(
requestId,
{from: payer});
await rn.requestEthereumService.paymentAction(
requestId,
arbitraryAmount,
0,
{from: payer});
try {
const result = await rn.requestEthereumService.refundAction(
'0x00000000000000',
arbitraryAmount,
{from: payer});
expect(false, 'exception not thrown').to.be.true;
} catch (e) {
utils.expectEqualsObject(e, Error('_requestId must be a 32 bytes hex string (eg.: \'0x0000000000000000000000000000000000000000000000000000000000000000\''),'exception not right');
}
});
it('payback request with not valid amount', async () => {
const result = await rn.requestEthereumService.accept(
requestId,
{from: payer});
await rn.requestEthereumService.paymentAction(
requestId,
arbitraryAmount,
0,
{from: payer});
try {
const result = await rn.requestEthereumService.refundAction(
requestId,
-1,
{from: payee});
expect(false, 'exception not thrown').to.be.true;
} catch (e) {
utils.expectEqualsObject(e, Error('_amount must a positive integer'),'exception not right');
}
});
it('payback request by payer', async () => {
const result = await rn.requestEthereumService.accept(
requestId,
{from: payer});
await rn.requestEthereumService.paymentAction(
requestId,
arbitraryAmount,
0,
{from: payer});
try {
const result = await rn.requestEthereumService.refundAction(
requestId,
arbitraryAmount,
{from: payer});
expect(false, 'exception not thrown').to.be.true;
} catch (e) {
utils.expectEqualsObject(e, Error('account must be payee'),'exception not right');
}
});
it('payback request by otherGuy', async () => {
const result = await rn.requestEthereumService.accept(
requestId,
{from: payer});
await rn.requestEthereumService.paymentAction(
requestId,
arbitraryAmount,
0,
{from: payer});
try {
const result = await rn.requestEthereumService.refundAction(
requestId,
arbitraryAmount,
{from: otherGuy});
expect(false, 'exception not thrown').to.be.true;
} catch (e) {
utils.expectEqualsObject(e, Error('account must be payee'),'exception not right');
}
});
it('payback request by otherGuy', async () => {
const result = await rn.requestEthereumService.accept(
requestId,
{from: payer});
await rn.requestEthereumService.paymentAction(
requestId,
10,
0,
{from: payer});
try {
const result = await rn.requestEthereumService.refundAction(
requestId,
11,
{from: otherGuy});
expect(false, 'exception not thrown').to.be.true;
} catch (e) {
utils.expectEqualsObject(e, Error('You cannot payback more than what has been paid'),'exception not right');
}
});
});