From aba110c65fd9e64db92ad2bdb02a5560a63aa739 Mon Sep 17 00:00:00 2001 From: Aitor <1726644+aaitor@users.noreply.github.com> Date: Fri, 15 Nov 2024 13:36:28 +0100 Subject: [PATCH] feat: burn batch (requires contracts v3.5.9) --- integration/nevermined/Credits.e2e.test.ts | 18 ++++++++++- package.json | 2 +- src/keeper/contracts/Nft1155Contract.ts | 22 +++++++++++++ src/nevermined/api/nfts/NFT1155Api.ts | 36 +++++++++++++++++++++- 4 files changed, 75 insertions(+), 3 deletions(-) diff --git a/integration/nevermined/Credits.e2e.test.ts b/integration/nevermined/Credits.e2e.test.ts index d349c0cd3..27126df98 100644 --- a/integration/nevermined/Credits.e2e.test.ts +++ b/integration/nevermined/Credits.e2e.test.ts @@ -123,10 +123,12 @@ describe('Credit Subscriptions using NFT ERC-1155 End-to-End', () => { console.log(`Running first test`) // Deploy NFT TestContractHandler.setConfig(config) + const networkName = await nevermined.keeper.getNetworkName() const contractABI = await TestContractHandler.getABIArtifact( 'NFT1155SubscriptionUpgradeable', - './test/resources/artifacts/', + config.artifactsFolder, + networkName, ) subscriptionNFT = await SubscriptionCreditsNFTApi.deployInstance( config, @@ -393,5 +395,19 @@ describe('Credit Subscriptions using NFT ERC-1155 End-to-End', () => { console.log(`Balance After TopUp: ${balanceAfterTopUp}`) assert.isTrue(balanceAfterTopUp > accessCostInCredits) }) + + it('Should be able to burn in batch', async () => { + const beforeBalance = await subscriptionNFT.balance(subscriptionDDO.id, subscriber.getId()) + + await nevermined.nfts1155.burnBatchFromHolders( + [subscriber.getId(), subscriber.getId()], + [subscriptionDDO.id, subscriptionDDO.id], + [1n, 2n], + editor, + ) + + const afterBalance = await subscriptionNFT.balance(subscriptionDDO.id, subscriber.getId()) + assert.equal(beforeBalance - 3n, afterBalance) + }) }) }) diff --git a/package.json b/package.json index 5d2302bc4..1d6616478 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@nevermined-io/sdk", - "version": "3.0.41", + "version": "3.0.42", "description": "Javascript SDK for connecting with Nevermined Data Platform ", "main": "./dist/node/sdk.js", "typings": "./dist/node/sdk.d.ts", diff --git a/src/keeper/contracts/Nft1155Contract.ts b/src/keeper/contracts/Nft1155Contract.ts index 3ec5f3455..e66dc69f0 100644 --- a/src/keeper/contracts/Nft1155Contract.ts +++ b/src/keeper/contracts/Nft1155Contract.ts @@ -204,6 +204,28 @@ export class Nft1155Contract extends NFTContractsBase { return this.send('burn', from, [holder, didToTokenId(tokenId), BigInt(amount)], txParams) } + /** + * It burns some editions of a NFT (ERC-1155) + * + * @param holders - Array of addresses of the accounts holding the NFT editions that are going to be burned + * @param tokenIds - Array of the NFT ids to burn + * @param amounts - Array of number of editions to burn + * @param from - Account burning the NFT editions + * @param txParams - Transaction additional parameters + * @returns Contract Receipt + */ + public async burnBatchFromHolders( + holders: string[], + tokenIds: string[], + amounts: bigint[], + from: NvmAccount, + txParams?: TxParameters, + ) { + const _tokenIds = tokenIds.map((did) => didToTokenId(did)) + const _amounts = amounts.map((amount) => BigInt(amount)) + return this.send('burnBatchFromHolders', from, [holders, _tokenIds, _amounts], txParams) + } + /** * It returns the NFT metadata uri * diff --git a/src/nevermined/api/nfts/NFT1155Api.ts b/src/nevermined/api/nfts/NFT1155Api.ts index 9294a4b9e..512ac615e 100644 --- a/src/nevermined/api/nfts/NFT1155Api.ts +++ b/src/nevermined/api/nfts/NFT1155Api.ts @@ -198,7 +198,7 @@ export class NFT1155Api extends NFTsBaseApi { * * @example * ```ts - * await nevermined.nfts1155.burnTo( + * await nevermined.nfts1155.burnFromHolder( * holder, * tokenId, * 2n, @@ -224,6 +224,40 @@ export class NFT1155Api extends NFTsBaseApi { return await this.nftContract.burnFromHolder(holder, tokenId, nftAmount, from, txParams) } + /** + * Burns NFTs associated with an asset of a specific account. + * + * @remarks + * The publisher can only burn NFTs of an account if is an operator. NFTs that were already transferred cannot be burned by the publisher. + * + * @example + * ```ts + * await nevermined.nfts1155.burnBatchFromHolders( + * [holder1, holder2], + * [tokenId, tokenId], + * [2n, 3n], + * burner + * ) + * ``` + * + * @param holders - Array of addresses of the accounts holding the NFT editions that are going to be burned + * @param tokenIds - Array of the NFT ids to burn + * @param amounts - Array of number of editions to burn + * @param from - The account of the publisher of the NFT. + * @param txParams - Optional transaction parameters. + * + * @returns The {@link TransactionReceipt} + */ + public async burnBatchFromHolders( + holders: string[], + tokenIds: string[], + amounts: bigint[], + from: NvmAccount, + txParams?: TxParameters, + ) { + return this.nftContract.burnBatchFromHolders(holders, tokenIds, amounts, from, txParams) + } + // TODO: We need to improve this to allow for secondary market sales // Right now it fetches the rewards from the DDO which don't change.