diff --git a/src/address.ts b/src/address.ts index 9739cfb..9bc6111 100644 --- a/src/address.ts +++ b/src/address.ts @@ -1,4 +1,4 @@ -export const PRIVATE_KEY = ""; -export const PUBLIC_KEY = ""; +export const PRIVATE_KEY = env.PRIVATE_KEY; +export const PUBLIC_KEY = "2ieyZg4CJLJvxhejhX5k2qkdbeJjGo2i4zsNZ2XupGFv"; -export const TOKEN_MINT_ADDRESS = ""; \ No newline at end of file +export const TOKEN_MINT_ADDRESS = env.TOKEN_MINT_ADDRESS; \ No newline at end of file diff --git a/src/index.ts b/src/index.ts index 641355f..74e2504 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,27 +1,41 @@ require('dotenv').config(); import express from 'express'; +import bodyParser from 'body-parser'; import { burnTokens, mintTokens, sendNativeTokens } from './mintTokens'; const app = express(); +// Middleware to parse JSON bodies +app.use(bodyParser.json()); -app.post('/helius', async(req, res) => { - const fromAddress = req.body.fromAddress; - const toAddress = req.body.toAddress; - const amount = req.body.amount; - const type = "received_native_sol"; +// Endpoint to handle incoming requests +app.post('/helius', async (req, res) => { + const { fromAddress, toAddress, amount } = req.body; + const type = "received_native_sol"; // This could be dynamic based on your logic - if (type === "received_native_sol") { - await mintTokens(fromAddress, toAddress, amount); - } else { - // What could go wrong here? - await burnTokens(fromAddress, toAddress, amount); - await sendNativeTokens(fromAddress, toAddress, amount); + // Input validation + if (!fromAddress || !toAddress || !amount) { + return res.status(400).send('Invalid request: Missing parameters'); } - res.send('Transaction successful'); + try { + if (type === "received_native_sol") { + // Mint tokens when native SOL is received + await mintTokens(fromAddress, toAddress, amount); + } else { + // Handle other types of transactions + await burnTokens(fromAddress, toAddress, amount); + await sendNativeTokens(fromAddress, toAddress, amount); + } + + res.status(200).send('Transaction successful'); + } catch (error) { + console.error('Error processing transaction:', error); + res.status(500).send('Transaction failed'); + } }); +// Start the server app.listen(3000, () => { - console.log('Server is running on port 3000'); + console.log('Server is running on port 3000'); }); \ No newline at end of file diff --git a/src/mintTokens.ts b/src/mintTokens.ts index 8e5a0eb..aac9d94 100644 --- a/src/mintTokens.ts +++ b/src/mintTokens.ts @@ -1,12 +1,43 @@ -export const mintTokens = async (fromAddress: string, toAddress: string, amount: number) => { +import { + Connection, + Keypair, + PublicKey, +} from '@solana/web3.js'; +import { + getOrCreateAssociatedTokenAccount, + mintTo, +} from '@solana/spl-token'; + +export const mintTokens = async (connection: Connection, payer: Keypair, mintAddress: PublicKey, recipientAddress: PublicKey, amount: number) => { console.log("Minting tokens"); -} -export const burnTokens = async (fromAddress: string, toAddress: string, amount: number) => { + const recipientTokenAccount = await getOrCreateAssociatedTokenAccount( + connection, + payer, + mintAddress, + recipientAddress + ); + + await mintTo( + connection, + payer, + mintAddress, + recipientTokenAccount.address, + payer.publicKey, + amount + ); + + console.log(`Minted ${amount} tokens to ${recipientAddress.toString()}`); +}; + +export const burnTokens = async (fromAddress: PublicKey, toAddress: PublicKey, amount: number) => { console.log("Burning tokens"); -} + +}; + -export const sendNativeTokens = async (fromAddress: string, toAddress: string, amount: number) => { +export const sendNativeTokens = async (fromAddress: PublicKey, toAddress: PublicKey, amount: number) => { console.log("Sending native tokens"); -} \ No newline at end of file + +}; \ No newline at end of file