Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mint #4

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions dist/address.js
Original file line number Diff line number Diff line change
@@ -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 = "";
42 changes: 42 additions & 0 deletions dist/index.js
Original file line number Diff line number Diff line change
@@ -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');
});
81 changes: 81 additions & 0 deletions dist/mintTokens.js
Original file line number Diff line number Diff line change
@@ -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;
204 changes: 204 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 10 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Loading