This repository has been archived by the owner on Jun 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 454
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 🏷️ Updates InteroperabilityMethod * 🌱 NFT TransferCrossChainCommand * ♻️ 📝 NFTErrorEventResult 🌱 DestroyEvent.error * 🏷️ Updates InteroperabilityMethod mock defintion * ♻️ Removes unwanted comments
- Loading branch information
Showing
10 changed files
with
682 additions
and
16 deletions.
There are no files selected for viewing
136 changes: 136 additions & 0 deletions
136
framework/src/modules/nft/commands/transfer_cross_chain.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
/* | ||
* 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 { validator } from '@liskhq/lisk-validator'; | ||
import { crossChainTransferParamsSchema } from '../schemas'; | ||
import { NFTStore } from '../stores/nft'; | ||
import { NFTMethod } from '../method'; | ||
import { LENGTH_CHAIN_ID, NFT_NOT_LOCKED } from '../constants'; | ||
import { TokenMethod } from '../../token'; | ||
import { InteroperabilityMethod } from '../types'; | ||
import { BaseCommand } from '../../base_command'; | ||
import { | ||
CommandExecuteContext, | ||
CommandVerifyContext, | ||
VerificationResult, | ||
VerifyStatus, | ||
} from '../../../state_machine'; | ||
import { InternalMethod } from '../internal_method'; | ||
|
||
export interface Params { | ||
nftID: Buffer; | ||
receivingChainID: Buffer; | ||
recipientAddress: Buffer; | ||
data: string; | ||
messageFee: bigint; | ||
messageFeeTokenID: Buffer; | ||
includeAttributes: boolean; | ||
} | ||
|
||
export class TransferCrossChainCommand extends BaseCommand { | ||
public schema = crossChainTransferParamsSchema; | ||
|
||
private _nftMethod!: NFTMethod; | ||
private _tokenMethod!: TokenMethod; | ||
private _interoperabilityMethod!: InteroperabilityMethod; | ||
private _internalMethod!: InternalMethod; | ||
|
||
public init(args: { | ||
nftMethod: NFTMethod; | ||
tokenMethod: TokenMethod; | ||
interoperabilityMethod: InteroperabilityMethod; | ||
internalMethod: InternalMethod; | ||
}): void { | ||
this._nftMethod = args.nftMethod; | ||
this._tokenMethod = args.tokenMethod; | ||
this._interoperabilityMethod = args.interoperabilityMethod; | ||
this._internalMethod = args.internalMethod; | ||
} | ||
|
||
public async verify(context: CommandVerifyContext<Params>): Promise<VerificationResult> { | ||
const { params } = context; | ||
|
||
validator.validate(this.schema, params); | ||
|
||
const nftStore = this.stores.get(NFTStore); | ||
const nftExists = await nftStore.has(context.getMethodContext(), params.nftID); | ||
|
||
if (!nftExists) { | ||
throw new Error('NFT substore entry does not exist'); | ||
} | ||
|
||
const owner = await this._nftMethod.getNFTOwner(context.getMethodContext(), params.nftID); | ||
|
||
if (owner.length === LENGTH_CHAIN_ID) { | ||
throw new Error('NFT is escrowed to another chain'); | ||
} | ||
|
||
const nftChainID = this._nftMethod.getChainID(params.nftID); | ||
|
||
if (!nftChainID.equals(context.chainID) && !nftChainID.equals(params.receivingChainID)) { | ||
throw new Error('NFT must be native to either the sending or the receiving chain'); | ||
} | ||
|
||
const messageFeeTokenID = await this._interoperabilityMethod.getMessageFeeTokenID( | ||
context.getMethodContext(), | ||
params.receivingChainID, | ||
); | ||
|
||
if (!params.messageFeeTokenID.equals(messageFeeTokenID)) { | ||
throw new Error('Mismatching message fee Token ID'); | ||
} | ||
|
||
if (!owner.equals(context.transaction.senderAddress)) { | ||
throw new Error('Transfer not initiated by the NFT owner'); | ||
} | ||
|
||
const lockingModule = await this._nftMethod.getLockingModule( | ||
context.getMethodContext(), | ||
params.nftID, | ||
); | ||
|
||
if (lockingModule !== NFT_NOT_LOCKED) { | ||
throw new Error('Locked NFTs cannot be transferred'); | ||
} | ||
|
||
const availableBalance = await this._tokenMethod.getAvailableBalance( | ||
context.getMethodContext(), | ||
context.transaction.senderAddress, | ||
params.messageFeeTokenID, | ||
); | ||
|
||
if (availableBalance < params.messageFee) { | ||
throw new Error('Insufficient balance for the message fee'); | ||
} | ||
|
||
return { | ||
status: VerifyStatus.OK, | ||
}; | ||
} | ||
|
||
public async execute(context: CommandExecuteContext<Params>): Promise<void> { | ||
const { params } = context; | ||
|
||
await this._internalMethod.transferCrossChainInternal( | ||
context.getMethodContext(), | ||
context.transaction.senderAddress, | ||
params.recipientAddress, | ||
params.nftID, | ||
params.receivingChainID, | ||
params.messageFee, | ||
params.data, | ||
params.includeAttributes, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.