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

Commit

Permalink
NFT module can not lock an NFT (#8992)
Browse files Browse the repository at this point in the history
* 👔 Removes validation logic from NFTMethod.getCollectionID

* 👔 NFTMethod.lock throws if locking module is nft

* Removes commented code

* Updates NFTMethod.lock
  • Loading branch information
has5aan authored Sep 15, 2023
1 parent 7c5ca58 commit c659e39
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
4 changes: 4 additions & 0 deletions framework/src/modules/nft/method.ts
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,10 @@ export class NFTMethod extends BaseMethod {
}

public async lock(methodContext: MethodContext, module: string, nftID: Buffer): Promise<void> {
if (module === NFT_NOT_LOCKED) {
throw new Error('Cannot be locked by NFT module');
}

const nftStore = this.stores.get(NFTStore);

const nftExists = await nftStore.has(methodContext, nftID);
Expand Down
29 changes: 17 additions & 12 deletions framework/test/unit/modules/nft/method.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ describe('NFTMethod', () => {

let methodContext!: MethodContext;

const lockingModule = 'token';
const nftStore = module.stores.get(NFTStore);
const userStore = module.stores.get(UserStore);
const supportedNFTsStore = module.stores.get(SupportedNFTsStore);
Expand Down Expand Up @@ -252,8 +253,6 @@ describe('NFTMethod', () => {
});

it('should return the lockingModule for the owner of the NFT', async () => {
const lockingModule = 'nft';

await nftStore.save(methodContext, nftID, {
owner,
attributesArray: [],
Expand Down Expand Up @@ -626,8 +625,14 @@ describe('NFTMethod', () => {
});

describe('lock', () => {
it('should throw if provided locking module is "nft"', async () => {
await expect(method.lock(methodContext, NFT_NOT_LOCKED, existingNFT.nftID)).rejects.toThrow(
'Cannot be locked by NFT module',
);
});

it('should throw and log LockEvent if NFT does not exist', async () => {
await expect(method.lock(methodContext, module.name, nftID)).rejects.toThrow(
await expect(method.lock(methodContext, lockingModule, nftID)).rejects.toThrow(
'NFT substore entry does not exist',
);

Expand All @@ -637,15 +642,15 @@ describe('NFTMethod', () => {
LockEvent,
0,
{
module: module.name,
module: lockingModule,
nftID,
},
NftEventResult.RESULT_NFT_DOES_NOT_EXIST,
);
});

it('should throw and log LockEvent if NFT is escrowed', async () => {
await expect(method.lock(methodContext, module.name, escrowedNFT.nftID)).rejects.toThrow(
await expect(method.lock(methodContext, lockingModule, escrowedNFT.nftID)).rejects.toThrow(
'NFT is escrowed to another chain',
);

Expand All @@ -655,7 +660,7 @@ describe('NFTMethod', () => {
LockEvent,
0,
{
module: module.name,
module: lockingModule,
nftID: escrowedNFT.nftID,
},
NftEventResult.RESULT_NFT_ESCROWED,
Expand All @@ -664,7 +669,7 @@ describe('NFTMethod', () => {

it('should throw and log LockEvent if NFT is locked', async () => {
await expect(
method.lock(methodContext, module.name, lockedExistingNFT.nftID),
method.lock(methodContext, lockingModule, lockedExistingNFT.nftID),
).rejects.toThrow('NFT is already locked');

checkEventResult<LockEventData>(
Expand All @@ -673,7 +678,7 @@ describe('NFTMethod', () => {
LockEvent,
0,
{
module: module.name,
module: lockingModule,
nftID: lockedExistingNFT.nftID,
},
NftEventResult.RESULT_NFT_LOCKED,
Expand All @@ -698,12 +703,12 @@ describe('NFTMethod', () => {
NftEventResult.RESULT_SUCCESSFUL,
);

const { lockingModule } = await userStore.get(
const { lockingModule: actualLockingModule } = await userStore.get(
methodContext,
userStore.getKey(existingNFT.owner, existingNFT.nftID),
);

expect(lockingModule).toEqual(expectedLockingModule);
expect(actualLockingModule).toEqual(expectedLockingModule);
});
});

Expand Down Expand Up @@ -785,12 +790,12 @@ describe('NFTMethod', () => {
NftEventResult.RESULT_SUCCESSFUL,
);

const { lockingModule } = await userStore.get(
const { lockingModule: expectedLockingModule } = await userStore.get(
methodContext,
userStore.getKey(lockedExistingNFT.owner, lockedExistingNFT.nftID),
);

expect(lockingModule).toEqual(NFT_NOT_LOCKED);
expect(expectedLockingModule).toEqual(NFT_NOT_LOCKED);
});
});

Expand Down

0 comments on commit c659e39

Please sign in to comment.