-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathMetadata.ts
More file actions
114 lines (99 loc) · 2.92 KB
/
Metadata.ts
File metadata and controls
114 lines (99 loc) · 2.92 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
import { add } from 'biggystring'
import { Disklet } from 'disklet'
import { EdgeLog } from 'edge-core-js/types'
import { makeMemlet } from 'memlet'
import AwaitLock from '../utxobased/engine/await-lock'
import { EngineEmitter, EngineEvent } from './EngineEmitter'
import { asLocalWalletMetadata, LocalWalletMetadata } from './types'
import { removeItem } from './utils'
const metadataPath = `metadata.json`
interface MetadataConfig {
disklet: Disklet
emitter: EngineEmitter
log: EdgeLog
}
export interface Metadata {
state: LocalWalletMetadata
clear: () => Promise<void>
}
export const makeMetadata = async (
config: MetadataConfig
): Promise<Metadata> => {
const { disklet, emitter, log } = config
const memlet = makeMemlet(disklet)
const lock = new AwaitLock()
const instance: Metadata = {
get state() {
return cache
},
clear: async () => {
await memlet.delete(metadataPath)
const cleanCache = await resetMetadata()
Object.assign(cache, cleanCache)
}
}
const updateWalletBalance = async (currencyCode: string): Promise<void> => {
const cumulativeBalance = Object.values(cache.addressBalances).reduce(
(sum, addressBalance) => add(sum, addressBalance),
'0'
)
cache.balance = cumulativeBalance
await setMetadata(cache)
emitter.emitWalletBalanceChanged(currencyCode, cumulativeBalance)
}
emitter.on(
EngineEvent.ADDRESS_BALANCE_CHANGED,
async (
currencyCode: string,
addressBalanceChanges: Array<{ scriptPubkey: string; balance: string }>
) => {
await lock.acquireAsync()
try {
addressBalanceChanges.forEach(({ scriptPubkey, balance }) => {
if (balance === '0') {
removeItem(cache.addressBalances, scriptPubkey)
} else {
cache.addressBalances[scriptPubkey] = balance
}
})
await updateWalletBalance(currencyCode)
} catch (err) {
log.error(err)
} finally {
lock.release()
}
}
)
emitter.on(
EngineEvent.BLOCK_HEIGHT_CHANGED,
async (_uri: string, height: number) => {
if (height > cache.lastSeenBlockHeight) {
cache.lastSeenBlockHeight = height
await setMetadata(cache)
}
}
)
const fetchMetadata = async (): Promise<LocalWalletMetadata> => {
try {
const metadata = await memlet.getJson(metadataPath)
return asLocalWalletMetadata(metadata)
} catch (err) {
log.error(err)
return await resetMetadata()
}
}
const resetMetadata = async (): Promise<LocalWalletMetadata> => {
const data: LocalWalletMetadata = {
balance: '0',
addressBalances: {},
lastSeenBlockHeight: 0
}
await memlet.setJson(metadataPath, data)
return data
}
const setMetadata = async (data: LocalWalletMetadata): Promise<void> => {
await memlet.setJson(metadataPath, data)
}
const cache = await fetchMetadata()
return instance
}