javascript - 我想在 solana 上铸造一个新的代币。如何使用 solana-web3.js 做到这一点?
问题描述
我正在使用,solana-web3.js
但找不到任何关于如何创建和铸造我自己的令牌的示例。最好的方法是什么?
解决方案
为此,您还需要使用我们的令牌程序 js 绑定。您可以通过 npm 导入它们,如下面的示例代码所示。
const web3 = require('@solana/web3.js');
const splToken = require('@solana/spl-token');
(async () => {
//create connection to devnet
const connection = new web3.Connection(web3.clusterApiUrl("devnet"));
//generate keypair and airdrop 1000000000 Lamports (1 SOL)
const myKeypair = web3.Keypair.generate();
await connection.requestAirdrop(myKeypair.publicKey, 1000000000);
console.log('solana public address: ' + myKeypair.publicKey.toBase58());
//set timeout to account for airdrop finalization
let mint;
var myToken
setTimeout(async function(){
//create mint
mint = await splToken.Token.createMint(connection, myKeypair, myKeypair.publicKey, null, 9, splToken.TOKEN_PROGRAM_ID)
console.log('mint public address: ' + mint.publicKey.toBase58());
//get the token accont of this solana address, if it does not exist, create it
myToken = await mint.getOrCreateAssociatedAccountInfo(
myKeypair.publicKey
)
console.log('token public address: ' + myToken.address.toBase58());
//minting 100 new tokens to the token address we just created
await mint.mintTo(myToken.address, myKeypair.publicKey, [], 1000000000);
console.log('done');
}, 20000);
})();
推荐阅读
- ios - Swift 从 ViewController 向 ViewController 发送数据
- selenium-webdriver - 如何解决“找不到日志类型'性能'”问题?
- php - 如何在return语句中编写循环?
- python - (Python) 在线程中关闭 pygame.display
- mysql - SQL查询查找在某些2日期增加值的所有产品
- php - 单个 PHP 文件,可以擦除包含子文件夹的文件夹内的所有内容以及文件本身
- python - 如何转换类型”
" 在 python 中听写 - javascript - 两个不同数组的 addEventListener - forEach -代码优化
- python - LightGBM - sklearnAPI 与训练和数据结构 API 以及 lgb.cv 与 gridsearchcv/randomisedsearchcv
- angular - 如何将 promise.all 转换为 observables