-
Notifications
You must be signed in to change notification settings - Fork 59
/
base.js
55 lines (48 loc) · 1.18 KB
/
base.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
class Base {
constructor(epochClient) {
this.client = epochClient
}
async getBlockHeight() {
const result = await this.client.get ('top')
return result.data.height
}
async waitForBlock(height, timeout = 5000) {
return await new Promise (
(resolve, reject) => {
let interval = setInterval (async () => {
let currentHeight = await this.getBlockHeight ()
if (currentHeight >= height) {
clearInterval (interval)
resolve (currentHeight)
}
}, timeout)
}
)
}
async waitNBlocks(delta) {
let currentHeight = await this.getBlockHeight ()
let resultBlock = await this.waitForBlock (currentHeight + delta)
return resultBlock
}
async spend(recipient, amount, fee) {
let success
try {
const result = await client.post ('spend-tx', {
'recipient_pubkey': recipient,
amount,
fee
})
success = true
if (result.data === {}) success = true
} catch (e) {
console.error (e)
success = false
}
if (success) {
return amount
} else {
throw `Could not transfer ${amount}`
}
}
}
module.exports = Base