|
| 1 | +import { AwsS3Service } from "@ai16z/plugin-node"; |
| 2 | +import { |
| 3 | + composeContext, |
| 4 | + elizaLogger, |
| 5 | + generateImage, |
| 6 | + getEmbeddingZeroVector, |
| 7 | + IAgentRuntime, |
| 8 | + Memory, |
| 9 | + ServiceType, |
| 10 | + stringToUuid, |
| 11 | +} from "@ai16z/eliza"; |
| 12 | +import { |
| 13 | + saveBase64Image, |
| 14 | + saveHeuristImage, |
| 15 | +} from "@ai16z/plugin-image-generation"; |
| 16 | +import { PublicKey } from "@solana/web3.js"; |
| 17 | +import WalletSolana from "../provider/wallet/walletSolana.ts"; |
| 18 | + |
| 19 | +const collectionImageTemplate = ` |
| 20 | +Generate a logo with the text "{{collectionName}}", using orange as the main color, with a sci-fi and mysterious background theme |
| 21 | +`; |
| 22 | + |
| 23 | +export async function createCollection({ |
| 24 | + runtime, |
| 25 | + collectionName, |
| 26 | + fee, |
| 27 | +}: { |
| 28 | + runtime: IAgentRuntime; |
| 29 | + collectionName: string; |
| 30 | + fee?: number; |
| 31 | +}) { |
| 32 | + const userId = runtime.agentId; |
| 33 | + elizaLogger.log("User ID:", userId); |
| 34 | + const awsS3Service: AwsS3Service = runtime.getService(ServiceType.AWS_S3); |
| 35 | + const agentName = runtime.character.name; |
| 36 | + const roomId = stringToUuid("nft_generate_room-" + agentName); |
| 37 | + // Create memory for the message |
| 38 | + const memory: Memory = { |
| 39 | + agentId: userId, |
| 40 | + userId, |
| 41 | + roomId, |
| 42 | + content: { |
| 43 | + text: "", |
| 44 | + |
| 45 | + source: "nft-generator", |
| 46 | + }, |
| 47 | + createdAt: Date.now(), |
| 48 | + embedding: getEmbeddingZeroVector(), |
| 49 | + }; |
| 50 | + const state = await runtime.composeState(memory, { |
| 51 | + collectionName, |
| 52 | + }); |
| 53 | + |
| 54 | + const prompt = composeContext({ |
| 55 | + state, |
| 56 | + template: collectionImageTemplate, |
| 57 | + }); |
| 58 | + const images = await generateImage( |
| 59 | + { |
| 60 | + prompt, |
| 61 | + width: 300, |
| 62 | + height: 300, |
| 63 | + }, |
| 64 | + runtime |
| 65 | + ); |
| 66 | + if (images.success && images.data && images.data.length > 0) { |
| 67 | + const image = images.data[0]; |
| 68 | + const filename = `collection-image`; |
| 69 | + if (image.startsWith("http")) { |
| 70 | + elizaLogger.log("Generating image url:", image); |
| 71 | + } |
| 72 | + // Choose save function based on image data format |
| 73 | + const filepath = image.startsWith("http") |
| 74 | + ? await saveHeuristImage(image, filename) |
| 75 | + : saveBase64Image(image, filename); |
| 76 | + |
| 77 | + const logoPath = await awsS3Service.uploadFile( |
| 78 | + filepath, |
| 79 | + `/${collectionName}`, |
| 80 | + false |
| 81 | + ); |
| 82 | + const publicKey = runtime.getSetting("SOLANA_PUBLIC_KEY"); |
| 83 | + const privateKey = runtime.getSetting("SOLANA_PRIVATE_KEY"); |
| 84 | + const adminPublicKey = runtime.getSetting("SOLANA_ADMIN_PUBLIC_KEY"); |
| 85 | + const collectionInfo = { |
| 86 | + name: `${collectionName}`, |
| 87 | + symbol: `${collectionName.toUpperCase()[0]}`, |
| 88 | + adminPublicKey, |
| 89 | + fee: fee || 0, |
| 90 | + uri: "", |
| 91 | + }; |
| 92 | + const jsonFilePath = await awsS3Service.uploadJson( |
| 93 | + { |
| 94 | + name: collectionInfo.name, |
| 95 | + description: `${collectionInfo.name}`, |
| 96 | + image: logoPath.url, |
| 97 | + }, |
| 98 | + "metadata.json", |
| 99 | + `${collectionName}` |
| 100 | + ); |
| 101 | + collectionInfo.uri = jsonFilePath.url; |
| 102 | + |
| 103 | + const wallet = new WalletSolana(new PublicKey(publicKey), privateKey); |
| 104 | + |
| 105 | + const collectionAddressRes = await wallet.createCollection({ |
| 106 | + ...collectionInfo, |
| 107 | + }); |
| 108 | + |
| 109 | + return { |
| 110 | + network: "solana", |
| 111 | + address: collectionAddressRes.address, |
| 112 | + link: collectionAddressRes.link, |
| 113 | + collectionInfo, |
| 114 | + }; |
| 115 | + } |
| 116 | + |
| 117 | + return; |
| 118 | +} |
0 commit comments