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

added ts logic to mint,burn token #7

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
2 changes: 1 addition & 1 deletion src/address.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export const PRIVATE_KEY = "";
export const PUBLIC_KEY = "";
export const PUBLIC_KEY = "BYq5RpPNPa4WEHJgNtapdP2Re5wtyzNSTLbcNwtdguM8";

export const TOKEN_MINT_ADDRESS = "";
24 changes: 20 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,28 @@ import express from 'express';
import { burnTokens, mintTokens, sendNativeTokens } from './mintTokens';

const app = express();

app.use(express.json());
app.get('/helius',(req,res)=>{
console.log("working");
res.status(400).json({
msg: "working"
})
})

// "nativeTransfers": [
// {
// "amount": 1000000000,
// "fromUserAccount": "7MBcR9GQs94CWwL2SwzgSuhTa8guzb1dAVTDPCpnFzr9",
// "toUserAccount": "BYq5RpPNPa4WEHJgNtapdP2Re5wtyzNSTLbcNwtdguM8"
// }

app.post('/helius', async(req, res) => {
const fromAddress = req.body.fromAddress;
const toAddress = req.body.toAddress;
const amount = req.body.amount;
// const body = req.body.json();
console.log(req.body);

const fromAddress = req.body.nativeTransfers[0].fromUserAccount;
const toAddress = req.body.nativeTransfers[0].toUserAccount;
const amount = req.body.nativeTransfers[0].amount;
const type = "received_native_sol";

if (type === "received_native_sol") {
Expand Down
95 changes: 93 additions & 2 deletions src/mintTokens.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,103 @@
import { createBurnCheckedInstruction, getOrCreateAssociatedTokenAccount, mintTo } from "@solana/spl-token";
import { Commitment, Connection, Keypair, PublicKey, TransactionMessage, VersionedTransaction, TransactionInstruction, SystemProgram } from "@solana/web3.js";
import { PRIVATE_KEY, TOKEN_MINT_ADDRESS } from "./address";
import bs58 from 'bs58';

const private_key = PRIVATE_KEY;
const mint = new PublicKey(TOKEN_MINT_ADDRESS);
const wallet = bs58.decode(private_key as string);
const commitment: Commitment = "confirmed";
const connection = new Connection("https://api.devnet.solana.com", commitment);
const keypair = Keypair.fromSecretKey(new Uint8Array(wallet));
const token_decimals = BigInt(1_000_000);

// Mint tokens to an associated token account
export const mintTokens = async (fromAddress: string, toAddress: string, amount: number) => {
const mintto = new PublicKey(fromAddress);
console.log("Minting tokens");
try {
const ata = await getOrCreateAssociatedTokenAccount(
connection,
keypair,
mint,
mintto,
);

const mintTx = await mintTo(
connection,
keypair,
mint,
ata.address,
keypair.publicKey,
token_decimals * BigInt(amount),
);
console.log(`Success! Minted transaction at ${mintTx}`);
console.log(`Success! Minted ${amount} tokens to ${ata.address.toBase58()}`);
} catch (error) {
console.error("Minting Error:", error);
}
}

export const burnTokens = async (fromAddress: string, toAddress: string, amount: number) => {
// Burn tokens from an associated token account
export const burnTokens = async (fromAddress: string, amount: number) => {
const burnAccount = new PublicKey(fromAddress);
console.log("Burning tokens");

try {
const ata = await getOrCreateAssociatedTokenAccount(
connection,
keypair,
mint,
burnAccount,
);

const burnTx = createBurnCheckedInstruction(
ata.address, // The token account to burn from
mint, // The mint address
keypair.publicKey, // Owner of the token account
token_decimals * BigInt(amount),
Number(token_decimals),
);

const { blockhash, lastValidBlockHeight } = await connection.getLatestBlockhash();
const messageV0 = new TransactionMessage({
payerKey: keypair.publicKey,
recentBlockhash: blockhash,
instructions: [burnTx],
}).compileToV0Message();

const transaction = new VersionedTransaction(messageV0);
transaction.sign([keypair]);

const txId = await connection.sendTransaction(transaction, { skipPreflight: false, maxRetries: 5 });
console.log(`Success! Burn transaction ID: ${txId}`);
} catch (error) {
console.error("Burning Error:", error);
}
}

// Send native SOL tokens
export const sendNativeTokens = async (fromAddress: string, toAddress: string, amount: number) => {
const recipientPubKey = new PublicKey(toAddress);
console.log("Sending native tokens");
}

try {
const transaction = new VersionedTransaction(new TransactionMessage({
payerKey: keypair.publicKey,
recentBlockhash: (await connection.getLatestBlockhash()).blockhash,
instructions: [
SystemProgram.transfer({
fromPubkey: keypair.publicKey,
toPubkey: recipientPubKey,
lamports: BigInt(amount) * BigInt(1_000_000_000) // Convert SOL to lamports
})
]
}).compileToV0Message());

transaction.sign([keypair]);
const txId = await connection.sendTransaction(transaction, { skipPreflight: false, maxRetries: 5 });
console.log(`Success! Native transfer transaction ID: ${txId}`);
} catch (error) {
console.error("Transfer Error:", error);
}
}
Empty file added wallet.json
Empty file.
Loading