|
| 1 | +import { |
| 2 | + Asset, |
| 3 | + Account, |
| 4 | + TransactionBuilder, |
| 5 | + Operation, |
| 6 | + Networks, |
| 7 | +} from "@stellar/stellar-sdk"; |
| 8 | + |
| 9 | +import { getHorizonServer } from "../services/horizon.js"; |
| 10 | +import { config } from "../config.js"; |
| 11 | +import { CreateTrustlineInputSchema } from "../schemas/tools.js"; |
| 12 | +import type { McpToolHandler } from "../types.js"; |
| 13 | +import { |
| 14 | + PulsarValidationError, |
| 15 | + PulsarNetworkError, |
| 16 | +} from "../errors.js"; |
| 17 | +import logger from "../logger.js"; |
| 18 | + |
| 19 | +export interface CreateTrustlineOutput { |
| 20 | + transaction_xdr: string; |
| 21 | + source_account: string; |
| 22 | + asset: { |
| 23 | + code: string; |
| 24 | + issuer: string; |
| 25 | + }; |
| 26 | + limit: string; |
| 27 | + network: string; |
| 28 | +} |
| 29 | + |
| 30 | +/** Resolve the stellar-base network passphrase. */ |
| 31 | +function resolveNetworkPassphrase(network: string): string { |
| 32 | + switch (network) { |
| 33 | + case "mainnet": |
| 34 | + return Networks.PUBLIC; |
| 35 | + case "futurenet": |
| 36 | + return Networks.FUTURENET; |
| 37 | + case "testnet": |
| 38 | + default: |
| 39 | + return Networks.TESTNET; |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +/** |
| 44 | + * Tool: create_trustline |
| 45 | + * |
| 46 | + * Builds a Stellar transaction for creating a trustline (allowing an account |
| 47 | + * to hold a specific issued asset). Returns the unsigned transaction XDR. |
| 48 | + * |
| 49 | + * A trustline must be created before an account can receive or hold an |
| 50 | + * issued asset (anything other than XLM). |
| 51 | + */ |
| 52 | +export const createTrustline: McpToolHandler< |
| 53 | + typeof CreateTrustlineInputSchema |
| 54 | +> = async (input: unknown) => { |
| 55 | + const validatedInput = CreateTrustlineInputSchema.safeParse(input); |
| 56 | + if (!validatedInput.success) { |
| 57 | + throw new PulsarValidationError( |
| 58 | + "Invalid input for create_trustline", |
| 59 | + validatedInput.error.format() |
| 60 | + ); |
| 61 | + } |
| 62 | + |
| 63 | + const data = validatedInput.data; |
| 64 | + const network = data.network ?? config.stellarNetwork; |
| 65 | + const networkPassphrase = resolveNetworkPassphrase(network); |
| 66 | + const sourceAccount = data.source_account; |
| 67 | + |
| 68 | + // ------------------------------------------------------------------ |
| 69 | + // 1. Fetch source account from Horizon for sequence number |
| 70 | + // ------------------------------------------------------------------ |
| 71 | + const horizonServer = getHorizonServer(network); |
| 72 | + let rawAccount; |
| 73 | + try { |
| 74 | + logger.debug( |
| 75 | + { account: sourceAccount, network }, |
| 76 | + "Loading source account for trustline creation" |
| 77 | + ); |
| 78 | + rawAccount = await horizonServer.loadAccount(sourceAccount); |
| 79 | + } catch (err: any) { |
| 80 | + if (err.response?.status === 404) { |
| 81 | + throw new PulsarNetworkError( |
| 82 | + `Source account ${sourceAccount} not found. Fund the account before creating a trustline.`, |
| 83 | + { status: 404, account_id: sourceAccount } |
| 84 | + ); |
| 85 | + } |
| 86 | + throw new PulsarNetworkError( |
| 87 | + `Failed to load source account: ${err.message}`, |
| 88 | + { originalError: err } |
| 89 | + ); |
| 90 | + } |
| 91 | + |
| 92 | + const account = |
| 93 | + typeof (rawAccount as any).incrementSequenceNumber === "function" && |
| 94 | + typeof (rawAccount as any).sequenceNumber === "function" |
| 95 | + ? (rawAccount as unknown as Account) |
| 96 | + : new Account( |
| 97 | + typeof (rawAccount as any).accountId === "function" |
| 98 | + ? (rawAccount as any).accountId() |
| 99 | + : (rawAccount as any).accountId, |
| 100 | + typeof (rawAccount as any).sequenceNumber === "function" |
| 101 | + ? (rawAccount as any).sequenceNumber() |
| 102 | + : (rawAccount as any).sequenceNumber |
| 103 | + ); |
| 104 | + |
| 105 | + // ------------------------------------------------------------------ |
| 106 | + // 2. Create the asset object |
| 107 | + // ------------------------------------------------------------------ |
| 108 | + let asset: Asset; |
| 109 | + try { |
| 110 | + asset = new Asset(data.asset_code, data.asset_issuer); |
| 111 | + } catch (err: any) { |
| 112 | + throw new PulsarValidationError( |
| 113 | + `Invalid asset or issuer: ${err.message}`, |
| 114 | + { asset_code: data.asset_code, asset_issuer: data.asset_issuer } |
| 115 | + ); |
| 116 | + } |
| 117 | + |
| 118 | + // ------------------------------------------------------------------ |
| 119 | + // 3. Build the changeTrust operation |
| 120 | + // ------------------------------------------------------------------ |
| 121 | + const changeTrustOp = Operation.changeTrust({ |
| 122 | + asset, |
| 123 | + limit: data.limit, |
| 124 | + }); |
| 125 | + |
| 126 | + logger.debug( |
| 127 | + { |
| 128 | + sourceAccount: data.source_account, |
| 129 | + assetCode: data.asset_code, |
| 130 | + assetIssuer: data.asset_issuer, |
| 131 | + limit: data.limit || "unlimited", |
| 132 | + }, |
| 133 | + "Building trustline creation transaction" |
| 134 | + ); |
| 135 | + |
| 136 | + // ------------------------------------------------------------------ |
| 137 | + // 4. Build the transaction |
| 138 | + // ------------------------------------------------------------------ |
| 139 | + const tx = new TransactionBuilder(account, { |
| 140 | + fee: "100", |
| 141 | + networkPassphrase, |
| 142 | + }) |
| 143 | + .addOperation(changeTrustOp) |
| 144 | + .setTimeout(30) |
| 145 | + .build(); |
| 146 | + |
| 147 | + return { |
| 148 | + transaction_xdr: tx.toXDR(), |
| 149 | + source_account: sourceAccount, |
| 150 | + asset: { |
| 151 | + code: data.asset_code, |
| 152 | + issuer: data.asset_issuer, |
| 153 | + }, |
| 154 | + limit: data.limit || "922337203685.4775807", // MAX_INT64 in stroops / 10^7 |
| 155 | + network, |
| 156 | + }; |
| 157 | +}; |
0 commit comments