Skip to content
This repository has been archived by the owner on Jun 11, 2024. It is now read-only.

Lock and Unlock method for NFT #8561

Merged
merged 5 commits into from
Jun 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions framework/src/modules/nft/events/lock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
LENGTH_NFT_ID,
MAX_LENGTH_MODULE_NAME,
MIN_LENGTH_MODULE_NAME,
NftErrorEventResult,
NftEventResult,
} from '../constants';

Expand Down Expand Up @@ -58,4 +59,8 @@ export class LockEvent extends BaseEvent<LockEventData & { result: NftEventResul
data.nftID,
]);
}

public error(ctx: EventQueuer, data: LockEventData, result: NftErrorEventResult) {
this.add(ctx, { ...data, result }, [Buffer.from(data.module), data.nftID]);
}
}
125 changes: 125 additions & 0 deletions framework/src/modules/nft/method.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import { UserStore } from './stores/user';
import { DestroyEvent } from './events/destroy';
import { SupportedNFTsStore } from './stores/supported_nfts';
import { CreateEvent } from './events/create';
import { LockEvent } from './events/lock';

export class NFTMethod extends BaseMethod {
private _config!: ModuleConfig;
Expand Down Expand Up @@ -295,4 +296,128 @@ export class NFTMethod extends BaseMethod {
collectionID,
});
}

public async lock(methodContext: MethodContext, module: string, nftID: Buffer): Promise<void> {
const nftStore = this.stores.get(NFTStore);

const nftExists = await nftStore.has(methodContext, nftID);

if (!nftExists) {
this.events.get(LockEvent).error(
methodContext,
{
module,
nftID,
},
NftEventResult.RESULT_NFT_DOES_NOT_EXIST,
);

throw new Error('NFT substore entry does not exist');
}

const owner = await this.getNFTOwner(methodContext, nftID);

if (owner.length === LENGTH_CHAIN_ID) {
this.events.get(LockEvent).error(
methodContext,
{
module,
nftID,
},
NftEventResult.RESULT_NFT_ESCROWED,
);

throw new Error('NFT is escrowed to another chain');
}

const userStore = this.stores.get(UserStore);
const userKey = userStore.getKey(owner, nftID);
const userData = await userStore.get(methodContext, userKey);

if (userData.lockingModule !== NFT_NOT_LOCKED) {
this.events.get(LockEvent).error(
methodContext,
{
module,
nftID,
},
NftEventResult.RESULT_NFT_LOCKED,
);

throw new Error('NFT is already locked');
}

userData.lockingModule = module;

await userStore.set(methodContext, userKey, userData);

this.events.get(LockEvent).log(methodContext, {
module,
nftID,
});
}

public async unlock(methodContext: MethodContext, module: string, nftID: Buffer): Promise<void> {
const nftStore = this.stores.get(NFTStore);

const nftExists = await nftStore.has(methodContext, nftID);

if (!nftExists) {
this.events.get(LockEvent).error(
methodContext,
{
module,
nftID,
},
NftEventResult.RESULT_NFT_DOES_NOT_EXIST,
);

throw new Error('NFT substore entry does not exist');
}

const nftData = await nftStore.get(methodContext, nftID);

if (nftData.owner.length === LENGTH_CHAIN_ID) {
throw new Error('NFT is escrowed to another chain');
}

const userStore = this.stores.get(UserStore);
const userKey = userStore.getKey(nftData.owner, nftID);
const userData = await userStore.get(methodContext, userKey);
Incede marked this conversation as resolved.
Show resolved Hide resolved

if (userData.lockingModule === NFT_NOT_LOCKED) {
this.events.get(LockEvent).error(
methodContext,
{
module,
nftID,
},
NftEventResult.RESULT_NFT_NOT_LOCKED,
);

throw new Error('NFT is not locked');
}

if (userData.lockingModule !== module) {
this.events.get(LockEvent).error(
methodContext,
{
module,
nftID,
},
NftEventResult.RESULT_UNAUTHORIZED_UNLOCK,
);

throw new Error('Unlocking NFT via module that did not lock it');
}

userData.lockingModule = NFT_NOT_LOCKED;

await userStore.set(methodContext, userKey, userData);

this.events.get(LockEvent).log(methodContext, {
module,
nftID,
});
}
}
Loading