-
Notifications
You must be signed in to change notification settings - Fork 6
/
cutxo.test.js
122 lines (104 loc) · 3.79 KB
/
cutxo.test.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
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
/* eslint-env jest */
const Client = require("bitcoin-core");
const { construct, broadcast } = require("./cutxo");
const sleep = (time) => new Promise((resolve) => setTimeout(resolve, time));
describe("Trades", () => {
const client = new Client({
host: "localhost",
port: 43782,
username: "root",
password: "toor",
});
beforeAll(async () => {
// wait till the wallet is ready
let ready = false
while (!ready) {
try {
await client.ping();
} catch (e) {
if (e.code === -28) {
await sleep(1000);
continue;
}
throw e;
}
ready = true
}
await client.generateToAddress(110, await client.getNewAddress());
});
afterAll(async () => {
await client.stop();
});
it("empty unspent", async () => {
expect(
construct({ client, maximumAmount: 0.001, feeRate: 1 })
).rejects.toThrow("No suitable UTXO found");
});
it("3 to one", async () => {
const send = {};
for (let type of ["legacy", "p2sh-segwit", "bech32"]) {
const address = await client.getNewAddress("", type);
send[address] = 0.001;
}
await client.sendMany("", send);
await client.generateToAddress(1, await client.getNewAddress());
const tx = await construct({ client, maximumAmount: 0.001, feeRate: 1 });
expect(tx.amountInput).toBe(0.003);
expect(tx.amountOutput).toBeLessThan(tx.amountInput);
expect(typeof tx.address).toBe("string");
expect(tx.fee).toBeLessThan(0.001);
expect(typeof tx.hex).toBe("string");
expect(tx.inputsTotal).toBe(3);
expect(tx.inputsUsed).toBe(3);
const txid = await broadcast({ client, hex: tx.hex });
expect(typeof txid).toBe("string");
await client.generateToAddress(1, await client.getNewAddress());
const unspent = await client.listUnspent(1, 9999999, [], true, {
maximumAmount: 0.001,
});
expect(unspent).toHaveLength(0);
});
it("1500 inputs", async () => {
let send = {};
for (let i of [...Array(1500)].map((_, i) => i + 1)) {
const address = await client.getNewAddress();
send[address] = 0.001;
if (i % 500 === 0) {
console.info('sendMany')
await client.sendMany("", send);
send = {};
}
}
await client.generateToAddress(1, await client.getNewAddress());
// first transaction
const tx = await construct({
client,
maximumAmount: 0.001,
feeRate: 1,
});
expect(tx.amountInput).toBeGreaterThan(0.8);
expect(tx.amountInput).toBeLessThan(1.3);
expect(tx.amountOutput).toBeLessThan(tx.amountInput);
expect(typeof tx.address).toBe("string");
expect(typeof tx.fee).toBe("number");
expect(typeof tx.hex).toBe("string");
expect(tx.inputsTotal).toBe(1500);
expect(tx.inputsUsed).toBeGreaterThan(800);
expect(tx.inputsUsed).toBeLessThan(1300);
const txid = await broadcast({ client, hex: tx.hex });
expect(typeof txid).toBe("string");
// second transaction
const tx2 = await construct({
client,
maximumAmount: 0.001,
feeRate: 1,
});
await broadcast({ client, hex: tx2.hex });
await client.generateToAddress(1, await client.getNewAddress());
// after secod tx unspent should be 0
const unspent = await client.listUnspent(1, 9999999, [], true, {
maximumAmount: 0.001,
});
expect(unspent).toHaveLength(0);
});
});