diff --git a/src/renderer/nav/TokenPage.tsx b/src/renderer/nav/TokenPage.tsx index 2edc1278..3af8586c 100644 --- a/src/renderer/nav/TokenPage.tsx +++ b/src/renderer/nav/TokenPage.tsx @@ -1,4 +1,4 @@ -import { useEffect, useState } from 'react'; +import { useState } from 'react'; import Stack from 'react-bootstrap/Stack'; import { Row, Col } from 'react-bootstrap'; import Button from 'react-bootstrap/Button'; @@ -7,24 +7,17 @@ import * as sol from '@solana/web3.js'; import * as metaplex from '@metaplex/js'; import { useConnection, useWallet } from '@solana/wallet-adapter-react'; -import { LAMPORTS_PER_SOL } from '@solana/web3.js'; import { - createMint, getOrCreateAssociatedTokenAccount, mintTo, setAuthority, transfer, - MINT_SIZE, - getMinimumBalanceForRentExemptMint, - TOKEN_PROGRAM_ID, - createInitializeMintInstruction, } from '@solana/spl-token'; import { toast } from 'react-toastify'; import createNewAccount from 'renderer/data/accounts/account'; -import { SendTransactionOptions } from '@solana/wallet-adapter-base'; +import * as walletWeb3 from '../wallet-adapter/web3'; import AccountView from '../components/AccountView'; import { TokenMetaView } from '../components/TokenView'; -import { useAppDispatch } from '../hooks'; // eslint-disable-next-line no-global-assign Buffer = require('buffer').Buffer; @@ -32,8 +25,6 @@ Buffer = require('buffer').Buffer; const logger = window.electron.log; function TokenPage() { - const dispatch = useAppDispatch(); - const fromKey = useWallet(); const { connection } = useConnection(); @@ -80,39 +71,19 @@ function TokenPage() { // null, // Account that will control the freezing of the token // 0 // Location of the decimal place // ); - const lamports = await getMinimumBalanceForRentExemptMint(connection); - const mintAuthority = myWallet; - const freezeAuthority = null; - const decimals = 9; - - // logger.info(`SVEVSVSVE: ${JSON.stringify(fromKey)}`); - - const transaction = new sol.Transaction().add( - sol.SystemProgram.createAccount({ - fromPubkey: myWallet, - newAccountPubkey: mintKey.publicKey, - space: MINT_SIZE, - lamports, - programId: TOKEN_PROGRAM_ID, - }), - createInitializeMintInstruction( - mintKey.publicKey, - decimals, - mintAuthority, - freezeAuthority, - TOKEN_PROGRAM_ID - ) - ); - - // transaction.partialSign(mint); - const options: SendTransactionOptions = { signers: [mintKey] }; - const signature = await fromKey.sendTransaction( - transaction, + const confirmOptions: sol.ConfirmOptions = { + commitment: 'finalized', + }; + const mint = await walletWeb3.createMint( connection, - options + fromKey, // Payer of the transaction + myWallet, // Account that will control the minting + null, // Account that will control the freezing of the token + 0, // Location of the decimal place + mintKey, // mint keypair - will be generated if not specified + confirmOptions ); - - await connection.confirmTransaction(signature, 'finalized'); + logger.info('Minted ', mint); } async function createOurMintMetadata() { if (!myWallet) { diff --git a/src/renderer/wallet-adapter/web3.ts b/src/renderer/wallet-adapter/web3.ts index 7f80436e..edc552b4 100644 --- a/src/renderer/wallet-adapter/web3.ts +++ b/src/renderer/wallet-adapter/web3.ts @@ -5,12 +5,14 @@ import { getMinimumBalanceForRentExemptMint, createInitializeMintInstruction, } from '@solana/spl-token'; +import { WalletContextState } from '@solana/wallet-adapter-react'; +import { SendTransactionOptions } from '@solana/wallet-adapter-base'; /** * Create and initialize a new mint * * @param connection Connection to use - * @param payer Payer of the transaction and initialization fees + * @param payer Payer of the transaction and initialization fees (can be a wallet-adapter context state for offloading signing and sending) * @param mintAuthority Account or multisig that will control minting * @param freezeAuthority Optional account or multisig that can freeze token accounts * @param decimals Location of the decimal place @@ -23,7 +25,7 @@ import { // https://github.com/solana-labs/solana-program-library/blob/f487f520bf10ca29bf8d491192b6ff2b4bf89710/token/js/src/actions/createMint.ts export async function createMint( connection: sol.Connection, - payer: sol.Signer, + payer: sol.Signer | WalletContextState, mintAuthority: sol.PublicKey, freezeAuthority: sol.PublicKey | null, decimals: number, @@ -31,6 +33,10 @@ export async function createMint( confirmOptions?: sol.ConfirmOptions, programId = splToken.TOKEN_PROGRAM_ID ): Promise { + if (!payer || !payer.publicKey) { + throw Error("Can't create a Mint without a payer Keypair or Wallet"); + } + const lamports = await getMinimumBalanceForRentExemptMint(connection); const transaction = new sol.Transaction().add( @@ -50,12 +56,37 @@ export async function createMint( ) ); - await sol.sendAndConfirmTransaction( - connection, - transaction, - [payer, keypair], - confirmOptions - ); + if ('privateKey' in payer) { + // payer is a sol.Signer + await sol.sendAndConfirmTransaction( + connection, + transaction, + [payer, keypair], + confirmOptions + ); + } else if ('sendTransaction' in payer) { + // payer is a WalletContextState + const options: SendTransactionOptions = { signers: [keypair] }; + const signature = await payer.sendTransaction( + transaction, + connection, + options + ); + + // await connection.confirmTransaction(signature, confirmOptions?.commitment); + const latestBlockHash = await connection.getLatestBlockhash(); + + await connection.confirmTransaction( + { + blockhash: latestBlockHash.blockhash, + lastValidBlockHeight: latestBlockHash.lastValidBlockHeight, + signature, + }, + confirmOptions?.commitment + ); + } else { + throw Error('payer not a Keypair or Wallet.'); + } return keypair.publicKey; }