Skip to content

Commit

Permalink
Update remove nft script to remove all ref tokens in one tx
Browse files Browse the repository at this point in the history
Add datum type check to remove only cip68 nfts
  • Loading branch information
nikhils9 committed Sep 15, 2023
1 parent 3af4b55 commit 3544757
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 21 deletions.
5 changes: 3 additions & 2 deletions off-chain/mint-nft.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ if (!userUtxos || !userUtxos.length) {
}

// TODO Idx of utxo < 256
// Selecting a utxo containing atleast 4 ADA to cover tx fees and min ADA
const selectedUtxo = getUtxoWithAssets(userUtxos, { ["lovelace"]: 4000000n });

// Selecting a utxo containing atleast 5 ADA to cover tx fees and min ADA
const selectedUtxo = getUtxoWithAssets(userUtxos, { ["lovelace"]: 5000000n });

// Calculating asset name from the utxo which will be spent in the minting tx
const assetNameSuffix = await getUniqueAssetNameSuffix(selectedUtxo);
Expand Down
40 changes: 21 additions & 19 deletions off-chain/remove-nft.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
// deno-lint-ignore-file no-explicit-any
import { Constr, Data, fromHex, fromText, toHex, toUnit, UTxO } from "lucid";
import { Constr, Data } from "lucid";
import { createLucidInstance, getCredential } from "./utils/lucid/utils.ts";
import { crypto } from "crypto";
import {
APPLIED_VALIDATOR_PATH,
NON_FUNGIBLE_TOKEN_LABEL,
REFERENCE_TOKEN_LABEL,
} from "./common/constants.ts";
import { APPLIED_VALIDATOR_PATH } from "./common/constants.ts";
import { AppliedValidator, MetaDatum } from "./common/types.ts";

const lucid = await createLucidInstance();
Expand All @@ -22,20 +16,28 @@ lucid.selectWalletFromPrivateKey(await getCredential("issuer.sk"));

const userAddr = await getCredential("user.addr");
const rdmr = Data.to(new Constr(1, []));
const utxo = scriptUtxos[0];
// const utxo = scriptUtxos[0];

const tx = lucid
.newTx()
.collectFrom(scriptUtxos, rdmr)
.payToAddressWithData(userAddr, { inline: utxo.datum! }, utxo.assets);

// scriptUtxos.forEach((utxo) => {
// if (utxo.datum) {
// tx.payToAddressWithData(userAddr, { inline: utxo.datum }, utxo.assets);
// // console.log(utxo.datum);
// // console.log(Data.from(utxo.datum));
// } else console.log("UTxO without datum found" + utxo);
// });
.collectFrom(scriptUtxos, rdmr);

scriptUtxos.forEach((utxo) => {
if (utxo.datum) {
// Deserialze to MetaDatum to confirm datum type. UTxOs with malformed or
// different datum types cannot be removed.
try {
const _datum = Data.from<MetaDatum>(utxo.datum, MetaDatum);
tx.payToAddressWithData(userAddr, { inline: utxo.datum }, utxo.assets);
} catch (e) {
console.log(
"Cannot cast the datum of utxo to object of type MetaDatum. " +
e.message,
);
console.log(utxo);
}
} else console.log("UTxO without inline datum found: " + utxo);
});

const completedTx = await tx.addSigner(await lucid.wallet.address())
.attachSpendingValidator(storeValidator.validator)
Expand Down

0 comments on commit 3544757

Please sign in to comment.