import { Client, Wallet } from "xrpl";
import * as xrpl from "xrpl";
import { z } from "zod";
import { server } from "../server/server.js";
import { getXrplClient } from "../core/services/clients.js";
import { MAINNET_URL, TESTNET_URL } from "../core/constants.js";
import { connectedWallet, isConnectedToTestnet } from "../core/state.js";
// Register XRPL transfer tool
server.tool(
"transfer-xrp",
"Transfer XRP between accounts using the connected wallet or a specified seed",
{
fromSeed: z
.string()
.optional()
.describe(
"Optional seed of the wallet to send XRP from. If not provided, the connected wallet will be used."
),
toAddress: z
.string()
.describe(
"XRP Ledger account address to send XRP to (starts with r)"
),
amount: z.string().describe("Amount of XRP to send"),
useTestnet: z
.boolean()
.optional()
.describe(
"Whether to use the testnet (true) or mainnet (false). If not provided, uses the network from the connected wallet."
),
},
async ({ fromSeed, toAddress, amount, useTestnet }) => {
let client: Client | null = null;
try {
// Determine which network to use
const useTestnetNetwork =
useTestnet !== undefined ? useTestnet : isConnectedToTestnet;
client = await getXrplClient(useTestnetNetwork);
// Use provided seed or connected wallet
let wallet: Wallet;
if (fromSeed) {
wallet = Wallet.fromSeed(fromSeed);
} else if (connectedWallet) {
wallet = connectedWallet;
} else {
throw new Error(
"No wallet connected. Please connect first using connect-to-xrpl tool or provide a fromSeed."
);
}
const payment = {
TransactionType: "Payment",
Account: wallet.address,
Amount: xrpl.xrpToDrops(amount),
Destination: toAddress,
};
const prepared = await client.autofill(payment as any);
const signed = wallet.sign(prepared);
const result = await client.submitAndWait(signed.tx_blob);
let status = "unknown";
if (typeof result.result.meta !== "string" && result.result.meta) {
status =
result.result.meta.TransactionResult === "tesSUCCESS"
? "success"
: "failed";
}
return {
content: [
{
type: "text",
text: JSON.stringify(
{
status,
hash: result.result.hash,
fromAddress: wallet.address,
toAddress,
amount,
network: useTestnetNetwork
? TESTNET_URL
: MAINNET_URL,
networkType: useTestnetNetwork
? "testnet"
: "mainnet",
result: result.result,
},
null,
2
),
},
],
};
} catch (error) {
return {
content: [
{
type: "text",
text: `Error transferring XRP: ${
error instanceof Error
? error.message
: String(error)
}`,
},
],
};
} finally {
if (client) {
await client.disconnect();
}
}
}
);