-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathClaimPaymentAbortedAgreement.test.ts
308 lines (250 loc) Β· 11.2 KB
/
ClaimPaymentAbortedAgreement.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
import { assert } from 'chai'
import { decodeJwt, JWTPayload } from 'jose'
import { Nevermined } from '../../src/nevermined/Nevermined'
import { generateId } from '../../src/common/helpers'
import { DDO } from '../../src/ddo/DDO'
import { NvmAccount } from '../../src/models/NvmAccount'
import { TransferNFT721Condition, Token, Nft721Contract, ContractHandler } from '../../src/keeper'
import config from '../../test/config'
import { getMetadata } from '../utils'
import '../globals'
import { mineBlocks } from '../utils/utils'
import { ConditionState } from '../../src/types/ContractTypes'
import { AssetPrice } from '../../src/models/AssetPrice'
import { NFTAttributes } from '../../src/models/NFTAttributes'
describe('Claim aborted agreements End-to-End', () => {
let publisher: NvmAccount
let collector1: NvmAccount
let other: NvmAccount
let nevermined: Nevermined
let token: Token
let transferNft721Condition: TransferNFT721Condition
let ddo: DDO
const metadata = getMetadata(Math.random(), generateId())
let agreementId: string
// Configuration of First Sale:
// Publisher -> Collector1, other account get a cut (25%)
let nftPrice = 20n
let amounts = [15n, 5n]
let receivers: string[]
let assetPrice1: AssetPrice
let scale: bigint
let neverminedNodeAddress: string
let nft
let nftContract: Nft721Contract
let payload: JWTPayload
const accessTimeout = 10
const accessTimelock = 3
before(async () => {
nevermined = await Nevermined.getInstance(config)
;[, publisher, collector1, , other] = nevermined.accounts.list()
const networkName = await nevermined.keeper.getNetworkName()
const erc721ABI = await ContractHandler.getABIArtifact(
'NFT721Upgradeable',
config.artifactsFolder,
networkName,
)
nft = await nevermined.utils.blockchain.deployAbi(erc721ABI, publisher, [
publisher.getId(),
nevermined.keeper.didRegistry.address,
'NFT721',
'NVM',
'',
'0',
nevermined.keeper.nvmConfig.address,
])
nftContract = await Nft721Contract.getInstance(
(nevermined.keeper as any).instanceConfig,
await nft.address,
)
await nevermined.contracts.loadNft721(nftContract.address)
const clientAssertion = await nevermined.utils.jwt.generateClientAssertion(publisher)
await nevermined.services.marketplace.login(clientAssertion)
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
payload = decodeJwt(config.marketplaceAuthToken!)
neverminedNodeAddress = await nevermined.services.node.getProviderAddress()
metadata.userId = payload.sub
// conditions
;({ transferNft721Condition } = nevermined.keeper.conditions)
// components
;({ token } = nevermined.keeper)
scale = 10n ** BigInt(await token.decimals())
nftPrice = nftPrice * scale
amounts = amounts.map((v) => v * scale)
receivers = [publisher.getId(), other.getId()]
assetPrice1 = new AssetPrice(
new Map([
[receivers[0], amounts[0]],
[receivers[1], amounts[1]],
]),
)
await nftContract.grantOperatorRole(transferNft721Condition.address, publisher)
await nevermined.accounts.requestTokens(collector1, (nftPrice / scale) * 10n)
})
describe('As a publisher I want to register a new asset', () => {
it('I want to register a new asset and tokenize (via NFT). The sales agreement expires in a few blocks', async () => {
const nftAttributes = NFTAttributes.getNFT721Instance({
metadata,
providers: [neverminedNodeAddress],
services: [
{
serviceType: 'nft-sales',
price: assetPrice1,
},
{
serviceType: 'nft-access',
},
],
nftContractAddress: nftContract.address,
fulfillAccessTimeout: accessTimeout,
fulfillAccessTimelock: accessTimelock,
})
ddo = await nevermined.nfts721.create(nftAttributes, publisher)
assert.isDefined(ddo)
console.log(ddo.id)
// Timeout & Timelock should only be set for the access condition
const nftSalesService = ddo.findServiceByType('nft-sales')
assert.equal(nftSalesService.attributes.serviceAgreementTemplate?.conditions[0].timeout, 0)
assert.equal(
nftSalesService.attributes.serviceAgreementTemplate?.conditions[1].timeout,
accessTimeout,
)
assert.equal(nftSalesService.attributes.serviceAgreementTemplate?.conditions[0].timeout, 0)
assert.equal(
nftSalesService.attributes.serviceAgreementTemplate?.conditions[1].timelock,
accessTimelock,
)
// Timeout & Timelock should not affect access services
const nftAccessService = ddo.findServiceByType('nft-access')
assert.equal(nftAccessService.attributes.serviceAgreementTemplate?.conditions[0].timeout, 0)
assert.equal(nftAccessService.attributes.serviceAgreementTemplate?.conditions[1].timeout, 0)
assert.equal(nftAccessService.attributes.serviceAgreementTemplate?.conditions[0].timeout, 0)
assert.equal(nftAccessService.attributes.serviceAgreementTemplate?.conditions[1].timelock, 0)
})
})
describe('As a user I want to buy a NFT with a timelock', () => {
it('I can not order the NFT until timelock happens', async () => {
const collector1BalanceBefore = await token.balanceOf(collector1.getId())
agreementId = await nevermined.nfts721.order(ddo.id, collector1)
assert.isDefined(agreementId)
const agreement = await nevermined.agreements.getAgreement(agreementId)
// Transfer NFT Condition must be Time Locked
assert.isFalse(
await nevermined.keeper.conditionStoreManager.isConditionTimeLocked(
agreement.conditionIds[0],
),
)
assert.isTrue(
await nevermined.keeper.conditionStoreManager.isConditionTimeLocked(
agreement.conditionIds[1],
),
)
assert.isFalse(
await nevermined.keeper.conditionStoreManager.isConditionTimeLocked(
agreement.conditionIds[2],
),
)
try {
assert.isTrue(
!(await nevermined.nfts721.claim(agreementId, publisher.getId(), collector1.getId())),
)
} catch (error) {
console.debug(`Unable to fullfill condition because timelock`)
}
const agreementStatusAfter =
await nevermined.keeper.templates.nftSalesTemplate.getAgreementStatus(agreementId)
assert.equal(agreementStatusAfter['lockPayment'].state, ConditionState.Fulfilled)
assert.equal(agreementStatusAfter['transferNFT'].state, ConditionState.Unfulfilled)
assert.equal(agreementStatusAfter['escrowPayment'].state, ConditionState.Unfulfilled)
const collector1BalanceAfter = await token.balanceOf(collector1.getId())
assert.equal(collector1BalanceAfter + nftPrice, collector1BalanceBefore)
})
it('I can order the NFT after the timelock', async () => {
await mineBlocks(nevermined, collector1, accessTimelock + 1)
const publisherBalanceBefore = await token.balanceOf(publisher.getId())
const collector1BalanceBefore = await token.balanceOf(collector1.getId())
const agreement = await nevermined.agreements.getAgreement(agreementId)
// Time Lock expired
assert.isFalse(
await nevermined.keeper.conditionStoreManager.isConditionTimeLocked(
agreement.conditionIds[0],
),
)
assert.isFalse(
await nevermined.keeper.conditionStoreManager.isConditionTimeLocked(
agreement.conditionIds[1],
),
)
assert.isFalse(
await nevermined.keeper.conditionStoreManager.isConditionTimeLocked(
agreement.conditionIds[2],
),
)
// Transfer NFT Condition is not timed out
assert.isFalse(
await nevermined.keeper.conditionStoreManager.isConditionTimedOut(
agreement.conditionIds[1],
),
)
await nevermined.nfts721.claim(agreementId, publisher.getId(), collector1.getId())
const agreementStatusAfter =
await nevermined.keeper.templates.nftSalesTemplate.getAgreementStatus(agreementId)
assert.equal(agreementStatusAfter['lockPayment'].state, ConditionState.Fulfilled)
assert.equal(agreementStatusAfter['transferNFT'].state, ConditionState.Fulfilled)
assert.equal(agreementStatusAfter['escrowPayment'].state, ConditionState.Fulfilled)
const publisherBalanceAfter = await token.balanceOf(publisher.getId())
const collector1BalanceAfter = await token.balanceOf(collector1.getId())
assert.equal(collector1BalanceBefore, collector1BalanceAfter)
assert.equal(publisherBalanceBefore + amounts[0], publisherBalanceAfter)
})
})
describe('As a user I want to buy a NFT with a timeout', () => {
it('I can not get the NFT after timeout so I claim back my funds', async () => {
const collector1BalanceBeforeOrder = await token.balanceOf(collector1.getId())
agreementId = await nevermined.nfts721.order(ddo.id, collector1)
const agreement = await nevermined.agreements.getAgreement(agreementId)
assert.isFalse(
await nevermined.keeper.conditionStoreManager.isConditionTimedOut(
agreement.conditionIds[1],
),
)
const collector1BalanceAfter = await token.balanceOf(collector1.getId())
assert.equal(collector1BalanceBeforeOrder - nftPrice, collector1BalanceAfter)
await mineBlocks(nevermined, collector1, accessTimeout + 1)
assert.isTrue(
await nevermined.keeper.conditionStoreManager.isConditionTimedOut(
agreement.conditionIds[1],
),
)
try {
assert.isFalse(
await nevermined.nfts721.claim(agreementId, publisher.getId(), collector1.getId()),
)
} catch (error) {
console.debug(`Unable to fullfill condition because timeout: ${error.message}`)
}
let agreementStatusAfter =
await nevermined.keeper.templates.nftSalesTemplate.getAgreementStatus(agreementId)
if (agreementStatusAfter['transferNFT'].state !== ConditionState.Aborted) {
// Condition is timed out so the collector aborts it and gets the escrowed amount
await nevermined.keeper.conditions.transferNft721Condition.abortByTimeOut(
agreement.conditionIds[1],
collector1,
)
agreementStatusAfter =
await nevermined.keeper.templates.nftSalesTemplate.getAgreementStatus(agreementId)
}
assert.equal(agreementStatusAfter['lockPayment'].state, ConditionState.Fulfilled)
assert.equal(agreementStatusAfter['transferNFT'].state, ConditionState.Aborted)
assert.equal(agreementStatusAfter['escrowPayment'].state, ConditionState.Unfulfilled)
assert.isTrue(await nevermined.nfts721.releaseRewards(agreementId, ddo.id, collector1))
const agreementStatusReleased =
await nevermined.keeper.templates.nftSalesTemplate.getAgreementStatus(agreementId)
assert.equal(agreementStatusReleased['lockPayment'].state, ConditionState.Fulfilled)
assert.equal(agreementStatusReleased['transferNFT'].state, ConditionState.Aborted)
assert.equal(agreementStatusReleased['escrowPayment'].state, ConditionState.Fulfilled)
const collector1BalanceReleased = await token.balanceOf(collector1.getId())
assert.equal(collector1BalanceBeforeOrder, collector1BalanceReleased)
})
})
})