From c4f4944ca4af8693cd42f3dc07bc7f2d31426c7a Mon Sep 17 00:00:00 2001 From: BearerOP Date: Fri, 1 Nov 2024 22:00:08 +0530 Subject: [PATCH] deploy --- dist/address.js | 6 ++ dist/index.js | 42 ++++++++++ dist/mintTokens.js | 81 ++++++++++++++++++ package-lock.json | 204 +++++++++++++++++++++++++++++++++++++++++++++ package.json | 11 ++- src/index.ts | 28 ++++--- src/mintTokens.ts | 121 +++++++++++++++++++++++++-- 7 files changed, 474 insertions(+), 19 deletions(-) create mode 100644 dist/address.js create mode 100644 dist/index.js create mode 100644 dist/mintTokens.js diff --git a/dist/address.js b/dist/address.js new file mode 100644 index 0000000..1f4209f --- /dev/null +++ b/dist/address.js @@ -0,0 +1,6 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.TOKEN_MINT_ADDRESS = exports.PUBLIC_KEY = exports.PRIVATE_KEY = void 0; +exports.PRIVATE_KEY = ""; +exports.PUBLIC_KEY = ""; +exports.TOKEN_MINT_ADDRESS = ""; diff --git a/dist/index.js b/dist/index.js new file mode 100644 index 0000000..4dc694f --- /dev/null +++ b/dist/index.js @@ -0,0 +1,42 @@ +"use strict"; +var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { + function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } + return new (P || (P = Promise))(function (resolve, reject) { + function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } + function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } + function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } + step((generator = generator.apply(thisArg, _arguments || [])).next()); + }); +}; +var __importDefault = (this && this.__importDefault) || function (mod) { + return (mod && mod.__esModule) ? mod : { "default": mod }; +}; +Object.defineProperty(exports, "__esModule", { value: true }); +require('dotenv').config(); +const express_1 = __importDefault(require("express")); +const mintTokens_1 = require("./mintTokens"); +const app = (0, express_1.default)(); +app.use(express_1.default.json()); // Ensure you can parse JSON request bodies +app.post('/helius', (req, res) => __awaiter(void 0, void 0, void 0, function* () { + const fromAddress = req.body.fromAddress; + const toAddress = req.body.toAddress; + const amount = req.body.amount; + const type = "received_native_sol"; // You may want to dynamically set this based on your application logic + try { + if (type === "received_native_sol") { + yield (0, mintTokens_1.mintTokens)({ fromAddress, toAddress, amount }); + } + else { + yield (0, mintTokens_1.burnTokens)({ fromAddress, toAddress, amount }); + yield (0, mintTokens_1.sendNativeTokens)({ fromAddress, toAddress, amount }); + } + res.send('Transaction successful'); + } + catch (error) { + console.error('Transaction failed:', error); + res.status(500).send('Transaction failed'); + } +})); +app.listen(3000, () => { + console.log('Server is running on port 3000'); +}); diff --git a/dist/mintTokens.js b/dist/mintTokens.js new file mode 100644 index 0000000..9d3de04 --- /dev/null +++ b/dist/mintTokens.js @@ -0,0 +1,81 @@ +"use strict"; +var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { + function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } + return new (P || (P = Promise))(function (resolve, reject) { + function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } + function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } + function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } + step((generator = generator.apply(thisArg, _arguments || [])).next()); + }); +}; +var __importDefault = (this && this.__importDefault) || function (mod) { + return (mod && mod.__esModule) ? mod : { "default": mod }; +}; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.sendNativeTokens = exports.burnTokens = exports.mintTokens = void 0; +const web3_js_1 = require("@solana/web3.js"); +const spl_token_1 = require("@solana/spl-token"); +const dotenv_1 = __importDefault(require("dotenv")); +dotenv_1.default.config(); +// Initialize connection to Solana +const connection = new web3_js_1.Connection(process.env.SOLANA_RPC_URL, 'confirmed'); +// Load mint authority and fee payer (for native SOL transfers) from environment variables +const mintAuthority = web3_js_1.Keypair.fromSecretKey(Uint8Array.from(JSON.parse(process.env.MINT_AUTHORITY_PRIVATE_KEY))); +const feePayer = web3_js_1.Keypair.fromSecretKey(Uint8Array.from(JSON.parse(process.env.FEE_PAYER_PRIVATE_KEY))); +// Mint tokens function +const mintTokens = (_a) => __awaiter(void 0, [_a], void 0, function* ({ fromAddress, toAddress, amount }) { + try { + console.log("Minting tokens"); + const mintPublicKey = new web3_js_1.PublicKey(process.env.TOKEN_MINT_ADDRESS); + const recipientPublicKey = new web3_js_1.PublicKey(toAddress); + console.log(`Mint Public Key: ${mintPublicKey.toBase58()}`); + console.log(`Recipient Public Key: ${recipientPublicKey.toBase58()}`); + ; + // Get or create the recipient's associated token account + const recipientTokenAccount = yield (0, spl_token_1.getOrCreateAssociatedTokenAccount)(connection, mintAuthority, mintPublicKey, recipientPublicKey); + // Mint tokens to the recipient's token account + yield (0, spl_token_1.mintTo)(connection, mintAuthority, mintPublicKey, recipientTokenAccount.address, mintAuthority, amount * Math.pow(10, Number(process.env.TOKEN_DECIMALS)) // Adjust for token decimals + ); + console.log(`Minted ${amount} tokens to ${recipientTokenAccount.address.toBase58()}`); + } + catch (error) { + console.error('Error minting tokens:', error); + throw error; // Propagate error for the outer catch block + } +}); +exports.mintTokens = mintTokens; +// Burn tokens function +const burnTokens = (_a) => __awaiter(void 0, [_a], void 0, function* ({ fromAddress, toAddress, amount }) { + try { + console.log("Burning tokens"); + const mintPublicKey = new web3_js_1.PublicKey(process.env.TOKEN_MINT_ADDRESS); + const userTokenAccount = new web3_js_1.PublicKey(fromAddress); + // Burn tokens from the user's token account + yield (0, spl_token_1.burn)(connection, mintAuthority, userTokenAccount, mintPublicKey, mintAuthority, amount * Math.pow(10, Number(process.env.TOKEN_DECIMALS)) // Adjust for token decimals + ); + console.log(`Burned ${amount} tokens from ${userTokenAccount.toBase58()}`); + } + catch (error) { + console.error('Error burning tokens:', error); + throw error; // Propagate error for the outer catch block + } +}); +exports.burnTokens = burnTokens; +// Send native SOL tokens function +const sendNativeTokens = (_a) => __awaiter(void 0, [_a], void 0, function* ({ fromAddress, toAddress, amount }) { + try { + console.log("Sending native tokens"); + const senderPublicKey = new web3_js_1.PublicKey(fromAddress); + const recipientPublicKey = new web3_js_1.PublicKey(toAddress); + // Create a transaction to transfer SOL + const transactionSignature = yield connection.requestAirdrop(recipientPublicKey, amount * web3_js_1.LAMPORTS_PER_SOL); + // Confirm the transaction + yield connection.confirmTransaction(transactionSignature); + console.log(`Sent ${amount} SOL from ${fromAddress} to ${toAddress}`); + } + catch (error) { + console.error('Error sending native tokens:', error); + throw error; // Propagate error for the outer catch block + } +}); +exports.sendNativeTokens = sendNativeTokens; diff --git a/package-lock.json b/package-lock.json index c7d7c9f..18055e3 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,6 +9,7 @@ "version": "1.0.0", "license": "ISC", "dependencies": { + "@solana/spl-token": "^0.4.9", "@solana/web3.js": "^1.95.4", "@types/express": "^5.0.0", "dotenv": "^16.4.5", @@ -67,6 +68,181 @@ "node": ">=5.10" } }, + "node_modules/@solana/buffer-layout-utils": { + "version": "0.2.0", + "resolved": "https://registry.npmjs.org/@solana/buffer-layout-utils/-/buffer-layout-utils-0.2.0.tgz", + "integrity": "sha512-szG4sxgJGktbuZYDg2FfNmkMi0DYQoVjN2h7ta1W1hPrwzarcFLBq9UpX1UjNXsNpT9dn+chgprtWGioUAr4/g==", + "license": "Apache-2.0", + "dependencies": { + "@solana/buffer-layout": "^4.0.0", + "@solana/web3.js": "^1.32.0", + "bigint-buffer": "^1.1.5", + "bignumber.js": "^9.0.1" + }, + "engines": { + "node": ">= 10" + } + }, + "node_modules/@solana/codecs": { + "version": "2.0.0-rc.1", + "resolved": "https://registry.npmjs.org/@solana/codecs/-/codecs-2.0.0-rc.1.tgz", + "integrity": "sha512-qxoR7VybNJixV51L0G1RD2boZTcxmwUWnKCaJJExQ5qNKwbpSyDdWfFJfM5JhGyKe9DnPVOZB+JHWXnpbZBqrQ==", + "license": "MIT", + "dependencies": { + "@solana/codecs-core": "2.0.0-rc.1", + "@solana/codecs-data-structures": "2.0.0-rc.1", + "@solana/codecs-numbers": "2.0.0-rc.1", + "@solana/codecs-strings": "2.0.0-rc.1", + "@solana/options": "2.0.0-rc.1" + }, + "peerDependencies": { + "typescript": ">=5" + } + }, + "node_modules/@solana/codecs-core": { + "version": "2.0.0-rc.1", + "resolved": "https://registry.npmjs.org/@solana/codecs-core/-/codecs-core-2.0.0-rc.1.tgz", + "integrity": "sha512-bauxqMfSs8EHD0JKESaNmNuNvkvHSuN3bbWAF5RjOfDu2PugxHrvRebmYauvSumZ3cTfQ4HJJX6PG5rN852qyQ==", + "license": "MIT", + "dependencies": { + "@solana/errors": "2.0.0-rc.1" + }, + "peerDependencies": { + "typescript": ">=5" + } + }, + "node_modules/@solana/codecs-data-structures": { + "version": "2.0.0-rc.1", + "resolved": "https://registry.npmjs.org/@solana/codecs-data-structures/-/codecs-data-structures-2.0.0-rc.1.tgz", + "integrity": "sha512-rinCv0RrAVJ9rE/rmaibWJQxMwC5lSaORSZuwjopSUE6T0nb/MVg6Z1siNCXhh/HFTOg0l8bNvZHgBcN/yvXog==", + "license": "MIT", + "dependencies": { + "@solana/codecs-core": "2.0.0-rc.1", + "@solana/codecs-numbers": "2.0.0-rc.1", + "@solana/errors": "2.0.0-rc.1" + }, + "peerDependencies": { + "typescript": ">=5" + } + }, + "node_modules/@solana/codecs-numbers": { + "version": "2.0.0-rc.1", + "resolved": "https://registry.npmjs.org/@solana/codecs-numbers/-/codecs-numbers-2.0.0-rc.1.tgz", + "integrity": "sha512-J5i5mOkvukXn8E3Z7sGIPxsThRCgSdgTWJDQeZvucQ9PT6Y3HiVXJ0pcWiOWAoQ3RX8e/f4I3IC+wE6pZiJzDQ==", + "license": "MIT", + "dependencies": { + "@solana/codecs-core": "2.0.0-rc.1", + "@solana/errors": "2.0.0-rc.1" + }, + "peerDependencies": { + "typescript": ">=5" + } + }, + "node_modules/@solana/codecs-strings": { + "version": "2.0.0-rc.1", + "resolved": "https://registry.npmjs.org/@solana/codecs-strings/-/codecs-strings-2.0.0-rc.1.tgz", + "integrity": "sha512-9/wPhw8TbGRTt6mHC4Zz1RqOnuPTqq1Nb4EyuvpZ39GW6O2t2Q7Q0XxiB3+BdoEjwA2XgPw6e2iRfvYgqty44g==", + "license": "MIT", + "dependencies": { + "@solana/codecs-core": "2.0.0-rc.1", + "@solana/codecs-numbers": "2.0.0-rc.1", + "@solana/errors": "2.0.0-rc.1" + }, + "peerDependencies": { + "fastestsmallesttextencoderdecoder": "^1.0.22", + "typescript": ">=5" + } + }, + "node_modules/@solana/errors": { + "version": "2.0.0-rc.1", + "resolved": "https://registry.npmjs.org/@solana/errors/-/errors-2.0.0-rc.1.tgz", + "integrity": "sha512-ejNvQ2oJ7+bcFAYWj225lyRkHnixuAeb7RQCixm+5mH4n1IA4Qya/9Bmfy5RAAHQzxK43clu3kZmL5eF9VGtYQ==", + "license": "MIT", + "dependencies": { + "chalk": "^5.3.0", + "commander": "^12.1.0" + }, + "bin": { + "errors": "bin/cli.mjs" + }, + "peerDependencies": { + "typescript": ">=5" + } + }, + "node_modules/@solana/errors/node_modules/commander": { + "version": "12.1.0", + "resolved": "https://registry.npmjs.org/commander/-/commander-12.1.0.tgz", + "integrity": "sha512-Vw8qHK3bZM9y/P10u3Vib8o/DdkvA2OtPtZvD871QKjy74Wj1WSKFILMPRPSdUSx5RFK1arlJzEtA4PkFgnbuA==", + "license": "MIT", + "engines": { + "node": ">=18" + } + }, + "node_modules/@solana/options": { + "version": "2.0.0-rc.1", + "resolved": "https://registry.npmjs.org/@solana/options/-/options-2.0.0-rc.1.tgz", + "integrity": "sha512-mLUcR9mZ3qfHlmMnREdIFPf9dpMc/Bl66tLSOOWxw4ml5xMT2ohFn7WGqoKcu/UHkT9CrC6+amEdqCNvUqI7AA==", + "license": "MIT", + "dependencies": { + "@solana/codecs-core": "2.0.0-rc.1", + "@solana/codecs-data-structures": "2.0.0-rc.1", + "@solana/codecs-numbers": "2.0.0-rc.1", + "@solana/codecs-strings": "2.0.0-rc.1", + "@solana/errors": "2.0.0-rc.1" + }, + "peerDependencies": { + "typescript": ">=5" + } + }, + "node_modules/@solana/spl-token": { + "version": "0.4.9", + "resolved": "https://registry.npmjs.org/@solana/spl-token/-/spl-token-0.4.9.tgz", + "integrity": "sha512-g3wbj4F4gq82YQlwqhPB0gHFXfgsC6UmyGMxtSLf/BozT/oKd59465DbnlUK8L8EcimKMavxsVAMoLcEdeCicg==", + "license": "Apache-2.0", + "dependencies": { + "@solana/buffer-layout": "^4.0.0", + "@solana/buffer-layout-utils": "^0.2.0", + "@solana/spl-token-group": "^0.0.7", + "@solana/spl-token-metadata": "^0.1.6", + "buffer": "^6.0.3" + }, + "engines": { + "node": ">=16" + }, + "peerDependencies": { + "@solana/web3.js": "^1.95.3" + } + }, + "node_modules/@solana/spl-token-group": { + "version": "0.0.7", + "resolved": "https://registry.npmjs.org/@solana/spl-token-group/-/spl-token-group-0.0.7.tgz", + "integrity": "sha512-V1N/iX7Cr7H0uazWUT2uk27TMqlqedpXHRqqAbVO2gvmJyT0E0ummMEAVQeXZ05ZhQ/xF39DLSdBp90XebWEug==", + "license": "Apache-2.0", + "dependencies": { + "@solana/codecs": "2.0.0-rc.1" + }, + "engines": { + "node": ">=16" + }, + "peerDependencies": { + "@solana/web3.js": "^1.95.3" + } + }, + "node_modules/@solana/spl-token-metadata": { + "version": "0.1.6", + "resolved": "https://registry.npmjs.org/@solana/spl-token-metadata/-/spl-token-metadata-0.1.6.tgz", + "integrity": "sha512-7sMt1rsm/zQOQcUWllQX9mD2O6KhSAtY1hFR2hfFwgqfFWzSY9E9GDvFVNYUI1F0iQKcm6HmePU9QbKRXTEBiA==", + "license": "Apache-2.0", + "dependencies": { + "@solana/codecs": "2.0.0-rc.1" + }, + "engines": { + "node": ">=16" + }, + "peerDependencies": { + "@solana/web3.js": "^1.95.3" + } + }, "node_modules/@solana/web3.js": { "version": "1.95.4", "resolved": "https://registry.npmjs.org/@solana/web3.js/-/web3.js-1.95.4.tgz", @@ -281,6 +457,15 @@ "node": ">= 10.0.0" } }, + "node_modules/bignumber.js": { + "version": "9.1.2", + "resolved": "https://registry.npmjs.org/bignumber.js/-/bignumber.js-9.1.2.tgz", + "integrity": "sha512-2/mKyZH9K85bzOEfhXDBFZTGd1CTs+5IHpeFQo9luiBG7hghdC851Pj2WAhb6E3R6b9tZj/XKhbg4fum+Kepug==", + "license": "MIT", + "engines": { + "node": "*" + } + }, "node_modules/bindings": { "version": "1.5.0", "resolved": "https://registry.npmjs.org/bindings/-/bindings-1.5.0.tgz", @@ -406,6 +591,18 @@ "url": "https://github.com/sponsors/ljharb" } }, + "node_modules/chalk": { + "version": "5.3.0", + "resolved": "https://registry.npmjs.org/chalk/-/chalk-5.3.0.tgz", + "integrity": "sha512-dLitG79d+GV1Nb/VYcCDFivJeK1hiukt9QjRNVOsUtTy1rR1YJsmpGGTZ3qJos+uw7WmWF4wUwBd9jxjocFC2w==", + "license": "MIT", + "engines": { + "node": "^12.17.0 || ^14.13 || >=16.0.0" + }, + "funding": { + "url": "https://github.com/chalk/chalk?sponsor=1" + } + }, "node_modules/commander": { "version": "2.20.3", "resolved": "https://registry.npmjs.org/commander/-/commander-2.20.3.tgz", @@ -651,6 +848,13 @@ "integrity": "sha512-wpYMUmFu5f00Sm0cj2pfivpmawLZ0NKdviQ4w9zJeR8JVtOpOxHmLaJuj0vxvGqMJQWyP/COUkF75/57OKyRag==", "license": "MIT" }, + "node_modules/fastestsmallesttextencoderdecoder": { + "version": "1.0.22", + "resolved": "https://registry.npmjs.org/fastestsmallesttextencoderdecoder/-/fastestsmallesttextencoderdecoder-1.0.22.tgz", + "integrity": "sha512-Pb8d48e+oIuY4MaM64Cd7OW1gt4nxCHs7/ddPPZ/Ic3sg8yVGM7O9wDvZ7us6ScaUupzM+pfBolwtYhN1IxBIw==", + "license": "CC0-1.0", + "peer": true + }, "node_modules/file-uri-to-path": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/file-uri-to-path/-/file-uri-to-path-1.0.0.tgz", diff --git a/package.json b/package.json index f63f319..45225fa 100644 --- a/package.json +++ b/package.json @@ -3,13 +3,22 @@ "version": "1.0.0", "main": "index.js", "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" + "test": "echo \"Error: no test specified\" && exit 1", + "start": "node dist/index.js", + "dev": "ts-node-dev --respawn --transpile-only src/index.ts", + "build": "tsc", + "lint": "eslint . --ext .ts", + "lint:fix": "eslint . --ext .ts --fix", + "prettier": "prettier --write .", + "deploy":"git add . && git commit -m 'deploy' && git push origin main" + }, "keywords": [], "author": "", "license": "ISC", "description": "", "dependencies": { + "@solana/spl-token": "^0.4.9", "@solana/web3.js": "^1.95.4", "@types/express": "^5.0.0", "dotenv": "^16.4.5", diff --git a/src/index.ts b/src/index.ts index 641355f..5f100e9 100644 --- a/src/index.ts +++ b/src/index.ts @@ -3,25 +3,31 @@ import express from 'express'; import { burnTokens, mintTokens, sendNativeTokens } from './mintTokens'; const app = express(); +app.use(express.json()); // Ensure you can parse JSON request bodies +app.post('/helius', async (req, res) => { -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"; + const type = "received_native_sol"; // You may want to dynamically set this based on your application 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); - } + try { + if (type === "received_native_sol") { + await mintTokens({ fromAddress, toAddress, amount }); + } else { + await burnTokens({ fromAddress, toAddress, amount }); + await sendNativeTokens({ fromAddress, toAddress, amount }); + } - res.send('Transaction successful'); + res.send('Transaction successful'); + } catch (error) { + console.error('Transaction failed:', error); + res.status(500).send('Transaction failed'); + } }); 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..a2ab502 100644 --- a/src/mintTokens.ts +++ b/src/mintTokens.ts @@ -1,12 +1,119 @@ +import { Connection, PublicKey, Keypair, LAMPORTS_PER_SOL } from '@solana/web3.js'; +import { createMint, getOrCreateAssociatedTokenAccount, mintTo, burn } from '@solana/spl-token'; +import dotenv from 'dotenv'; -export const mintTokens = async (fromAddress: string, toAddress: string, amount: number) => { - console.log("Minting tokens"); +dotenv.config(); + +// Initialize connection to Solana +const connection = new Connection(process.env.SOLANA_RPC_URL!, 'confirmed'); + +// Load mint authority and fee payer (for native SOL transfers) from environment variables +const mintAuthority = Keypair.fromSecretKey( + Uint8Array.from(JSON.parse(process.env.MINT_AUTHORITY_PRIVATE_KEY!)) +); +const feePayer = Keypair.fromSecretKey( + Uint8Array.from(JSON.parse(process.env.FEE_PAYER_PRIVATE_KEY!)) +); + +interface MintTokensParams { + fromAddress: string; + toAddress: string; + amount: number; } -export const burnTokens = async (fromAddress: string, toAddress: string, amount: number) => { - console.log("Burning tokens"); +// Mint tokens function +export const mintTokens = async ({ fromAddress, toAddress, amount }: MintTokensParams): Promise => { + try { + console.log("Minting tokens"); + + + const mintPublicKey = new PublicKey(process.env.TOKEN_MINT_ADDRESS!); + const recipientPublicKey = new PublicKey(toAddress) + console.log(`Mint Public Key: ${mintPublicKey.toBase58()}`); +console.log(`Recipient Public Key: ${recipientPublicKey.toBase58()}`);; + + // Get or create the recipient's associated token account + const recipientTokenAccount = await getOrCreateAssociatedTokenAccount( + connection, + mintAuthority, + mintPublicKey, + recipientPublicKey + ); + + // Mint tokens to the recipient's token account + await mintTo( + connection, + mintAuthority, + mintPublicKey, + recipientTokenAccount.address, + mintAuthority, + amount * Math.pow(10, Number(process.env.TOKEN_DECIMALS)) // Adjust for token decimals + ); + + console.log(`Minted ${amount} tokens to ${recipientTokenAccount.address.toBase58()}`); + } catch (error) { + console.error('Error minting tokens:', error); + throw error; // Propagate error for the outer catch block + } +}; + +interface BurnTokensParams { + fromAddress: string; + toAddress: string; + amount: number; } -export const sendNativeTokens = async (fromAddress: string, toAddress: string, amount: number) => { - console.log("Sending native tokens"); -} \ No newline at end of file +// Burn tokens function +export const burnTokens = async ({ fromAddress, toAddress, amount }: BurnTokensParams): Promise => { + try { + console.log("Burning tokens"); + + const mintPublicKey = new PublicKey(process.env.TOKEN_MINT_ADDRESS!); + const userTokenAccount = new PublicKey(fromAddress); + + // Burn tokens from the user's token account + await burn( + connection, + mintAuthority, + userTokenAccount, + mintPublicKey, + mintAuthority, + amount * Math.pow(10, Number(process.env.TOKEN_DECIMALS)) // Adjust for token decimals + ); + + console.log(`Burned ${amount} tokens from ${userTokenAccount.toBase58()}`); + } catch (error) { + console.error('Error burning tokens:', error); + throw error; // Propagate error for the outer catch block + } +}; + +interface SendNativeTokensParams { + fromAddress: string; + toAddress: string; + amount: number; +} + +// Send native SOL tokens function +export const sendNativeTokens = async ({ fromAddress, toAddress, amount }: SendNativeTokensParams): Promise => { + try { + console.log("Sending native tokens"); + + const senderPublicKey = new PublicKey(fromAddress); + const recipientPublicKey = new PublicKey(toAddress); + + // Create a transaction to transfer SOL + const transactionSignature = await connection.requestAirdrop( + recipientPublicKey, + amount * LAMPORTS_PER_SOL + ); + + // Confirm the transaction + await connection.confirmTransaction(transactionSignature); + + console.log(`Sent ${amount} SOL from ${fromAddress} to ${toAddress}`); + } catch (error) { + console.error('Error sending native tokens:', error); + throw error; // Propagate error for the outer catch block + } +}; \ No newline at end of file