@ocap/statedb-memory 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/lib/db.d.cts ADDED
@@ -0,0 +1,31 @@
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
+ import Loki from "lokijs";
4
+
5
+ //#region src/db.d.ts
6
+
7
+ /**
8
+ * 内存 StateDB 实现
9
+ * 使用 LokiJS 作为内存存储引擎
10
+ */
11
+ declare class MemoryStateDB extends StateDB {
12
+ name: string;
13
+ version: string;
14
+ db: Loki;
15
+ balance: IBalanceTable;
16
+ account: IStateTable<IAccountState>;
17
+ factory: IStateTable<IAssetFactoryState>;
18
+ stake: IStateTable<IStakeState>;
19
+ asset: IStateTable<IAssetState>;
20
+ delegation: IStateTable<IDelegateState>;
21
+ tx: IStateTable<ITxState>;
22
+ token: ITokenTable;
23
+ chain: IStateTable<IChainState>;
24
+ rollup: IRollupTable;
25
+ rollupBlock: IStateTable<IRollupBlock>;
26
+ evidence: IStateTable<IEvidenceState>;
27
+ tokenFactory: IStateTable<ITokenFactoryState>;
28
+ constructor();
29
+ }
30
+ //#endregion
31
+ export { MemoryStateDB as default };
package/lib/index.cjs ADDED
@@ -0,0 +1,4 @@
1
+ Object.defineProperty(exports, '__esModule', { value: true });
2
+ const require_db = require('./db.cjs');
3
+
4
+ exports.default = require_db.default;
@@ -0,0 +1,2 @@
1
+ import MemoryStateDB from "./db.cjs";
2
+ export { MemoryStateDB as default };
@@ -0,0 +1,81 @@
1
+
2
+ //#region package.json
3
+ var name = "@ocap/statedb-memory";
4
+ var package_default = {
5
+ name,
6
+ description: "OCAP statedb adapter that uses memory as backend statedb, just for test purpose",
7
+ version: "1.20.2",
8
+ author: "wangshijun <shijun@arcblock.io> (https://www.arcblock.io)",
9
+ bugs: {
10
+ "url": "https://github.com/ArcBlock/blockchain/issues",
11
+ "email": "shijun@arcblock.io"
12
+ },
13
+ publishConfig: { "access": "public" },
14
+ contributors: ["wangshijun <shijun@arcblock.io> (https://www.arcblock.io)"],
15
+ devDependencies: { "@types/lokijs": "^1.5.14" },
16
+ homepage: "https://github.com/ArcBlock/blockchain/tree/master/statedb/memory",
17
+ keywords: [
18
+ "ocap",
19
+ "statedb",
20
+ "memory"
21
+ ],
22
+ license: "Apache-2.0",
23
+ type: "module",
24
+ main: "./lib/index.cjs",
25
+ module: "./esm/index.mjs",
26
+ types: "./esm/index.d.mts",
27
+ exports: {
28
+ ".": {
29
+ "types": "./esm/index.d.mts",
30
+ "import": "./esm/index.mjs",
31
+ "default": "./lib/index.cjs"
32
+ },
33
+ "./lib/*.js": {
34
+ "types": "./esm/*.d.mts",
35
+ "import": "./esm/*.mjs",
36
+ "default": "./lib/*.cjs"
37
+ },
38
+ "./lib/*": {
39
+ "types": "./esm/*.d.mts",
40
+ "import": "./esm/*.mjs",
41
+ "default": "./lib/*.cjs"
42
+ }
43
+ },
44
+ files: ["lib", "esm"],
45
+ repository: {
46
+ "type": "git",
47
+ "url": "https://github.com/ArcBlock/blockchain/tree/master/statedb/memory"
48
+ },
49
+ scripts: {
50
+ "build": "tsdown",
51
+ "prebuild": "rm -rf lib esm",
52
+ "lint": "biome check",
53
+ "lint:fix": "biome check --write",
54
+ "test": "bun test",
55
+ "coverage": "npm run test -- --coverage"
56
+ },
57
+ gitHead: "87990c8b5e215107fc587c1ced0d6b3e2cd2483e",
58
+ dependencies: {
59
+ "@ocap/state": "workspace:*",
60
+ "@ocap/statedb": "workspace:*",
61
+ "@ocap/types": "workspace:*",
62
+ "@ocap/util": "workspace:*",
63
+ "debug": "^4.4.3",
64
+ "lodash": "^4.17.23",
65
+ "lokijs": "^1.5.12"
66
+ }
67
+ };
68
+
69
+ //#endregion
70
+ Object.defineProperty(exports, 'default', {
71
+ enumerable: true,
72
+ get: function () {
73
+ return package_default;
74
+ }
75
+ });
76
+ Object.defineProperty(exports, 'name', {
77
+ enumerable: true,
78
+ get: function () {
79
+ return name;
80
+ }
81
+ });
@@ -0,0 +1,29 @@
1
+ Object.defineProperty(exports, '__esModule', { value: true });
2
+ const require_rolldown_runtime = require('../_virtual/rolldown_runtime.cjs');
3
+ const require_table_base = require('./base.cjs');
4
+ let _ocap_state_lib_states_account = require("@ocap/state/lib/states/account");
5
+
6
+ //#region src/table/account.ts
7
+ /**
8
+ * Account 表
9
+ * 扩展基础表,增加账户迁移追踪功能
10
+ */
11
+ var AccountStateDB = class extends require_table_base.default {
12
+ async _get(address, context = {}) {
13
+ const { traceMigration = true, ...restContext } = context;
14
+ const current = await super._get((0, _ocap_state_lib_states_account.ensureChecksumAddress)(address), restContext);
15
+ if (current && traceMigration && Array.isArray(current.migratedTo) && current.migratedTo.length) return this._get(current.migratedTo[0], context);
16
+ return current;
17
+ }
18
+ _create(key, attrs = {}, context) {
19
+ const address = (0, _ocap_state_lib_states_account.ensureChecksumAddress)(key);
20
+ return super._create(address, {
21
+ ...attrs,
22
+ [this.uniqIndex]: address
23
+ }, context);
24
+ }
25
+ };
26
+ var account_default = AccountStateDB;
27
+
28
+ //#endregion
29
+ exports.default = account_default;
@@ -0,0 +1,17 @@
1
+ import MemoryTable from "./base.cjs";
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 AccountStateDB extends MemoryTable<IAccountState> {
13
+ _get(address: string, context?: AccountOperationContext): Promise<IAccountState | null>;
14
+ _create(key: string, attrs?: Partial<IAccountState>, context?: IOperationContext): Promise<IAccountState>;
15
+ }
16
+ //#endregion
17
+ export { AccountStateDB as default };
@@ -0,0 +1,45 @@
1
+ Object.defineProperty(exports, '__esModule', { value: true });
2
+ const require_rolldown_runtime = require('../_virtual/rolldown_runtime.cjs');
3
+ const require_table_base = require('./base.cjs');
4
+ let _ocap_state_lib_states_account = require("@ocap/state/lib/states/account");
5
+
6
+ //#region src/table/balance.ts
7
+ /**
8
+ * Balance 表
9
+ * 扩展基础表,增加余额管理功能
10
+ */
11
+ var BalanceTable = class extends require_table_base.default {
12
+ async getBalance(address, _context) {
13
+ return (await this.collection.find({ address: (0, _ocap_state_lib_states_account.ensureChecksumAddress)(address) }) || []).reduce((acc, token) => {
14
+ acc[token.tokenAddress] = token.balance;
15
+ return acc;
16
+ }, {});
17
+ }
18
+ async updateBalance({ address, tokens, context = {} }, ctx) {
19
+ if (!Object.keys(tokens).length) return {};
20
+ const tokenBalances = await this.getBalance(address);
21
+ const updatedTokens = Object.keys(tokens).filter((token) => tokenBalances[token] && tokenBalances[token] !== "0" || tokens[token] && tokens[token] !== "0").filter((token) => tokenBalances[token] !== tokens[token]);
22
+ await Promise.all(updatedTokens.map(async (token) => {
23
+ const key = {
24
+ address,
25
+ tokenAddress: token
26
+ };
27
+ if (tokenBalances[token]) await this.update(key, {
28
+ ...key,
29
+ balance: tokens[token],
30
+ context
31
+ }, ctx);
32
+ else await this.create(key, {
33
+ ...key,
34
+ balance: tokens[token],
35
+ context
36
+ }, ctx);
37
+ tokenBalances[token] = tokens[token];
38
+ }));
39
+ return tokens;
40
+ }
41
+ };
42
+ var balance_default = BalanceTable;
43
+
44
+ //#endregion
45
+ exports.default = balance_default;
@@ -0,0 +1,23 @@
1
+ import MemoryTable from "./base.cjs";
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 MemoryTable<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,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
+ let _ocap_statedb = require("@ocap/statedb");
5
+ let debug = require("debug");
6
+ debug = require_rolldown_runtime.__toESM(debug);
7
+ let lodash_omit = require("lodash/omit");
8
+ lodash_omit = require_rolldown_runtime.__toESM(lodash_omit);
9
+
10
+ //#region src/table/base.ts
11
+ const debug$1 = (0, debug.default)(require_package.name);
12
+ /**
13
+ * 内存表基类
14
+ * 使用 LokiJS Collection 作为底层存储
15
+ */
16
+ var MemoryTable = class MemoryTable extends _ocap_statedb.StateDBTable {
17
+ constructor({ name: name$1, uniqIndex, balanceTable, syncBalance = false, db }) {
18
+ super(uniqIndex);
19
+ this.name = name$1;
20
+ this.balanceTable = balanceTable;
21
+ this.syncBalance = syncBalance;
22
+ if (this.syncBalance && !this.balanceTable) throw new Error("balanceTable is required when syncBalance is true");
23
+ if (!db) throw new Error("db is required for MemoryTable");
24
+ this.collection = db.addCollection(name$1, {
25
+ unique: [this.primaryKey],
26
+ clone: true
27
+ });
28
+ this.markReady();
29
+ }
30
+ async _create(key, attrs = {}, _context) {
31
+ const id = this.generatePrimaryKey(key);
32
+ if (await MemoryTable.prototype._get.call(this, id)) throw new Error(`${this.name} already exists: ${key}`);
33
+ debug$1(`insert ${this.name}`, attrs);
34
+ const insertAttrs = { ...attrs };
35
+ if (this.syncBalance) delete insertAttrs.tokens;
36
+ const result = await this.collection.insert({
37
+ [this.primaryKey]: id,
38
+ ...insertAttrs
39
+ });
40
+ if (this.syncBalance && attrs.tokens) {
41
+ debug$1(`update balance for ${this.name}`, attrs);
42
+ result.tokens = await this.balanceTable.updateBalance({
43
+ address: attrs.address,
44
+ tokens: attrs.tokens,
45
+ context: attrs.context
46
+ });
47
+ }
48
+ return result;
49
+ }
50
+ async _get(key, _context) {
51
+ if (!key) return null;
52
+ const id = this.generatePrimaryKey(key);
53
+ const result = await this.collection.by(this.primaryKey, id) ?? null;
54
+ if (result && this.syncBalance) {
55
+ const balance = await this.balanceTable.getBalance(result.address);
56
+ result.tokens = {
57
+ ...result.tokens || {},
58
+ ...balance
59
+ };
60
+ }
61
+ return result;
62
+ }
63
+ _history(_key, _context) {
64
+ return [];
65
+ }
66
+ async _update(key, updates, _context) {
67
+ const id = this.generatePrimaryKey(key);
68
+ const doc = await MemoryTable.prototype._get.call(this, id);
69
+ if (!doc) throw new Error(`${this.name} does not exists: ${key}`);
70
+ Object.assign(doc, updates);
71
+ const updateAttrs = { ...doc };
72
+ if (this.syncBalance) delete updateAttrs.tokens;
73
+ await this.collection.update(updateAttrs);
74
+ if (this.syncBalance && doc.tokens) {
75
+ debug$1(`update balance for ${this.name}`, doc);
76
+ doc.tokens = await this.balanceTable.updateBalance({
77
+ address: doc.address,
78
+ tokens: doc.tokens,
79
+ context: doc.context
80
+ });
81
+ }
82
+ return doc;
83
+ }
84
+ updateOrCreate(exist, state, ctx) {
85
+ const id = this.generatePrimaryKey(state);
86
+ const attrs = (0, lodash_omit.default)(state, this.uniqIndex ?? []);
87
+ if (!id) throw new Error("Cannot update or create without uniq index");
88
+ if (exist) return this.update(id, attrs, ctx);
89
+ return this.create(id, attrs, ctx);
90
+ }
91
+ _reset(_context) {
92
+ this.collection.removeWhere({});
93
+ }
94
+ };
95
+ var base_default = MemoryTable;
96
+
97
+ //#endregion
98
+ exports.default = base_default;
@@ -0,0 +1,37 @@
1
+ import { StateDBTable } from "@ocap/statedb";
2
+ import { IBalanceTable, IOperationContext } from "@ocap/types";
3
+ import Loki from "lokijs";
4
+
5
+ //#region src/table/base.d.ts
6
+ interface TableOptions {
7
+ name: string;
8
+ uniqIndex: string | string[];
9
+ syncBalance?: boolean;
10
+ balanceTable?: IBalanceTable;
11
+ db: Loki;
12
+ }
13
+ /**
14
+ * 内存表基类
15
+ * 使用 LokiJS Collection 作为底层存储
16
+ */
17
+ declare class MemoryTable<T = unknown> extends StateDBTable<T> {
18
+ name: string;
19
+ balanceTable?: IBalanceTable;
20
+ syncBalance: boolean;
21
+ collection: Collection<LokiObj & Record<string, unknown>>;
22
+ constructor({
23
+ name,
24
+ uniqIndex,
25
+ balanceTable,
26
+ syncBalance,
27
+ db
28
+ }: TableOptions);
29
+ _create(key: string | Record<string, unknown>, attrs?: Partial<T>, _context?: IOperationContext): Promise<T>;
30
+ _get(key: string | Record<string, unknown>, _context?: IOperationContext): Promise<T | null>;
31
+ _history(_key?: string, _context?: IOperationContext): T[];
32
+ _update(key: string | Record<string, unknown>, updates: Partial<T>, _context?: IOperationContext): Promise<T>;
33
+ updateOrCreate(exist: unknown, state: Partial<T>, ctx?: IOperationContext): Promise<T>;
34
+ _reset(_context?: IOperationContext): void;
35
+ }
36
+ //#endregion
37
+ export { MemoryTable as default };
@@ -0,0 +1,21 @@
1
+ Object.defineProperty(exports, '__esModule', { value: true });
2
+ const require_table_base = require('./base.cjs');
3
+
4
+ //#region src/table/rollup.ts
5
+ /**
6
+ * Rollup 表
7
+ * 扩展基础表,增加按代币查询功能
8
+ */
9
+ var RollupTable = class extends require_table_base.default {
10
+ async existByToken(token) {
11
+ if (!token) return false;
12
+ return this.collection.count({
13
+ tokenAddress: token,
14
+ paused: false
15
+ }) > 0;
16
+ }
17
+ };
18
+ var rollup_default = RollupTable;
19
+
20
+ //#endregion
21
+ exports.default = rollup_default;
@@ -0,0 +1,14 @@
1
+ import MemoryTable from "./base.cjs";
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 MemoryTable<IRollupState> {
11
+ existByToken(token?: string): Promise<boolean>;
12
+ }
13
+ //#endregion
14
+ export { RollupTable as default };
@@ -0,0 +1,18 @@
1
+ Object.defineProperty(exports, '__esModule', { value: true });
2
+ const require_table_base = require('./base.cjs');
3
+
4
+ //#region src/table/token.ts
5
+ /**
6
+ * Token 表
7
+ * 扩展基础表,增加按符号查询功能
8
+ */
9
+ var TokenTable = class extends require_table_base.default {
10
+ async existBySymbol(symbol) {
11
+ if (!symbol) throw new Error("param symbol is required");
12
+ return this.collection.count({ symbol }) > 0;
13
+ }
14
+ };
15
+ var token_default = TokenTable;
16
+
17
+ //#endregion
18
+ exports.default = token_default;
@@ -0,0 +1,14 @@
1
+ import MemoryTable from "./base.cjs";
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 MemoryTable<ITokenState> {
11
+ existBySymbol(symbol: string): Promise<boolean>;
12
+ }
13
+ //#endregion
14
+ export { TokenTable as default };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@ocap/statedb-memory",
3
3
  "description": "OCAP statedb adapter that uses memory as backend statedb, just for test purpose",
