@ercworldio/blockchain-shared 1.0.0-dev.26 → 1.0.0-dev.28
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.
|
@@ -15,7 +15,11 @@ declare class BalanceService extends BaseErrors {
|
|
|
15
15
|
private config;
|
|
16
16
|
private logger;
|
|
17
17
|
constructor(dbPool: IDatabasePool, config: IConfig);
|
|
18
|
-
|
|
18
|
+
/**
|
|
19
|
+
* Creates the balance row with zero balance if it doesnt exist.
|
|
20
|
+
* Used for wallets that were partially initialized
|
|
21
|
+
*/
|
|
22
|
+
private getOrCreateEntity;
|
|
19
23
|
getBalance(userId: number, blockchain: string, chainId: ChainId, paymentToken: string): Promise<GetWalletBalanceResult>;
|
|
20
24
|
getBalancesNeedSweepByToken(blockchain: BlockchainType, chainId: ChainId, paymentToken: string): Promise<DepositWalletBalance[]>;
|
|
21
25
|
removeWalletBalances(walletId: number, blockchain: string): Promise<boolean>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"BalanceService.d.ts","sourceRoot":"","sources":["../../src/services/BalanceService.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,oBAAoB,EAAE,MAAM,kCAAkC,CAAC;AACxE,OAAO,EAAE,cAAc,EAAgB,OAAO,EAAgB,WAAW,EAAE,cAAc,EAAE,MAAM,eAAe,CAAC;AAOjH,OAAO,EAAE,aAAa,EAAE,MAAM,wBAAwB,CAAC;AACvD,OAAO,EAAE,OAAO,EAAE,MAAM,sBAAsB,CAAC;AAC/C,OAAO,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AACvC,OAAO,UAAU,MAAM,kBAAkB,CAAC;AAI1C,KAAK,aAAa,GAAG,KAAK,GAAG,QAAQ,CAAC;AAEtC,UAAU,sBAAsB;IAC5B,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,EAAE,MAAM,CAAC;IACf,QAAQ,EAAE,MAAM,CAAC;CACpB;AAED,cAAM,cAAe,SAAQ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2BzD,wBAAwB,CAAC,QAAQ,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM;IAI7D,kBAAkB,CAAC,QAAQ,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM;YAmD/C,aAAa;IAwBd,mBAAmB,CAAC,MAAM,EAAE,YAAY,EAAE;IA0EjD,aAAa,CAAC,MAAM,EAAE,MAAM,EAAE,UAAU,EAAE,cAAc,EAAE,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,OAAO,EAAE,YAAY,EAAE,MAAM,EAAE,WAAW,EAAE,MAAM,EAAE,aAAa,EAAE,aAAa;;;;IA2FpK,qBAAqB,CAAC,SAAS,EAAE,MAAM,EAAE,UAAU,EAAE,cAAc,EAAE,cAAc,EAAE,MAAM,EAAE,aAAa,GAAE,OAAe;IAmE3H,cAAc,CAAC,MAAM,EAAE,MAAM,EAAE,UAAU,EAAE,cAAc;IAUlD,uBAAuB;IAuBvB,kBAAkB,CAAC,KAAK,GAAE,MAAY;YAOrC,gBAAgB;IAoBxB,yBAAyB,CAAC,mBAAmB,EAAE,WAAW,EAAE;IAY5D,6BAA6B,CAAC,mBAAmB,EAAE,cAAc,EAAE;CA8D5E;AAED,eAAe,cAAc,CAAC"}
|
|
@@ -28,24 +28,45 @@ class BalanceService extends errors_1.default {
|
|
|
28
28
|
this.config = config;
|
|
29
29
|
this.logger = (0, Logger_1.createLogger)(`BalanceService`, this.config.pipelineConfig.azureAppName);
|
|
30
30
|
}
|
|
31
|
-
|
|
31
|
+
/**
|
|
32
|
+
* Creates the balance row with zero balance if it doesnt exist.
|
|
33
|
+
* Used for wallets that were partially initialized
|
|
34
|
+
*/
|
|
35
|
+
getOrCreateEntity(userId, blockchain, chainId, paymentToken) {
|
|
32
36
|
return __awaiter(this, void 0, void 0, function* () {
|
|
37
|
+
var _a, _b;
|
|
33
38
|
const dbDriver = this.dbPool;
|
|
34
39
|
if (!dbDriver.balancesRepo || !dbDriver.accountsRepo) {
|
|
35
40
|
throw new Error(`Db Driver appDataSource is not initialized.`);
|
|
36
41
|
}
|
|
37
42
|
const accountsService = new DepositAddressService_1.default(this.dbPool);
|
|
38
|
-
const
|
|
39
|
-
if (!
|
|
43
|
+
const account = yield accountsService.findByUserId(userId, blockchain);
|
|
44
|
+
if (!account) {
|
|
40
45
|
throw new Error(`Account not found for userId ${userId} on ${blockchain} chain`);
|
|
41
46
|
}
|
|
42
|
-
const
|
|
43
|
-
|
|
47
|
+
const normalizedToken = (0, Wallets_1.normalizeAddress)(paymentToken, blockchain);
|
|
48
|
+
const existing = yield dbDriver.balancesRepo.findOne({
|
|
49
|
+
where: { blockchain, chain_id: chainId, wallet_id: account.id, token: normalizedToken }
|
|
44
50
|
});
|
|
45
|
-
if (
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
51
|
+
if (existing)
|
|
52
|
+
return existing;
|
|
53
|
+
// Entity is missing, wallet initialization was probably interrupted
|
|
54
|
+
// Create it with zero balance so the update doesnt fail
|
|
55
|
+
const chainManager = ChainManager_1.default.getInstance(this.config);
|
|
56
|
+
const tokenConfig = (_a = chainManager.getPaymentTokensForChain(parseInt(chainId))) === null || _a === void 0 ? void 0 : _a.find(t => (0, Wallets_1.normalizeAddress)(t.address, blockchain) === normalizedToken);
|
|
57
|
+
this.logger.warn(`Balance entity missing for userId=${userId} blockchain=${blockchain} chainId=${chainId} token=${normalizedToken} — creating with zero balance`);
|
|
58
|
+
const entity = new DepositWalletBalance_1.DepositWalletBalance();
|
|
59
|
+
entity.blockchain = blockchain;
|
|
60
|
+
entity.chain_id = chainId;
|
|
61
|
+
entity.wallet_id = account.id;
|
|
62
|
+
entity.token = normalizedToken;
|
|
63
|
+
entity.decimals = (_b = tokenConfig === null || tokenConfig === void 0 ? void 0 : tokenConfig.decimals) !== null && _b !== void 0 ? _b : 0;
|
|
64
|
+
entity.balance = BigInt(0);
|
|
65
|
+
entity.transaction_count = 0;
|
|
66
|
+
entity.needs_sweep = false;
|
|
67
|
+
entity.created_at = new Date();
|
|
68
|
+
entity.updated_at = new Date();
|
|
69
|
+
return dbDriver.balancesRepo.save(entity);
|
|
49
70
|
});
|
|
50
71
|
}
|
|
51
72
|
// Gets the token balance of the wallet on the given chain
|
|
@@ -212,7 +233,7 @@ class BalanceService extends errors_1.default {
|
|
|
212
233
|
for (const wallet of walletMap.values()) {
|
|
213
234
|
const { balance: currentBalance, decimals } = yield (0, custodial_1.getWalletBalance)(wallet.blockchain, wallet.chainId, wallet.depositWallet, wallet.tokenAddress, this.dbPool, this.config);
|
|
214
235
|
// Load entity from DB
|
|
215
|
-
const entity = yield this.
|
|
236
|
+
const entity = yield this.getOrCreateEntity(wallet.userId, wallet.blockchain, wallet.chainId, wallet.tokenAddress);
|
|
216
237
|
// Update balance
|
|
217
238
|
entity.balance = currentBalance;
|
|
218
239
|
// Optional: check sweep thresholds (reuse your current logic)
|
|
@@ -265,7 +286,7 @@ class BalanceService extends errors_1.default {
|
|
|
265
286
|
if (!account) {
|
|
266
287
|
throw new Error(`Account not found for userId ${userId} on ${blockchain} chain`);
|
|
267
288
|
}
|
|
268
|
-
const entity = yield this.
|
|
289
|
+
const entity = yield this.getOrCreateEntity(userId, blockchain, chainId, paymentToken);
|
|
269
290
|
let needs_sweep = false;
|
|
270
291
|
switch (operationType) {
|
|
271
292
|
case "add":
|