-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathtest.spec.ts
More file actions
116 lines (94 loc) · 3.8 KB
/
test.spec.ts
File metadata and controls
116 lines (94 loc) · 3.8 KB
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
import { readFileSync } from "fs";
describe('Evaluate submission', () => {
let txid: string;
let minerInputAddress: string;
let minerInputAmount: number;
let traderInputAddress: string;
let traderInputAmount: number;
let minerChangeAddress: string;
let minerChangeAmount: number;
let fee: number;
let blockHeight: number;
let blockHash: string;
let tx: any;
it('should read data from out.txt and perform sanity checks', () => {
// read txid from out.txt
const data = readFileSync('out.txt', 'utf8').trim().split('\n');
expect(data.length).toBe(10);
txid = data[0].trim();
expect(txid).toBeDefined();
expect(txid).toHaveLength(64);
minerInputAddress = data[1].trim();
expect(minerInputAddress).toBeDefined();
minerInputAmount = parseFloat(data[2].trim());
expect(minerInputAmount).toBeDefined();
expect(minerInputAmount).toBeGreaterThan(0);
traderInputAddress = data[3].trim();
expect(traderInputAddress).toBeDefined();
traderInputAmount = parseFloat(data[4].trim());
expect(traderInputAmount).toBeDefined();
expect(traderInputAmount).toBeGreaterThan(0);
minerChangeAddress = data[5].trim();
expect(minerChangeAddress).toBeDefined();
minerChangeAmount = parseFloat(data[6].trim());
expect(minerChangeAmount).toBeDefined();
expect(minerChangeAmount).toBeGreaterThan(0);
fee = parseFloat(data[7].trim());
expect(fee).toBeDefined();
if (fee < 0) fee = -fee;
expect(fee).toBeGreaterThan(0);
blockHeight = parseInt(data[8].trim());
expect(blockHeight).toBeDefined();
expect(blockHeight).toBeGreaterThan(0);
blockHash = data[9].trim();
expect(blockHash).toBeDefined();
expect(blockHash).toHaveLength(64);
});
it('should get transaction details from node', async () => {
const RPC_USER = "alice";
const RPC_PASSWORD = "password";
const RPC_HOST = "http://127.0.0.1:18443/wallet/Miner";
const response = await fetch(RPC_HOST, {
method: 'post',
body: JSON.stringify({
jsonrpc: '1.0',
id: 'curltest',
method: 'gettransaction',
params: [txid, null, true]
}),
headers: {
'Content-Type': 'text/plain',
'Authorization': 'Basic ' + Buffer.from(`${RPC_USER}:${RPC_PASSWORD}`).toString('base64'),
}
});
const result = (await response.json()).result as any;
expect(result).not.toBeNull();
expect(result.txid).toBe(txid);
tx = result;
});
it('should have the correct block height', () => {
expect(tx.blockheight).toBe(blockHeight);
});
it('should have the correct block hash', () => {
expect(tx.blockhash).toBe(blockHash);
});
it('should have the correct number of vins', () => {
expect(tx.decoded.vin.length).toBe(1);
});
it('should have the correct number of vouts', () => {
expect(tx.decoded.vout.length).toBe(2);
});
it('should have the correct miner output', () => {
const minerOutput = tx.decoded.vout.find((o: any) => o.scriptPubKey.address.includes(minerChangeAddress));
expect(minerOutput).toBeDefined();
expect(minerOutput.value).toBe(minerChangeAmount);
});
it('should have the correct trader output', () => {
const traderOutput = tx.decoded.vout.find((o: any) => o.scriptPubKey.address.includes(traderInputAddress));
expect(traderOutput).toBeDefined();
expect(traderOutput.value).toBe(traderInputAmount);
});
it('should have the correct fee', () => {
expect(Math.abs(tx.fee)).toBe(fee);
});
});