4
- "version": "1.28.8",
4
+ "version": "1.29.0",
5
5
  "author": "wangshijun <shijun@arcblock.io> (https://www.arcblock.io)",
6
6
  "bugs": {
7
7
  "url": "https://github.com/ArcBlock/blockchain/issues",
@@ -13,7 +13,9 @@
13
13
  "contributors": [
14
14
  "wangshijun <shijun@arcblock.io> (https://www.arcblock.io)"
15
15
  ],
16
- "devDependencies": {},
16
+ "devDependencies": {
17
+ "@types/lokijs": "^1.5.14"
18
+ },
17
19
  "homepage": "https://github.com/ArcBlock/blockchain/tree/master/statedb/memory",
18
20
  "keywords": [
19
21
  "ocap",
@@ -21,15 +23,38 @@
21
23
  "memory"
22
24
  ],
23
25
  "license": "Apache-2.0",
24
- "main": "./lib/index.js",
26
+ "type": "module",
27
+ "main": "./lib/index.cjs",
28
+ "module": "./esm/index.mjs",
29
+ "types": "./esm/index.d.mts",
30
+ "exports": {
31
+ ".": {
32
+ "types": "./esm/index.d.mts",
33
+ "import": "./esm/index.mjs",
34
+ "default": "./lib/index.cjs"
35
+ },
36
+ "./lib/*.js": {
37
+ "types": "./esm/*.d.mts",
38
+ "import": "./esm/*.mjs",
39
+ "default": "./lib/*.cjs"
40
+ },
41
+ "./lib/*": {
42
+ "types": "./esm/*.d.mts",
43
+ "import": "./esm/*.mjs",
44
+ "default": "./lib/*.cjs"
45
+ }
46
+ },
25
47
  "files": [
26
- "lib"
48
+ "lib",
49
+ "esm"
27
50
  ],
28
51
  "repository": {
29
52
  "type": "git",
30
53
  "url": "https://github.com/ArcBlock/blockchain/tree/master/statedb/memory"
31
54
  },
32
55
  "scripts": {
56
+ "build": "tsdown",
57
+ "prebuild": "rm -rf lib esm",
33
58
  "lint": "biome check",
34
59
  "lint:fix": "biome check --write",
35
60
  "test": "bun test",
@@ -37,11 +62,12 @@
37
62
  },
38
63
  "gitHead": "87990c8b5e215107fc587c1ced0d6b3e2cd2483e",
39
64
  "dependencies": {
40
- "@ocap/state": "1.28.8",
41
- "@ocap/statedb": "1.28.8",
42
- "@ocap/util": "1.28.8",
43
- "debug": "^4.3.6",
44
- "lodash": "^4.17.21",
65
+ "@ocap/state": "1.29.0",
66
+ "@ocap/statedb": "1.29.0",
67
+ "@ocap/types": "1.29.0",
68
+ "@ocap/util": "1.29.0",
69
+ "debug": "^4.4.3",
70
+ "lodash": "^4.17.23",
45
71
  "lokijs": "^1.5.12"
46
72
  }
47
73
  }
