Trading
Quote, review and execute buys and sells of tokenized stocks.
Buy
const quote = await account.prepareTradeQuote({
inputAsset: { name: 'credits', amount: { unit: 'human', value: '25' } },
outputAsset: { name: 'AAPL' },
slippageTolerance: 0.005, // 0.5%, the default
})
quote.inputAsset // { name, address, amount: { units: { human, raw } } }
quote.outputAsset // what you would receive, same shape
quote.orderId // the pending order
const result = await quote.execute()
result.transactionHash
result.inputAmount // bigint, raw units
result.outputAmount // bigint, the quoted amount
result.purchase // { id }
Parameters
| Field | Type | Notes |
|---|---|---|
inputAsset.name |
string |
'credits' or a token symbol. |
inputAsset.amount |
{ unit: 'human' or 'raw', value: string or bigint } |
human is a decimal string. |
outputAsset.name |
string |
The asset to receive. Must be a listed token. |
slippageTolerance |
number |
Fraction, 0.005 is 0.5%. Defaults to 0.005. |
What a quote contains
| Field | Meaning |
|---|---|
inputAsset, outputAsset |
Name, contract address and amount (human and raw). |
priceImpact |
How far your trade moves the pool price. Show this to the user. |
swapRoute |
The hops used: from, to, stable, factory. A buy of a stock from credits is two hops. |
slippageTolerance |
The tolerance you asked for. |
What execute() does
- Encodes an
approveand aswapExactTokensForTokenscall and batches them into one user operation. - Checks that the quote has an order to submit, so nobody is asked to sign a trade that cannot be sent. Then the signer signs. For a passkey this is one prompt.
- Submits it to the order endpoint and returns the transaction hash.
The two calls land together or not at all.
Resume a quote
A quote can be rebuilt from its id, for example after a reload:
import { TradeQuoteClient } from '@dattex/sdk'
const quote = await TradeQuoteClient.fromOrderId({
sdk, orderId, accountId: account.id, quoteData, chainId: 8453,
})
Also available: fromPurchaseId, fromSaleId and fromSellOrderId. Each throws if the order is already completed, or is the wrong kind (a sell id passed to a buy builder).
Slippage is enforced on-chain. execute() sends the swap with a minimum output of outputAsset × (1 − slippageTolerance), rounded to whole basis points. If the pool moves further than that before the transaction lands, the whole batch reverts and nothing is spent. slippageTolerance must be a number from 0 up to, but not including, 1; anything else throws before the passkey prompt.
outputAmount is the quoted amount. The result returns what the quote promised, not what the chain delivered. Read the account's balance if you need the final figure.
Prices
Quotes use pool prices. For a reference price, use the generated client: sdk.getInternalClient().stockPriceChecker.getStockPrice(…).