├── README.md └── index6.js /README.md: -------------------------------------------------------------------------------- 1 | # send-solana-spl-token-typescript 2 |

Send solana spl tokens like USDC, Orca, etc using NodeJS, TypeScript...

3 | Support by buying one of my Official NFTs that will power my DAO -------------------------------------------------------------------------------- /index6.js: -------------------------------------------------------------------------------- 1 | const web3 = require('@solana/web3.js'); 2 | import { PublicKey } from "@solana/web3.js"; 3 | import { TOKEN_PROGRAM_ID, getMint, createMint} from "@solana/spl-token"; 4 | 5 | const spl = require('@solana/spl-token'); 6 | const { publicKey } = require('@project-serum/anchor/dist/cjs/utils'); 7 | 8 | (async () => { 9 | // Connect to cluster 10 | const connection = new web3.Connection(web3.clusterApiUrl('devnet'), 'confirmed'); 11 | 12 | // connect to a previously generated wallet 13 | let secretKey = Uint8Array.from("YOUR SECRET KEY"); 14 | 15 | const myKeypair = web3.Keypair.fromSecretKey(secretKey); 16 | 17 | const fromWallet = myKeypair; 18 | 19 | 20 | // Generate a new wallet to receive newly minted token 21 | const walletTo = "YOUR WALLET" 22 | const destPublicKey = new web3.PublicKey(walletTo); 23 | const destMint: PublicKey = new web3.PublicKey("YOUR TOKEN ADDRESS"); 24 | 25 | const tokenM = new web3.PublicKey("YOUR TOKEN ADDRESS") 26 | //console.log(toWallet.publicKey) 27 | // Create new token mint 28 | 29 | 30 | 31 | 32 | 33 | 34 | // Get the token account of the fromWallet address, and if it does not exist, create it 35 | const fromTokenAccount = await spl.getOrCreateAssociatedTokenAccount( 36 | connection, 37 | fromWallet, 38 | tokenM, 39 | fromWallet.publicKey 40 | ); 41 | 42 | // Get the token account of the toWallet address, and if it does not exist, create it 43 | const toTokenAccount = await spl.getOrCreateAssociatedTokenAccount(connection, fromWallet, tokenM, destPublicKey); 44 | 45 | // Mint 1 new token to the "fromTokenAccount" account we just created 46 | let signature = await spl.mintTo( 47 | connection, 48 | fromWallet, 49 | destMint, 50 | fromTokenAccount.address, 51 | fromWallet.publicKey, 52 | 3*web3.LAMPORTS_PER_SOL 53 | ); 54 | console.log('mint tx:', signature); 55 | 56 | // Transfer the new token to the "toTokenAccount" we just created 57 | signature = await spl.transfer( 58 | connection, 59 | fromWallet, 60 | fromTokenAccount.address, 61 | toTokenAccount.address, 62 | fromWallet.publicKey, 63 | 10*web3.LAMPORTS_PER_SOL 64 | ); 65 | })(); 66 | --------------------------------------------------------------------------------