@halliday-sdk/commerce
v3.2.0
Published
## Headless SDK
Downloads
2,220
Keywords
Readme
Halliday Commerce
Headless SDK
The Halliday headless SDK allows you to build commerce into your application without rendering any Halliday UI elements. You can import the headless client like so:
import { HallidaySwapClient } from '@halliday-sdk/commerce'
Usage
First, create a swap client using your apiKey
and initialize it:
const client = await HallidaySwapClient.create({ apiKey })
If you want to test using testnets, you can construct a sandbox client:
const client = await HallidaySwapClient.create({ apiKey, useSandbox: true })
To proceed, you will have to authenticate to the client by having the user sign a message:
await client.login(signer)
Next, you can create a swap by providing a source of funds sourceAddress
and recipient destinationAddress
:
const swap = await client.createSwap({ sourceAddress, destinationAddress })
Now you can fetch a quote for a given inputToken
, outputToken
, and inputAmount
:
const quote = await swap.getQuoteExactInput({ inputToken, outputToken, inputAmount })
At this point, if the user would like to accept the quote, simply call:
await quote.accept()
Alternatively, you may ask for another quote using the same method as above.
You should then make sure the user approves or transfers in any necessary funds to perform the swap:
const approvalReqs = quote.approvalNeeded()
const transferReqs = quote.transferInNeeded()
The approvalReqs
will describe the amount and token needing approval, if necessary.
The transferReqs
will describe the amount and token needing to be transferred in, if necessary.
Finally, you may wait for the swap to complete. In the event that the quote can no longer be obtained without slippage, new adjusted quotes will be provided that you may accept or reject:
for await (const newQuote of quote.acceptAndWait()) {
if (await user.ask(newQuote))
newQuote.accept()
else
newQuote.reject()
}
Recovery
If a session is interrupted by closing the browser window, you may resume by getting the active swaps and recovering:
const swaps = await client.getActiveSwaps()
...
const swap = await client.recover({ swapId })
await swap.execute()
await swap.wait()