Skip to content

Commit 91ea6a0

Browse files
committed
fixes after review
1 parent 901954c commit 91ea6a0

File tree

7 files changed

+16
-344
lines changed

7 files changed

+16
-344
lines changed

src/endpoints/accounts-v2/account.controller.v2.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import { Controller, Get, NotFoundException, Param, Query, UseInterceptors } from '@nestjs/common';
22
import { ApiOkResponse, ApiOperation, ApiQuery, ApiTags } from '@nestjs/swagger';
33
import { AccountServiceV2 } from './account.service.v2';
4-
import { AccountDetailed } from './entities/account.detailed';
5-
import { ParseAddressPipe, ParseBoolPipe, ParseIntPipe } from '@multiversx/sdk-nestjs-common';
4+
import { ParseAddressPipe, ParseBoolPipe } from '@multiversx/sdk-nestjs-common';
65
import { DeepHistoryInterceptor } from 'src/interceptors/deep-history.interceptor';
7-
import { AccountFetchOptions } from './entities/account.fetch.options';
86
import { NoCache } from '@multiversx/sdk-nestjs-cache';
7+
import { AccountDetailed } from '../accounts/entities/account.detailed';
8+
import { AccountFetchOptions } from '../accounts/entities/account.fetch.options';
99

1010
@Controller('')
1111
@ApiTags('accounts')
@@ -33,7 +33,6 @@ export class AccountControllerV2 {
3333
@Query('withScrCount', ParseBoolPipe) withScrCount?: boolean,
3434
@Query('withTimestamp', ParseBoolPipe) withTimestamp?: boolean,
3535
@Query('withAssets', ParseBoolPipe) withAssets?: boolean,
36-
@Query('timestamp', ParseIntPipe) _timestamp?: number,
3736
): Promise<AccountDetailed> {
3837
const account = await this.accountServiceV2.getAccount(
3938
address,

src/endpoints/accounts-v2/account.module.v2.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import { AccountServiceV2 } from "./account.service.v2";
1616
import { ProviderModule } from "../providers/provider.module";
1717
import { KeysModule } from "../keys/keys.module";
1818
import { MongoDbModule } from "src/common/indexer/db";
19+
import { AccountService } from "../accounts/account.service";
1920

2021
@Module({
2122
imports: [
@@ -37,9 +38,11 @@ import { MongoDbModule } from "src/common/indexer/db";
3738
MongoDbModule,
3839
],
3940
providers: [
41+
AccountService,
4042
AccountServiceV2,
4143
],
4244
exports: [
45+
AccountService,
4346
AccountServiceV2,
4447
],
4548
})
Lines changed: 10 additions & 178 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,34 @@
1-
import { forwardRef, HttpStatus, Inject, Injectable } from '@nestjs/common';
2-
import { AccountDetailed } from './entities/account.detailed';
3-
import { ApiConfigService } from 'src/common/api-config/api.config.service';
1+
import { forwardRef, Inject, Injectable } from '@nestjs/common';
42
import { PluginService } from 'src/common/plugins/plugin.service';
5-
import { TransferService } from '../transfers/transfer.service';
6-
import { TransactionType } from '../transactions/entities/transaction.type';
73
import { AssetsService } from 'src/common/assets/assets.service';
8-
import { TransactionFilter } from '../transactions/entities/transaction.filter';
94
import { CacheService } from "@multiversx/sdk-nestjs-cache";
105
import { AddressUtils, OriginLogger } from '@multiversx/sdk-nestjs-common';
11-
import { ApiService } from "@multiversx/sdk-nestjs-http";
12-
import { GatewayService } from 'src/common/gateway/gateway.service';
136
import { IndexerService } from "src/common/indexer/indexer.service";
147
import { CacheInfo } from 'src/utils/cache.info';
158
import { UsernameService } from '../usernames/username.service';
16-
import { ProtocolService } from 'src/common/protocol/protocol.service';
179
import { ProviderService } from '../providers/provider.service';
18-
import { AccountFetchOptions } from './entities/account.fetch.options';
1910
import { Provider } from '../providers/entities/provider';
2011
import { AccountDetailsRepository } from 'src/common/indexer/db';
2112
import { StateChangesConsumerService } from 'src/state-changes/state.changes.consumer.service';
13+
import { AccountService } from '../accounts/account.service';
14+
import { AccountDetailed } from '../accounts/entities/account.detailed';
15+
import { AccountFetchOptions } from '../accounts/entities/account.fetch.options';
2216

2317
@Injectable()
2418
export class AccountServiceV2 {
2519
private readonly logger = new OriginLogger(AccountServiceV2.name);
2620

2721
constructor(
2822
private readonly indexerService: IndexerService,
29-
private readonly gatewayService: GatewayService,
3023
private readonly cachingService: CacheService,
31-
private readonly apiConfigService: ApiConfigService,
3224
@Inject(forwardRef(() => PluginService))
3325
private readonly pluginService: PluginService,
34-
@Inject(forwardRef(() => TransferService))
35-
private readonly transferService: TransferService,
3626
private readonly assetsService: AssetsService,
3727
private readonly usernameService: UsernameService,
38-
private readonly apiService: ApiService,
39-
private readonly protocolService: ProtocolService,
4028
@Inject(forwardRef(() => ProviderService))
4129
private readonly providerService: ProviderService,
4230
private readonly accountDetailsDepository: AccountDetailsRepository,
31+
private readonly accountServiceV1: AccountService,
4332
) { }
4433

4534
private async getAccountWithFallBack(address: string, options?: AccountFetchOptions): Promise<AccountDetailed | null> {
@@ -50,7 +39,7 @@ export class AccountServiceV2 {
5039
}
5140
if (!accountFromDb) {
5241
// Second use the legacy method
53-
return await this.getAccountRaw(address, options?.withAssets);
42+
return await this.accountServiceV1.getAccountRaw(address, options?.withAssets);
5443
}
5544
if (options && options.withAssets === true) {
5645
const assets = await this.assetsService.getAllAccountAssets();
@@ -82,23 +71,23 @@ export class AccountServiceV2 {
8271
account.username = await this.usernameService.getUsernameForAddress(address) ?? undefined;
8372
}
8473
} else {
85-
account = await this.getAccountRaw(address, options?.withAssets);
74+
account = await this.accountServiceV1.getAccountRaw(address, options?.withAssets);
8675
}
8776

8877
if (!account) {
8978
return null;
9079
}
9180

9281
if (options?.withTxCount === true) {
93-
account.txCount = await this.getAccountTxCount(address);
82+
account.txCount = await this.accountServiceV1.getAccountTxCount(address);
9483
}
9584

9685
if (options?.withScrCount === true) {
97-
account.scrCount = await this.getAccountScResults(address);
86+
account.scrCount = await this.accountServiceV1.getAccountScResults(address);
9887
}
9988

10089
if (options?.withGuardianInfo === true) {
101-
await this.applyGuardianInfo(account);
90+
await this.accountServiceV1.applyGuardianInfo(account);
10291
}
10392

10493
if (options?.withTimestamp) {
@@ -127,161 +116,4 @@ export class AccountServiceV2 {
127116

128117
return account;
129118
}
130-
131-
async applyGuardianInfo(account: AccountDetailed): Promise<void> {
132-
try {
133-
const guardianResult = await this.gatewayService.getGuardianData(account.address);
134-
const guardianData = guardianResult?.guardianData;
135-
if (guardianData) {
136-
const activeGuardian = guardianData.activeGuardian;
137-
if (activeGuardian) {
138-
account.activeGuardianActivationEpoch = activeGuardian.activationEpoch;
139-
account.activeGuardianAddress = activeGuardian.address;
140-
account.activeGuardianServiceUid = activeGuardian.serviceUID;
141-
}
142-
143-
const pendingGuardian = guardianData.pendingGuardian;
144-
if (pendingGuardian) {
145-
account.pendingGuardianActivationEpoch = pendingGuardian.activationEpoch;
146-
account.pendingGuardianAddress = pendingGuardian.address;
147-
account.pendingGuardianServiceUid = pendingGuardian.serviceUID;
148-
}
149-
150-
account.isGuarded = guardianData.guarded;
151-
}
152-
} catch (error) {
153-
this.logger.error(`Error when getting guardian data for address '${account.address}'`);
154-
this.logger.error(error);
155-
}
156-
}
157-
158-
async getAccountRaw(address: string, withAssets?: boolean): Promise<AccountDetailed | null> {
159-
try {
160-
const {
161-
account: { nonce, balance, code, codeHash, rootHash, developerReward, ownerAddress, codeMetadata },
162-
} = await this.gatewayService.getAddressDetails(address);
163-
164-
const shardCount = await this.protocolService.getShardCount();
165-
const shard = AddressUtils.computeShard(AddressUtils.bech32Decode(address), shardCount);
166-
let account = new AccountDetailed({ address, nonce, balance, code, codeHash, rootHash, shard, developerReward, ownerAddress, scamInfo: undefined, nftCollections: undefined, nfts: undefined });
167-
168-
if (withAssets === true) {
169-
const assets = await this.assetsService.getAllAccountAssets();
170-
account.assets = assets[address];
171-
account.ownerAssets = assets[ownerAddress];
172-
}
173-
174-
if (AddressUtils.isSmartContractAddress(address) && account.code) {
175-
const codeAttributes = AddressUtils.decodeCodeMetadata(codeMetadata);
176-
if (codeAttributes) {
177-
account = { ...account, ...codeAttributes };
178-
}
179-
180-
const deployTxHash = await this.getAccountDeployedTxHash(address);
181-
if (deployTxHash) {
182-
account.deployTxHash = deployTxHash;
183-
}
184-
185-
const deployedAt = await this.getAccountDeployedAt(address);
186-
if (deployedAt) {
187-
account.deployedAt = deployedAt;
188-
}
189-
190-
const isVerified = await this.getAccountIsVerified(address, account.codeHash);
191-
if (isVerified) {
192-
account.isVerified = isVerified;
193-
}
194-
}
195-
196-
if (!AddressUtils.isSmartContractAddress(address)) {
197-
account.username = await this.usernameService.getUsernameForAddress(address) ?? undefined;
198-
account.isPayableBySmartContract = undefined;
199-
account.isUpgradeable = undefined;
200-
account.isReadable = undefined;
201-
account.isPayable = undefined;
202-
}
203-
204-
await this.pluginService.processAccount(account);
205-
return account;
206-
} catch (error) {
207-
this.logger.error(error);
208-
this.logger.error(`Error when getting account details for address '${address}'`);
209-
return null;
210-
}
211-
}
212-
213-
async getAccountTxCount(address: string): Promise<number> {
214-
return await this.transferService.getTransfersCount(new TransactionFilter({ address, type: TransactionType.Transaction }));
215-
}
216-
217-
async getAccountScResults(address: string): Promise<number> {
218-
return await this.transferService.getTransfersCount(new TransactionFilter({ address, type: TransactionType.SmartContractResult }));
219-
}
220-
221-
async getAccountDeployedAt(address: string): Promise<number | null> {
222-
return await this.cachingService.getOrSet(
223-
CacheInfo.AccountDeployedAt(address).key,
224-
async () => await this.getAccountDeployedAtRaw(address),
225-
CacheInfo.AccountDeployedAt(address).ttl
226-
);
227-
}
228-
229-
async getAccountDeployedAtRaw(address: string): Promise<number | null> {
230-
const scDeploy = await this.indexerService.getScDeploy(address);
231-
if (!scDeploy) {
232-
return null;
233-
}
234-
235-
const txHash = scDeploy.deployTxHash;
236-
if (!txHash) {
237-
return null;
238-
}
239-
240-
const transaction = await this.indexerService.getTransaction(txHash);
241-
if (!transaction) {
242-
return null;
243-
}
244-
245-
return transaction.timestamp;
246-
}
247-
248-
async getAccountDeployedTxHash(address: string): Promise<string | null> {
249-
return await this.cachingService.getOrSet(
250-
CacheInfo.AccountDeployTxHash(address).key,
251-
async () => await this.getAccountDeployedTxHashRaw(address),
252-
CacheInfo.AccountDeployTxHash(address).ttl,
253-
);
254-
}
255-
256-
async getAccountDeployedTxHashRaw(address: string): Promise<string | null> {
257-
const scDeploy = await this.indexerService.getScDeploy(address);
258-
if (!scDeploy) {
259-
return null;
260-
}
261-
262-
return scDeploy.deployTxHash;
263-
}
264-
265-
async getAccountIsVerified(address: string, codeHash: string): Promise<boolean | null> {
266-
return await this.cachingService.getOrSet(
267-
CacheInfo.AccountIsVerified(address).key,
268-
async () => await this.getAccountIsVerifiedRaw(address, codeHash),
269-
CacheInfo.AccountIsVerified(address).ttl
270-
);
271-
}
272-
273-
async getAccountIsVerifiedRaw(address: string, codeHash: string): Promise<boolean | null> {
274-
try {
275-
// eslint-disable-next-line require-await
276-
const { data } = await this.apiService.get(`${this.apiConfigService.getVerifierUrl()}/verifier/${address}/codehash`, undefined, async (error) => error.response?.status === HttpStatus.NOT_FOUND);
277-
278-
if (data.codeHash === Buffer.from(codeHash, 'base64').toString('hex')) {
279-
return true;
280-
}
281-
} catch {
282-
// ignore
283-
}
284-
285-
return null;
286-
}
287119
}

src/endpoints/accounts-v2/entities/account.detailed.ts

Lines changed: 0 additions & 74 deletions
This file was deleted.

src/endpoints/accounts-v2/entities/account.fetch.options.ts

Lines changed: 0 additions & 11 deletions
This file was deleted.

0 commit comments

Comments
 (0)