@ocap/statedb-fs 1.28.8 → 1.29.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -2
- package/esm/db.d.mts +29 -0
- package/esm/db.mjs +96 -0
- package/esm/index.d.mts +2 -0
- package/esm/index.mjs +3 -0
- package/esm/package.mjs +6 -0
- package/esm/table/account.d.mts +17 -0
- package/esm/table/account.mjs +27 -0
- package/esm/table/balance.d.mts +23 -0
- package/esm/table/balance.mjs +42 -0
- package/esm/table/base.d.mts +38 -0
- package/esm/table/base.mjs +108 -0
- package/esm/table/rollup.d.mts +14 -0
- package/esm/table/rollup.mjs +20 -0
- package/esm/table/token.d.mts +14 -0
- package/esm/table/token.mjs +17 -0
- package/lib/_virtual/rolldown_runtime.cjs +29 -0
- package/lib/db.cjs +98 -0
- package/lib/db.d.cts +29 -0
- package/lib/index.cjs +4 -0
- package/lib/index.d.cts +2 -0
- package/lib/package.cjs +18 -0
- package/lib/table/account.cjs +29 -0
- package/lib/table/account.d.cts +17 -0
- package/lib/table/balance.cjs +43 -0
- package/lib/table/balance.d.cts +23 -0
- package/lib/table/base.cjs +115 -0
- package/lib/table/base.d.cts +38 -0
- package/lib/table/rollup.cjs +21 -0
- package/lib/table/rollup.d.cts +14 -0
- package/lib/table/token.cjs +18 -0
- package/lib/table/token.d.cts +14 -0
- package/package.json +33 -9
- package/lib/db.js +0 -54
- package/lib/index.js +0 -1
- package/lib/table/account.js +0 -20
- package/lib/table/balance.js +0 -41
- package/lib/table/base.js +0 -150
- package/lib/table/rollup.js +0 -13
- package/lib/table/token.js +0 -13
package/README.md
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
# [**@ocap/statedb-fs**](https://github.com/arcblock/blockchain)
|
|
2
2
|
|
|
3
|
-
[](https://github.com/prettier/prettier)
|
|
4
3
|
|
|
5
4
|
> OCAP statedb adapter that stores data to the disk through lokijs
|
|
6
5
|
|
|
@@ -15,7 +14,7 @@ bun install @ocap/statedb-fs
|
|
|
15
14
|
## Usage
|
|
16
15
|
|
|
17
16
|
```js
|
|
18
|
-
const FsStateDB = require('@ocap/statedb-fs');
|
|
17
|
+
const FsStateDB = require('@ocap/statedb-fs').default;
|
|
19
18
|
const statedb = new FsStateDB(os.tmpdir());
|
|
20
19
|
|
|
21
20
|
statedb.account.create('123', { key: 'value' });
|
package/esm/db.d.mts
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { StateDB } from "@ocap/statedb";
|
|
2
|
+
import { IAccountState, IAssetFactoryState, IAssetState, IBalanceTable, IChainState, IDelegateState, IEvidenceState, IRollupBlock, IRollupTable, IStakeState, IStateTable, ITokenFactoryState, ITokenTable, ITxState } from "@ocap/types";
|
|
3
|
+
|
|
4
|
+
//#region src/db.d.ts
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* 文件系统 StateDB 实现
|
|
8
|
+
* 使用 LokiJS + FSAdapter 作为文件存储引擎
|
|
9
|
+
*/
|
|
10
|
+
declare class FsStateDB extends StateDB {
|
|
11
|
+
name: string;
|
|
12
|
+
version: string;
|
|
13
|
+
balance: IBalanceTable;
|
|
14
|
+
account: IStateTable<IAccountState>;
|
|
15
|
+
factory: IStateTable<IAssetFactoryState>;
|
|
16
|
+
stake: IStateTable<IStakeState>;
|
|
17
|
+
asset: IStateTable<IAssetState>;
|
|
18
|
+
delegation: IStateTable<IDelegateState>;
|
|
19
|
+
tx: IStateTable<ITxState>;
|
|
20
|
+
token: ITokenTable;
|
|
21
|
+
chain: IStateTable<IChainState>;
|
|
22
|
+
rollup: IRollupTable;
|
|
23
|
+
rollupBlock: IStateTable<IRollupBlock>;
|
|
24
|
+
evidence: IStateTable<IEvidenceState>;
|
|
25
|
+
tokenFactory: IStateTable<ITokenFactoryState>;
|
|
26
|
+
constructor(dataDir: string);
|
|
27
|
+
}
|
|
28
|
+
//#endregion
|
|
29
|
+
export { FsStateDB as default };
|
package/esm/db.mjs
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import { name, version } from "./package.mjs";
|
|
2
|
+
import base_default from "./table/base.mjs";
|
|
3
|
+
import account_default from "./table/account.mjs";
|
|
4
|
+
import balance_default from "./table/balance.mjs";
|
|
5
|
+
import rollup_default from "./table/rollup.mjs";
|
|
6
|
+
import token_default from "./table/token.mjs";
|
|
7
|
+
import { StateDB } from "@ocap/statedb";
|
|
8
|
+
|
|
9
|
+
//#region src/db.ts
|
|
10
|
+
/**
|
|
11
|
+
* 文件系统 StateDB 实现
|
|
12
|
+
* 使用 LokiJS + FSAdapter 作为文件存储引擎
|
|
13
|
+
*/
|
|
14
|
+
var FsStateDB = class extends StateDB {
|
|
15
|
+
constructor(dataDir) {
|
|
16
|
+
super();
|
|
17
|
+
this.name = name;
|
|
18
|
+
this.version = version;
|
|
19
|
+
this.balance = new balance_default({
|
|
20
|
+
name: "balance",
|
|
21
|
+
dataDir,
|
|
22
|
+
uniqIndex: ["address", "tokenAddress"]
|
|
23
|
+
});
|
|
24
|
+
this.account = new account_default({
|
|
25
|
+
name: "account",
|
|
26
|
+
dataDir,
|
|
27
|
+
uniqIndex: "address",
|
|
28
|
+
balanceTable: this.balance,
|
|
29
|
+
syncBalance: true
|
|
30
|
+
});
|
|
31
|
+
this.factory = new base_default({
|
|
32
|
+
name: "factory",
|
|
33
|
+
dataDir,
|
|
34
|
+
uniqIndex: "address",
|
|
35
|
+
syncBalance: true,
|
|
36
|
+
balanceTable: this.balance
|
|
37
|
+
});
|
|
38
|
+
this.stake = new base_default({
|
|
39
|
+
name: "stake",
|
|
40
|
+
dataDir,
|
|
41
|
+
uniqIndex: "address",
|
|
42
|
+
syncBalance: true,
|
|
43
|
+
balanceTable: this.balance
|
|
44
|
+
});
|
|
45
|
+
this.asset = new base_default({
|
|
46
|
+
name: "asset",
|
|
47
|
+
dataDir,
|
|
48
|
+
uniqIndex: "address"
|
|
49
|
+
});
|
|
50
|
+
this.delegation = new base_default({
|
|
51
|
+
name: "delegation",
|
|
52
|
+
dataDir,
|
|
53
|
+
uniqIndex: "address"
|
|
54
|
+
});
|
|
55
|
+
this.tx = new base_default({
|
|
56
|
+
name: "tx",
|
|
57
|
+
dataDir,
|
|
58
|
+
uniqIndex: "hash"
|
|
59
|
+
});
|
|
60
|
+
this.token = new token_default({
|
|
61
|
+
name: "token",
|
|
62
|
+
dataDir,
|
|
63
|
+
uniqIndex: "address"
|
|
64
|
+
});
|
|
65
|
+
this.chain = new base_default({
|
|
66
|
+
name: "chain",
|
|
67
|
+
dataDir,
|
|
68
|
+
uniqIndex: "address"
|
|
69
|
+
});
|
|
70
|
+
this.rollup = new rollup_default({
|
|
71
|
+
name: "rollup",
|
|
72
|
+
dataDir,
|
|
73
|
+
uniqIndex: "address"
|
|
74
|
+
});
|
|
75
|
+
this.rollupBlock = new base_default({
|
|
76
|
+
name: "rollupBlock",
|
|
77
|
+
dataDir,
|
|
78
|
+
uniqIndex: "hash"
|
|
79
|
+
});
|
|
80
|
+
this.evidence = new base_default({
|
|
81
|
+
name: "evidence",
|
|
82
|
+
dataDir,
|
|
83
|
+
uniqIndex: "hash"
|
|
84
|
+
});
|
|
85
|
+
this.tokenFactory = new base_default({
|
|
86
|
+
name: "tokenFactory",
|
|
87
|
+
dataDir,
|
|
88
|
+
uniqIndex: "address"
|
|
89
|
+
});
|
|
90
|
+
this.attachReadyListeners();
|
|
91
|
+
}
|
|
92
|
+
};
|
|
93
|
+
var db_default = FsStateDB;
|
|
94
|
+
|
|
95
|
+
//#endregion
|
|
96
|
+
export { db_default as default };
|
package/esm/index.d.mts
ADDED
package/esm/index.mjs
ADDED
package/esm/package.mjs
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import FsTable from "./base.mjs";
|
|
2
|
+
import { IAccountState, IOperationContext } from "@ocap/types";
|
|
3
|
+
|
|
4
|
+
//#region src/table/account.d.ts
|
|
5
|
+
interface AccountOperationContext extends IOperationContext {
|
|
6
|
+
traceMigration?: boolean;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Account 表
|
|
10
|
+
* 扩展基础表,增加账户迁移追踪功能
|
|
11
|
+
*/
|
|
12
|
+
declare class AccountTable extends FsTable<IAccountState> {
|
|
13
|
+
_get(address: string | Record<string, unknown>, context?: AccountOperationContext): Promise<IAccountState | null>;
|
|
14
|
+
_create(key: string | Record<string, unknown>, attrs?: Partial<IAccountState>, ctx?: IOperationContext): Promise<IAccountState>;
|
|
15
|
+
}
|
|
16
|
+
//#endregion
|
|
17
|
+
export { AccountTable as default };
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import base_default from "./base.mjs";
|
|
2
|
+
import { ensureChecksumAddress } from "@ocap/state/lib/states/account";
|
|
3
|
+
|
|
4
|
+
//#region src/table/account.ts
|
|
5
|
+
/**
|
|
6
|
+
* Account 表
|
|
7
|
+
* 扩展基础表,增加账户迁移追踪功能
|
|
8
|
+
*/
|
|
9
|
+
var AccountTable = class extends base_default {
|
|
10
|
+
async _get(address, context = {}) {
|
|
11
|
+
const { traceMigration = true, ...restContext } = context;
|
|
12
|
+
const current = await super._get(ensureChecksumAddress(address), restContext);
|
|
13
|
+
if (current && traceMigration && Array.isArray(current.migratedTo) && current.migratedTo.length) return this._get(current.migratedTo[0], context);
|
|
14
|
+
return current;
|
|
15
|
+
}
|
|
16
|
+
_create(key, attrs = {}, ctx) {
|
|
17
|
+
const address = ensureChecksumAddress(key);
|
|
18
|
+
return super._create(address, {
|
|
19
|
+
...attrs,
|
|
20
|
+
address
|
|
21
|
+
}, ctx);
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
var account_default = AccountTable;
|
|
25
|
+
|
|
26
|
+
//#endregion
|
|
27
|
+
export { account_default as default };
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import FsTable from "./base.mjs";
|
|
2
|
+
import { IBalanceState, IOperationContext } from "@ocap/types";
|
|
3
|
+
|
|
4
|
+
//#region src/table/balance.d.ts
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Balance 表
|
|
8
|
+
* 扩展基础表,增加余额管理功能
|
|
9
|
+
*/
|
|
10
|
+
declare class BalanceTable extends FsTable<IBalanceState> {
|
|
11
|
+
getBalance(address: string, _context?: IOperationContext): Promise<Record<string, string>>;
|
|
12
|
+
updateBalance({
|
|
13
|
+
address,
|
|
14
|
+
tokens,
|
|
15
|
+
context
|
|
16
|
+
}: {
|
|
17
|
+
address: string;
|
|
18
|
+
tokens: Record<string, string>;
|
|
19
|
+
context?: unknown;
|
|
20
|
+
}, ctx?: IOperationContext): Promise<Record<string, string>>;
|
|
21
|
+
}
|
|
22
|
+
//#endregion
|
|
23
|
+
export { BalanceTable as default };
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import base_default from "./base.mjs";
|
|
2
|
+
|
|
3
|
+
//#region src/table/balance.ts
|
|
4
|
+
/**
|
|
5
|
+
* Balance 表
|
|
6
|
+
* 扩展基础表,增加余额管理功能
|
|
7
|
+
*/
|
|
8
|
+
var BalanceTable = class extends base_default {
|
|
9
|
+
async getBalance(address, _context) {
|
|
10
|
+
return (await this.collection.find({ address }) || []).reduce((acc, token) => {
|
|
11
|
+
acc[token.tokenAddress] = token.balance;
|
|
12
|
+
return acc;
|
|
13
|
+
}, {});
|
|
14
|
+
}
|
|
15
|
+
async updateBalance({ address, tokens, context = {} }, ctx) {
|
|
16
|
+
if (!Object.keys(tokens).length) return {};
|
|
17
|
+
const tokenBalances = await this.getBalance(address);
|
|
18
|
+
const updatedTokens = Object.keys(tokens).filter((token) => tokenBalances[token] && tokenBalances[token] !== "0" || tokens[token] && tokens[token] !== "0").filter((token) => tokenBalances[token] !== tokens[token]);
|
|
19
|
+
await Promise.all(updatedTokens.map(async (token) => {
|
|
20
|
+
const key = {
|
|
21
|
+
address,
|
|
22
|
+
tokenAddress: token
|
|
23
|
+
};
|
|
24
|
+
if (tokenBalances[token]) await this.update(key, {
|
|
25
|
+
...key,
|
|
26
|
+
balance: tokens[token],
|
|
27
|
+
context
|
|
28
|
+
}, ctx);
|
|
29
|
+
else await this.create(key, {
|
|
30
|
+
...key,
|
|
31
|
+
balance: tokens[token],
|
|
32
|
+
context
|
|
33
|
+
}, ctx);
|
|
34
|
+
tokenBalances[token] = tokens[token];
|
|
35
|
+
}));
|
|
36
|
+
return tokens;
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
var balance_default = BalanceTable;
|
|
40
|
+
|
|
41
|
+
//#endregion
|
|
42
|
+
export { balance_default as default };
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { StateDBTable } from "@ocap/statedb";
|
|
2
|
+
import Lokijs from "lokijs";
|
|
3
|
+
import { IBalanceTable, IOperationContext } from "@ocap/types";
|
|
4
|
+
|
|
5
|
+
//#region src/table/base.d.ts
|
|
6
|
+
interface FsTableOptions {
|
|
7
|
+
name: string;
|
|
8
|
+
dataDir: string;
|
|
9
|
+
uniqIndex: string | string[];
|
|
10
|
+
balanceTable?: IBalanceTable;
|
|
11
|
+
syncBalance?: boolean;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* 文件系统表基类
|
|
15
|
+
* 使用 LokiJS + FSAdapter 作为底层存储
|
|
16
|
+
*/
|
|
17
|
+
declare class FsTable<T = unknown> extends StateDBTable<T> {
|
|
18
|
+
name: string;
|
|
19
|
+
dataDir: string;
|
|
20
|
+
collection: Lokijs.Collection | null;
|
|
21
|
+
balanceTable?: IBalanceTable;
|
|
22
|
+
syncBalance: boolean;
|
|
23
|
+
constructor({
|
|
24
|
+
name,
|
|
25
|
+
dataDir,
|
|
26
|
+
uniqIndex,
|
|
27
|
+
balanceTable,
|
|
28
|
+
syncBalance
|
|
29
|
+
}: FsTableOptions);
|
|
30
|
+
_create(key: string | Record<string, unknown>, attrs?: Partial<T>, _context?: IOperationContext): Promise<T>;
|
|
31
|
+
_get(key: string | Record<string, unknown>, _context?: IOperationContext): Promise<T | null>;
|
|
32
|
+
_history(_key?: string | Record<string, unknown>, _context?: IOperationContext): T[];
|
|
33
|
+
_update(key: string | Record<string, unknown>, updates: Partial<T>, _context?: IOperationContext): Promise<T>;
|
|
34
|
+
updateOrCreate(exist: unknown, state: Partial<T>, ctx?: IOperationContext): Promise<T>;
|
|
35
|
+
_reset(_context?: IOperationContext): void;
|
|
36
|
+
}
|
|
37
|
+
//#endregion
|
|
38
|
+
export { FsTable as default };
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import { name } from "../package.mjs";
|
|
2
|
+
import { StateDBTable } from "@ocap/statedb";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import omit from "lodash/omit.js";
|
|
5
|
+
import Lokijs from "lokijs";
|
|
6
|
+
import FsAdapter from "lokijs/src/loki-fs-structured-adapter.js";
|
|
7
|
+
import Debug from "debug";
|
|
8
|
+
|
|
9
|
+
//#region src/table/base.ts
|
|
10
|
+
const debug = Debug(name);
|
|
11
|
+
/**
|
|
12
|
+
* 文件系统表基类
|
|
13
|
+
* 使用 LokiJS + FSAdapter 作为底层存储
|
|
14
|
+
*/
|
|
15
|
+
var FsTable = class FsTable extends StateDBTable {
|
|
16
|
+
constructor({ name: name$1, dataDir, uniqIndex, balanceTable, syncBalance = false }) {
|
|
17
|
+
super(uniqIndex);
|
|
18
|
+
this.name = name$1;
|
|
19
|
+
this.dataDir = dataDir;
|
|
20
|
+
this.collection = null;
|
|
21
|
+
this.balanceTable = balanceTable;
|
|
22
|
+
this.syncBalance = syncBalance;
|
|
23
|
+
if (this.syncBalance && !this.balanceTable) throw new Error("balanceTable is required when syncBalance is true");
|
|
24
|
+
const adapter = new FsAdapter();
|
|
25
|
+
const db = new Lokijs(path.join(dataDir, `${name$1}.db`), {
|
|
26
|
+
adapter,
|
|
27
|
+
autoload: true,
|
|
28
|
+
autosave: true,
|
|
29
|
+
autosaveInterval: 1e3,
|
|
30
|
+
autoloadCallback: () => {
|
|
31
|
+
this.collection = db.getCollection(name$1);
|
|
32
|
+
if (this.collection === null) this.collection = db.addCollection(name$1, {
|
|
33
|
+
unique: [this.primaryKey],
|
|
34
|
+
clone: true
|
|
35
|
+
});
|
|
36
|
+
this.markReady();
|
|
37
|
+
}
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
async _create(key, attrs = {}, _context) {
|
|
41
|
+
const id = this.generatePrimaryKey(key);
|
|
42
|
+
if (await FsTable.prototype._get.call(this, id)) throw new Error(`${this.name} already exists: ${key}`);
|
|
43
|
+
debug(`insert ${this.name}`, attrs);
|
|
44
|
+
const insertAttrs = { ...attrs };
|
|
45
|
+
if (this.syncBalance) delete insertAttrs.tokens;
|
|
46
|
+
const result = await this.collection.insert({
|
|
47
|
+
[this.primaryKey]: id,
|
|
48
|
+
...insertAttrs
|
|
49
|
+
});
|
|
50
|
+
if (this.syncBalance && attrs.tokens) {
|
|
51
|
+
debug(`update balance for ${this.name}`, attrs);
|
|
52
|
+
result.tokens = await this.balanceTable.updateBalance({
|
|
53
|
+
address: attrs.address,
|
|
54
|
+
tokens: attrs.tokens,
|
|
55
|
+
context: attrs.context
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
return result;
|
|
59
|
+
}
|
|
60
|
+
async _get(key, _context) {
|
|
61
|
+
if (!key) return null;
|
|
62
|
+
const id = this.generatePrimaryKey(key);
|
|
63
|
+
const result = await this.collection.by(this.primaryKey, id);
|
|
64
|
+
if (result && this.syncBalance) {
|
|
65
|
+
const balance = await this.balanceTable.getBalance(result.address);
|
|
66
|
+
result.tokens = {
|
|
67
|
+
...result.tokens || {},
|
|
68
|
+
...balance
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
return result;
|
|
72
|
+
}
|
|
73
|
+
_history(_key, _context) {
|
|
74
|
+
return [];
|
|
75
|
+
}
|
|
76
|
+
async _update(key, updates, _context) {
|
|
77
|
+
const id = this.generatePrimaryKey(key);
|
|
78
|
+
const doc = await FsTable.prototype._get.call(this, id);
|
|
79
|
+
if (!doc) throw new Error(`${this.name} does not exists: ${key}`);
|
|
80
|
+
Object.assign(doc, updates);
|
|
81
|
+
const updateAttrs = { ...doc };
|
|
82
|
+
if (this.syncBalance) delete updateAttrs.tokens;
|
|
83
|
+
await this.collection.update(updateAttrs);
|
|
84
|
+
if (this.syncBalance && doc.tokens) {
|
|
85
|
+
debug(`update balance for ${this.name}`, doc);
|
|
86
|
+
doc.tokens = await this.balanceTable.updateBalance({
|
|
87
|
+
address: doc.address,
|
|
88
|
+
tokens: doc.tokens,
|
|
89
|
+
context: doc.context
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
return doc;
|
|
93
|
+
}
|
|
94
|
+
updateOrCreate(exist, state, ctx) {
|
|
95
|
+
const id = this.generatePrimaryKey(state);
|
|
96
|
+
const attrs = omit(state, this.primaryKey);
|
|
97
|
+
if (!id) throw new Error("Cannot update or create without uniq index");
|
|
98
|
+
if (exist) return this.update(id, attrs, ctx);
|
|
99
|
+
return this.create(id, attrs, ctx);
|
|
100
|
+
}
|
|
101
|
+
_reset(_context) {
|
|
102
|
+
this.collection.removeWhere({});
|
|
103
|
+
}
|
|
104
|
+
};
|
|
105
|
+
var base_default = FsTable;
|
|
106
|
+
|
|
107
|
+
//#endregion
|
|
108
|
+
export { base_default as default };
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import FsTable from "./base.mjs";
|
|
2
|
+
import { IRollupState } from "@ocap/types";
|
|
3
|
+
|
|
4
|
+
//#region src/table/rollup.d.ts
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Rollup 表
|
|
8
|
+
* 扩展基础表,增加按代币查询功能
|
|
9
|
+
*/
|
|
10
|
+
declare class RollupTable extends FsTable<IRollupState> {
|
|
11
|
+
existByToken(token?: string | null): Promise<boolean>;
|
|
12
|
+
}
|
|
13
|
+
//#endregion
|
|
14
|
+
export { RollupTable as default };
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import base_default from "./base.mjs";
|
|
2
|
+
|
|
3
|
+
//#region src/table/rollup.ts
|
|
4
|
+
/**
|
|
5
|
+
* Rollup 表
|
|
6
|
+
* 扩展基础表,增加按代币查询功能
|
|
7
|
+
*/
|
|
8
|
+
var RollupTable = class extends base_default {
|
|
9
|
+
async existByToken(token) {
|
|
10
|
+
if (!token) return false;
|
|
11
|
+
return this.collection.count({
|
|
12
|
+
tokenAddress: token,
|
|
13
|
+
paused: false
|
|
14
|
+
}) > 0;
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
var rollup_default = RollupTable;
|
|
18
|
+
|
|
19
|
+
//#endregion
|
|
20
|
+
export { rollup_default as default };
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import FsTable from "./base.mjs";
|
|
2
|
+
import { ITokenState } from "@ocap/types";
|
|
3
|
+
|
|
4
|
+
//#region src/table/token.d.ts
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Token 表
|
|
8
|
+
* 扩展基础表,增加按符号查询功能
|
|
9
|
+
*/
|
|
10
|
+
declare class TokenTable extends FsTable<ITokenState> {
|
|
11
|
+
existBySymbol(symbol?: string | null): Promise<boolean>;
|
|
12
|
+
}
|
|
13
|
+
//#endregion
|
|
14
|
+
export { TokenTable as default };
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import base_default from "./base.mjs";
|
|
2
|
+
|
|
3
|
+
//#region src/table/token.ts
|
|
4
|
+
/**
|
|
5
|
+
* Token 表
|
|
6
|
+
* 扩展基础表,增加按符号查询功能
|
|
7
|
+
*/
|
|
8
|
+
var TokenTable = class extends base_default {
|
|
9
|
+
async existBySymbol(symbol) {
|
|
10
|
+
if (!symbol) throw new Error("param symbol is required");
|
|
11
|
+
return this.collection.count({ symbol }) > 0;
|
|
12
|
+
}
|
|
13
|
+
};
|
|
14
|
+
var token_default = TokenTable;
|
|
15
|
+
|
|
16
|
+
//#endregion
|
|
17
|
+
export { token_default as default };
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
//#region rolldown:runtime
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __copyProps = (to, from, except, desc) => {
|
|
9
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
10
|
+
for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
|
|
11
|
+
key = keys[i];
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except) {
|
|
13
|
+
__defProp(to, key, {
|
|
14
|
+
get: ((k) => from[k]).bind(null, key),
|
|
15
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
return to;
|
|
21
|
+
};
|
|
22
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
23
|
+
value: mod,
|
|
24
|
+
enumerable: true
|
|
25
|
+
}) : target, mod));
|
|
26
|
+
|
|
27
|
+
//#endregion
|
|
28
|
+
|
|
29
|
+
exports.__toESM = __toESM;
|
package/lib/db.cjs
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
2
|
+
const require_rolldown_runtime = require('./_virtual/rolldown_runtime.cjs');
|
|
3
|
+
const require_package = require('./package.cjs');
|
|
4
|
+
const require_table_base = require('./table/base.cjs');
|
|
5
|
+
const require_table_account = require('./table/account.cjs');
|
|
6
|
+
const require_table_balance = require('./table/balance.cjs');
|
|
7
|
+
const require_table_rollup = require('./table/rollup.cjs');
|
|
8
|
+
const require_table_token = require('./table/token.cjs');
|
|
9
|
+
let _ocap_statedb = require("@ocap/statedb");
|
|
10
|
+
|
|
11
|
+
//#region src/db.ts
|
|
12
|
+
/**
|
|
13
|
+
* 文件系统 StateDB 实现
|
|
14
|
+
* 使用 LokiJS + FSAdapter 作为文件存储引擎
|
|
15
|
+
*/
|
|
16
|
+
var FsStateDB = class extends _ocap_statedb.StateDB {
|
|
17
|
+
constructor(dataDir) {
|
|
18
|
+
super();
|
|
19
|
+
this.name = require_package.name;
|
|
20
|
+
this.version = require_package.version;
|
|
21
|
+
this.balance = new require_table_balance.default({
|
|
22
|
+
name: "balance",
|
|
23
|
+
dataDir,
|
|
24
|
+
uniqIndex: ["address", "tokenAddress"]
|
|
25
|
+
});
|
|
26
|
+
this.account = new require_table_account.default({
|
|
27
|
+
name: "account",
|
|
28
|
+
dataDir,
|
|
29
|
+
uniqIndex: "address",
|
|
30
|
+
balanceTable: this.balance,
|
|
31
|
+
syncBalance: true
|
|
32
|
+
});
|
|
33
|
+
this.factory = new require_table_base.default({
|
|
34
|
+
name: "factory",
|
|
35
|
+
dataDir,
|
|
36
|
+
uniqIndex: "address",
|
|
37
|
+
syncBalance: true,
|
|
38
|
+
balanceTable: this.balance
|
|
39
|
+
});
|
|
40
|
+
this.stake = new require_table_base.default({
|
|
41
|
+
name: "stake",
|
|
42
|
+
dataDir,
|
|
43
|
+
uniqIndex: "address",
|
|
44
|
+
syncBalance: true,
|
|
45
|
+
balanceTable: this.balance
|
|
46
|
+
});
|
|
47
|
+
this.asset = new require_table_base.default({
|
|
48
|
+
name: "asset",
|
|
49
|
+
dataDir,
|
|
50
|
+
uniqIndex: "address"
|
|
51
|
+
});
|
|
52
|
+
this.delegation = new require_table_base.default({
|
|
53
|
+
name: "delegation",
|
|
54
|
+
dataDir,
|
|
55
|
+
uniqIndex: "address"
|
|
56
|
+
});
|
|
57
|
+
this.tx = new require_table_base.default({
|
|
58
|
+
name: "tx",
|
|
59
|
+
dataDir,
|
|
60
|
+
uniqIndex: "hash"
|
|
61
|
+
});
|
|
62
|
+
this.token = new require_table_token.default({
|
|
63
|
+
name: "token",
|
|
64
|
+
dataDir,
|
|
65
|
+
uniqIndex: "address"
|
|
66
|
+
});
|
|
67
|
+
this.chain = new require_table_base.default({
|
|
68
|
+
name: "chain",
|
|
69
|
+
dataDir,
|
|
70
|
+
uniqIndex: "address"
|
|
71
|
+
});
|
|
72
|
+
this.rollup = new require_table_rollup.default({
|
|
73
|
+
name: "rollup",
|
|
74
|
+
dataDir,
|
|
75
|
+
uniqIndex: "address"
|
|
76
|
+
});
|
|
77
|
+
this.rollupBlock = new require_table_base.default({
|
|
78
|
+
name: "rollupBlock",
|
|
79
|
+
dataDir,
|
|
80
|
+
uniqIndex: "hash"
|
|
81
|
+
});
|
|
82
|
+
this.evidence = new require_table_base.default({
|
|
83
|
+
name: "evidence",
|
|
84
|
+
dataDir,
|
|
85
|
+
uniqIndex: "hash"
|
|
86
|
+
});
|
|
87
|
+
this.tokenFactory = new require_table_base.default({
|
|
88
|
+
name: "tokenFactory",
|
|
89
|
+
dataDir,
|
|
90
|
+
uniqIndex: "address"
|
|
91
|
+
});
|
|
92
|
+
this.attachReadyListeners();
|
|
93
|
+
}
|
|
94
|
+
};
|
|
95
|
+
var db_default = FsStateDB;
|
|
96
|
+
|
|
97
|
+
//#endregion
|
|
98
|
+
exports.default = db_default;
|
package/lib/db.d.cts
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { StateDB } from "@ocap/statedb";
|
|
2
|
+
import { IAccountState, IAssetFactoryState, IAssetState, IBalanceTable, IChainState, IDelegateState, IEvidenceState, IRollupBlock, IRollupTable, IStakeState, IStateTable, ITokenFactoryState, ITokenTable, ITxState } from "@ocap/types";
|
|
3
|
+
|
|
4
|
+
//#region src/db.d.ts
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* 文件系统 StateDB 实现
|
|
8
|
+
* 使用 LokiJS + FSAdapter 作为文件存储引擎
|
|
9
|
+
*/
|
|
10
|
+
declare class FsStateDB extends StateDB {
|
|
11
|
+
name: string;
|
|
12
|
+
version: string;
|
|
13
|
+
balance: IBalanceTable;
|
|
14
|
+
account: IStateTable<IAccountState>;
|
|
15
|
+
factory: IStateTable<IAssetFactoryState>;
|
|
16
|
+
stake: IStateTable<IStakeState>;
|
|
17
|
+
asset: IStateTable<IAssetState>;
|
|
18
|
+
delegation: IStateTable<IDelegateState>;
|
|
19
|
+
tx: IStateTable<ITxState>;
|
|
20
|
+
token: ITokenTable;
|
|
21
|
+
chain: IStateTable<IChainState>;
|
|
22
|
+
rollup: IRollupTable;
|
|
23
|
+
rollupBlock: IStateTable<IRollupBlock>;
|
|
24
|
+
evidence: IStateTable<IEvidenceState>;
|
|
25
|
+
tokenFactory: IStateTable<ITokenFactoryState>;
|
|
26
|
+
constructor(dataDir: string);
|
|
27
|
+
}
|
|
28
|
+
//#endregion
|
|
29
|
+
export { FsStateDB as default };
|
package/lib/index.cjs
ADDED
package/lib/index.d.cts
ADDED
package/lib/package.cjs
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
|
|
2
|
+
//#region package.json
|
|
3
|
+
var name = "@ocap/statedb-fs";
|
|
4
|
+
var version = "1.20.2";
|
|
5
|
+
|
|
6
|
+
//#endregion
|
|
7
|
+
Object.defineProperty(exports, 'name', {
|
|
8
|
+
enumerable: true,
|
|
9
|
+
get: function () {
|
|
10
|
+
return name;
|
|
11
|
+
}
|
|
12
|
+
});
|
|
13
|
+
Object.defineProperty(exports, 'version', {
|
|
14
|
+
enumerable: true,
|
|
15
|
+
get: function () {
|
|
16
|
+
return version;
|
|
17
|
+
}
|
|
18
|
+
});
|