-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: nvm backend integration to record asset txs
- Loading branch information
Showing
14 changed files
with
173 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { Module } from '@nestjs/common' | ||
import { HttpModule } from '@nestjs/axios' | ||
import { BackendService } from './backend.service' | ||
import { ConfigModule } from '../config/config.module' | ||
|
||
@Module({ | ||
imports: [ConfigModule, HttpModule], | ||
providers: [BackendService], | ||
exports: [BackendService], | ||
}) | ||
export class BackendModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import { Injectable, Logger } from '@nestjs/common' | ||
import { InternalServerErrorException } from '@nestjs/common' | ||
import { ConfigService } from '../config/config.service' | ||
import { firstValueFrom } from 'rxjs' | ||
import { HttpModuleOptions, HttpService } from '@nestjs/axios' | ||
|
||
export interface AssetTransaction { | ||
assetDid: string | ||
assetOwner: string | ||
assetConsumer: string | ||
txType: string | ||
price: string | ||
currency: string | ||
paymentType: string | ||
txHash?: string | ||
metadata?: string | ||
} | ||
|
||
@Injectable() | ||
export class BackendService { | ||
isNVMBackendEnabled = false | ||
trackBackendTxs: boolean | ||
backendUrl: string | ||
backendAuth: string | ||
|
||
constructor(private config: ConfigService, private readonly httpService: HttpService) {} | ||
|
||
async onModuleInit() { | ||
Logger.debug(`Starting BackendService`) | ||
|
||
try { | ||
this.trackBackendTxs = this.config.backendConfig().trackBackendTxs | ||
this.backendUrl = this.config.backendConfig().backendUrl | ||
this.backendAuth = this.config.backendConfig().backendAuth | ||
this.isNVMBackendEnabled = this.config.backendConfig().isNVMBackendEnabled | ||
|
||
Logger.log(`Backend Config: `) | ||
Logger.log(`Is Backend Enabled: ${this.isNVMBackendEnabled}`) | ||
Logger.log(this.trackBackendTxs) | ||
Logger.log(this.backendUrl) | ||
Logger.log(this.backendAuth) | ||
} catch (e) { | ||
Logger.warn(e) | ||
} | ||
} | ||
|
||
public isBackendEnabled(): boolean { | ||
return this.isNVMBackendEnabled | ||
} | ||
|
||
public async recordAssetTransaction(assetTx: AssetTransaction): Promise<boolean> { | ||
if (!this.isNVMBackendEnabled || !this.trackBackendTxs) { | ||
Logger.warn('Backend transactions recording is disabled by config') | ||
return false | ||
} | ||
|
||
const requestConfig: HttpModuleOptions = { | ||
url: `${this.backendUrl}/api/v1/transactions/asset`, | ||
method: 'POST', | ||
headers: { | ||
Authorization: `Bearer ${this.backendAuth}`, | ||
'Content-Type': 'application/json', | ||
}, | ||
data: assetTx, | ||
} | ||
|
||
const response = await firstValueFrom(this.httpService.request(requestConfig)) | ||
const obj = response.data | ||
|
||
if (obj.error) { | ||
Logger.error('Backend returned an error message:', obj.error) | ||
throw new InternalServerErrorException(obj.error) | ||
} | ||
|
||
return true | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters