Created
September 10, 2022 07:06
-
-
Save aeither/b35177214b5ec2a74cbd9867aa728a9b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as web3 from "@solana/web3.js"; | |
import { initializeKeypair } from "./initializeKeypair"; | |
// Token Program | |
import * as token from "@solana/spl-token"; | |
async function main() { | |
const connection = new web3.Connection(web3.clusterApiUrl("devnet")); | |
const user = await initializeKeypair(connection); | |
// Create new token mint | |
const mint = await token.createMint( | |
connection, // connection: web3.Connection, | |
user, // payer: web3.Keypair, | |
user.publicKey, // mintAuthority: web3.PublicKey, | |
user.publicKey, // freezeAuthority: web3.PublicKey, | |
2 // decimals: number | |
); | |
console.log( | |
`Token Mint: https://solana.fm/address/${mint}?cluster=devnet-solana&mode=pro` | |
); | |
// Create token account | |
const tokenAccount = await token.getOrCreateAssociatedTokenAccount( | |
connection, // connection: web3.Connection, | |
user, // payer: web3.Keypair, | |
mint, // mint: web3.PublicKey, | |
user.publicKey // owner: web3.PublicKey | |
); | |
console.log( | |
`Token Account: https://solana.fm/address/${tokenAccount.address}?cluster=devnet-solana&mode=pro` | |
); | |
token.getMint; | |
// Mint tokens | |
const mintTokenstransactionSignature = await token.mintTo( | |
connection, // connection: web3.Connection, | |
user, // payer: web3.Keypair, | |
mint, // mint: web3.PublicKey, | |
tokenAccount.address, // destination: web3.PublicKey, | |
user, // authority: web3.Keypair, | |
100 // amount: number | |
); | |
console.log( | |
`Mint Token Transaction: https://solana.fm/tx/${mintTokenstransactionSignature}?cluster=devnet-solana&mode=pro` | |
); | |
// Transfer tokens | |
const receiver = web3.Keypair.generate().publicKey; | |
const receiverTokenAccount = await token.getOrCreateAssociatedTokenAccount( | |
connection, // connection: web3.Connection, | |
user, // payer: web3.Keypair, | |
mint, // mint: web3.PublicKey, | |
receiver // owner: web3.PublicKey | |
); | |
const transferTransactionSignature = await token.transfer( | |
connection, | |
user, | |
tokenAccount.address, | |
receiverTokenAccount.address, | |
user, | |
50 | |
); | |
console.log( | |
`Transfer Transaction: https://solana.fm/tx/${transferTransactionSignature}?cluster=devnet-solana&mode=pro` | |
); | |
// Burn tokens | |
const burnTransactionSignature = await token.burn( | |
connection, // connection: web3.Connection, | |
user, // payer: web3.Keypair, | |
tokenAccount.address, // account: web3.PublicKey, | |
mint, // mint: web3.PublicKey, | |
user, // owner: web3.Keypair, | |
25 // amount: number | |
); | |
console.log( | |
`Burn Transaction: https://solana.fm/tx/${burnTransactionSignature}?cluster=devnet-solana&mode=pro` | |
); | |
console.log("PublicKey:", user.publicKey.toBase58()); | |
} | |
main() | |
.then(() => { | |
console.log("Finished successfully"); | |
process.exit(0); | |
}) | |
.catch((error) => { | |
console.log(error); | |
process.exit(1); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment