Buy DEX Tokens
dex.buy
Buys an exact amount of tokens from the DEX orderbook.
Usage
import { } from './viem.config'
const { } = await ..({
: 100000000n,
: 150000000n,
: '0x20c0000000000000000000000000000000000000',
: '0x20c0000000000000000000000000000000000001',
})
.('Transaction hash:', .)
Transaction hash: 0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdefimport { , } from 'viem/tempo'
export const = .({
: .('0x...'),
})Recipes
Derive the Slippage Bound from a Fresh Quote
Combine dex.getBuyQuote with dex.buySync to derive maxAmountIn from the live price instead of guessing a limit.
import { } from './viem.config'
const = '0x20c0000000000000000000000000000000000001'
const = '0x20c0000000000000000000000000000000000000'
const = 250_000000n
const = await ..({
: ,
: ,
: ,
})
const { } = await ..({
: ,
: ( * 1_005n) / 1_000n,
: ,
: ,
})import { , } from 'viem/tempo'
export const = .({
: .('0x...'),
})The bound above tolerates 0.5% of price movement between the quote and the swap. The buy reverts instead of overpaying if the price moves further.
Pay an Invoice in a Stablecoin You Don't Hold
Batch the buy with dex.withdraw and token.transfer to convert, withdraw, and pay an exact invoice in one atomic transaction.
import { } from 'viem/tempo'
import { } from './viem.config'
const = '0x20c0000000000000000000000000000000000001'
const = '0x20c0000000000000000000000000000000000000'
const = 250_000000n
const = await ..({
: [
...({
: ,
: 251_000000n,
: ,
: ,
}),
...({ : , : }),
...({
: ,
: '0x742d35Cc6634C0532925a3b844Bc9e7595f0bEbb',
: ,
}),
],
})import { , } from 'viem/tempo'
export const = .({
: .('0x...'),
})Return Value
type ReturnType = {
/** Transaction receipt. */
receipt: TransactionReceipt
}The transaction receipt for the buy.
Parameters
amountOut
- Type:
bigint
Amount of tokenOut to buy.
maxAmountIn
- Type:
bigint
Maximum amount of tokenIn to spend.
tokenIn
- Type:
Address | bigint
Token to spend: a TIP-20 token id or a contract address.
tokenOut
- Type:
Address | bigint
Token to buy: a TIP-20 token id or a contract address.
account (optional)
- Type:
Account | Address
Account that will be used to send the transaction.
feePayer (optional)
- Type:
Account | boolean
Fee payer for the transaction (TIP-1 gas sponsorship).
Pass true to defer the fee token to an external fee payer (e.g. a relay), or a local Account to co-sign the transaction as the fee payer.
feeToken (optional)
- Type:
Address | bigint
Fee token for the transaction.
Can be an unpaused USD-denominated TIP-20 token address or ID.
gas (optional)
- Type:
bigint
Gas limit for the transaction.
keyAuthorization (optional)
- Type:
KeyAuthorization
Signed key authorization to include with the transaction, authorizing an access key to act for the sending account.
maxFeePerGas (optional)
- Type:
bigint
Max fee per gas for the transaction.
maxPriorityFeePerGas (optional)
- Type:
bigint
Max priority fee per gas for the transaction.
nonce (optional)
- Type:
number
Nonce for the transaction.
nonceKey (optional)
- Type:
'expiring' | 'random' | bigint
Nonce key for the transaction (TIP-1009 2D nonces).
Use 'expiring' to select an expiring nonce, which enables concurrent transaction submission without nonce ordering. Use 'random' to select a random key.
throwOnReceiptRevert (optional)
- Type:
boolean - Default:
true
Whether a Sync action throws when the receipt reports a revert.
validAfter (optional)
- Type:
number
Unix timestamp after which the transaction can be included.
validBefore (optional)
- Type:
number
Unix timestamp before which the transaction must be included.