import { Client, Wallet } 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 approve-token-spending tool (TrustSet)
server.tool(
"approve-token-spending",
"Establish trust line to approve token usage",
{
fromSeed: z
.string()
.optional()
.describe("Seed of the wallet, if not using connected wallet"),
currency: z.string().describe("Currency code"),
issuer: z.string().describe("Issuer address for the token"),
limit: z.string().describe("Maximum amount approved for use"),
useTestnet: z
.boolean()
.optional()
.describe("Whether to use testnet or mainnet"),
},
async ({ fromSeed, currency, issuer, limit, useTestnet }) => {
let client: Client | null = null;
try {
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 or provide a fromSeed."
);
}
// Create trust line transaction
const trustSet = {
TransactionType: "TrustSet",
Account: wallet.address,
LimitAmount: {
currency,
issuer,
value: limit,
},
};
const prepared = await client.autofill(trustSet 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,
account: wallet.address,
currency,
issuer,
limit,
_meta: {
network: useTestnetNetwork
? TESTNET_URL
: MAINNET_URL,
networkType: useTestnetNetwork
? "testnet"
: "mainnet",
},
},
null,
2
),
},
],
};
} catch (error) {
return {
content: [
{
type: "text",
text: `Error setting trust line: ${
error instanceof Error
? error.message
: String(error)
}`,
},
],
};
} finally {
if (client) {
await client.disconnect();
}
}
}
);