-
Notifications
You must be signed in to change notification settings - Fork 1
/
ledger.js
60 lines (49 loc) · 1.41 KB
/
ledger.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
const Memory = require('./adapters/memory');
const { Account } = require('./account');
const { Transaction } = require('./transaction');
class Ledger {
constructor ({ adapter = new Memory() } = {}) {
Object.defineProperty(this, 'adapter', {
configurable: true,
get: () => adapter,
});
}
async populate (accounts = [], options) {
// await this.adapter._connect(this);
await this.insertAccounts(this, accounts, options);
}
async post (tx, options) {
if (tx instanceof Transaction === false) {
tx = new Transaction(tx, this.adapter);
}
await tx.validate(options);
await this.adapter._post(tx, options);
return tx.trace;
}
/**
* Get account by code
*
* @param {string} code
*/
async getAccount (code, options) {
let rawAccount = await this.adapter._get(code, options);
if (!rawAccount) {
return;
}
return new Account(rawAccount, this.adapter);
}
getEntries (criteria, options) {
return this.adapter._entries(criteria, options);
}
async insertAccounts (parent, accounts = [], options) {
for (let def of accounts) {
let account = new Account(def);
account.parent = parent.code || '';
await this.adapter._connect(account, options);
if (def.children && def.children.length) {
await this.insertAccounts(account, def.children, options);
}
}
}
}
module.exports = { Ledger };