Withdraw NFT

Withdraw Loopring L2 NFT to Ethereum L1
When token is not deployed the fee is different:
deployInWithdraw: tokenInfo?.deploymentStatus === DEPLOYMENT_STATUS.NOT_DEPLOYED
Step 1: Get Account
Prepare Layer 2 account. Retrieve account information.
1
const {accInfo} = await LoopringAPI.exchangeAPI.getAccount({
2
owner: LOOPRING_EXPORTED_ACCOUNT.address,
3
});
4
console.log("accInfo:", accInfo);
Step 2: Get eddsaKey
Generate EdDSA key.
1
const eddsaKey = await signatureKeyPairMock(accInfo);
2
console.log("eddsaKey:", eddsaKey.sk);
Step 3: Get apiKey
Retrieve the account's API key.
1
const {apiKey} = await LoopringAPI.userAPI.getUserApiKey(
2
{
3
accountId: accInfo.accountId,
4
},
5
eddsaKey.sk
6
);
7
console.log("apiKey:", apiKey);
Step 4: Get storageId
Get NFT tokenId storageId
1
const storageId = await LoopringAPI.userAPI.getNextStorageId(
2
{
3
accountId: accInfo.accountId,
4
sellTokenId: LOOPRING_EXPORTED_ACCOUNT.nftTokenId,
5
},
6
apiKey
7
);
8
console.log("storageId:", storageId);
9
10
//Step 5. getUserNFTBalances
11
const {userNFTBalances} = await LoopringAPI.userAPI.getUserNFTBalances(
12
{accountId: LOOPRING_EXPORTED_ACCOUNT.accountId},
13
apiKey
14
);
15
const tokenInfo = userNFTBalances.find(
16
(item) =>
17
item.tokenAddress?.toLowerCase() ===
18
LOOPRING_EXPORTED_ACCOUNT.nftTokenAddress.toLowerCase() &&
19
item.nftId &&
20
web3.utils.hexToNumberString(item.nftId) ===
21
LOOPRING_EXPORTED_ACCOUNT.nftTokenId.toString()
22
);
Step 5: Withdraw fee
Get withdrawal fee.
If the tokenAddress hasn't been deployed, deployInWithdraw set to true will retrieve the deployment fee plus withdrawal fee.
If the tokenAddress is deployed, deployInWithdraw set to true will return withdrawal fee.
1
const fee = await LoopringAPI.userAPI.getNFTOffchainFeeAmt(
2
{
3
accountId: accInfo.accountId,
4
requestType: sdk.OffchainNFTFeeReqType.NFT_WITHDRAWAL,
5
tokenAddress: LOOPRING_EXPORTED_ACCOUNT.nftTokenAddress,
6
deployInWithdraw:
7
tokenInfo?.deploymentStatus === DEPLOYMENT_STATUS.NOT_DEPLOYED, // when token is not deploy the fee is diff
8
},
9
apiKey
10
);
11
console.log("fee:", fee);
Step 6: Withdraw
1
const response = await LoopringAPI.userAPI.submitNFTWithdraw({
2
request: {
3
exchange: LOOPRING_EXPORTED_ACCOUNT.exchangeAddress,
4
accountId: LOOPRING_EXPORTED_ACCOUNT.accountId,
5
counterFactualInfo: undefined,
6
hashApproved: "",
7
maxFee: {
8
tokenId: TOKEN_INFO.tokenMap["LRC"].tokenId,
9
amount: fee.fees["LRC"].fee ?? "9400000000000000000",
10
},
11
minGas: 0,
12
owner: LOOPRING_EXPORTED_ACCOUNT.address,
13
to: LOOPRING_EXPORTED_ACCOUNT.address,
14
storageId: 0,
15
token: {
16
tokenId: LOOPRING_EXPORTED_ACCOUNT.nftTokenId,
17
nftData: LOOPRING_EXPORTED_ACCOUNT.nftData,
18
amount: "1",
19
},
20
validUntil: 0,
21
},
22
web3,
23
chainId: sdk.ChainId.GOERLI,
24
walletType: sdk.ConnectorNames.MetaMask,
25
eddsaKey: eddsaKey.sk,
26
apiKey,
27
});
28
console.log("response:", response);
Congratulations! You have just withdrawn your NFT to Layer 1 from Loopring Layer 2!
Last modified 3mo ago