Skip to content

Commit 733229c

Browse files
authored
fix: issue with USDC hardcoded (#362)
* fix: issue with USDC hardcoded * fix: calculate correctly symbol and price
1 parent 23c948e commit 733229c

File tree

3 files changed

+27
-7
lines changed

3 files changed

+27
-7
lines changed

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "node-ts",
3-
"version": "3.0.19",
3+
"version": "3.0.20",
44
"description": "Nevermined Node",
55
"main": "main.ts",
66
"scripts": {
@@ -131,5 +131,6 @@
131131
]
132132
},
133133
"author": "Nevermined",
134-
"license": "Apache-2.0"
134+
"license": "Apache-2.0",
135+
"packageManager": "[email protected]+sha512.a6b2f7906b721bba3d67d4aff083df04dad64c399707841b7acf00f6b133b7ac24255f2652fa22ae3534329dc6180534e98d17432037ff6fd140556e2bb3137e"
135136
}

src/access/access.controller.ts

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ import { AssetResult, NeverminedService } from '../shared/nevermined/nvm.service
4848
import { TransferDto } from './dto/transfer'
4949
import { UploadDto } from './dto/upload'
5050
import { UploadResult } from './dto/upload-result'
51+
import { formatUnits } from 'viem'
5152

5253
export enum UploadBackends {
5354
IPFS = 'ipfs',
@@ -254,14 +255,31 @@ export class AccessController {
254255
if (!this.backendService.isBackendEnabled()) {
255256
Logger.log(`NVM Backend not enabled, skipping tracking transaction in the database`)
256257
} else {
257-
const assetPrice = this.nvmService.getAssetPrice(service) / 10n ** BigInt(4)
258+
const assetPrice = this.nvmService.getAssetPrice(service).getTotalPrice()
259+
const erc20TokenAddress =
260+
this.nvmService.getAssetPrice(service)?.getTokenAddress() ||
261+
this.nvmService.getNevermined().utils.token.getAddress()
262+
263+
let currency: string
264+
let decimals: number
265+
if (erc20TokenAddress === ZeroAddress) {
266+
currency = 'ETH'
267+
decimals = 18
268+
} else {
269+
const erc20 = await this.nvmService.getNevermined().contracts.loadErc20(erc20TokenAddress)
270+
currency = await erc20.symbol()
271+
decimals = await erc20.decimals()
272+
}
273+
274+
const priceHighestDenomination = +formatUnits(assetPrice, decimals)
275+
258276
const assetTx: AssetTransaction = {
259277
assetDid: did.getDid(),
260278
assetOwner: subscriptionDDO.proof.creator,
261279
assetConsumer: transferData.nftReceiver,
262280
txType: 'Mint',
263-
price: (Number(assetPrice) / 100).toString(),
264-
currency: 'USDC',
281+
price: priceHighestDenomination.toString(),
282+
currency: currency,
265283
paymentType: 'Crypto',
266284
txHash: JSON.stringify(txs),
267285
metadata: '',

src/shared/nevermined/nvm.service.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
StreamableFile,
1010
} from '@nestjs/common'
1111
import {
12+
AssetPrice,
1213
DDO,
1314
DDOError,
1415
DDOServiceNotFoundError,
@@ -472,10 +473,10 @@ export class NeverminedService {
472473
return Number(duration) || 0
473474
}
474475

475-
public getAssetPrice(service: ServiceCommon): bigint {
476+
public getAssetPrice(service: ServiceCommon): AssetPrice {
476477
const assetPrice = DDO.getAssetPriceFromService(service)
477478

478-
if (assetPrice) return assetPrice.getTotalPrice()
479+
if (assetPrice) return assetPrice
479480
throw new DDOError(`No price found for asset ${service.index}`)
480481
}
481482

0 commit comments

Comments
 (0)