Skip to content

Commit db42a2a

Browse files
authored
Merge pull request #741 from nevermined-io/test/olas_plan_reg
test: adding Olas e2e test with multiple receivers
2 parents aa8767b + c4284c0 commit db42a2a

File tree

3 files changed

+299
-4
lines changed

3 files changed

+299
-4
lines changed

CHANGELOG.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2407,7 +2407,7 @@ Generated by [`auto-changelog`](https://github.com/CookPete/auto-changelog).
24072407

24082408
> 2 May 2022
24092409
2410-
- add aave service agreement to ddo when creating nft721 asset [`#248`](https://github.com/nevermined-io/sdk-js/pull/248)
2410+
- add aave service agreement to ddo when creating nft721 asset [`#248`](https://github.com/nevermined-io/sdk-js/pull/248)
24112411
- Adding v0.19.21 Changelog updates [`c93cdc5`](https://github.com/nevermined-io/sdk-js/commit/c93cdc55f139a43db4130ccb0f80924d2645a931)
24122412

24132413
#### [v0.19.21](https://github.com/nevermined-io/sdk-js/compare/v0.19.20...v0.19.21)
@@ -2670,7 +2670,7 @@ Generated by [`auto-changelog`](https://github.com/CookPete/auto-changelog).
26702670

26712671
> 18 January 2022
26722672
2673-
- Removing not used parameter [`#186`](https://github.com/nevermined-io/sdk-js/pull/186)
2673+
- Removing not used parameter [`#186`](https://github.com/nevermined-io/sdk-js/pull/186)
26742674
- [wip] Create agreement+pay in one transaction [`#183`](https://github.com/nevermined-io/sdk-js/pull/183)
26752675
- Adapting to contracts `v1.3.3` [`#177`](https://github.com/nevermined-io/sdk-js/pull/177)
26762676
- Adding v0.17.2 Changelog updates [`5eddda4`](https://github.com/nevermined-io/sdk-js/commit/5eddda43954e013e6e6f7344c9a877d801aacb5c)
@@ -3176,7 +3176,7 @@ Generated by [`auto-changelog`](https://github.com/CookPete/auto-changelog).
31763176
- Feature/sync develop [`#268`](https://github.com/nevermined-io/sdk-js/pull/268)
31773177
- Quick fix for non-eip1559 networks [`#266`](https://github.com/nevermined-io/sdk-js/pull/266)
31783178
- integrate Permissions and refactor search query [`#264`](https://github.com/nevermined-io/sdk-js/pull/264)
3179-
- add aave service agreement to ddo when creating nft721 asset [`#248`](https://github.com/nevermined-io/sdk-js/pull/248)
3179+
- add aave service agreement to ddo when creating nft721 asset [`#248`](https://github.com/nevermined-io/sdk-js/pull/248)
31803180
- Get the keeper version from the artifacts instead of package version [`#244`](https://github.com/nevermined-io/sdk-js/pull/244)
31813181
- replace `metadata-api` url by `marketplace-api` and sort type [`#243`](https://github.com/nevermined-io/sdk-js/pull/243)
31823182
- fixing issues with BigNumbers [`#246`](https://github.com/nevermined-io/sdk-js/pull/246)
@@ -3215,7 +3215,7 @@ Generated by [`auto-changelog`](https://github.com/CookPete/auto-changelog).
32153215
- Feature/190 add did to sec markets [`#191`](https://github.com/nevermined-io/sdk-js/pull/191)
32163216
- Adding utility methods for getting nft token uri [`#189`](https://github.com/nevermined-io/sdk-js/pull/189)
32173217
- Lint ... [`#187`](https://github.com/nevermined-io/sdk-js/pull/187)
3218-
- Removing not used parameter [`#186`](https://github.com/nevermined-io/sdk-js/pull/186)
3218+
- Removing not used parameter [`#186`](https://github.com/nevermined-io/sdk-js/pull/186)
32193219
- [wip] Create agreement+pay in one transaction [`#183`](https://github.com/nevermined-io/sdk-js/pull/183)
32203220
- Adapting to contracts `v1.3.3` [`#177`](https://github.com/nevermined-io/sdk-js/pull/177)
32213221
- Correct typo in route [`#184`](https://github.com/nevermined-io/sdk-js/pull/184)

integration/external/Olas.e2e.test.ts

Lines changed: 271 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,271 @@
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+
})

test/config.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,30 @@ if (process.env.NETWORK_NAME === 'geth-localnet') {
3131
ipfsProjectSecret,
3232
verbose: logLevel,
3333
} as NeverminedOptions
34+
} else if (process.env.NETWORK_NAME === 'one-staging') {
35+
config = {
36+
chainId: 421614,
37+
marketplaceUri: 'https://marketplace-api.staging.nevermined.app',
38+
neverminedNodeUri: 'https://node.staging.nevermined.app',
39+
neverminedNodeAddress: '0x5838B5512cF9f12FE9f2beccB20eb47211F9B0bc',
40+
web3ProviderUri: `https://arbitrum-sepolia.infura.io/v3/${infuraToken}`,
41+
graphHttpUri: 'https://api.thegraph.com/subgraphs/name/nevermined-io/public',
42+
// verbose: LogLevel.Verbose,
43+
artifactsFolder: './artifacts',
44+
circuitsFolder: './circuits',
45+
} as NeverminedOptions
46+
} else if (process.env.NETWORK_NAME === 'gnosis') {
47+
config = {
48+
chainId: 100,
49+
marketplaceUri: 'https://marketplace-api.gnosis.nevermined.app',
50+
neverminedNodeUri: 'https://node.gnosis.nevermined.app',
51+
neverminedNodeAddress: '0x824dbcE5E9C96C5b8ce2A35a25a5ab87eD1D00b1',
52+
web3ProviderUri: `https://rpc.gnosischain.com/`,
53+
graphHttpUri: undefined,
54+
// verbose: LogLevel.Verbose,
55+
artifactsFolder: './artifacts',
56+
circuitsFolder: './circuits',
57+
} as NeverminedOptions
3458
} else {
3559
// default unit tests
3660
config = {

0 commit comments

Comments
 (0)