forked from rohenaz/bmap-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathactions.ts
154 lines (140 loc) · 3.57 KB
/
actions.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
import { bmap } from 'bmapjs'
import chalk from 'chalk'
import { getDbo } from './db.js'
let confirmedTxs = []
const { TransformTx } = bmap
const saveTx = async (tx) => {
let t
let dbo
// Transform
try {
dbo = await getDbo()
} catch (e) {
// await closeDb()
let txid = tx && tx.tx ? tx.tx.h : undefined
throw new Error('Failed to get dbo ' + txid + ' : ' + e)
}
try {
t = await TransformTx(tx)
} catch (e) {
throw new Error('Failed to transform tx ' + tx)
}
if (t) {
let collection = t.blk ? 'c' : 'u'
let txId = tx && tx.tx ? tx.tx.h : undefined
t._id = txId
try {
let timestamp = t.timestamp
delete t.timestamp
console.log({ tx })
await dbo.collection(collection).updateOne(
{ _id: t._id },
{
$set: t,
$setOnInsert: {
timestamp: timestamp || Math.round(new Date().getTime() / 1000),
},
},
{
upsert: true,
}
)
// if this was a confirmed tx, make sure its not in mempool
if (t.blk) {
confirmedTxs.push(t.tx.h)
}
return t
} catch (e) {
console.log('not inserted', e)
console.log(
collection === 'u'
? (chalk.green('saved'), chalk.magenta('unconfirmed'))
: '',
(chalk.cyan('saved'), chalk.green(t.tx.h))
)
throw new Error('Failed to insert tx ' + txId + ' : ' + e)
}
} else {
throw new Error('Invalid tx')
}
}
const clearConfirmed = (txIds: string[]) => {
return new Promise<void>(async (res, rej) => {
let dbo = await getDbo()
try {
let collection = 'u'
await dbo.collection(collection).deleteMany({ _id: { $in: txIds } })
console.log('txids removed from unconfirmed collection', txIds)
// ToDo - This can throw errors during sync
// await dbo.collection('u').drop(async function (err, delOK) {
// if (err) {
// // await closeDb()
// rej(err)
// return
// }
// if (delOK) res()
// })
// await closeDb()
res()
} catch (e) {
// await closeDb()
rej(e)
}
})
}
const clearUnconfirmed = () => {
return new Promise<void>(async (res, rej) => {
let dbo = await getDbo()
dbo
.listCollections({ name: 'u' })
.toArray(async function (err, collections) {
if (
collections
.map((c) => {
return c.name
})
.indexOf('u') !== -1
) {
try {
// ToDo - This can throw errors during sync
await dbo.collection('u').drop(async function (err, delOK) {
if (err) {
// await closeDb()
rej(err)
return
}
if (delOK) res()
})
// await closeDb()
res()
} catch (e) {
// await closeDb()
rej(e)
}
}
})
})
}
export { saveTx, clearUnconfirmed, clearConfirmed }
const bapApiUrl = `https://bap-api.com/v1`
const getBAPIdByAddress = async function (address, block, timestamp) {
if (bapApiUrl) {
const result = await fetch(`${bapApiUrl}/identity/validByAddress`, {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({
address,
block,
timestamp,
}),
})
const data = await result.json()
if (data && data.status === 'OK' && data.result) {
return data.result
}
}
return false
}