Skip to content
This repository has been archived by the owner on Aug 28, 2023. It is now read-only.

TypeError: Cannot read property 'forEach' of undefined #5

Closed
dlm001 opened this issue Jul 15, 2021 · 15 comments
Closed

TypeError: Cannot read property 'forEach' of undefined #5

dlm001 opened this issue Jul 15, 2021 · 15 comments

Comments

@dlm001
Copy link

dlm001 commented Jul 15, 2021

Hey all,

Currently trying to mint a one-off CNFT using this amazing repo, but seem to run into an issue when attempting the minting process. More specifically, here is the error message I get when running my minting script:

/<MY-PATH>/node_modules/cardanocli-js/helper.js:145
  minting.action.forEach((mint, index, arr) => {
                 ^

TypeError: Cannot read property 'forEach' of undefined
    at exports.mintToString (/<MY-PATH>/node_modules/cardanocli-js/helper.js:145:18)
    at CardanocliJs.transactionBuildRaw (/<MY-PATH>/node_modules/cardanocli-js/index.js:854:39)
    at buildTransaction (/<MY-PATH>/src/mint-asset.js:99:25)
    at Object.<anonymous> (/<MY-PATH>/src/mint-asset.js:110:13)

See actual minting script below:

const cardano = require("./cardano")

// 1. Get the wallet

const wallet = cardano.wallet("<MY-WALLET-NAME>");

// 2. Define mint script

const mintScript = {
    type: "all",
    scripts: [
        {
            slot: 49483967,
            type: "before"
        },
        {
            keyHash: cardano.addressKeyHash(wallet.name),
            type: "sig"
        }
    ]
}

// 3. Create POLICY_ID

const POLICY_ID = cardano.transactionPolicyid(mintScript)

// 4. Define ASSET_NAME

const ASSET_NAME = "TestCNFT001"

// 5. Create ASSET_ID

const ASSET_ID = POLICY_ID + "." + ASSET_NAME

// 6. Define metadata

const metadata = {
    721: {
        [POLICY_ID]: {
            [ASSET_NAME]: {
                name: ASSET_NAME,
                image: "ipfs://<IPFS-HASH>",
                description: "Test CNFT 001",
                type: "image/png",
            }
        }
    }
}

// 7. Define transaction

const tx = {
    txIn: wallet.balance().utxo,
    txOut: [
        {
            address: wallet.paymentAddr,
            value: { ...wallet.balance().value, [ASSET_ID]: 1 }
        }
    ],
    mint: {
        actions: [{ type: "mint", quantity: 1, asset: ASSET_ID }],
        script: [mintScript]
    },
    metadata,
    witnessCount: 2
}

// 8. Build transaction

const buildTransaction = (tx) => {

    const raw = cardano.transactionBuildRaw(tx)
    const fee = cardano.transactionCalculateMinFee({
        ...tx,
        txBody: raw
    })

    tx.txOut[0].value.lovelace -= fee

    return cardano.transactionBuildRaw({ ...tx, fee })
}

const raw = buildTransaction(tx)

// 9. Sign transaction

const signTransaction = (wallet, tx) => {

    return cardano.transactionSign({
        signingKeys: [wallet.payment.skey, wallet.payment.skey],
        txBody: tx
    })
}

const signed = signTransaction(wallet, raw)

// 10. Submit transaction

const txHash = cardano.transactionSubmit(signed)

console.log(txHash)

Any clues as to why this error is being triggered?

@MarceloArraes
Copy link

Im having the same exact error! Do you figer out the solution? If yes i would be glad to hear!

@Ibrahim-Achkar
Copy link

Ibrahim-Achkar commented Sep 26, 2021

this transaction info worked for me:
const tx = { txIn: wallet.balance().utxo, txOut: [ { address: wallet.paymentAddr, value: { ...wallet.balance().value, [ASSET_ID]: 1 }, }, ], mint: [{ action: 'mint', quantity: 1, asset: ASSET_ID, script: mintScript }], metadata, witnessCount: 2, };

@MDragon84
Copy link

this transaction info worked for me: const tx = { txIn: wallet.balance().utxo, txOut: [ { address: wallet.paymentAddr, value: { ...wallet.balance().value, [ASSET_ID]: 1 }, }, ], mint: [{ action: 'mint', quantity: 1, asset: ASSET_ID, script: mintScript }], metadata, witnessCount: 2, };

I tried this... but "+undefined+1" is showing up after the payment address in the output.

I wish someone would repair this script properly...

@Ibrahim-Achkar
Copy link

hmm sorry i don't think i have any more help to give, are you on 4.0.1 of the cardanocli-js package?

@MarceloArraes
Copy link

There was a addition on this last update of cardano-cli.
The command: cardano-cli query utxo --address {address} --mainnet

Has changed it's output format recently. It now returns an extra field at the end of each TX.

@Ibrahim-Achkar
Copy link

i can send you both my minting script which works on the current version of the package if you would like?

@YazanAlhalabi
Copy link

i can send you both my minting script which works on the current version of the package if you would like?

@Development-Person Hey! for some reasons sending one asset back to a wallet doesn't work. is it working with you?
if yes, can I see how you handled the txInfo object?

Thanks!

@Ibrahim-Achkar
Copy link

Ibrahim-Achkar commented Oct 6, 2021

hello @YazanAlhalabi, i had some trouble getting the example send to work because the transaction ins and outs were not being balanced. See below for how I got around the issue. Please be careful I am not an expert in this so do all your testing on the testnet :)

//creating an object containing lovelace and all tokens on the wallet utxo
const walletBalance = {
    ...sender.balance().value,
};

//subtracting 1.5 ada to be sent along with NFT from
//the wallet UTXOValue.lovelace amount but leaving reference to all other
//tokens unchanged
walletBalance .lovelace = walletBalance .lovelace - cardano.toLovelace(1.5);

//setting value of NFT being sent from
//the wallet UTXOValue to 0
//but leaving reference to all other tokens unchanged
walletBalance [NFT] = 0;

//create raw transaction
const txInfo = {
  txIn: cardano.queryUtxo(sender.paymentAddr),
  txOut: [
    {
      address: sender.paymentAddr,
      value: { ...walletBalance }, //this value contains the correct amount of lovelace
      // left over after transaction, and sets the NFT being sent to 0
      //txIn will match txOut minus the payment
    }, //value going back to sender
    {
      address: receiver,
      value: {
        lovelace: cardano.toLovelace(1.5),
        [NFT]: 1,
      },
    },
  ],
  witnessCount: 1,
};

@MarceloArraes
Copy link

I will try everything tomorrow! Thank you @Development-Person. I'm more of a noob so any help is well received. Could you still send me the scripts you used? Would be a great step for me to win this error that is stopping me for weeks..

@Ibrahim-Achkar
Copy link

are you happy for me to email them to you? i will make them public on github at some point but not before they are properly tested as i dont want to lead people down the wrong path lol

@MarceloArraes
Copy link

That would be perfect! Send to marcelo.arraes@gmail.com. You don't have to worry about me because i'm playing on the Testnet for now! Thank you!

@MarceloArraes
Copy link

@Development-Person my email is Marcelo.arraes@gmail.com . Thank you for your help dude!

@AliWisam
Copy link

AliWisam commented Jan 3, 2022

@MarceloArraes Did you managed to resolve the issue?

@MarceloArraes
Copy link

@AliWisam yes! First of all you have to use the most recent version of the cardanocli-js, the one on the Berry repository https://github.com/Berry-Pool/cardanocli-js
Then you should make the transactions like the examples inside the Berry repository. Some things changed on the way we write the transactions from this version to the current.

@AstroWa3l
Copy link
Member

Have fixed most the issues for minting a nft using this repo. Going to close this issue now. Thanks for all your help 👍

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants