87 lines
4.0 KiB
JavaScript
87 lines
4.0 KiB
JavaScript
import { PrivyClient } from '@privy-io/server-auth';
|
|
import { PublicKey } from '@solana/web3.js';
|
|
import { SolanaAgentKit } from '@/solana-agent-kit'
|
|
import pool from "@/server/db";
|
|
import { createPrivyEmbeddedWallet } from '@/lib/solana/PrivyEmbeddedWallet';
|
|
import {TOKENS} from "@/solana-agent-kit/constants"
|
|
|
|
const PRIVY_APP_ID = process.env.NEXT_PUBLIC_PRIVY_APP_ID;
|
|
const PRIVY_APP_SECRET = process.env.PRIVY_APP_SECRET;
|
|
const PRIVY_WALLET_SIGNING_KEY = process.env.PRIVY_WALLET_SIGNING_KEY;
|
|
const RPC_URL = process.env.RPC_URL;
|
|
const OPENAI_API_KEY = process.env.OPENAI_API_KEY;
|
|
const HELIUS_API_KEY = process.env.HELIUS_API_KEY;
|
|
|
|
|
|
export default async function handler(req, res) {
|
|
console.log("Received request:", req.method, req.url);
|
|
|
|
const cookieAuthToken = req.cookies["privy-id-token"];
|
|
if (!cookieAuthToken) {
|
|
console.log("No authentication token found.");
|
|
return res.status(401).json({ error: "Unauthorized: No auth token" });
|
|
}
|
|
|
|
try {
|
|
const PRIVY_SERVER_CLIENT = new PrivyClient(PRIVY_APP_ID, PRIVY_APP_SECRET, {
|
|
walletApi: {
|
|
authorizationPrivateKey: PRIVY_WALLET_SIGNING_KEY,
|
|
},
|
|
});
|
|
|
|
console.log("Verifying auth token...");
|
|
const claims = await PRIVY_SERVER_CLIENT.verifyAuthToken(cookieAuthToken);
|
|
const userId = claims.userId;
|
|
console.log("Authenticated user ID:", userId);
|
|
|
|
const dbRes = await pool.query("SELECT userid, publickey, tg_userid, delegated FROM users WHERE userid = $1", [userId]);
|
|
if (dbRes.rows.length === 1) {
|
|
const { userid, publickey } = dbRes.rows[0];
|
|
console.log("User found in database:", userid, "with public key:", publickey);
|
|
|
|
const walletAdapter = new createPrivyEmbeddedWallet(
|
|
PRIVY_SERVER_CLIENT,
|
|
new PublicKey(publickey)
|
|
);
|
|
|
|
const agent = new SolanaAgentKit(walletAdapter, RPC_URL, {
|
|
OPENAI_API_KEY,
|
|
HELIUS_API_KEY
|
|
});
|
|
|
|
console.log("Performing trade...");
|
|
// working
|
|
// const signature = await agent.transfer(new PublicKey('FM3ZZEPrfqwwVAnfBhfXq2ExitG58dUfiymcrYd9SLeD'),0.001)
|
|
// const signature = await agent.createImage("happy nature")
|
|
// const signature = await agent.getWalletAddress()
|
|
// const signature = await agent.createGibworkTask("Testing my agent","Testing my agent","Testing my agent",["TEST"],"So11111111111111111111111111111111111111112",0.055)
|
|
// {
|
|
// status: 'success',
|
|
// taskId: 'eae20ca6-019f-4a8c-a1aa-83fb7727a739',
|
|
// signature: '4ezTKYMcnopRimFxRsgydrgMndy9WcojsczeCAhZR64psqS1DMQ582qMRKvmn75P1QeaVYBX2aG4HHVu3G2B7XuK'
|
|
// }
|
|
// const signature = await agent.getAllAssetsbyOwner('ADG9zh4E2CoieEAqQn8YamhLhs5pkUwEysaD4LTqvvTx',20)
|
|
// const signature = await agent.heliusParseTransactions('52Cy4FTEnS5KfQg7WuizFa2Yu6ieYAAPLwwvvqQJzh5J7V8vycMiB8yffkfHoNZvrKNc5fbEZCW8iyo4Pq4F4Xeu')
|
|
// const signature = await agent.fetchTokenPrice('B5WTLaRwaUQpKk7ir1wniNB6m5o8GgMrimhKMYan2R6B')
|
|
// const signature = await agent.stake(0.0001)
|
|
// const signature = await agent.trade(TOKENS.USDC,0.0001,TOKENS.SOL)
|
|
// write my own implementation
|
|
// const signature = await agent.launchPumpFunToken('test','test','test','https://pump.mypinata.cloud/ipfs/QmRCWhURvy2Bnt3CbKjZVvEA5QcGy7iMd1XcmVv95V3fwP?img-width=800&img-dpr=2&img-onerror=redirect')
|
|
// const signature = await agent.fetchTokenDetailedReport(TOKENS.BONK)
|
|
// const signature = await agent.fetchTokenReportSummary(TOKENS.BONK)
|
|
// const signature = await agent.resolveSolDomain("balls.sol")
|
|
// const signature = await agent.closeEmptyTokenAccounts()
|
|
|
|
|
|
console.log("Trade completed with signature:", signature);
|
|
return res.status(200).json({ success: true, signature });
|
|
} else {
|
|
console.log("User not found in database.");
|
|
return res.status(404).json({ error: "User not found" });
|
|
}
|
|
} catch (error) {
|
|
console.error("Error during request handling:", error);
|
|
return res.status(500).json({ error: "Internal Server Error", details: error.message });
|
|
}
|
|
}
|