GithubHelp home page GithubHelp logo

nikhils9 / cip68-nfts Goto Github PK

View Code? Open in Web Editor NEW
3.0 3.0 2.0 84 KB

A project to kickstart minting and updating CIP68 NFTs

License: GNU General Public License v3.0

TypeScript 78.71% Gleam 21.29%

cip68-nfts's People

Contributors

nikhils9 avatar

Stargazers

 avatar

Watchers

 avatar

Forkers

besiwims wimsio

cip68-nfts's Issues

unexpected type tag at position 0

Hi, I am trying to use your cip68 Aiken smart contract but I am getting the error:

Redeemer (Mint, 0): The provided Plutus code called 'error'.

ExBudget {
    mem: 167311,
    cpu: 69164532,
}

code

export const readValidator = (
  blueprint: Blueprint,
  title: string
): Validator => {
  const validator = blueprint.validators.find((v) => v.title === title);
  if (!validator) {
    throw new Error('Gift Card validator not found');
  }
  return {
    type: 'PlutusV2',
    script: validator.compiledCode,
  };
};
export const applyValidatorsParams = (
  rawValidator: Validator,
  params: Data[],
  Lucid: Lucid
) => {
  const script = applyParamsToScript(rawValidator.script, params);
  const validator: Validator = {
    type: 'PlutusV2',
    // script: toHex(cbor.encode(fromHex(script))),
    script: applyDoubleCborEncoding(script),
  };

  const policyID = Lucid.utils.validatorToScriptHash(validator);

  const lockedAddress = Lucid.utils.validatorToAddress(validator);

  return {
    validator,
    policyID,
    lockedAddress,
  };
};

When I change the script: applyDoubleCborEncoding(script), to script: toHex(cbor.encode(fromHex(script))), I get different error

{
    "message": "Redeemer (Mint, 0): unexpected type tag at position 0: expected bytes (definite length)"
}

building transaction

export default async function handler(
  req: NextApiRequestToken,
  res: NextApiResponse
) {
  const { meerkatAddress, metadata } = req.body;
  console.log(meerkatAddress);
  console.log(metadata);
  const lucid = await Lucid.new(
    new Blockfrost(
      "https://cardano-preprod.blockfrost.io/api/v0/",
      "preprod"
    ),
    "Preprod"
  );
  // const privateKey = lucid.utils.generatePrivateKey();
  // console.log(privateKey)
  // const issuerAddress = await lucid
  //   .selectWalletFromPrivateKey(
  //     "ed25519_sk"
  //   )
  //   .wallet.address();
  // console.log(issuerAddress);

  const privateKey = C.PrivateKey.from_bytes(
    fromHex(
      "5820"
    )
  );
  const issuerAddress = await lucid
    .selectWalletFromPrivateKey(privateKey.to_bech32())
    .wallet.address();

    
  lucid.selectWalletFrom({ address: meerkatAddress });

  const userUtxos = await lucid.utxosAt(meerkatAddress);

  if (!userUtxos || !userUtxos.length) {
    console.error(`No UTxO found at user address: ${meerkatAddress}`);
  }

  const publicKeyHash =
    lucid.utils.getAddressDetails(issuerAddress).paymentCredential?.hash!;

  const store = applyValidatorsParams(
    readValidator(blueprint, "store.store"),
    [publicKeyHash],
    lucid
  );
  const minter = applyValidatorsParams(
    readValidator(blueprint, "minter.mint"),
    [publicKeyHash, store.policyID],
    lucid
  );

  const selectedUtxo = getUtxoWithAssets(userUtxos, { lovelace: 5000000n });
  console.log(store.lockedAddress);
  // const assetNameSuffix = await getUniqueAssetName(userUtxos[0]!);
  const assetNameSuffix = await getUniqueAssetName(selectedUtxo);
  const refNFT = toUnit(
    minter.policyID,
    assetNameSuffix,
    REFERENCE_TOKEN_LABEL
  );

  const userNFT = toUnit(
    minter.policyID,
    assetNameSuffix,
    NON_FUNGIBLE_TOKEN_LABEL
  );
  console.log(refNFT);
  console.log(userNFT);
  const metadataData = Data.fromJson({ metadata });
  const version = 1n;
  const extra = Data.void();
  const metadatum = Data.to(new Constr(0, [metadataData, version, extra]));
  const mintRdmr = Data.to(new Constr(0, []));

  // TODO: Instead of selecting all utxos at script and user addresses, select based on tx costs
  try {
    const tx = await lucid
      .newTx()
      .collectFrom([selectedUtxo])
      .mintAssets({ [userNFT]: 1n, [refNFT]: 1n }, mintRdmr)
      .attachMintingPolicy(minter.validator)
      .payToAddress(meerkatAddress, { [userNFT]: 1n })
      .payToContract(
        store.lockedAddress,
        { inline: metadatum },
        {
          [refNFT]: 1n,
        }
      )
      .addSigner(issuerAddress)
      .addSigner(meerkatAddress)
      .validFrom(Date.now() - 60 * 1000) // Substracting 1 minute to offset diff (blockfrost server time - local system time)
      .validTo(Date.now() + 15 * 60 * 2000)
      .complete({
        change: { address: meerkatAddress },
        coinSelection: false, // Setting to false to avoid using distributor funds
      });
    console.log("I am here");
    const partialSignedTx = await tx.partialSign();

    res.status(200).json({ tx, partialSignedTx });
  } catch (error: any) {
    console.log(error);
    res.status(404).json({ message: error });
  }
}

I appreciate your help. thank you.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.