The Treasures swap, on your site
Paste two lines and your users can buy and sell tokenized stocks from their own wallets, without leaving your page. Every trade is signed in the user’s wallet; neither you nor Treasures ever holds their funds or keys. No sign-up needed to embed it.
Add it
Put this where the swap box should appear:
<script src="https://swap.treasures.io/widget/v1.js" async></script>
<treasures-swap integrator="intg_…" stock="AAPL" theme="light"></treasures-swap>The box is up to 520 px wide and fills narrower containers. It grows to fit its content, from a minimum of 510 px tall, which already fits a quote, so the page below it moves only when a notice or the quote’s details appear. To keep your layout still, set height and the box scrolls inside instead.
Options
integrator- Your integrator ID (
intg_…), from us. It credits trades to you, and charges your widget fee if you set one. Leave it out and the widget still works, credited to no one. stock- A ticker to open on, such as
AAPL. sidebuy(default) orsell.themelightordark. Left out, it follows the user’s system.chains- Trade only on some chains, comma-separated:
sol,eth,base,arbitrum,robinhood,bnb. connect- The only wallets your users can connect:
evm(MetaMask, Rabby, …) orsolana(Phantom, Solflare, …). The widget then never offers the other kind or the stocks only it can buy. Left out, users can connect both. wallethost: use the wallets your page already connected, so your users connect once. See “Use your page’s wallet” below. Left out, the widget connects its own.amount- An amount to start with: dollars on a buy, shares on a sell, such as
100. One under 0.00001 is ignored. stocks- Offer only these stocks, comma-separated, such as
AAPL,TSLA,NVDA. Astockoutside the list opens on its first. lock- What the user can’t change, comma-separated:
stock(no stock picker),side(no Buy/Sell switch),amount(the amount is fixed). Each needs its option set (stock,amount);sidealone holds it on buy. A lockedamountholds the side too, since it is dollars on a buy and shares on a sell. A locked stock that can’t be traded right now says so, instead of offering another. palette- Your colours, corners and font, as JSON. See “Design” below.
height- A fixed height in pixels, such as
700: the box stays that size and scrolls inside when its content is taller. At least 510. Left out, the box grows to fit its content.
An unknown value is ignored, never an error.
<!-- A "Buy Tesla" box: $100 to start, stock and side fixed. -->
<treasures-swap stock="TSLA" side="buy" amount="100" lock="stock,side"></treasures-swap>Changing options
Change an attribute and the widget follows. stock, side, amount, stocks, lock, theme and palette change in place, keeping what the user typed (a new side clears the amount, as dollars become shares). A change to the trade (stock, side, amount, stocks, lock) made while the user is reviewing it waits until they close Review; theme and palette apply at once. Setting the same values again does nothing, so re-rendering with React or Vue is safe. integrator, wallet, chains and connect reload the widget.
// Your page's own stock list drives the widget, keeping what the user typed:
widget.setAttribute('stock', 'NVDA');Design
Match the widget to your site with palette: a JSON object in the attribute, or an object passed to setPalette() (null takes it away). Give only what you want to change; everything else keeps our design. Keys at the top apply in both themes; keys under light or dark apply in that theme only.
<treasures-swap
theme="light"
palette='{"accent":"#6d28d9","background":"transparent","radius":12,"font":"inter"}'>
</treasures-swap>
<script>
// The same from script, here with a colour per theme (for a widget following the
// user's system): shared keys first, then the shown theme's own.
document.querySelector('treasures-swap').setPalette({
accent: '#6d28d9',
radiusControl: 8,
light: { surface: '#ffffff', text: '#111111' },
dark: { surface: '#17151f', text: '#f4f2ff' },
});
</script>Colours are #rgb, #rrggbb (with or without alpha) or rgb()/rgba(). Hover shades, tints and the text on a coloured button are worked out for you unless you give them.
background- Behind the box. `transparent` shows your page.
surface- The box and popups.
surfaceInset- Amount fields, list hovers.
surfaceControl- Small buttons, the search box.
surfaceChip- Pills and tags.
border- Hairlines around the box, fields and pills.
divider- Lines between rows.
text- Main text.
textMuted- Labels and secondary text.
textFaint- Footnotes, placeholders, timestamps.
accent- The main button, selection and focus rings. Its tints are worked out from it.
accentHover- Main button, hovered. Default: from `accent`.
accentActive- Main button, pressed. Default: from `accent`.
onAccent- Text on the main button. Default: black or white, whichever reads better.
up- Price up, buy.
down- Price down, sell.
onTrade- Text on an `up`/`down` fill. Default: black or white, whichever reads better.
info- Tips. Background and border are worked out from it.
warning- Warnings, likewise.
danger- Errors, likewise.
scrim- Behind a popup. Give it some transparency.
radius- The box’s corners in pixels, 0 to 32. Ours: 16.
radiusControl- Buttons’ and fields’ corners in pixels, 0 to 32; smaller parts scale with it. Ours: 12.
font- One of
inter,manrope,dm-sans,ibm-plex-sans,system. We host these, so your users’ browsers fetch nothing from anyone else. shadownonefor a flat box, ordefault.
- A value that isn’t valid is ignored and named in your browser’s console; the rest applies. So is any colour pair too faint to read (under WCAG AA’s 4.5:1), though it is still applied: the colours are yours.
- If
upanddownare too alike to tell apart, both keep ours. - Some things stay whatever the palette: the “Powered by Treasures” line, every fee line, the Terms, and errors and notices, at their full size.
- The wallet-connect window follows your accent, background, font and corners.
Events
The element re-dispatches what happens in the widget as DOM events named treasures:<type>, with the details in event.detail. They say what happened, never who: no event carries a wallet address, balance or transaction.
ready- { integrator_recognized }: the widget has loaded. null while your ID is still unchecked.
integrator- { recognized }: whether your ID is a registered integrator’s, after the first quote.
resize- { height }: the widget’s content height. The frame follows it unless you set a height (handled for you).
connected- { ecosystems }: which wallets are connected, "solana" and/or "evm".
connect_requested- { ecosystem }: with wallet="host", the user clicked Connect for a wallet your page owns but hasn’t passed in. Open your own connect.
popup- { open }: a dialog opened (true), or the last open one closed (false); the widget scrolls into view on open.
trade_submitted- { trade_id, side, ticker, amount_usd }: the user signed a trade and it went out. trade_id is a random ID for this trade, repeated in the event that ends it.
trade_filled- { trade_id, side, ticker }: the trade filled. Sent even if the user closed the review before the fill.
trade_failed- { trade_id, code }: the trade failed; code is the API’s error code. trade_id is null when the failure came before a quote.
bridge- { pending }: a cross-chain top-up is in flight (also element.bridgePending).
const widget = document.querySelector('treasures-swap');
widget.addEventListener('treasures:trade_filled', (event) => {
console.log('Filled', event.detail.trade_id, event.detail.side, event.detail.ticker);
});
// Warn before leaving while a bridge is in flight:
window.addEventListener('beforeunload', (event) => {
if (widget.bridgePending) event.preventDefault();
});Use your page’s wallet
If your site already connects wallets, set wallet="host" and pass them in with setWallet. The widget then signs with them: your users connect once, on your page, and their wallet asks them to sign from your site.
<treasures-swap integrator="intg_…" wallet="host"></treasures-swap>
<script>
const widget = document.querySelector('treasures-swap');
// Your EVM wallet: any EIP-1193 provider (window.ethereum, or wagmi's
// `await connector.getProvider()`). Pass null while none is connected.
widget.setWallet({ evm: provider });
// Your Solana wallet, e.g. from @solana/wallet-adapter-react's useWallet():
widget.setWallet({
solana: {
address: publicKey.toBase58(),
signMessage: (bytes) => signMessage(bytes),
signTransaction: async (bytes) =>
(await signTransaction(VersionedTransaction.deserialize(bytes))).serialize(),
},
});
// The user clicked Connect in the widget: open your own connect.
widget.addEventListener('treasures:connect_requested', (event) => {
openConnectModal(event.detail.ecosystem); // 'evm', 'solana' or null (either)
});
</script>- Pass
evm,solanaor both. A kind you pass, even asnull, is yours: the widget connects it only through you. A kind you don’t pass keeps the widget’s own Connect. - The widget asks your wallet only to sign: a sign-in (once an hour, no gas), trade orders, a one-time token approval, and switching chain (your wallet’s chain follows the trade’s). It never asks your wallet to send a trade. Every other request is refused, in the widget and in the loader.
- The widget checks every answer before using it: a signature over anything other than what it asked is refused, and nothing is sent.
- When your user disconnects or switches account, the widget follows: tell it with
setWallet, or let your EVM provider’saccountsChangedandchainChangedevents do it.
Strict Content Security Policy
If your site sends a CSP, allow the loader and the widget’s frame:
frame-src https://swap.treasures.io;
script-src https://swap.treasures.io;Your page’s address
The widget shows on pages served over https, and on http://localhost or http://127.0.0.1 while you test. On any other plain-http page it stays blank. Passing in your page’s own wallet needs a public https address even in testing: use a tunnel such as cloudflared or ngrok.
Fees
The widget trades at the same price as swap.treasures.io. A registered integrator may add its own fee, up to 1%, which we set with you (never in the page, so a copied snippet can’t raise it). It is shown to the user as its own line, with your name, before they sign. To register, get an integrator ID or set a fee, write to hello@treasures.io.
Trades credited to your ID appear in your API’s /public/v1/settlements, with the trade’s wallet and transaction, as API trades do; ?channel=widget lists the widget’s alone. That includes trades from any site that pastes your ID.
What your users see
The Treasures swap box, under Treasures’ Terms, which the user accepts in the widget before their first trade. They connect their own wallet in the widget (or on your page, with wallet="host"), and their wallet shows every amount before they sign. The widget isn’t available in every country.