-
Notifications
You must be signed in to change notification settings - Fork 2
/
createFungibleAsset.js
127 lines (104 loc) · 4.15 KB
/
createFungibleAsset.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
const dotenv = require("dotenv");
const { mplTokenMetadata, createFungibleAsset, mintV1, TokenStandard } = require("@metaplex-foundation/mpl-token-metadata");
const { Connection, clusterApiUrl, Keypair } = require("@solana/web3.js");
const { createNoopSigner, signerIdentity, percentAmount } = require('@metaplex-foundation/umi');
const { createUmi } = require('@metaplex-foundation/umi-bundle-defaults');
const { toWeb3JsTransaction } = require("@metaplex-foundation/umi-web3js-adapters");
const { nftStorageUploader } = require("@metaplex-foundation/umi-uploader-nft-storage");
const fs = require("fs");
dotenv.config();
const sftData = {
name: "Monkey",
symbol: "M-SFT",
description: "Monkey semi fungible token",
sellerFeeBasisPoints: 15.99,
imageFile: "sft_monkey.jpg"
}
/**
* Reference: https://solana.stackexchange.com/questions/5004/how-to-properly-serialize-and-deserialize-versioned-transaction
* https://developers.metaplex.com/token-metadata/mint
* https://developers.metaplex.com/token-metadata/token-standard
* https://developers.metaplex.com/umi/public-keys-and-signers#signers
*/
async function main() {
// create a new connection to the cluster's API
const connection = new Connection(clusterApiUrl('devnet'), 'confirmed');
console.log(`cluster: ${clusterApiUrl('devnet')}`);
// Use the RPC endpoint of your choice.
const umi = createUmi('https://api.devnet.solana.com')
.use(nftStorageUploader({token: process.env.NFT_STORAGE_TOKEN}))
.use(mplTokenMetadata());
const secret = JSON.parse(process.env.PRIVATE_KEY ?? "");
const payer = Keypair.fromSecretKey(new Uint8Array(secret));
console.log(`payer public key: ${payer.publicKey}`);
const mintWallet = Keypair.generate();
const mintAddress = mintWallet.publicKey.toString();
console.log(`mintAddress: ${mintAddress}`);
const mint = createNoopSigner(mintAddress);
const sourceWallet = Keypair.generate();
console.log(`sourceWallet: ${sourceWallet.publicKey}`);
console.log(`Assigning sourceWAllet as signerIdentity`);
umi.use(signerIdentity(createNoopSigner(sourceWallet.publicKey), false));
umi.payer = createNoopSigner(payer.publicKey);
console.log('Step1');
const uri = await uploadMetadata(umi, sftData);
const builderIx = createFungibleAsset(umi,
{
mint: mint,
name: sftData.name,
uri: uri,
// amount: 20000000,
sellerFeeBasisPoints: percentAmount(sftData.sellerFeeBasisPoints, 2),
symbol: sftData.symbol,
}
).getInstructions();
const mintSFTIx = mintV1(umi,
{
mint: mint,
authority: createNoopSigner(sourceWallet.publicKey),
amount: 10000000,
tokenOwner: sourceWallet.publicKey,
tokenStandard: TokenStandard.FungibleAsset,
}
).getInstructions();
console.log('Step2');
let latestBlockhash = await umi.rpc.getLatestBlockhash();
let umiTransaction = umi.transactions.create({
version: 0,
payer: payer.publicKey,
instructions: [...builderIx, ...mintSFTIx],
blockhash: latestBlockhash.blockhash,
});
let transaction = toWeb3JsTransaction(umiTransaction);
console.log('Step3');
// Sign the Transaction with the signer's private key
transaction.sign([payer, mintWallet, sourceWallet]);
let signedTransactionData = transaction.serialize();
let txnSignature = await connection.sendRawTransaction(
Buffer.from(signedTransactionData, 'base64')
);
console.log(`txnSignature: ${txnSignature}`);
console.log('step4');
}
async function uploadMetadata(umi, sftData) {
// file to buffer
const fileBuffer = fs.readFileSync("data/" + sftData.imageFile);
const [imageUri] = await umi.uploader.upload([fileBuffer]);
console.log(`image uri: ${imageUri}`);
const uri = await umi.uploader.uploadJson({
name: sftData.name,
description: sftData.description,
image: imageUri,
});
console.log(`metadata uri: ${uri}`);
return uri;
}
main()
.then(() => {
console.log("Finished successfully")
process.exit(0)
})
.catch(error => {
console.log(error)
process.exit(1)
});