Skip to content

Commit

Permalink
update plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
tcm390 committed Jan 12, 2025
1 parent 7817056 commit 57f0eff
Show file tree
Hide file tree
Showing 10 changed files with 546 additions and 562 deletions.
102 changes: 55 additions & 47 deletions packages/plugin-nft-collections/src/actions/get-collections.ts
Original file line number Diff line number Diff line change
@@ -1,52 +1,60 @@
import { Action, IAgentRuntime, Memory } from "@elizaos/core";
import { nftCollectionProvider } from "../providers/nft-collections";
import { Action, IAgentRuntime, Memory, Provider } from "@elizaos/core";

export const getCollectionsAction: Action = {
name: "GET_NFT_COLLECTIONS",
similes: ["LIST_NFT_COLLECTIONS", "SHOW_NFT_COLLECTIONS"],
description:
"Fetches information about curated NFT collections on Ethereum",
validate: async (runtime: IAgentRuntime, message: Memory) => {
return message.content.text.toLowerCase().includes("nft collections");
},
handler: async (runtime: IAgentRuntime, message: Memory) => {
try {
const response = await nftCollectionProvider.get(runtime, message);
await runtime.messageManager.createMemory({
id: message.id,
content: { text: response },
roomId: message.roomId,
userId: message.userId,
agentId: runtime.agentId,
});
return true;
} catch (error) {
console.error("Error fetching NFT collections:", error);
await runtime.messageManager.createMemory({
id: message.id,
content: { text: "Failed to fetch NFT collection data." },
roomId: message.roomId,
userId: message.userId,
agentId: runtime.agentId,
});
return false;
}
},
examples: [
[
{
user: "{{user1}}",
content: {
text: "Can you tell me about the top NFT collections?",
export const getCollectionsAction = (
nftCollectionProvider: Provider
): Action => {
return {
name: "GET_NFT_COLLECTIONS",
similes: ["LIST_NFT_COLLECTIONS", "SHOW_NFT_COLLECTIONS"],
description:
"Fetches information about curated NFT collections on Ethereum",
validate: async (runtime: IAgentRuntime, message: Memory) => {
return message.content.text
.toLowerCase()
.includes("nft collections");
},
handler: async (runtime: IAgentRuntime, message: Memory) => {
try {
const response = await nftCollectionProvider.get(
runtime,
message
);
await runtime.messageManager.createMemory({
id: message.id,
content: { text: response },
roomId: message.roomId,
userId: message.userId,
agentId: runtime.agentId,
});
return true;
} catch (error) {
console.error("Error fetching NFT collections:", error);
await runtime.messageManager.createMemory({
id: message.id,
content: { text: "Failed to fetch NFT collection data." },
roomId: message.roomId,
userId: message.userId,
agentId: runtime.agentId,
});
return false;
}
},
examples: [
[
{
user: "{{user1}}",
content: {
text: "Can you tell me about the top NFT collections?",
},
},
},
{
user: "{{user2}}",
content: {
text: "Certainly! Here are the top NFT collections on Ethereum:",
action: "GET_NFT_COLLECTIONS",
{
user: "{{user2}}",
content: {
text: "Certainly! Here are the top NFT collections on Ethereum:",
action: "GET_NFT_COLLECTIONS",
},
},
},
],
],
],
};
};
229 changes: 116 additions & 113 deletions packages/plugin-nft-collections/src/actions/list-nft.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Action, IAgentRuntime, Memory, State } from "@elizaos/core";
import { NFTService } from "../types";
import { ReservoirService } from "../services/reservoir";

// Helper function to extract NFT listing details from the message
function extractListingDetails(text: string): {
Expand All @@ -18,132 +18,135 @@ function extractListingDetails(text: string): {
};
}

export const listNFTAction: Action = {
name: "LIST_NFT",
similes: ["SELL_NFT", "CREATE_LISTING"],
description:
"Lists an NFT for sale on ikigailabs.xyz marketplace at double the purchase price.",
export const listNFTAction = (nftService: ReservoirService): Action => {
return {
name: "LIST_NFT",
similes: ["SELL_NFT", "CREATE_LISTING"],
description:
"Lists an NFT for sale on ikigailabs.xyz marketplace at double the purchase price.",

validate: async (runtime: IAgentRuntime, message: Memory) => {
const content = message.content.text.toLowerCase();
return (
(content.includes("list") || content.includes("sell")) &&
content.includes("nft") &&
(content.includes("0x") ||
content.includes("token") ||
content.includes("#"))
);
},
validate: async (runtime: IAgentRuntime, message: Memory) => {
const content = message.content.text.toLowerCase();
return (
(content.includes("list") || content.includes("sell")) &&
content.includes("nft") &&
(content.includes("0x") ||
content.includes("token") ||
content.includes("#"))
);
},

handler: async (runtime: IAgentRuntime, message: Memory, state?: State) => {
try {
const {
collectionAddress,
tokenId,
price: userSpecifiedPrice,
} = extractListingDetails(message.content.text);
handler: async (
runtime: IAgentRuntime,
message: Memory,
state?: State
) => {
try {
const {
collectionAddress,
tokenId,
price: userSpecifiedPrice,
} = extractListingDetails(message.content.text);

if (!collectionAddress || !tokenId) {
throw new Error(
"Please provide the collection address and token ID"
);
}
if (!collectionAddress || !tokenId) {
throw new Error(
"Please provide the collection address and token ID"
);
}

const nftService = runtime.services.get(
"nft" as any
) as unknown as NFTService;
if (!nftService) {
throw new Error("NFT service not found");
}
if (!nftService) {
throw new Error("NFT service not found");
}

// Verify ownership before listing
const ownedNFTs = await nftService.getOwnedNFTs(message.userId);
const ownedNFT = ownedNFTs.find(
(nft) =>
nft.collectionAddress.toLowerCase() ===
collectionAddress.toLowerCase() &&
nft.tokenId === tokenId
);
// Verify ownership before listing
const ownedNFTs = await nftService.getOwnedNFTs(message.userId);
const ownedNFT = ownedNFTs.find(
(nft) =>
nft.collectionAddress.toLowerCase() ===
collectionAddress.toLowerCase() &&
nft.tokenId === tokenId
);

if (!ownedNFT) {
throw new Error("You don't own this NFT");
}
if (!ownedNFT) {
throw new Error("You don't own this NFT");
}

// Create the listing on ikigailabs
const listing = await nftService.createListing({
tokenId,
collectionAddress,
price: userSpecifiedPrice || 0, // Default to 0 if no price specified
marketplace: "ikigailabs",
expirationTime:
Math.floor(Date.now() / 1000) + 30 * 24 * 60 * 60, // 30 days
});
// Create the listing on ikigailabs
const listing = await nftService.createListing({
tokenId,
collectionAddress,
price: userSpecifiedPrice || 0, // Default to 0 if no price specified
marketplace: "ikigailabs",
expirationTime:
Math.floor(Date.now() / 1000) + 30 * 24 * 60 * 60, // 30 days
});

const response =
`Successfully created listing on ikigailabs.xyz:\n` +
`• Collection: ${collectionAddress}\n` +
`• Token ID: ${tokenId}\n` +
`• Listing Price: ${userSpecifiedPrice} ETH\n` +
`• Status: ${listing.status}\n` +
`• Listing URL: ${listing.marketplaceUrl}\n` +
(listing.transactionHash
? `• Transaction: ${listing.transactionHash}\n`
: "");
const response =
`Successfully created listing on ikigailabs.xyz:\n` +
`• Collection: ${collectionAddress}\n` +
`• Token ID: ${tokenId}\n` +
`• Listing Price: ${userSpecifiedPrice} ETH\n` +
`• Status: ${listing.status}\n` +
`• Listing URL: ${listing.marketplaceUrl}\n` +
(listing.transactionHash
? `• Transaction: ${listing.transactionHash}\n`
: "");

await runtime.messageManager.createMemory({
id: message.id,
content: { text: response },
roomId: message.roomId,
userId: message.userId,
agentId: runtime.agentId,
});
await runtime.messageManager.createMemory({
id: message.id,
content: { text: response },
roomId: message.roomId,
userId: message.userId,
agentId: runtime.agentId,
});

return true;
} catch (error) {
console.error("NFT listing failed:", error);
await runtime.messageManager.createMemory({
id: message.id,
content: {
text: `Failed to list NFT: ${error.message}`,
},
roomId: message.roomId,
userId: message.userId,
agentId: runtime.agentId,
});
return false;
}
},
return true;
} catch (error) {
console.error("NFT listing failed:", error);
await runtime.messageManager.createMemory({
id: message.id,
content: {
text: `Failed to list NFT: ${error.message}`,
},
roomId: message.roomId,
userId: message.userId,
agentId: runtime.agentId,
});
return false;
}
},

examples: [
[
{
user: "{{user1}}",
content: {
text: "List token #123 from collection 0x1234...abcd",
examples: [
[
{
user: "{{user1}}",
content: {
text: "List token #123 from collection 0x1234...abcd",
},
},
},
{
user: "{{user2}}",
content: {
text: "Creating listing on ikigailabs.xyz at 2x purchase price...",
action: "LIST_NFT",
{
user: "{{user2}}",
content: {
text: "Creating listing on ikigailabs.xyz at 2x purchase price...",
action: "LIST_NFT",
},
},
},
],
[
{
user: "{{user1}}",
content: {
text: "List token #123 from collection 0x1234...abcd for 5 ETH",
],
[
{
user: "{{user1}}",
content: {
text: "List token #123 from collection 0x1234...abcd for 5 ETH",
},
},
},
{
user: "{{user2}}",
content: {
text: "Creating listing on ikigailabs.xyz with specified price...",
action: "LIST_NFT",
{
user: "{{user2}}",
content: {
text: "Creating listing on ikigailabs.xyz with specified price...",
action: "LIST_NFT",
},
},
},
],
],
],
};
};
Loading

0 comments on commit 57f0eff

Please sign in to comment.