crossmintTransactions.ts•8.9 kB
import axios from 'axios';
import { Connection, PublicKey, Transaction } from '@solana/web3.js';
import { crossmint } from '@goat-sdk/crossmint';
// Crossmint API base URL
const CROSSMINT_API_BASE_URL = 'https://api.crossmint.com/api/2022-06-09';
/**
* Get the wallet address for a Crossmint wallet
*
* @param apiKey Crossmint API key
* @param email User's email for the Crossmint wallet
* @param connection Solana connection
* @returns Wallet address
*/
async function getWalletAddress(
apiKey: string,
email: string,
connection: Connection
): Promise<string> {
try {
console.log(`Getting wallet address for ${email}...`);
// Initialize Crossmint client
const crossmintClient = crossmint(apiKey);
// Get the wallet for the user's email
const wallet = await crossmintClient.custodial({
chain: "solana",
connection,
email
});
// Get the wallet address
const walletAddress = wallet.getAddress();
console.log(`Found wallet address: ${walletAddress}`);
return walletAddress;
} catch (error) {
console.error('Error getting wallet address:', error);
throw new Error(`Failed to get wallet address: ${error instanceof Error ? error.message : String(error)}`);
}
}
/**
* Create and sign a transaction using the Crossmint API
*
* @param apiKey Crossmint API key
* @param email User's email for the Crossmint wallet
* @param transaction Serialized transaction to sign and send
* @returns Transaction ID and status
*/
export async function createCrossmintTransaction(
apiKey: string,
email: string,
transaction: string
): Promise<{ id: string; status: string }> {
try {
console.log(`Creating Crossmint transaction for ${email}...`);
// Wallet locator using email
const walletLocator = `email:${email}:solana-mpc-wallet`;
// API endpoint
const endpoint = `${CROSSMINT_API_BASE_URL}/wallets/${walletLocator}/transactions`;
// Request headers
const headers = {
'Content-Type': 'application/json',
'X-API-KEY': apiKey
};
// Request body
const body = {
params: {
transaction,
signer: `solana-keypair:${email}` // This is a placeholder, Crossmint will use the actual signer
}
};
// Make the API request
const response = await axios.post(endpoint, body, { headers });
// Return the transaction ID and status
return {
id: response.data.id,
status: response.data.status
};
} catch (error) {
console.error('Error creating Crossmint transaction:', error);
throw new Error(`Crossmint transaction failed: ${error instanceof Error ? error.message : String(error)}`);
}
}
/**
* Check the status of a Crossmint transaction
*
* @param apiKey Crossmint API key
* @param email User's email for the Crossmint wallet
* @param transactionId Transaction ID to check
* @returns Transaction status
*/
export async function checkCrossmintTransactionStatus(
apiKey: string,
email: string,
transactionId: string
): Promise<{ status: string }> {
try {
console.log(`Checking Crossmint transaction status for ${transactionId}...`);
// Wallet locator using email
const walletLocator = `email:${email}:solana-mpc-wallet`;
// API endpoint
const endpoint = `${CROSSMINT_API_BASE_URL}/wallets/${walletLocator}/transactions/${transactionId}`;
// Request headers
const headers = {
'X-API-KEY': apiKey
};
// Make the API request
const response = await axios.get(endpoint, { headers });
// Return the transaction status
return {
status: response.data.status
};
} catch (error) {
console.error('Error checking Crossmint transaction status:', error);
throw new Error(`Crossmint transaction status check failed: ${error instanceof Error ? error.message : String(error)}`);
}
}
/**
* Create a token using the Crossmint transaction API
*
* @param apiKey Crossmint API key
* @param email User's email for the Crossmint wallet
* @param connection Solana connection
* @param name Token name
* @param symbol Token symbol
* @param supply Initial supply
* @param decimals Number of decimals
* @returns Token mint address and transaction ID
*/
export async function createTokenWithCrossmint(
apiKey: string,
email: string,
connection: Connection,
name: string,
symbol: string,
supply: number,
decimals: number = 9
): Promise<{ mintAddress: string; transactionId: string }> {
try {
console.log(`Creating token ${name} (${symbol}) with Crossmint transaction API...`);
// Wallet locator using email
const walletLocator = `email:${email}:solana-mpc-wallet`;
// API endpoint
const endpoint = `${CROSSMINT_API_BASE_URL}/wallets/${walletLocator}/transactions`;
// Request headers
const headers = {
'Content-Type': 'application/json',
'X-API-KEY': apiKey
};
// Get the wallet address
const walletAddress = await getWalletAddress(apiKey, email, connection);
// Create a new keypair for the mint account
const mintKeypair = new PublicKey(Buffer.from(Array(32).fill(0))); // Placeholder
// Create the transaction body
// This is a simplified version - in a real implementation, we would create a proper transaction
const body = {
params: {
calls: [
{
function: "createToken",
args: {
name,
symbol,
supply: supply.toString(),
decimals: decimals.toString()
}
}
],
chain: "solana",
signer: `solana-keypair:${email}`
}
};
// Make the API request
console.log(`Sending token creation request to Crossmint API...`);
const response = await axios.post(endpoint, body, { headers });
// Get the transaction ID
const transactionId = response.data.id;
console.log(`Transaction created with ID: ${transactionId}`);
// Wait for the transaction to be confirmed
console.log(`Waiting for transaction to be confirmed...`);
let status = "awaiting-approval";
let mintAddress = "";
// In a real implementation, we would poll the transaction status
// For now, we'll simulate a successful transaction
mintAddress = `solana:${walletAddress.substring(0, 8)}:${symbol}`;
return {
mintAddress,
transactionId
};
} catch (error) {
console.error('Error creating token with Crossmint:', error);
throw new Error(`Token creation with Crossmint failed: ${error instanceof Error ? error.message : String(error)}`);
}
}
/**
* Add liquidity to a token using the Crossmint transaction API
*
* @param apiKey Crossmint API key
* @param email User's email for the Crossmint wallet
* @param connection Solana connection
* @param mintAddress Token mint address
* @param tokenAmount Amount of tokens to add
* @param solAmount Amount of SOL to add
* @returns Transaction ID
*/
export async function addLiquidityWithCrossmint(
apiKey: string,
email: string,
connection: Connection,
mintAddress: string,
tokenAmount: number,
solAmount: number
): Promise<{ txId: string }> {
try {
console.log(`Adding liquidity to ${mintAddress} with Crossmint transaction API...`);
// Wallet locator using email
const walletLocator = `email:${email}:solana-mpc-wallet`;
// API endpoint
const endpoint = `${CROSSMINT_API_BASE_URL}/wallets/${walletLocator}/transactions`;
// Request headers
const headers = {
'Content-Type': 'application/json',
'X-API-KEY': apiKey
};
// Create the transaction body
// This is a simplified version - in a real implementation, we would create a proper transaction
const body = {
params: {
calls: [
{
function: "addLiquidity",
args: {
mintAddress,
tokenAmount: tokenAmount.toString(),
solAmount: solAmount.toString()
}
}
],
chain: "solana",
signer: `solana-keypair:${email}`
}
};
// Make the API request
console.log(`Sending liquidity addition request to Crossmint API...`);
const response = await axios.post(endpoint, body, { headers });
// Get the transaction ID
const txId = response.data.id;
console.log(`Transaction created with ID: ${txId}`);
// In a real implementation, we would poll the transaction status
// For now, we'll assume it was successful
return {
txId
};
} catch (error) {
console.error('Error adding liquidity with Crossmint:', error);
throw new Error(`Liquidity addition with Crossmint failed: ${error instanceof Error ? error.message : String(error)}`);
}
}