Closed mikacmn closed 5 months ago
Same
Basically, I put the image and metadata.json files on my server/domain.
const fs = require('fs');
const {createToken} = require('./src/create_token.js')
const { NFTStorage, Blob,File} = require ('nft.storage')
const {
NFT_STORAGE_TOKEN,
revokeMintBool,
revokeFreezeBool,
tokenInfo,
metaDataforToken
} = require('./config.js')
async function main() {
// uploadMetaData
const metadata_url = await uploadMetaData()
if (!metadata_url){
console.log("Metadata failed")
return;
}
tokenInfo.metadata = metadata_url
// Create token
console.log("Creating Token...")
const mintAddress = await createToken(tokenInfo, revokeMintBool, revokeFreezeBool)
console.log(`Mint Link: https://solscan.io/token/${mintAddress.toString()}`)
}
async function uploadMetaData() {
// console.log('cow')
const imageUrl = `https://domain.mine/image.png`
metaDataforToken.image = imageUrl
// // store as a json file
// run this to generate metadata.json
// const jsonString = JSON.stringify(metaDataforToken, null, 2);
// const file = new File([jsonString], "metadata.json", {type: "application/json"});
// try {
// const filePath = './metadata.json';
// fs.writeFileSync(filePath, jsonString);
// console.log('File saved successfully:', filePath);
// } catch (error) {
// console.error('Error saving file:', error);
// }
const metadata_url = `https://domain.mine/metadata.json`
console.log('Metadata URI: ', metadata_url)
return metadata_url
}
main()
src/createtoken also has an error fix like this:
const {
Keypair,
PublicKey,
SystemProgram,
Transaction,
sendAndConfirmTransaction,
} = require("@solana/web3.js");
const {
AuthorityType,
MINT_SIZE,
TOKEN_PROGRAM_ID,
getMinimumBalanceForRentExemptMint,
getAssociatedTokenAddress,
createInitializeMintInstruction,
createAssociatedTokenAccountInstruction,
createMintToInstruction,
createSetAuthorityInstruction,
} = require("@solana/spl-token");
const {
createCreateMetadataAccountV3Instruction,
PROGRAM_ID,
} = require("@metaplex-foundation/mpl-token-metadata");
const { connection, myKeyPair } = require("../config.js");
async function createToken(tokenInfo, revokeMintBool, revokeFreezeBool) {
const lamports = await getMinimumBalanceForRentExemptMint(connection);
const mintKeypair = Keypair.generate();
const myPublicKey = myKeyPair.publicKey;
const tokenATA = await getAssociatedTokenAddress(
mintKeypair.publicKey,
myPublicKey
);
const createMetadataInstruction = createCreateMetadataAccountV3Instruction(
{
metadata: PublicKey.findProgramAddressSync(
[
Buffer.from("metadata"),
PROGRAM_ID.toBuffer(),
mintKeypair.publicKey.toBuffer(),
],
PROGRAM_ID
)[0],
mint: mintKeypair.publicKey,
mintAuthority: myPublicKey,
payer: myPublicKey,
updateAuthority: myPublicKey,
},
{
createMetadataAccountArgsV3: {
data: {
name: tokenInfo.tokenName,
symbol: tokenInfo.symbol,
uri: tokenInfo.metadata,
creators: null,
sellerFeeBasisPoints: 0,
uses: null,
collection: null,
},
isMutable: true,
collectionDetails: null,
},
}
);
let blockhash = await connection.getLatestBlockhash("finalized");
console.log("blockhash", blockhash);
const createNewTokenTransaction = new Transaction(blockhash).add(
SystemProgram.createAccount({
fromPubkey: myPublicKey,
newAccountPubkey: mintKeypair.publicKey,
space: MINT_SIZE,
lamports: lamports,
programId: TOKEN_PROGRAM_ID,
}),
createInitializeMintInstruction(
mintKeypair.publicKey,
tokenInfo.decimals,
myPublicKey,
myPublicKey,
TOKEN_PROGRAM_ID
),
createAssociatedTokenAccountInstruction(
myPublicKey,
tokenATA,
myPublicKey,
mintKeypair.publicKey
),
createMintToInstruction(
mintKeypair.publicKey,
tokenATA,
myPublicKey,
tokenInfo.amount * Math.pow(10, tokenInfo.decimals)
),
createMetadataInstruction
);
console.log("Previous Error: resolved")
createNewTokenTransaction.feePayer = myKeyPair.publicKey;
if (revokeMintBool) {
let revokeMint = createSetAuthorityInstruction(
mintKeypair.publicKey, // mint acocunt || token account
myPublicKey, // current auth
AuthorityType.MintTokens, // authority type
null
);
createNewTokenTransaction.add(revokeMint);
}
if (revokeFreezeBool) {
let revokeFreeze = createSetAuthorityInstruction(
mintKeypair.publicKey, // mint acocunt || token account
myPublicKey, // current auth
AuthorityType.FreezeAccount, // authority type
null
);
createNewTokenTransaction.add(revokeFreeze);
}
// createNewTokenTransaction.recentBlockhash = blockhash;
const signature = await sendAndConfirmTransaction(
connection,
createNewTokenTransaction,
[myKeyPair, mintKeypair]
);
console.log("Token mint transaction sent. Signature:", signature);
console.log("Token Created : ", tokenInfo);
console.log("Token Mint Address :", mintKeypair.publicKey.toString());
return mintKeypair.publicKey;
}
module.exports = {
createToken,
};
@tundachef Did it cost any Sol to create the new token? I am new to this.
@tundachef Did it cost any Sol to create the new token? I am new to this.
Of course it did, but about 3 USD worth of solana, including revoke freeze authority and mint authority
@tundachef thanks mate. But if it's creating new wallets on the fly, how may one add some Sol to it? Or do you recommend using an existing keypair?
const mintKeypair = Keypair.generate();
const myPublicKey = myKeyPair.publicKey;
No it only uses your public key follow the repository instructions
I got it right after I asked you. Thanks a million! 👍
Getting this error when running it:
C:\Users\Administrator\Desktop\Solana-Token-Creation-main>node ./main.js bigint: Failed to load bindings, pure JS will be used (try npm run rebuild?) node:internal/process/promises:289 triggerUncaughtException(err, true / fromPromise /); ^
Error at pRetry__default.default.retries (C:\Users\Administrator\Desktop\Solana-Token-Creation-main\node_modules\nft.storage\dist\src\lib.cjs:242:23) at process.processTicksAndRejections (node:internal/process/task_queues:95:5) at async RetryOperation._fn (C:\Users\Administrator\Desktop\Solana-Token-Creation-main\node_modules\p-retry\index.js:50:12)
Node.js v20.12.2
Can't seem to find a fix Did everything like in documentation