-
Notifications
You must be signed in to change notification settings - Fork 458
/
module.ts
314 lines (284 loc) · 11.4 KB
/
module.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
309
310
311
312
313
314
/*
* Copyright © 2023 Lisk Foundation
*
* See the LICENSE file at the top-level directory of this distribution
* for licensing information.
*
* Unless otherwise agreed in a custom licensing agreement with the Lisk Foundation,
* no part of this software, including this file, may be copied, modified,
* propagated, or distributed except according to the terms contained in the
* LICENSE file.
*
* Removal or modification of this copyright notice is prohibited.
*/
import { dataStructures } from '@liskhq/lisk-utils';
import { codec } from '@liskhq/lisk-codec';
import { validator } from '@liskhq/lisk-validator';
import { GenesisBlockExecuteContext } from '../../state_machine';
import { ModuleInitArgs, ModuleMetadata } from '../base_module';
import { BaseInteroperableModule } from '../interoperability';
import { InteroperabilityMethod, FeeMethod, GenesisNFTStore, TokenMethod } from './types';
import { NFTInteroperableMethod } from './cc_method';
import { NFTEndpoint } from './endpoint';
import { AllNFTsFromChainSupportedEvent } from './events/all_nfts_from_chain_suported';
import { AllNFTsFromChainSupportRemovedEvent } from './events/all_nfts_from_chain_support_removed';
import { AllNFTsFromCollectionSupportRemovedEvent } from './events/all_nfts_from_collection_support_removed';
import { AllNFTsFromCollectionSupportedEvent } from './events/all_nfts_from_collection_suppported';
import { AllNFTsSupportRemovedEvent } from './events/all_nfts_support_removed';
import { AllNFTsSupportedEvent } from './events/all_nfts_supported';
import { CcmTransferEvent } from './events/ccm_transfer';
import { CreateEvent } from './events/create';
import { DestroyEvent } from './events/destroy';
import { LockEvent } from './events/lock';
import { RecoverEvent } from './events/recover';
import { SetAttributesEvent } from './events/set_attributes';
import { TransferEvent } from './events/transfer';
import { TransferCrossChainEvent } from './events/transfer_cross_chain';
import { UnlockEvent } from './events/unlock';
import { InternalMethod } from './internal_method';
import { NFTMethod } from './method';
import {
isCollectionIDSupportedRequestSchema,
isCollectionIDSupportedResponseSchema,
getSupportedCollectionIDsResponseSchema,
getEscrowedNFTIDsRequestSchema,
getEscrowedNFTIDsResponseSchema,
getNFTRequestSchema,
getNFTResponseSchema,
getNFTsRequestSchema,
getNFTsResponseSchema,
hasNFTRequestSchema,
hasNFTResponseSchema,
isNFTSupportedRequestSchema,
isNFTSupportedResponseSchema,
genesisNFTStoreSchema,
} from './schemas';
import { EscrowStore } from './stores/escrow';
import { NFTStore } from './stores/nft';
import { SupportedNFTsStore } from './stores/supported_nfts';
import { UserStore } from './stores/user';
import { CrossChainTransferCommand as CrossChainTransferMessageCommand } from './cc_commands/cc_transfer';
import { TransferCrossChainCommand } from './commands/transfer_cross_chain';
import { TransferCommand } from './commands/transfer';
import {
ALL_SUPPORTED_NFTS_KEY,
LENGTH_ADDRESS,
LENGTH_CHAIN_ID,
MODULE_NAME_NFT,
} from './constants';
/**
* The `NFTModule` is used for creating, destroying NFTs (non-fungible tokens), and transferring them in the Lisk ecosystem.
*
* @see [LIP 0052 - Introduce NFT module](https://github.com/LiskHQ/lips/blob/main/proposals/lip-0052.md)
*/
export class NFTModule extends BaseInteroperableModule {
public method = new NFTMethod(this.stores, this.events);
public endpoint = new NFTEndpoint(this.stores, this.offchainStores);
public crossChainMethod = new NFTInteroperableMethod(this.stores, this.events);
public crossChainTransferCommand = new CrossChainTransferMessageCommand(this.stores, this.events);
public crossChainCommand = [this.crossChainTransferCommand];
private readonly _transferCommand = new TransferCommand(this.stores, this.events);
private readonly _ccTransferCommand = new TransferCrossChainCommand(this.stores, this.events);
private readonly _internalMethod = new InternalMethod(this.stores, this.events);
private _interoperabilityMethod!: InteroperabilityMethod;
private _feeMethod!: FeeMethod;
private _tokenMethod!: TokenMethod;
public commands = [this._transferCommand, this._ccTransferCommand];
/**
* Blockchain {@link events | Events} and {@link stores | Stores} of the module are registered in the constructor of a module, for later use in the module.
*/
public constructor() {
super();
this.events.register(TransferEvent, new TransferEvent(this.name));
this.events.register(TransferCrossChainEvent, new TransferCrossChainEvent(this.name));
this.events.register(CcmTransferEvent, new CcmTransferEvent(this.name));
this.events.register(CreateEvent, new CreateEvent(this.name));
this.events.register(DestroyEvent, new DestroyEvent(this.name));
this.events.register(DestroyEvent, new DestroyEvent(this.name));
this.events.register(LockEvent, new LockEvent(this.name));
this.events.register(UnlockEvent, new UnlockEvent(this.name));
this.events.register(SetAttributesEvent, new SetAttributesEvent(this.name));
this.events.register(RecoverEvent, new RecoverEvent(this.name));
this.events.register(AllNFTsSupportedEvent, new AllNFTsSupportedEvent(this.name));
this.events.register(AllNFTsSupportRemovedEvent, new AllNFTsSupportRemovedEvent(this.name));
this.events.register(
AllNFTsFromChainSupportedEvent,
new AllNFTsFromChainSupportedEvent(this.name),
);
this.events.register(
AllNFTsFromChainSupportRemovedEvent,
new AllNFTsFromChainSupportRemovedEvent(this.name),
);
this.events.register(
AllNFTsFromCollectionSupportedEvent,
new AllNFTsFromCollectionSupportedEvent(this.name),
);
this.events.register(
AllNFTsFromCollectionSupportRemovedEvent,
new AllNFTsFromCollectionSupportRemovedEvent(this.name),
);
this.stores.register(NFTStore, new NFTStore(this.name, 0));
this.stores.register(UserStore, new UserStore(this.name, 1));
this.stores.register(EscrowStore, new EscrowStore(this.name, 2));
this.stores.register(SupportedNFTsStore, new SupportedNFTsStore(this.name, 3));
}
/**
* @returns {@link MODULE_NAME_NFT}
*/
public get name(): string {
return MODULE_NAME_NFT;
}
/**
* Adds dependencies from other modules.
*
* This method should be called where the module is registered to the app (generally in the `app.ts` or `modules.ts` file).
*
* @param interoperabilityMethod {@link InteroperabilityMethod}
* @param feeMethod {@link FeeMethod}
* @param tokenMethod {@link TokenMethod}
*/
public addDependencies(
interoperabilityMethod: InteroperabilityMethod,
feeMethod: FeeMethod,
tokenMethod: TokenMethod,
) {
this._interoperabilityMethod = interoperabilityMethod;
this._feeMethod = feeMethod;
this._tokenMethod = tokenMethod;
this.method.addDependencies(this._internalMethod, feeMethod);
this._internalMethod.addDependencies(this.method, this._interoperabilityMethod, tokenMethod);
this.crossChainMethod.addDependencies(interoperabilityMethod);
this.endpoint.addDependencies(this.method);
}
public metadata(): ModuleMetadata {
return {
...this.baseMetadata(),
endpoints: [
{
name: this.endpoint.isCollectionIDSupported.name,
request: isCollectionIDSupportedRequestSchema,
response: isCollectionIDSupportedResponseSchema,
},
{
name: this.endpoint.getSupportedCollectionIDs.name,
response: getSupportedCollectionIDsResponseSchema,
},
{
name: this.endpoint.getEscrowedNFTIDs.name,
request: getEscrowedNFTIDsRequestSchema,
response: getEscrowedNFTIDsResponseSchema,
},
{
name: this.endpoint.getNFT.name,
request: getNFTRequestSchema,
response: getNFTResponseSchema,
},
{
name: this.endpoint.getNFTs.name,
request: getNFTsRequestSchema,
response: getNFTsResponseSchema,
},
{
name: this.endpoint.hasNFT.name,
request: hasNFTRequestSchema,
response: hasNFTResponseSchema,
},
{
name: this.endpoint.isNFTSupported.name,
request: isNFTSupportedRequestSchema,
response: isNFTSupportedResponseSchema,
},
],
assets: [],
};
}
// eslint-disable-next-line @typescript-eslint/require-await
public async init(args: ModuleInitArgs) {
const ownChainID = Buffer.from(args.genesisConfig.chainID, 'hex');
this._internalMethod.init({ ownChainID });
this.method.init({ ownChainID });
this.crossChainTransferCommand.init({
method: this.method,
internalMethod: this._internalMethod,
feeMethod: this._feeMethod,
});
this._ccTransferCommand.init({
internalMethod: this._internalMethod,
interoperabilityMethod: this._interoperabilityMethod,
nftMethod: this.method,
tokenMethod: this._tokenMethod,
});
this._transferCommand.init({ internalMethod: this._internalMethod });
}
public async initGenesisState(context: GenesisBlockExecuteContext): Promise<void> {
const assetBytes = context.assets.getAsset(this.name);
if (!assetBytes) {
return;
}
const genesisStore = codec.decode<GenesisNFTStore>(genesisNFTStoreSchema, assetBytes);
validator.validate(genesisNFTStoreSchema, genesisStore);
const nftIDKeySet = new dataStructures.BufferSet();
for (const nft of genesisStore.nftSubstore) {
if (![LENGTH_CHAIN_ID, LENGTH_ADDRESS].includes(nft.owner.length)) {
throw new Error(`nftID ${nft.nftID.toString('hex')} has invalid owner`);
}
if (nftIDKeySet.has(nft.nftID)) {
throw new Error(`nftID ${nft.nftID.toString('hex')} duplicated`);
}
const attributeSet: Record<string, number> = {};
for (const attribute of nft.attributesArray) {
attributeSet[attribute.module] = (attributeSet[attribute.module] ?? 0) + 1;
if (attributeSet[attribute.module] > 1) {
throw new Error(
`nftID ${nft.nftID.toString('hex')} has a duplicate attribute for ${
attribute.module
} module`,
);
}
}
nftIDKeySet.add(nft.nftID);
}
const allNFTsSupported = genesisStore.supportedNFTsSubstore.some(supportedNFTs =>
supportedNFTs.chainID.equals(ALL_SUPPORTED_NFTS_KEY),
);
if (genesisStore.supportedNFTsSubstore.length > 1 && allNFTsSupported) {
throw new Error(
'SupportedNFTsSubstore should contain only one entry if all NFTs are supported',
);
}
if (
allNFTsSupported &&
genesisStore.supportedNFTsSubstore[0].supportedCollectionIDArray.length !== 0
) {
throw new Error('supportedCollectionIDArray must be empty if all NFTs are supported');
}
const supportedChainsKeySet = new dataStructures.BufferSet();
for (const supportedNFT of genesisStore.supportedNFTsSubstore) {
if (supportedChainsKeySet.has(supportedNFT.chainID)) {
throw new Error(`chainID ${supportedNFT.chainID.toString('hex')} duplicated`);
}
supportedChainsKeySet.add(supportedNFT.chainID);
}
for (const nft of genesisStore.nftSubstore) {
const { owner, nftID, attributesArray } = nft;
await this._internalMethod.createNFTEntry(
context.getMethodContext(),
owner,
nftID,
attributesArray,
);
if (owner.length === LENGTH_CHAIN_ID) {
await this._internalMethod.createEscrowEntry(context.getMethodContext(), owner, nftID);
} else {
await this._internalMethod.createUserEntry(context.getMethodContext(), owner, nftID);
}
}
for (const supportedNFT of genesisStore.supportedNFTsSubstore) {
const { chainID, supportedCollectionIDArray } = supportedNFT;
const supportedNFTsSubstore = this.stores.get(SupportedNFTsStore);
await supportedNFTsSubstore.save(context, chainID, {
supportedCollectionIDArray,
});
}
}
}