Skip to main content

Withdraw ERC721 Assets

Learn how to withdraw ERC721 assets from Myria L2 to the Ethereum L1 network.

Prerequisites

  • Mint or buy at least one ERC721 asset.

Withdrawal process

Withdrawal process of ERC721 assets includes four steps:

  1. Get a list of assets to withdraw
  2. Initiate the withdrawal
  3. Get the withdrawals list
  4. Complete the withdrawal

Implementation

info

You can find the full React implementation for ERC721 withdrawals in the Myria React Samples repository.

1. Set up a new React project

Withdrawal of ERC721 tokens requires client-side interaction with the MetaMask wallet. It's recommended to use React to implement such behavior.

You can create a React app via Create React App. Note, the below project relies on Web3.js library and needs custom configuration.

2. Create a myria-client.ts helper

It's recommended to have a separate .ts file for quick access to the MyriaClient.

For more details, use the Myria Core SDK overview.

3. Retrieve the assets

First, get a list of available assets in your wallet. You can use the getAssetByStarkKey() function of the OnchainAssetManager.

The getMyriaErc721ByStarkKey() function has the following parameters:

  • client: MyriaClient instance
  • starkKey: Stark Key of your wallet
import { MyriaClient, OnchainAssetManager } from "myria-core-sdk";

export async function getMyriaErc721ByStarkKey(client: MyriaClient, starkKey: string) {
const assetManager: OnchainAssetManager = new OnchainAssetManager(client);

return await assetManager.getAssetByStarkKey(starkKey).then((data: any) => {
assets = data.data.items
});
}

4. Initiate a withdrawal

You can initiate a withdrawal using the withdrawNftOffChain() function of the WithdrawalModule.

The withdrawErc721() function has the following parameters:

  • client: MyriaClient instance
  • nft: an asset to withdraw
  • account: a public key of your wallet
  • starkKey: a Stark Key of your wallet
import { MyriaClient, WithdrawalModule, WithdrawNftOffChainParams } from "myria-core-sdk";

export async function withdrawErc721(client: MyriaClient, asset: any, account: string, starkKey: string) {
const withdrawalModule: WithdrawalModule = new WithdrawalModule(client);

const params: WithdrawNftOffChainParams = {
id: asset.id,
tokenId: asset.tokenId,
tokenAddress: asset.tokenAddress,
senderVaultId: asset.vaultId,
senderPublicKey: starkKey,
receiverPublicKey: account,
assetId: asset.assetId,
quantizedAmount: asset.amount
}

return await withdrawalModule.withdrawNftOffChain(params);
}

5. Get a list of withdrawals

You can retrieve a list of withdrawals using the getTransactionList() function of the TransactionManager.

The getWithdrawalsList() function has the following parameters:

  • client: MyriaClient instance
  • starkKey: a Stark Key of your wallet
import { MyriaClient, TransactionManager } from "myria-core-sdk";

export async function getWithdrawalsList(client: MyriaClient, starkKey: string) {
const trxManager: TransactionManager = new TransactionManager(client);

return await trxManager.getTransactionList(starkKey)
.then((data) => {
if (data.data) {
withdrawals = data.data
.filter((item: any) => item.transactionType === "WithdrawalRequest" && item.transactionStatus === "Success")
}
});
}

6. Complete a withdrawal

You can complete a withdrawal using the withdrawAndMint() function of the TransactionManager.

The completeErc721Withdrawal() function has the following parameters:

  • client: MyriaClient instance
  • account: a public key of your wallet
  • asset: an asset to withdraw
  • starkKey: a Stark Key of your wallet
import { MyriaClient, TokenType, WithdrawalModule, WithdrawAndMintParams } from "myria-core-sdk";

export async function completeErc721Withdrawal(client: MyriaClient, account: string, asset: any, starkKey: string) {
const withdrawalModule: WithdrawalModule = new WithdrawalModule(client);

const params: WithdrawAndMintParams = {
starkKey: starkKey,
walletAddress: account,
tokenType: TokenType.MINTABLE_ERC721,
assetType: asset.assetType,
tokenAddress: asset.tokenAddress,
mintingBlob: asset.blueprint
}

return await withdrawalModule.withdrawAndMint(params);
}