11import { NextResponse } from 'next/server' ;
22import { Connection , Cluster , clusterApiUrl , PublicKey , Transaction , Keypair , Signer } from '@solana/web3.js' ;
3- import { TOKEN_PROGRAM_ID , createMintToInstruction } from '@solana/spl-token' ;
3+ import { TOKEN_PROGRAM_ID , createMintToInstruction , createTransferInstruction , getAssociatedTokenAddressSync } from '@solana/spl-token' ;
44import * as anchor from "@coral-xyz/anchor" ;
55import { Program , AnchorProvider } from "@coral-xyz/anchor" ;
66import { Vesting , IDL } from "../../types/vesting" ;
7+ import { getDecimalsAndSupplyToken } from '@/app/lib/utils' ;
78
89export async function POST ( req : Request ) {
910 const contentType = req . headers . get ( 'content-type' ) ;
@@ -12,54 +13,61 @@ export async function POST(req: Request) {
1213 return NextResponse . json ( { success : false , error : 'Content-Type must be application/json' } , { status : 400 } ) ;
1314 }
1415
15- const { company_name, mint, beneficiary } = await req . json ( ) ;
16+ const { company_name, mint, signer , treasuryAmount } = await req . json ( ) ;
1617
17- // Validate input
18- if ( ! company_name || ! mint || ! beneficiary ) {
18+ if ( ! company_name || ! mint || ! signer || ! treasuryAmount ) {
1919 return NextResponse . json ( { success : false , error : 'Missing required fields' } , { status : 400 } ) ;
2020 }
2121
2222 try {
2323 const cluster = 'devnet' as Cluster ;
2424 const connection = new Connection ( clusterApiUrl ( cluster ) , "confirmed" ) ;
25- const beneficiaryPubKey = new PublicKey ( beneficiary ) ;
26- const wall = { publicKey : beneficiaryPubKey } as anchor . Wallet ;
25+ const vestingAccountOwner = new PublicKey ( signer ) ;
26+ const wall = { publicKey : vestingAccountOwner } as anchor . Wallet ;
2727 const provider = new AnchorProvider ( connection , wall ) ;
2828
2929 anchor . setProvider ( provider ) ;
30- // const program = anchor.workspace.Vesting as Program<Vesting>;
3130 const program = new Program < Vesting > ( IDL as Vesting , provider ) ;
3231
3332 const createVestingAccIxn = await program . methods . createVestingAccount ( company_name )
3433 . accounts ( {
35- signer : beneficiaryPubKey ,
34+ signer : provider . wallet . publicKey ,
3635 mint : new PublicKey ( mint ) ,
3736 tokenProgram : TOKEN_PROGRAM_ID
3837 } )
3938 . instruction ( ) ;
4039
41- let [ treasuryTokenAccount ] = PublicKey . findProgramAddressSync (
42- [ Buffer . from ( "vesting treasury" ) , Buffer . from ( company_name ) ] ,
43- program . programId
44- ) ;
40+ const sourceATA = getAssociatedTokenAddressSync (
41+ new PublicKey ( mint ) ,
42+ vestingAccountOwner
43+ ) ;
44+
45+ let [ treasuryTokenAccount ] = PublicKey . findProgramAddressSync (
46+ [ Buffer . from ( "vesting treasury" ) , Buffer . from ( company_name ) ] ,
47+ program . programId
48+ ) ;
49+
50+ let decimals = 9
51+ const tokenData = await getDecimalsAndSupplyToken ( connection , mint ) ;
52+ if ( tokenData ) {
53+ decimals = tokenData . decimals ;
54+ }
4555
46- const amount = 10_000 * 10 ** 9 ;
47- const mintTokensIxn = createMintToInstruction (
48- new PublicKey ( mint ) ,
56+ const amount = treasuryAmount * ( 10 ** decimals ) ;
57+ const transferTokensToTreasury = createTransferInstruction (
58+ sourceATA ,
4959 treasuryTokenAccount ,
50- beneficiaryPubKey ,
60+ provider . wallet . publicKey ,
5161 amount ,
52- [ ] ,
53- TOKEN_PROGRAM_ID ,
5462 ) ;
5563
5664 const tx = new Transaction ( ) ;
5765 tx . add ( createVestingAccIxn ) ;
58- tx . add ( mintTokensIxn ) ;
66+ tx . add ( transferTokensToTreasury ) ;
5967
6068 const { blockhash } = await connection . getLatestBlockhash ( ) ;
6169 const keypair = Keypair . generate ( ) ;
62- tx . feePayer = new PublicKey ( beneficiary ) ;
70+ tx . feePayer = provider . wallet . publicKey ;
6371 tx . recentBlockhash = blockhash ;
6472
6573 // tx.partialSign(keypair);
0 commit comments