Create a market
Create a market for two mints, fund its pool with the first amounts that set its first price, and open it for trading.
A market is a pair of two tokens with its own vaults, pool and book. Anyone can create one for a pair that has none. The creator supplies the pool's first liquidity from their wallet, and the two first amounts set the market's first price.
The two mints
A market takes two different mints. Which mints are supported, and which mint extensions are refused, is in Supported mints. A mint outside them is refused with UnsupportedMint.
You decide which mint is the base and which is the quote. Sizes are counted in the base token. Prices, limits, volumes and fees are counted in the quote token, per unit of base.
The market's address is derived from the two mints in that order:
market = PDA( "market", base mint, quote mint )
vault = PDA( "vault", market, mint ) one per mintSo a pair has one address per choice of base and quote, and anyone can compute it before the market exists. All seeds are in the program reference.
import { marketAddress } from '@hadal/sdk'
const market = marketAddress(baseMint, quoteMint)The first amounts and the first price
You fund the pool with an amount of each token, both above zero, from your wallet's token accounts. These two amounts are public: they move in a plain token transfer, and the program stores the price they set.
first price = quote amount / base amountIn the program's unit that is priceOf(quoteAmount, baseAmount): see Units. It must lie inside the first price range. Amounts that set a price outside it are refused with PriceOutOfRange. The first price is the market's price on the surface until its first sounding. Everything the pool does after it opens is sealed.
What you receive
The first liquidity makes you the pool's first liquidity provider. Your sealed balance in the new market is credited with
shares = 2 × quote amount, in atomsThey are shares like any other: they earn the sweep fee and are removed the same way. See Provide liquidity.
Create the market
Creating a market takes two transactions and one computation.
| Transaction | Instructions | What it does |
|---|---|---|
1. Create | create_market, open_balance | Creates the market's account and its two vaults, records you as the creator, and opens your sealed balance in the market, sealed to your reading key. |
2. Fund and open | fund_market, open_pool | Moves the first amounts from your wallet to the vaults and sets the first price, then queues the computation that builds the sealed pool and credits your shares. |
The creator recorded by the first transaction sends the second. A market left without funds can be claimed: see A market left unopened.
Open the dialog
On Pools, press Create market at the top of the page. Connect a wallet and press Unlock if the dialog asks for it: your shares are sealed to your reading key.
Enter the mints
Paste the Base token mint and the Quote token mint. The dialog flags an entry that is not a mint address and two mints that are the same. The row Supported names the mints a market takes.
Enter the first amounts
Enter the First base amount and the First quote amount. The row First price, public shows the price they set. The first liquidity comes from your wallet.
Press Create market
Press Create market and sign the two transactions. The four stages run: sealed in your wallet, signed, computed by the cluster, done. The dialog then shows Market created with the first price, and the market appears in the table on Pools.
The market becoming ready
Between the two transactions and until the computation lands, the market exists and is not ready: its account and vaults are there, and every order, deal and liquidity instruction on it is refused with MarketNotReady.
When the computation lands, the market is ready:
- its sealed book holds the pool with your two amounts as reserves and no resting orders,
- its public price is the first price,
- your sealed balance holds the shares, and its receipt has
oktrue with the shares inswept, - the interval to its first sounding starts.
From then on anyone can open a sealed balance in it, deposit, trade and add liquidity. In the app the market is listed on Pools as created, and its figures follow with its first sounding. See Soundings.
Retry an opening that expired
If the cluster does not finish the computation, awaitComputation throws at its timeout, the market stays not ready and your amounts stay in its vaults as its first liquidity. Once the release window has passed, free the market and your balance, then open the pool again with openPool. It sends open_pool alone and moves no tokens. In the app, a market you created whose pool did not open is listed on Pools with Open pool in its row: press it and sign, and the app confirms with Pool open.
If the second transaction itself never landed, the market holds no first liquidity yet. Its row on Pools reads Created. Its first liquidity has not landed yet. with Fund and open pool, which sends the second transaction again: with the SDK, send created.open again.
const released = await hadal.releaseMarket(created.market)
const freed = await hadal.reclaim({ market: created.market })
await sendWithKeypairs(connection, [...released.instructions, ...freed.instructions], [signer])
const retry = await hadal.openPool(created.market)
await sendWithKeypairs(connection, retry.instructions, [signer])
await hadal.awaitComputation(retry.computation!)Sending fund_market again adds its amounts to those already in the vaults, and the first price is set from the totals. The release window is in Limits, and the lifecycle of a computation in Arcium.
A second attempt for the same pair
A market's address is fixed by its base and quote mints, so one market exists per ordered pair.
| Situation | Refused with | What to do |
|---|---|---|
The market exists | create_market fails | The first transaction creates the market's account and cannot create it twice. Use the market that is there. |
It is ready | MarketAlreadyOpen | fund_market and open_pool run once per market. To supply its pool, add liquidity. |
It is not ready and another wallet created it | NotOwner | fund_market and open_pool accept the creator only. A market that still holds no funds once the claim window has passed can be claimed by funding it. |
It is not ready and you created it | | The second transaction failed or was never sent. Send it again and skip the first: the market and your balance exist. |
A market left unopened
A market that still holds no first liquidity once the claim window has passed since its creation can be claimed. From then on fund_market accepts any wallet, and the wallet that funds the market becomes its creator. open_pool is always the creator's. No pair stays held without a pool.
To claim, open your sealed balance in the market, then fund and open the pool. createMarket builds the same second transaction for an existing market:
const balance = await hadal.openBalance(market, key)
await sendWithKeypairs(connection, balance.instructions, [signer])
const claimed = await hadal.createMarket({ baseMint, quoteMint, baseAmount, quoteAmount, key })
await sendWithKeypairs(connection, claimed.open.instructions, [signer])
await hadal.awaitComputation(claimed.open.computation!)A market that has been funded stays its creator's, whatever time has passed.
Check before creating
import { marketAddress } from '@hadal/sdk'
const address = marketAddress(baseMint, quoteMint)
const exists = (await connection.getAccountInfo(address)) !== null
const ready = exists && (await hadal.market(address)).readyErrors
| Error | Code | Raised when |
|---|---|---|
Busy | 6001 | The market's book, the balance or the deal the instruction needs already has a computation in flight. Also raised by process_order, by cancel_order on a pending order and by fund_market while the market has one. Send again once that computation lands, or release it after it expires. |
NotOwner | 6006 | The signer does not own what the instruction acts on: collect or cancel_order by a wallet other than the order's owner (before expiry, or on a pending order), cancel_deal of an open deal by a wallet other than the maker, open_pool by a wallet other than the market's creator, fund_market by a wallet other than the creator unless the market holds no funds and its claim window has passed. |
UnsupportedMint | 6009 | create_market was given a Token-2022 mint with one of the refused extensions: transfer fee, transfer hook, permanent delegate, default account state or pausable. |
SameMint | 6010 | create_market was given the same mint as base and quote. |
InvalidAmount | 6011 | An amount of 0 was passed to deposit, withdraw or fund_market. |
MarketAlreadyOpen | 6016 | fund_market or open_pool was sent for a market whose pool is already open. |
NotFunded | 6017 | open_pool was sent before fund_market put both tokens in the vaults. |
PriceOutOfRange | 6018 | fund_market would set a first price of 0 or of 2^32 quote atoms per base atom or more. |
createMarket in the SDK also throws Mint not found when one of the two mints is not an account on chain.