1

i am building a test NFT-minting website on replit.co as a nodeJS project with an Express server

i am willing to use the Metaplex JS SDK and its Candy Machines in order to do so

i instantiated a Metaplex object like so

const m = require("@metaplex-foundation/js");
const s = require("@solana/web3.js");

// i couldn't figure out a way to use import statements somehow
// it might have something to see with this message i get anytime i run my app:
// bigint: Failed to load bindings, pure JS will be used

const connection = new s.Connection(s.clusterApiUrl("devnet"));

const mainAuthority = s.Keypair.generate();

// below i define a makeMetaplex() function rather than directly a metaplex variable as in documentation
// because the arrow function wouldn't return and i'd have to use metaplex().candyMachines()
// rather than metaplex.candyMachines() every time which i find inelegant
// it makes me feel like i can't use ES features somehow and that might be the root of all 
// the evil i'm going through
 
const makeMetaplex = () => {
  return m.Metaplex.make(connection)
    .use(m.keypairIdentity(mainAuthority))
    .use(m.bundlrStorage());
};
const metaplex = makeMetaplex();

// i also had to remove all 'await' syntax compared to documentation otherwise i'd get an error telling me
//  i can only use it in top levels of modules (which i'm supposed to be on) or async functions
// maybe this is the problem or part of it  

and created a collection NFT like so

const collectionAuthority = s.Keypair.generate();

// below when i put 'const { nft: collectionNft }' like in documentation
// i got an error later in the code saying collectionNft was undefined
// i guess that could be the problem too
// i am unfamiliar with this syntax which looks a bit like the desctructuring syntax i learned 
// on FreeCodeCamp but not quite or i'm missing something

const collectionNft = metaplex.nfts().create({
  name: "ULT:VOID Collection NFT",
  uri: "",
  sellerFeeBasisPoints: 0,
  isCollection: true,
  updateAuthority: collectionAuthority,
});

then i set up my Candy Machine settings like so

const candyMachineSettings = {
  authority: mainAuthority,
  sellerFeeBasisPoints: 200
  symbol: "MYNFT",
  maxEditionSupply: m.toBigNumber(0),
  isMutable: true,
  collection: {
    address: collectionNft.address, // that's when i'd get an 'unedefined' error for collectionNft
    updateAuthority: collectionAuthority,
  },
  itemsAvailable: m.toBigNumber(1111),
  itemSettings: {
    type: "configLines",
    prefixName: "My NFT #$ID+1$",
    nameLength: 0,
    prefixUri: "", //"https://arweave.net/",
    uriLength: null,//43,
    isSequential: true,
  }
};

when i now create my Candy Machine here's what it logs for the returned Object and one of its alleged properties

// line below i got rid of await as mentioned earlier
const candyMachine = metaplex.candyMachines().create(candyMachineSettings); 

console.log(candyMachine); // logs Promise { <Pending> }
console.log(candyMachine.address); // logs undefined

when i mouse over my candyMachine variable a tooltip from Replit says

Promise<m.CreateCandyMachineOutput<m.CandyGuardDefaultSettings>>

i get the strong feeling that my basic JS skills have reached their limits and that there is something beyond the scope of my comprehension of the language and.or framework and.or Metaplex tool that i'm missing

any help?

1 Answer 1

0

Promises are core to the async/await paradigm in js. They allow you to run multiple time consuming functions in parallel. More info here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise

In this specific case you'll need to wait for the Promise to fulfill (job to complete) by awaiting it:

await metaplex.candyMachines().create(candyMachineSettings)

You can only await Promises inside async functions. If you're running a simple script I usually format like this to make it async:

async function run() {
    await metaplex.candyMachines().create(candyMachineSettings)
}

run().then(() => {
    console.log("Done")
    process.exit(0)
}).catch((err) => {
    console.error(err)
    process.exit(1)
})
1
  • thank you for helping me to get that! all my intuitions were right so a lot was going wrong in the code i mentioned above and indeed i was returned a pending Promise because i was not 'await'-ing for it to fulfill Dec 7, 2022 at 1:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.