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' ;
42import { PluginService } from 'src/common/plugins/plugin.service' ;
5- import { TransferService } from '../transfers/transfer.service' ;
6- import { TransactionType } from '../transactions/entities/transaction.type' ;
73import { AssetsService } from 'src/common/assets/assets.service' ;
8- import { TransactionFilter } from '../transactions/entities/transaction.filter' ;
94import { CacheService } from "@multiversx/sdk-nestjs-cache" ;
105import { AddressUtils , OriginLogger } from '@multiversx/sdk-nestjs-common' ;
11- import { ApiService } from "@multiversx/sdk-nestjs-http" ;
12- import { GatewayService } from 'src/common/gateway/gateway.service' ;
136import { IndexerService } from "src/common/indexer/indexer.service" ;
147import { CacheInfo } from 'src/utils/cache.info' ;
158import { UsernameService } from '../usernames/username.service' ;
16- import { ProtocolService } from 'src/common/protocol/protocol.service' ;
179import { ProviderService } from '../providers/provider.service' ;
18- import { AccountFetchOptions } from './entities/account.fetch.options' ;
1910import { Provider } from '../providers/entities/provider' ;
2011import { AccountDetailsRepository } from 'src/common/indexer/db' ;
2112import { 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 ( )
2418export 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}
0 commit comments