Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(plugin-sui): add support for swapping and transferring by domain name, and converting domain name to address #2140

Closed
wants to merge 16 commits into from
Closed
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/plugin-sui/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ Contributions are welcome! Please see the [CONTRIBUTING.md](CONTRIBUTING.md) fil
This plugin integrates with and builds upon several key technologies:

- [Sui Blockchain](https://sui.io/): Next-generation smart contract platform
- [@mysten/sui.js](https://www.npmjs.com/package/@mysten/sui.js): Official Sui SDK
- [@mysten/sui](https://www.npmjs.com/package/@mysten/sui): Official new version Sui SDK
- [bignumber.js](https://github.com/MikeMcl/bignumber.js/): Precise number handling
- [node-cache](https://www.npmjs.com/package/node-cache): Caching implementation

Expand Down
3 changes: 3 additions & 0 deletions packages/plugin-sui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
"@elizaos/core": "workspace:*",
"@elizaos/plugin-trustdb": "workspace:*",
"@mysten/sui": "^1.16.0",
"@mysten/sui.js": "^0.54.1",
"@mysten/suins": "^0.4.2",
"aftermath-ts-sdk": "^1.2.45",
"bignumber": "1.1.0",
"bignumber.js": "9.1.2",
"node-cache": "5.1.2",
Expand Down
222 changes: 222 additions & 0 deletions packages/plugin-sui/src/actions/convertNameToAddress.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,222 @@
import {
ActionExample,
Content,
HandlerCallback,
IAgentRuntime,
Memory,
ModelClass,
State,
composeContext,
elizaLogger,
generateObject,
type Action,
} from "@elizaos/core";
import { z } from "zod";

import { SuiClient, getFullnodeUrl } from "@mysten/sui/client";
import { SuinsClient } from "@mysten/suins";

import { walletProvider } from "../providers/wallet";

type SuiNetwork = "mainnet" | "testnet" | "devnet" | "localnet";

export interface NameToAddressContent extends Content {
recipientName: string;
}

function isNameToAddressContent(
content: Content
): content is NameToAddressContent {
console.log("Content for show address", content);
return typeof content.recipientName === "string";
}

const nameToAddressTemplate = `Extract the SUI domain name from the recent messages and return it in a JSON format.

Example input: "Convert adeniyi.sui to address" or "What's the address for adeniyi.sui"
Example output:
\`\`\`json
{
"recipientName": "adeniyi.sui"
}
\`\`\`

{{recentMessages}}

Extract the SUI domain name (ending in .sui) that needs to be converted to an address.
If no valid .sui domain is found, return null.`;

export default {
name: "CONVERT_SUINS_TO_ADDRESS",
similes: [
"CONVERT_SUI_NAME_TO_ADDRESS",
"CONVERT_DOMAIN_TO_ADDRESS",
"SHOW_ADDRESS_BY_NAME",
"SHOW_ADDRESS_BY_DOMAIN",
],
validate: async (runtime: IAgentRuntime, message: Memory) => {
console.log(
"Validating sui name to address from user:",
message.userId
);
//add custom validate logic here
/*
const adminIds = runtime.getSetting("ADMIN_USER_IDS")?.split(",") || [];
//console.log("Admin IDs from settings:", adminIds);

const isAdmin = adminIds.includes(message.userId);

if (isAdmin) {
//console.log(`Authorized transfer from user: ${message.userId}`);
return true;
}
else
{
//console.log(`Unauthorized transfer attempt from user: ${message.userId}`);
return false;
}
*/
return true;
},
description: "Convert a name service domain to an sui address",
handler: async (
runtime: IAgentRuntime,
message: Memory,
state: State,
_options: { [key: string]: unknown },
callback?: HandlerCallback
): Promise<boolean> => {
elizaLogger.log("Starting CONVERT_SUINS_TO_ADDRESS handler...");
const walletInfo = await walletProvider.get(runtime, message, state);
state.walletInfo = walletInfo;

// Initialize or update state
if (!state) {
state = (await runtime.composeState(message)) as State;
} else {
state = await runtime.updateRecentMessageState(state);
}

// Define the schema for the expected output
const nameToAddressSchema = z.object({
recipientName: z.string(),
});

// Compose transfer context
const nameToAddressContext = composeContext({
state,
template: nameToAddressTemplate,
});

// Generate transfer content with the schema
const content = await generateObject({
runtime,
context: nameToAddressContext,
schema: nameToAddressSchema,
modelClass: ModelClass.SMALL,
});

const nameToAddressContent = content.object as NameToAddressContent;

// Validate transfer content
if (!isNameToAddressContent(nameToAddressContent)) {
console.error(
"Invalid content for CONVERT_SUINS_TO_ADDRESS action."
);
if (callback) {
callback({
text: "Unable to process name to address request. Invalid content provided.",
content: { error: "Invalid name to address content" },
});
}
return false;
}

try {
const network = runtime.getSetting("SUI_NETWORK");
const suiClient = new SuiClient({
url: getFullnodeUrl(network as SuiNetwork),
});
const suinsClient = new SuinsClient({
client: suiClient,
network: network as Exclude<SuiNetwork, "devnet" | "localnet">,
});

console.log(
"Getting address for name:",
nameToAddressContent.recipientName
);

const address = await suinsClient.getNameRecord(
nameToAddressContent.recipientName
);
console.log("Address:", address);

if (callback) {
callback({
text: `Successfully convert ${nameToAddressContent.recipientName} to ${address.targetAddress}`,
content: {
success: true,
address: address.targetAddress,
},
});
}

return true;
} catch (error) {
console.error("Error during name to address conversion:", error);
if (callback) {
callback({
text: `Error during name to address conversion: ${error.message}`,
content: { error: error.message },
});
}
return false;
}
},

examples: [
[
{
user: "{{user1}}",
content: {
text: "Convert adeniyi.sui to address",
},
},
{
user: "{{user2}}",
content: {
text: "Converting adeniyi.sui to address...",
action: "CONVERT_SUINS_TO_ADDRESS",
},
},
{
user: "{{user2}}",
content: {
text: "Successfully convert adeniyi.sui to 0x1eb7c57e3f2bd0fc6cb9dcffd143ea957e4d98f805c358733f76dee0667fe0b1",
},
},
],
[
{
user: "{{user1}}",
content: {
text: "Convert @adeniyi to address",
},
},
{
user: "{{user2}}",
content: {
text: "Convert @adeniyi to address",
action: "CONVERT_NAME_TO_ADDRESS",
},
},
{
user: "{{user2}}",
content: {
text: "Successfully convert @adeniyi to 0x1eb7c57e3f2bd0fc6cb9dcffd143ea957e4d98f805c358733f76dee0667fe0b1",
},
},
],
] as ActionExample[][],
} as Action;
Loading
Loading