|
| 1 | +import chai, { assert } from 'chai' |
| 2 | +import chaiAsPromised from 'chai-as-promised' |
| 3 | + |
| 4 | +import { DDO } from '../../src/ddo/DDO' |
| 5 | +import { NvmAccount } from '../../src/models/NvmAccount' |
| 6 | +import { Nevermined } from '../../src/nevermined/Nevermined' |
| 7 | +import { MetaData } from '../../src/types/DDOTypes' |
| 8 | +import config from '../../test/config' |
| 9 | + |
| 10 | +import { jsonReplacer } from '../../src/common/helpers' |
| 11 | +import { EscrowPaymentCondition } from '../../src/keeper/contracts/conditions' |
| 12 | +import { Token } from '../../src/keeper/contracts/Token' |
| 13 | +import { AssetPrice } from '../../src/models/AssetPrice' |
| 14 | +import { NFTAttributes } from '../../src/models/NFTAttributes' |
| 15 | +import { NFT1155Api } from '../../src/nevermined/api' |
| 16 | +// import { getRoyaltyAttributes, RoyaltyAttributes } from '../../src/nevermined/api/AssetsApi' |
| 17 | +// import { RoyaltyKind } from '../../src/types/MetadataTypes' |
| 18 | +import TestContractHandler from '../../test/keeper/TestContractHandler' |
| 19 | +import { getMetadata } from '../utils/ddo-metadata-generator' |
| 20 | +import { ZeroAddress } from '../../src' |
| 21 | + |
| 22 | +chai.use(chaiAsPromised) |
| 23 | + |
| 24 | +describe('OLAS e2e tests', () => { |
| 25 | + const SUBSCRIPTION_NFT_ADDRESS: string = process.env.SUBSCRIPTION_NFT_ADDRESS || ZeroAddress |
| 26 | + const OLAS_MARKETPLACE_ADDRESS: string = process.env.OLAS_MARKETPLACE_ADDRESS || ZeroAddress |
| 27 | + const TOKEN_ADDRESS: string = process.env.TOKEN_ADDRESS || ZeroAddress |
| 28 | + |
| 29 | + let IS_NATIVE_TOKEN = false |
| 30 | + |
| 31 | + // 10000 |
| 32 | + // const AMOUNT_OLAS_FEE = 10000n |
| 33 | + // const AMOUNT_PLAN_PRICE = 98000n |
| 34 | + let PLAN_FEE_NVM: bigint |
| 35 | + let PLAN_FEE_MKT: bigint |
| 36 | + let PLAN_PRICE_MECHS: bigint |
| 37 | + let AMOUNT_TOTAL: bigint |
| 38 | + |
| 39 | + let RECEIVER_NVM_FEE: string |
| 40 | + const RECEIVER_OLAS_FEE = OLAS_MARKETPLACE_ADDRESS |
| 41 | + const RECEIVER_PLAN = process.env.RECEIVER_PLAN || ZeroAddress |
| 42 | + |
| 43 | + // This is the number of credits that the subscriber will get when purchase the subscription |
| 44 | + // In the DDO this will be added in the `_numberNFTs` value of the `nft-sales` service of the subscription |
| 45 | + const SUBSCRIPTION_CREDITS = BigInt(process.env.SUBSCRIPTION_CREDITS || '100') |
| 46 | + |
| 47 | + let publisher: NvmAccount |
| 48 | + let subscriber: NvmAccount |
| 49 | + |
| 50 | + let nevermined: Nevermined |
| 51 | + let token: Token |
| 52 | + let escrowPaymentCondition: EscrowPaymentCondition |
| 53 | + let subscriptionDDO: DDO |
| 54 | + |
| 55 | + let agreementId: string |
| 56 | + |
| 57 | + let planPrice: AssetPrice |
| 58 | + |
| 59 | + let subscriptionMetadata: MetaData |
| 60 | + let subsSalesService |
| 61 | + |
| 62 | + const preMint = false |
| 63 | + const nftTransfer = false |
| 64 | + |
| 65 | + let initialBalances: any |
| 66 | + |
| 67 | + let subscriptionNFT: NFT1155Api |
| 68 | + let neverminedNodeAddress |
| 69 | + |
| 70 | + before(async () => { |
| 71 | + TestContractHandler.setConfig(config) |
| 72 | + |
| 73 | + nevermined = await Nevermined.getInstance(config) |
| 74 | + ;[publisher, subscriber] = nevermined.accounts.list() |
| 75 | + |
| 76 | + const clientAssertion = await nevermined.utils.jwt.generateClientAssertion(publisher) |
| 77 | + await nevermined.services.marketplace.login(clientAssertion) |
| 78 | + |
| 79 | + subscriptionMetadata = getMetadata(undefined, 'OLAS Plan') |
| 80 | + subscriptionMetadata.main.type = 'subscription' |
| 81 | + |
| 82 | + neverminedNodeAddress = await nevermined.services.node.getProviderAddress() |
| 83 | + |
| 84 | + // conditions |
| 85 | + ;({ escrowPaymentCondition } = nevermined.keeper.conditions) |
| 86 | + |
| 87 | + console.log(`Using Token Address: ${TOKEN_ADDRESS}`) |
| 88 | + console.log(`Publisher: ${publisher.getId()}`) |
| 89 | + console.log(`Subscriber: ${subscriber.getId()}`) |
| 90 | + |
| 91 | + RECEIVER_NVM_FEE = await nevermined.keeper.nvmConfig.getFeeReceiver() |
| 92 | + |
| 93 | + PLAN_FEE_NVM = BigInt(process.env.PLAN_FEE_NVM || '0') |
| 94 | + PLAN_FEE_MKT = BigInt(process.env.PLAN_FEE_MKT || '0') |
| 95 | + PLAN_PRICE_MECHS = BigInt(process.env.PLAN_PRICE_MECHS || '0') |
| 96 | + |
| 97 | + console.log(`PLAN_FEES: ${PLAN_FEE_NVM} - ${PLAN_FEE_MKT} - ${PLAN_PRICE_MECHS}`) |
| 98 | + |
| 99 | + const distPayments = new Map() |
| 100 | + if (PLAN_FEE_NVM > 0n) distPayments.set(RECEIVER_NVM_FEE, PLAN_FEE_NVM) |
| 101 | + if (PLAN_FEE_MKT > 0n) distPayments.set(RECEIVER_OLAS_FEE, PLAN_FEE_MKT) |
| 102 | + if (PLAN_PRICE_MECHS > 0n) distPayments.set(RECEIVER_PLAN, PLAN_PRICE_MECHS) |
| 103 | + |
| 104 | + console.log(distPayments) |
| 105 | + assert.isTrue(distPayments.size > 0) |
| 106 | + |
| 107 | + planPrice = new AssetPrice(distPayments).setTokenAddress(TOKEN_ADDRESS) |
| 108 | + |
| 109 | + console.log(`Distribution of payments: ${JSON.stringify(distPayments.values(), jsonReplacer)}`) |
| 110 | + console.log(`Plan Price: ${JSON.stringify(planPrice, jsonReplacer)}`) |
| 111 | + |
| 112 | + AMOUNT_TOTAL = planPrice.getTotalPrice() |
| 113 | + IS_NATIVE_TOKEN = TOKEN_ADDRESS === ZeroAddress |
| 114 | + |
| 115 | + if (!IS_NATIVE_TOKEN) { |
| 116 | + token = await nevermined.contracts.loadErc20(TOKEN_ADDRESS) |
| 117 | + initialBalances = { |
| 118 | + publisher: await token.balanceOf(publisher.getId()), |
| 119 | + subscriber: await token.balanceOf(subscriber.getId()), |
| 120 | + olas: await token.balanceOf(OLAS_MARKETPLACE_ADDRESS), |
| 121 | + escrowPaymentCondition: await token.balanceOf(escrowPaymentCondition.address), |
| 122 | + } |
| 123 | + } else { |
| 124 | + initialBalances = { |
| 125 | + publisher: await nevermined.client.public.getBalance({ address: publisher.getId() }), |
| 126 | + subscriber: await nevermined.client.public.getBalance({ address: subscriber.getId() }), |
| 127 | + olas: await nevermined.client.public.getBalance({ |
| 128 | + address: OLAS_MARKETPLACE_ADDRESS as `0x${string}`, |
| 129 | + }), |
| 130 | + escrowPaymentCondition: nevermined.client.public.getBalance({ |
| 131 | + address: escrowPaymentCondition.address, |
| 132 | + }), |
| 133 | + } |
| 134 | + } |
| 135 | + console.log(`Initial Balances: ${JSON.stringify(initialBalances, jsonReplacer)}`) |
| 136 | + console.log(`Asset Price: ${JSON.stringify(planPrice, jsonReplacer)}`) |
| 137 | + }) |
| 138 | + |
| 139 | + describe('As PUBLISHER I want to register a new Plan', () => { |
| 140 | + it('I want to register the Plan', async () => { |
| 141 | + // Deploy NFT |
| 142 | + TestContractHandler.setConfig(config) |
| 143 | + |
| 144 | + subscriptionNFT = await nevermined.contracts.loadNft1155(SUBSCRIPTION_NFT_ADDRESS) |
| 145 | + |
| 146 | + console.debug(`Using Subscription NFT address: ${subscriptionNFT.address}`) |
| 147 | + |
| 148 | + assert.equal(nevermined.nfts1155.getContract.address, subscriptionNFT.address) |
| 149 | + |
| 150 | + const nftAttributes = NFTAttributes.getCreditsSubscriptionInstance({ |
| 151 | + metadata: subscriptionMetadata, |
| 152 | + services: [ |
| 153 | + { |
| 154 | + serviceType: 'nft-sales', |
| 155 | + price: planPrice, |
| 156 | + nft: { amount: SUBSCRIPTION_CREDITS, nftTransfer }, |
| 157 | + }, |
| 158 | + ], |
| 159 | + providers: [neverminedNodeAddress, OLAS_MARKETPLACE_ADDRESS], |
| 160 | + nftContractAddress: subscriptionNFT.address, |
| 161 | + preMint, |
| 162 | + }) |
| 163 | + subscriptionDDO = await nevermined.nfts1155.create(nftAttributes, publisher) |
| 164 | + |
| 165 | + assert.equal(await subscriptionNFT.balance(subscriptionDDO.id, publisher.getId()), 0n) |
| 166 | + assert.isDefined(subscriptionDDO) |
| 167 | + console.log(`OLAS Plan DID: ${subscriptionDDO.id}`) |
| 168 | + console.log(` DID Providerss: ${neverminedNodeAddress} - ${OLAS_MARKETPLACE_ADDRESS}`) |
| 169 | + }) |
| 170 | + |
| 171 | + it('should grant Nevermined the operator role', async () => { |
| 172 | + assert.isTrue( |
| 173 | + await nevermined.nfts1155.isOperatorOfDID( |
| 174 | + subscriptionDDO.id, |
| 175 | + nevermined.keeper.conditions.transferNftCondition.address, |
| 176 | + ), |
| 177 | + ) |
| 178 | + }) |
| 179 | + }) |
| 180 | + |
| 181 | + describe('As a subscriber I want to buy the OLAS plan', () => { |
| 182 | + it('I check the details of the subscription NFT', async () => { |
| 183 | + const details = await nevermined.nfts1155.details(subscriptionDDO.id) |
| 184 | + assert.equal(details.owner, publisher.getId()) |
| 185 | + }) |
| 186 | + |
| 187 | + it('I am ordering the subscription NFT', async () => { |
| 188 | + subsSalesService = subscriptionDDO.findServiceByType('nft-sales') |
| 189 | + console.debug(`Ordering service with index ${subsSalesService.index}`) |
| 190 | + agreementId = await nevermined.nfts1155.order( |
| 191 | + subscriptionDDO.id, |
| 192 | + SUBSCRIPTION_CREDITS, |
| 193 | + subscriber, |
| 194 | + ) |
| 195 | + |
| 196 | + assert.isDefined(agreementId) |
| 197 | + console.debug(`Agreement ID: ${agreementId}`) |
| 198 | + const subscriberBalanceAfter = IS_NATIVE_TOKEN |
| 199 | + ? await nevermined.client.public.getBalance({ address: subscriber.getId() }) |
| 200 | + : await token.balanceOf(subscriber.getId()) |
| 201 | + |
| 202 | + if (IS_NATIVE_TOKEN) { |
| 203 | + assert.isTrue(subscriberBalanceAfter < initialBalances.subscriber) |
| 204 | + } else { |
| 205 | + assert.equal(subscriberBalanceAfter, initialBalances.subscriber - AMOUNT_TOTAL) |
| 206 | + } |
| 207 | + }) |
| 208 | + |
| 209 | + it('The credits seller can check the payment and transfer the NFT to the subscriber', async () => { |
| 210 | + // Let's use the Node to mint the subscription and release the payments |
| 211 | + |
| 212 | + const creditsBalanceBefore = await subscriptionNFT.balance( |
| 213 | + subscriptionDDO.id, |
| 214 | + subscriber.getId(), |
| 215 | + ) |
| 216 | + console.log(`Credits Balance Before: ${creditsBalanceBefore}`) |
| 217 | + console.log( |
| 218 | + `Credits Balance Before (JSON): ${JSON.stringify(creditsBalanceBefore.toString())}`, |
| 219 | + ) |
| 220 | + assert.isTrue(creditsBalanceBefore === 0n) |
| 221 | + |
| 222 | + try { |
| 223 | + const receipt = await nevermined.nfts1155.claim( |
| 224 | + agreementId, |
| 225 | + publisher.getId(), |
| 226 | + subscriber.getId(), |
| 227 | + SUBSCRIPTION_CREDITS, |
| 228 | + subscriptionDDO.id, |
| 229 | + ) |
| 230 | + assert.isTrue(receipt) |
| 231 | + } catch (e) { |
| 232 | + console.error(e.message) |
| 233 | + assert.fail(e.message) |
| 234 | + } |
| 235 | + |
| 236 | + console.log(`Got the receipt`) |
| 237 | + const creditsBalanceAfter = await subscriptionNFT.balance( |
| 238 | + subscriptionDDO.id, |
| 239 | + subscriber.getId(), |
| 240 | + ) |
| 241 | + console.log(`Credits Balance After: ${creditsBalanceAfter}`) |
| 242 | + |
| 243 | + assert.equal(creditsBalanceAfter, creditsBalanceBefore + SUBSCRIPTION_CREDITS) |
| 244 | + }) |
| 245 | + |
| 246 | + it('the subscriber can check the balance with the new NFTs received', async () => { |
| 247 | + console.log( |
| 248 | + `Checking the balance of DID [${ |
| 249 | + subscriptionDDO.id |
| 250 | + }] of the subscriber ${subscriber.getId()}`, |
| 251 | + ) |
| 252 | + const balanceAfter = await subscriptionNFT.balance(subscriptionDDO.id, subscriber.getId()) |
| 253 | + console.log(`Balance After Purchase is completed: ${balanceAfter}`) |
| 254 | + assert.isTrue(balanceAfter === SUBSCRIPTION_CREDITS) |
| 255 | + }) |
| 256 | + |
| 257 | + it('the editor and reseller can receive their payment', async () => { |
| 258 | + const tokenBalance = IS_NATIVE_TOKEN |
| 259 | + ? await nevermined.client.public.getBalance({ |
| 260 | + address: OLAS_MARKETPLACE_ADDRESS as `0x${string}`, |
| 261 | + }) |
| 262 | + : await token.balanceOf(OLAS_MARKETPLACE_ADDRESS) |
| 263 | + |
| 264 | + console.log(`OLAS Marketplace Token Balance: ${tokenBalance}`) |
| 265 | + console.log('Initial Balances: ', initialBalances) |
| 266 | + console.log('Amounts OLAS FEE: ', PLAN_FEE_MKT) |
| 267 | + |
| 268 | + assert.isTrue(tokenBalance > initialBalances.olas) |
| 269 | + }) |
| 270 | + }) |
| 271 | +}) |
0 commit comments