Skip to content

Commit

Permalink
fix: better validations and messages
Browse files Browse the repository at this point in the history
  • Loading branch information
aaitor committed Nov 8, 2023
1 parent 5d4601c commit 177dda2
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 34 deletions.
6 changes: 3 additions & 3 deletions integration/nevermined/NFT1155.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 () => {
Expand Down
4 changes: 2 additions & 2 deletions integration/nevermined/NVMAppFlows.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
},
],
Expand Down
15 changes: 9 additions & 6 deletions src/keeper/contracts/templates/NFTAccessTemplate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -168,11 +169,7 @@ export class NFTAccessTemplate extends BaseTemplate<NFTAccessTemplateParams, Ser
: ddo.findServiceByType(this.service())
) as ServiceNFTAccess

// const amount = DDO.getNftAmountFromService(nftAccessService)
// if (amount <= 0n) return true

// params.nft_amount
const amount = NFTServiceAttributes.getCreditsToConsume(
const amount = NFTServiceAttributes.getCreditsToCharge(
nftAccessService.attributes.main.nftAttributes,
)

Expand All @@ -186,8 +183,14 @@ export class NFTAccessTemplate extends BaseTemplate<NFTAccessTemplateParams, Ser

const nftContract = await this.nevermined.contracts.loadNft1155(contractAddress)

await nftContract.burnFromHolder(params.consumer_address, tokenId, amount, from, txparams)
const balance = await nftContract.balance(tokenId, params.consumer_address)
if (balance < amount) {
throw new DynamicCreditsUnderLimit(
`Balance is under the number of required credits to be burned: ${balance} < ${amount}`,
)
}

await nftContract.burnFromHolder(params.consumer_address, tokenId, amount, from, txparams)
return true
}
}
51 changes: 28 additions & 23 deletions src/models/NFTAttributes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ export class NFTServiceAttributes {
* The maximum number of credits that can be charged to the subscriber.
* If not specified, the subscription cost is not capped
*/
maxCreditsCharged?: bigint
maxCreditsToCharge?: bigint

/**
* The minimum number of credits that will be charged to the subscriber.
* If not specified, the amount defined in the service agreement or 1 credit will be charged
*/
minCreditsCharged?: bigint
minCreditsToCharge?: bigint

/**
* The minimum number of credits that the subscribers needs to hold to access the asset.
Expand All @@ -57,8 +57,8 @@ export class NFTServiceAttributes {
duration: 0, // Because it's not a subscription it doesn't have a duration
amount: 1n, // By default just one edition
tokenId: '', // By default no tokenId
maxCreditsCharged: 0n, // Max credits charged equals to 0 means the subscription cost is not capped
minCreditsCharged: 0n, // Min credits charged equals to 0 means the amount defined in the service agreement or 1 credit will be charged
maxCreditsToCharge: 0n, // Max credits to charge equals to 0 means the subscription cost is not capped
minCreditsToCharge: 0n, // Min credits to charge equals to 0 means the amount defined in the service agreement or 1 credit will be charged
minCreditsRequired: 1n, // One credit required to access
}

Expand All @@ -73,25 +73,30 @@ export class NFTServiceAttributes {
* @param dynamicAmount the dynamic amount of credits asked to be consumed
* @returns amount to consume
*/
static getCreditsToConsume(nftAttributes: NFTServiceAttributes, dynamicAmount?: bigint) {
static getCreditsToCharge(nftAttributes: NFTServiceAttributes, dynamicAmount?: bigint) {
if (!nftAttributes) throw new NFTError('NFT attributes are not defined')

if (dynamicAmount) {
if (dynamicAmount > 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
}

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 177dda2

Please sign in to comment.