package/lib/db.js DELETED
@@ -1,60 +0,0 @@
1
- const { StateDB } = require('@ocap/statedb');
2
- const Lokijs = require('lokijs');
3
- const Table = require('./table/base');
4
- const Account = require('./table/account');
5
- const Token = require('./table/token');
6
- const Rollup = require('./table/rollup');
7
- const Balance = require('./table/balance');
8
-
9
- const { name, version } = require('../package.json');
10
-
11
- let instanceCounter = 0;
12
-
13
- class MemoryStateDB extends StateDB {
14
- constructor() {
15
- super();
16
-
17
- this.name = name;
18
- this.version = version;
19
-
20
- // Each instance has its own LokiJS database to ensure test isolation
21
- instanceCounter += 1;
22
- this.db = new Lokijs(`ocap-memory-statedb-${instanceCounter}.db`);
23
-
24
- this.balance = new Balance({ name: 'balance', uniqIndex: ['address', 'tokenAddress'], db: this.db });
25
- this.account = new Account({
26
- name: 'account',
27
- uniqIndex: 'address',
28
- balanceTable: this.balance,
29
- syncBalance: true,
30
- db: this.db,
31
- });
32
- this.factory = new Table({
33
- name: 'factory',
34
- uniqIndex: 'address',
35
- syncBalance: true,
36
- balanceTable: this.balance,
37
- db: this.db,
38
- });
39
- this.stake = new Table({
40
- name: 'stake',
41
- uniqIndex: 'address',
42
- syncBalance: true,
43
- balanceTable: this.balance,
44
- db: this.db,
45
- });
46
- this.asset = new Table({ name: 'asset', uniqIndex: 'address', db: this.db });
47
- this.delegation = new Table({ name: 'delegation', uniqIndex: 'address', db: this.db });
48
- this.tx = new Table({ name: 'tx', uniqIndex: 'hash', db: this.db });
49
- this.token = new Token({ name: 'token', uniqIndex: 'address', db: this.db });
50
- this.chain = new Table({ name: 'chain', uniqIndex: 'address', db: this.db });
51
- this.rollup = new Rollup({ name: 'rollup', uniqIndex: 'address', db: this.db });
52
- this.rollupBlock = new Table({ name: 'rollupBlock', uniqIndex: 'hash', db: this.db });
53
- this.evidence = new Table({ name: 'evidence', uniqIndex: 'hash', db: this.db });
54
- this.tokenFactory = new Table({ name: 'tokenFactory', uniqIndex: 'address', db: this.db });
55
-
56
- this.attachReadyListeners();
57
- }
58
- }
59
-
60
- module.exports = MemoryStateDB;
package/lib/index.js DELETED
@@ -1 +0,0 @@
1
- module.exports = require('./db');
@@ -1,20 +0,0 @@
1
- const { ensureChecksumAddress } = require('@ocap/state/lib/states/account');
2
- const MemoryStateDB = require('./base');
3
-
4
- class AccountStateDB extends MemoryStateDB {
5
- async _get(address, { traceMigration = true } = {}) {
6
- const current = await super._get(ensureChecksumAddress(address));
7
- if (current && traceMigration && Array.isArray(current.migratedTo) && current.migratedTo.length) {
8
- return this._get(current.migratedTo[0]);
9
- }
10
-
11
- return current;
12
- }
13
-
14
- _create(key, attrs = {}) {
15
- const address = ensureChecksumAddress(key);
16
- return super._create(address, { ...attrs, [this.uniqIndex]: address });
17
- }
18
- }
19
-
20
- module.exports = AccountStateDB;
@@ -1,41 +0,0 @@
1
- const { ensureChecksumAddress } = require('@ocap/state/lib/states/account');
2
- const MemoryStateDB = require('./base');
3
-
4
- class BalanceTable extends MemoryStateDB {
5
- async getBalance(address) {
6
- const tokens = await this.collection.find({ address: ensureChecksumAddress(address) });
7
- return (tokens || []).reduce((acc, token) => {
8
- acc[token.tokenAddress] = token.balance;
9
- return acc;
10
- }, {});
11
- }
12
-
13
- async updateBalance({ address, tokens, context = {} }, ctx) {
14
- if (!Object.keys(tokens).length) return {};
15
-
16
- const tokenBalances = await this.getBalance(address);
17
- const updatedTokens = Object.keys(tokens)
18
- .filter(
19
- (token) => (tokenBalances[token] && tokenBalances[token] !== '0') || (tokens[token] && tokens[token] !== '0')
20
- )
21
- .filter((token) => tokenBalances[token] !== tokens[token]);
22
-
23
- await Promise.all(
24
- updatedTokens.map(async (token) => {
25
- const key = { address, tokenAddress: token };
26
-
27
- if (tokenBalances[token]) {
28
- await this.update(key, { ...key, balance: tokens[token], context }, ctx);
29
- } else {
30
- await this.create(key, { ...key, balance: tokens[token], context }, ctx);
31
- }
32
-
33
- tokenBalances[token] = tokens[token];
34
- })
35
- );
36
-
37
- return tokens;
38
- }
39
- }
40
-
41
- module.exports = BalanceTable;