Skip to main content

Connecting Wallets

Every trade in the widget is signed by the user's own wallet. By default the widget connects that wallet itself. If your site already connects wallets, the widget can use yours instead, so your users connect once.

The widget's own connect​

With no wallet options set, the widget has its own Connect button and wallet picker, as on swap.treasures.io:

  • A user can connect up to one EVM wallet (MetaMask, Rabby and others, for Ethereum, Base, Arbitrum, Robinhood Chain and BNB Smart Chain) and one Solana wallet (Phantom, Solflare and others) at the same time.
  • After connecting, the user signs a short sign-in message. It costs no gas and is reused for an hour, so they are not asked again on every quote. See Ownership Proof for what it proves.
  • The wallet picker follows your palette's accent, background, font and corners.
  • The user disconnects a wallet from the widget's wallet settings menu.

The connected event tells your page which wallet types are connected, without saying which wallet.

Limiting the wallet type​

Two attributes narrow what the widget offers:

  • connect="evm" or connect="solana": the only wallet type your users can connect. The widget never offers the other kind, or the stocks only the other kind can buy.
  • chains: trade only on some chains, for example chains="base,arbitrum". A wallet type the listed chains leave no chain for is not offered.
<!-- EVM wallets only, trading on Base and Arbitrum. -->
<treasures-swap connect="evm" chains="base,arbitrum"></treasures-swap>

Use your page's wallet​

Set wallet="host" and pass your page's wallets 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>

setWallet({ evm, solana })​

KeyValue
evmAn EIP-1193 provider, or null while none is connected
solana{ address, signMessage(bytes), signTransaction(bytes) }, or null while none is connected. address is the base58 public key. Both methods take a Uint8Array and resolve to one: the signature for signMessage, the signed serialized transaction for signTransaction
  • A key you pass is yours. Pass evm, solana or both. A wallet type you pass, even as null, is connected only through you: the widget's Connect for it fires treasures:connect_requested instead of opening its own picker, and the widget's settings menu does not offer to disconnect it.
  • A key you leave out keeps the widget's own connect. Pass only evm, and users can still connect a Solana wallet in the widget.
  • Call it again whenever your wallet changes. A setWallet call changes only the keys it names. When your user connects, disconnects or switches account, call setWallet with the new value. An EVM provider's own accountsChanged, chainChanged and disconnect events are followed for you.
  • You can call setWallet before or after the element is on the page.

What the widget asks your wallet​

The widget asks your wallet only to sign. The element forwards exactly these requests and refuses every other one (EIP-1193 error code 4200), in the widget and again in the loader:

WalletRequestUsed for
EVMpersonal_signThe sign-in, once an hour, no gas
EVMeth_signTypedData_v4Signing trade orders
EVMeth_sendTransactionA one-time token approval, and nothing else
EVMwallet_switchEthereumChain, wallet_addEthereumChainMoving your wallet to the trade's chain
SolanasignMessageThe sign-in, once an hour
SolanasignTransactionSigning trades (never sign-and-send)
  • It never asks your wallet to send a trade. Treasures relays the signed order.
  • It checks every answer before using it: a signature over anything other than what it asked for is refused, and nothing is sent.
  • The sign-in message names your site, not swap.treasures.io, so wallets that check the requesting site show no warning.