|
| 1 | +import express from "express"; |
| 2 | + |
| 3 | +import { AgentRuntime } from "@ai16z/eliza"; |
| 4 | +import { createCollection } from "./handlers/createCollection.ts"; |
| 5 | +import { createNFT, createNFTMetadata } from "./handlers/createNFT.ts"; |
| 6 | +import { verifyNFT } from "./handlers/verifyNFT.ts"; |
| 7 | + |
| 8 | +export function createNFTApiRouter(agents: Map<string, AgentRuntime>) { |
| 9 | + const router = express.Router(); |
| 10 | + |
| 11 | + router.post( |
| 12 | + "/api/nft-generation/create-collection", |
| 13 | + async (req: express.Request, res: express.Response) => { |
| 14 | + const agentId = req.body.agentId; |
| 15 | + const fee = req.body.fee || 0; |
| 16 | + const runtime = agents.get(agentId); |
| 17 | + if (!runtime) { |
| 18 | + res.status(404).send("Agent not found"); |
| 19 | + return; |
| 20 | + } |
| 21 | + try { |
| 22 | + const collectionAddressRes = await createCollection({ |
| 23 | + runtime, |
| 24 | + collectionName: runtime.character.name, |
| 25 | + fee |
| 26 | + }); |
| 27 | + |
| 28 | + res.json({ |
| 29 | + success: true, |
| 30 | + data: collectionAddressRes, |
| 31 | + }); |
| 32 | + } catch (e: any) { |
| 33 | + console.log(e); |
| 34 | + res.json({ |
| 35 | + success: false, |
| 36 | + data: JSON.stringify(e), |
| 37 | + }); |
| 38 | + } |
| 39 | + } |
| 40 | + ); |
| 41 | + |
| 42 | + |
| 43 | + router.post( |
| 44 | + "/api/nft-generation/create-nft-metadata", |
| 45 | + async (req: express.Request, res: express.Response) => { |
| 46 | + const agentId = req.body.agentId; |
| 47 | + const collectionName = req.body.collectionName; |
| 48 | + const collectionAddress = req.body.collectionAddress; |
| 49 | + const collectionAdminPublicKey = req.body.collectionAdminPublicKey; |
| 50 | + const collectionFee = req.body.collectionFee; |
| 51 | + const tokenId = req.body.tokenId; |
| 52 | + const runtime = agents.get(agentId); |
| 53 | + if (!runtime) { |
| 54 | + res.status(404).send("Agent not found"); |
| 55 | + return; |
| 56 | + } |
| 57 | + |
| 58 | + try { |
| 59 | + const nftInfo = await createNFTMetadata({ |
| 60 | + runtime, |
| 61 | + collectionName, |
| 62 | + collectionAdminPublicKey, |
| 63 | + collectionFee, |
| 64 | + tokenId, |
| 65 | + }); |
| 66 | + |
| 67 | + res.json({ |
| 68 | + success: true, |
| 69 | + data: { |
| 70 | + ...nftInfo, |
| 71 | + collectionAddress, |
| 72 | + }, |
| 73 | + }); |
| 74 | + } catch (e: any) { |
| 75 | + console.log(e); |
| 76 | + res.json({ |
| 77 | + success: false, |
| 78 | + data: JSON.stringify(e), |
| 79 | + }); |
| 80 | + } |
| 81 | + } |
| 82 | + ); |
| 83 | + |
| 84 | + router.post( |
| 85 | + "/api/nft-generation/create-nft", |
| 86 | + async (req: express.Request, res: express.Response) => { |
| 87 | + const agentId = req.body.agentId; |
| 88 | + const collectionName = req.body.collectionName; |
| 89 | + const collectionAddress = req.body.collectionAddress; |
| 90 | + const collectionAdminPublicKey = req.body.collectionAdminPublicKey; |
| 91 | + const collectionFee = req.body.collectionFee; |
| 92 | + const tokenId = req.body.tokenId; |
| 93 | + const runtime = agents.get(agentId); |
| 94 | + if (!runtime) { |
| 95 | + res.status(404).send("Agent not found"); |
| 96 | + return; |
| 97 | + } |
| 98 | + |
| 99 | + try { |
| 100 | + const nftRes = await createNFT({ |
| 101 | + runtime, |
| 102 | + collectionName, |
| 103 | + collectionAddress, |
| 104 | + collectionAdminPublicKey, |
| 105 | + collectionFee, |
| 106 | + tokenId, |
| 107 | + }); |
| 108 | + |
| 109 | + res.json({ |
| 110 | + success: true, |
| 111 | + data: nftRes, |
| 112 | + }); |
| 113 | + } catch (e: any) { |
| 114 | + console.log(e); |
| 115 | + res.json({ |
| 116 | + success: false, |
| 117 | + data: JSON.stringify(e), |
| 118 | + }); |
| 119 | + } |
| 120 | + } |
| 121 | + ); |
| 122 | + |
| 123 | + |
| 124 | + router.post( |
| 125 | + "/api/nft-generation/verify-nft", |
| 126 | + async (req: express.Request, res: express.Response) => { |
| 127 | + const agentId = req.body.agentId; |
| 128 | + const collectionAddress = req.body.collectionAddress; |
| 129 | + const NFTAddress = req.body.nftAddress; |
| 130 | + const token = req.body.token; |
| 131 | + |
| 132 | + const runtime = agents.get(agentId); |
| 133 | + if (!runtime) { |
| 134 | + res.status(404).send("Agent not found"); |
| 135 | + return; |
| 136 | + } |
| 137 | + const verifyToken = runtime.getSetting('SOLANA_VERIFY_TOKEN') |
| 138 | + if (token !== verifyToken) { |
| 139 | + res.status(401).send(" Access denied for translation"); |
| 140 | + return; |
| 141 | + } |
| 142 | + try { |
| 143 | + const {success} = await verifyNFT({ |
| 144 | + runtime, |
| 145 | + collectionAddress, |
| 146 | + NFTAddress, |
| 147 | + }); |
| 148 | + |
| 149 | + res.json({ |
| 150 | + success: true, |
| 151 | + data: success ? 'verified' : 'unverified', |
| 152 | + }); |
| 153 | + } catch (e: any) { |
| 154 | + console.log(e); |
| 155 | + res.json({ |
| 156 | + success: false, |
| 157 | + data: JSON.stringify(e), |
| 158 | + }); |
| 159 | + } |
| 160 | + } |
| 161 | + ); |
| 162 | + |
| 163 | + |
| 164 | + return router; |
| 165 | +} |
0 commit comments