Documentation: spare-sdk

Documentation

spare-sdk

Add a per-transaction USDC micro-donation to any Stellar app. The donation goes straight to the foundation's account; Spare never holds funds. Circle USDC only, domain-verified foundations only.

Install

pnpm add spare-sdk @stellar/stellar-sdk

@stellar/stellar-sdk is a peer dependency: install it next to spare-sdk (one copy shared with your app).

Classic transaction: addDonation

import { addDonation, HorizonAccountPort } from "spare-sdk";

const network = "testnet"; // "mainnet" in production

const result = await addDonation(unsignedTx, {
  foundation: { account: "G...", domain: "foundation.org" }, // must be listed in its stellar.toml
  amount: "0.50",            // or: roundUp: true (only when the purchase is USDC)
  appId: "my-app",           // <= 22 ASCII bytes, memo becomes "spare:my-app"
  maxAmount: "5",            // your cap per donation
  accounts: new HorizonAccountPort({ network }),
  network,
});

switch (result.status) {
  case "offered":      // ask the user "Add US$<result.donation.amount>?", then sign result.transaction
  case "not-offered":  // sign the original tx; result.reason says why (e.g. "insufficient-balance")
  case "passthrough":  // signed / fee-bump / multisig / extra signers / Soroban: sign the original tx
}

offered returns a new unsigned transaction (new hash, fee raised by one operation, memo set only if it was empty and no destination requires one). Nothing is added without the user's explicit yes.

Round-up

Only when the whole transaction is a Circle-USDC purchase (payments, pathPaymentStrictSend, sell offers). The SDK reports what it used, so show these instead of recomputing: donation.purchase / purchaseStroops (the USDC purchase) and roundedTotal / roundedTotalStroops (purchase plus donation). They are set on round-up only.

  • Already whole (for example 5.00): round-up-already-whole.
  • pathPaymentStrictReceive, XLM or other-asset operations, or unmodelled operations in the same transaction: round-up-needs-usdc-purchase (the real spend is unknown up front, so Spare never rounds sendMax).

Paying the donation in XLM

Pass payWith: "xlm" and a quotes port so a donor who holds XLM can pay the donation. It becomes a pathPaymentStrictReceive: the foundation receives exactly the amount in Circle USDC (same foundation checks as always), and the donor spends at most the quoted XLM plus slippageBps (default 200 = 2%, integer 0 to 1000) as sendMax. Unused XLM stays with the donor. The donor needs no USDC trustline: the balance check is XLM above the minimum reserve covering sendMax, the fee and any XLM the transaction already sends. Works for addDonation and donateAfter; USDC stays the default.

import { addDonation, HorizonAccountPort, HorizonPathQuotePort } from "spare-sdk";

const result = await addDonation(unsignedTx, {
  foundation, amount: "0.25", appId: "my-app", maxAmount: "5",
  accounts: new HorizonAccountPort({ network }),
  network,
  payWith: "xlm",                                // donor spends XLM...
  quotes: new HorizonPathQuotePort({ network }), // ...priced via Horizon /paths/strict-receive
  slippageBps: 200,                              // sendMax = quote + 2% (the default)
});
if (result.status === "offered") {
  // the foundation receives exactly 0.25 USDC; show "≈ <result.donation.sendMax> XLM max"
}

HorizonPathQuotePort asks Horizon /paths/strict-receive with source_assets=native and keeps the cheapest path of at most 5 hops. No path or a failed quote gives not-offered / no-path; nothing throws. donation.payWith, sendMax and quotedXlm tell you what to show, for example “≈ N XLM max”.

In addDonation the XLM-paid donation shares the transaction with the purchase. If the price moves past sendMax before it applies, the whole transaction fails, purchase included; nothing moves and only the fee is spent. Quote right before signing. A USDC purchase next to an XLM-paid donation still needs the donor's USDC trustline and balance. Known limitation: a donor's own open offers on the XLM/USDC book can make the path payment fail (OFFER_CROSS_SELF). The XLM check uses the account's current reserve: subentries your own transaction adds (changeTrust, manageData, offers) are not counted, so the transaction can still fail with op_underfunded.

A Soroban transaction holds one operation, so the donation is a separate payment after success. It needs a second signature: tell the user before they accept.

import { donateAfter } from "spare-sdk";

const follow = await donateAfter(
  { successful: true, sourceAccount: userAccount, hash },
  { foundation, amount: "0.50", appId: "my-app", maxAmount: "5",
    accounts, network },
);
if (follow.status === "offered") { /* confirm, then sign follow.transaction */ }

The follow-up is a single payment (medium threshold). If the user's account needs more than its master key to sign that, nothing is offered (multisig).

Results and reason codes

StatusMeaning
offeredA new unsigned transaction, the donation info and the fee. Ask the user, then sign it.
not-offeredThe transaction is fine but no donation must be offered. reason says why. Sign the original.
passthroughSpare cannot safely modify the transaction. It is left untouched. Sign the original.

passthrough reasons

