-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtransactions.js
48 lines (45 loc) · 1.21 KB
/
transactions.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
import { postSelect, postInsert } from './postgres'
import { isValidNanoAddress } from './nano'
import config from './config'
const { DATABASE } = config
export function getTransactionsByAddress(address) {
if (!address) {
throw new Error('No address')
}
if (!isValidNanoAddress(address)) {
throw new Error('Invalid address')
}
return postSelect(
DATABASE.transactionsTable,
{
address: address
},
['*'],
'ORDER BY created_at DESC'
)
}
export function addTransaction(tx) {
if (!tx) {
throw new Error('No transaction')
}
if (!tx.hasOwnProperty('address') || !isValidNanoAddress(tx.address)) {
throw new Error('Missing or invalid address')
}
if (!tx.hasOwnProperty('link') || !isValidNanoAddress(tx.link)) {
throw new Error('Missing or invalid `link` address')
}
if (
!tx.hasOwnProperty('type') ||
!tx.hasOwnProperty('nano_value') ||
!tx.hasOwnProperty('currency') ||
!tx.hasOwnProperty('fiat_value') ||
!tx.hasOwnProperty('send_block') ||
!tx.hasOwnProperty('token')
) {
throw new Error('Transaction is missing a required attribute')
}
return postInsert(DATABASE.transactionsTable, {
...tx,
created_at: new Date()
})
}