Sending a Transaction
Sending a transaction on the Solana network requires the creation of a Transaction object, followed by the transaction being signed and submitted by the user's Magic Eden wallet.
Creating a Transaction
Before you can sign or send a transaction, you must create one! Below is a basic example of creating a tx that has a user send funds right back to themselves
import {
Transaction,
SystemProgram,
Connection,
PublicKey,
} from "@solana/web3.js";
/**
* Creates an example transaction to transfer 100 lamports to the same account.
* @param {String} publicKey a wallet public key
* @param {Connection} connection an RPC connection
* @returns {Transaction} a transaction
*/
export const createTransaction = async (
publicKey: PublicKey,
connection: Connection
): Promise<Transaction> => {
const latestBlockhash = (await connection.getLatestBlockhash()).blockhash;
const transaction = new Transaction().add(
SystemProgram.transfer({
fromPubkey: publicKey,
toPubkey: publicKey,
lamports: 1000,
})
);
transaction.feePayer = publicKey;
transaction.recentBlockhash = latestBlockhash;
return transaction;
};
Signing and Sending a Transaction
Once the transaction is created, you can then prompt the user to sign and send it using the signAndSendTransaction
method exposed by the provider
import { Transaction } from "@solana/web3.js";
import { MagicEdenProvider } from "../types/types";
/**
* Signs and sends the given created transaction using the MagicEdenProvider object.
* @param {MagicEdenProvider} provider The MagicEdenProvider object
* @param {Transaction} transaction The transaction to sign and send
* @returns {Promise<string>} A promise that resolves to the signature of the transaction
*/
export const signAndSendTransaction = async (
provider: MagicEdenProvider,
transaction: Transaction
): Promise<string> => {
try {
const { signature } = await provider.signAndSendTransaction(transaction);
return signature;
} catch (error) {
console.warn(error);
throw new Error(
"An unexpected error occured while signing the transaction."
);
}
};
All of this functionality and more can be found at our Solana Demo, so feel free to check it out if you want to learn more about handling wallet functionality when connecting directly to the Magic Eden wallet.
Last updated