ReasonMeaning
fee-bumpThe transaction is a fee-bump.
already-signedThe transaction already carries signatures.
sorobanA Soroban transaction: use donateAfter.
multiple-sourcesMore than one source account.
multisigThe master key alone cannot meet the transaction's threshold: medium, or high with setOptions / accountMerge.
extra-signersThe CAP-21 extraSigners precondition is set.

not-offered reasons

ReasonMeaning
invalid-optionsOptions are malformed, including a malformed baseFee.
invalid-app-idappId is not valid (at most 22 ASCII bytes).
invalid-amountThe amount is not a valid USDC amount.
invalid-foundationThe foundation is malformed.
amount-over-capThe donation is above your maxAmount.
round-up-needs-usdc-purchaseRound-up needs a Circle-USDC purchase (see above).
network-mismatchThe transaction was built for a different network than network.
round-up-already-wholeThe purchase is already a whole amount.
foundation-not-verifiedThe foundation account is not listed in its website's stellar.toml.
foundation-trustline-missingThe foundation has no USDC trustline.
foundation-trustline-unauthorizedThe foundation's USDC trustline is not authorized.
foundation-trustline-fullNo room under the foundation's trustline limit.
user-trustline-missingThe user has no USDC trustline.
user-trustline-unauthorizedThe user's USDC trustline is not authorized.
unsupported-operationThe transaction has operations that can spend funds in ways Spare does not model, for example manageBuyOffer, createClaimableBalance, liquidityPoolDeposit, accountMerge.
multisigFrom donateAfter only: the follow-up needs more than the master key.
insufficient-balanceNot enough USDC (paying in XLM: not enough XLM above the reserve for sendMax), or XLM for the fee plus any XLM the transaction sends.
no-pathPaying in XLM: there is no XLM to USDC path right now, or the quote failed.
account-lookup-failedThe account could not be loaded.
main-tx-not-successfuldonateAfter only: the main transaction did not succeed.
account-not-synceddonateAfter only: Horizon had not caught up with the main transaction in time, so nothing was built.

DonationInfo

FieldDescription
amountAmount in USDC, 7 decimals. Show this to the user before signing.
amountStroopsThe same amount in stroops (bigint).
foundationThe foundation account.
attributedFalse when the transaction already had a memo, so the donation cannot be tagged for the app.
purchase, purchaseStroopsRound-up only: the USDC purchase the transaction already makes.
roundedTotal, roundedTotalStroopsRound-up only: the purchase plus the donation.
payWith"usdc" or "xlm": what the donor spends. The foundation always receives USDC.
sendMax, sendMaxStroopsXLM only: the most XLM the donor can spend (quote plus margin).
quotedXlm, quotedXlmStroopsXLM only: the quote it came from.

Options

OptionDescription
foundation{ account, domain }: the G... account and the bare hostname whose stellar.toml must list it.
appIdAt most 22 ASCII bytes. The memo becomes spare:<appId>.
maxAmountYour cap per donation, in USDC.
accountsAn AccountPort (for example HorizonAccountPort).
fetchOptional. Injectable fetch, defaults to the global one.
baseFeeOptional. Per-operation fee in stroops, digits only, minimum 100.
payWithOptional. "usdc" (default) or "xlm".
quotesRequired with payWith: "xlm": a PathQuotePort, for example HorizonPathQuotePort.
slippageBpsOptional. XLM margin over the quote in basis points, integer 0 to 1000, default 200.
sendMaxOptional, XLM only. Exact cap instead of quote plus margin; offered only if the live quote fits under it (else no-path).

Account port

AccountPort.loadAccount(id) returns an AccountSnapshot, or null when the account does not exist. HorizonAccountPort fills it from Horizon (headroom = limit - balance - buying liabilities; memoRequired from the config.memo_required data entry). Spare decides about multiple signatures from masterWeight and thresholds per transaction.

FieldDescription
sequenceCurrent sequence number (the next transaction uses sequence + 1).
xlmAvailableXLM spendable on fees: balance minus reserve and selling liabilities.
usdcCircle-USDC trustline { balance, authorized, headroom }, or null when there is none.
masterWeightWeight of the account's own key; 0 when it is disabled.
thresholds{ low, medium, high }, 0-255.
memoRequiredSEP-29: the account expects a memo on incoming payments.

Memo, SEP-29 and fees

If a payment, path payment or merge destination has config.memo_required set, Spare does not set its memo, so your app's own checkMemoRequired still fires. The donation is still offered, with donation.attributed === false. A destination that cannot be read is treated as memo-required.

baseFee is per operation, digits only, minimum 100 stroops. A malformed value gives not-offered / invalid-options; it never throws. A transaction with a fee of 0 is raised to 100 per operation, and the same number is used for the balance check and the built transaction.

Helpers

  • verifyFoundation(foundation) and listVerifiedFoundations(candidates): the SEP-1 ACCOUNTS check (https only, no redirects, 5 s timeout, body capped at 100 KB).
  • parseUsdc, formatStroops, roundUpToWholeDollar: amount helpers.
  • isValidAppId, memoFor: app id validation and the memo.
  • USDC_ISSUER, USDC_CODE: the Circle USDC constants.
  • Network access (fetch, AccountPort) is injectable.