diff --git a/integration/nevermined/NFT1155.test.ts b/integration/nevermined/NFT1155.test.ts index 406409b88..296d9e201 100644 --- a/integration/nevermined/NFT1155.test.ts +++ b/integration/nevermined/NFT1155.test.ts @@ -107,7 +107,7 @@ describe('NFT1155 End-to-End', () => { }, { serviceType: 'nft-access', - nft: { amount: numberNFTs, maxCreditsCharged: 100n, minCreditsCharged: 1n }, + nft: { amount: numberNFTs, maxCreditsToCharge: 100n, minCreditsToCharge: 1n }, }, ], nftContractAddress: nftUpgradeable.address, @@ -127,8 +127,8 @@ describe('NFT1155 End-to-End', () => { console.log(JSON.stringify(ddoAttributes)) assert.equal(ddoAttributes.amount, numberNFTs) - assert.equal(ddoAttributes.maxCreditsCharged, 100n) - assert.equal(ddoAttributes.minCreditsCharged, 1n) + assert.equal(ddoAttributes.maxCreditsToCharge, 100n) + assert.equal(ddoAttributes.minCreditsToCharge, 1n) }) it('Should be able to approve permissions', async () => { diff --git a/integration/nevermined/NVMAppFlows.ts b/integration/nevermined/NVMAppFlows.ts index a8ff4dd5c..79689b87a 100644 --- a/integration/nevermined/NVMAppFlows.ts +++ b/integration/nevermined/NVMAppFlows.ts @@ -396,8 +396,8 @@ describe('NVM App main flows using Credit NFTs (ERC-1155)', () => { duration: subscriptionSilverDuration, amount: accessCostInCreditsDataset, nftTransfer, - maxCreditsCharged: 100n, - minCreditsCharged: 1n, + maxCreditsToCharge: 100n, + minCreditsToCharge: 1n, }, }, ], diff --git a/src/keeper/contracts/templates/NFTAccessTemplate.ts b/src/keeper/contracts/templates/NFTAccessTemplate.ts index 8c277145b..58adcf412 100644 --- a/src/keeper/contracts/templates/NFTAccessTemplate.ts +++ b/src/keeper/contracts/templates/NFTAccessTemplate.ts @@ -16,6 +16,7 @@ import { AgreementInstance, AgreementTemplate } from './AgreementTemplate.abstra import { BaseTemplate } from './BaseTemplate.abstract' import { nftAccessTemplateServiceAgreementTemplate } from './NFTAccessTemplate.serviceAgreementTemplate' import { NFTAccessCondition, NFTHolderCondition } from '../conditions' +import { DynamicCreditsUnderLimit } from '../../../errors/NFTError' export interface NFTAccessTemplateParams { holderAddress: string @@ -168,11 +169,7 @@ export class NFTAccessTemplate extends BaseTemplate nftAttributes.maxCreditsCharged) { - // TODO: Evalue if thow an exception or return the max credits that can be charged - //throw new DynamicCreditsOverLimit(`The amount of credits to consume ${dynamicAmount} is higher than the max credits charged ${this.maxCreditsCharged}`) - return nftAttributes.maxCreditsCharged - } else if (dynamicAmount < nftAttributes.minCreditsCharged) { - // TODO: Evalue if thow an exception or return the min amount to be charged - //throw new DynamicCreditsUnderLimit(`The amount of credits to consume ${dynamicAmount} is lower than the min credits charged ${this.minCreditsCharged}`) - nftAttributes.minCreditsCharged + if (dynamicAmount !== undefined) { + if (dynamicAmount > nftAttributes.maxCreditsToCharge) { + // TODO: Evaluate if thow an exception or return the max credits that can be charged + //throw new DynamicCreditsOverLimit(`The amount of credits to consume ${dynamicAmount} is higher than the max credits charged ${this.maxCreditsToCharge}`) + return nftAttributes.maxCreditsToCharge + } else if (dynamicAmount < nftAttributes.minCreditsToCharge) { + // TODO: Evaluate if thow an exception or return the min amount to be charged + //throw new DynamicCreditsUnderLimit(`The amount of credits to consume ${dynamicAmount} is lower than the min credits charged ${this.minCreditsToCharge}`) + nftAttributes.minCreditsToCharge } return dynamicAmount } - if (nftAttributes.minCreditsCharged && nftAttributes.minCreditsCharged >= 0n) - return nftAttributes.amount > nftAttributes.minCreditsCharged + + if (dynamicAmount === 0n || nftAttributes.amount === 0n) + // If the amount is 0 means the access is Free + return 0n + + if (nftAttributes.minCreditsToCharge && nftAttributes.minCreditsToCharge >= 0n) + return nftAttributes.amount > nftAttributes.minCreditsToCharge ? nftAttributes.amount - : nftAttributes.minCreditsCharged + : nftAttributes.minCreditsToCharge else return nftAttributes.amount } @@ -124,17 +129,17 @@ export class NFTServiceAttributes { if (service.nft.amount === undefined) service.nft.amount = this.defaultValues.amount if (service.serviceType === 'nft-access') { - if (!service.nft.minCreditsCharged) service.nft.minCreditsCharged = service.nft.amount - if (!service.nft.maxCreditsCharged) service.nft.maxCreditsCharged = service.nft.amount + if (!service.nft.minCreditsToCharge) service.nft.minCreditsToCharge = service.nft.amount + if (!service.nft.maxCreditsToCharge) service.nft.maxCreditsToCharge = service.nft.amount if (!service.nft.minCreditsRequired) - service.nft.minCreditsRequired = service.nft.minCreditsCharged + service.nft.minCreditsRequired = service.nft.minCreditsToCharge if ( - service.nft.amount < service.nft.minCreditsCharged || - service.nft.amount > service.nft.maxCreditsCharged + service.nft.amount < service.nft.minCreditsToCharge || + service.nft.amount > service.nft.maxCreditsToCharge ) throw new NFTError( - `The amount of credits to consume ${service.nft.amount} is not between the min credits charged ${service.nft.minCreditsCharged} and the max credits charged ${service.nft.maxCreditsCharged}`, + `The amount of credits to consume ${service.nft.amount} is not between the min credits charged ${service.nft.minCreditsToCharge} and the max credits charged ${service.nft.maxCreditsToCharge}`, ) } else if (service.serviceType === 'nft-sales') { if (nftAttributes.ercType == 721) service.nft.amount = 1n