import type { Account, BaseErrorType } from 'viem' import { Actions } from 'viem/tempo' import { getConnectorClient } from '../../actions/getConnectorClient.js' import type { Config } from '../../createConfig.js' import type { ChainIdParameter, ConnectorParameter, } from '../../types/properties.js' import type { UnionLooseOmit } from '../../types/utils.js' import type { QueryOptions, QueryParameter } from './utils.js' import { filterQueryOptions } from './utils.js' /** * Gets the reserves for a liquidity pool. * * @example * ```ts * import { createConfig, http } from '@wagmi/core' * import { tempo } from '@wagmi/core/chains' * import { Actions } from '@wagmi/core/tempo' * * const config = createConfig({ * chains: [tempoModerato], * transports: { * [tempo.id]: http(), * }, * }) * * const pool = await Actions.amm.getPool(config, { * userToken: '0x...', * validatorToken: '0x...', * }) * ``` * * @param config - Config. * @param parameters - Parameters. * @returns The pool reserves. */ export function getPool( config: config, parameters: getPool.Parameters, ): Promise { const { chainId, ...rest } = parameters const client = config.getClient({ chainId }) return Actions.amm.getPool(client, rest) } export namespace getPool { export type Parameters = ChainIdParameter & Actions.amm.getPool.Parameters export type ReturnValue = Actions.amm.getPool.ReturnValue export type ErrorType = BaseErrorType export function queryKey( parameters: Parameters, ) { return ['getPool', filterQueryOptions(parameters)] as const } export type QueryKey = ReturnType< typeof queryKey > export function queryOptions( config: Config, parameters: queryOptions.Parameters, ): queryOptions.ReturnValue { const { query, ...rest } = parameters return { ...query, enabled: Boolean( rest.userToken && rest.validatorToken && (query?.enabled ?? true), ), queryKey: queryKey(rest), async queryFn({ queryKey }) { const [, parameters] = queryKey return await getPool(config, parameters) }, } } export declare namespace queryOptions { export type Parameters< config extends Config, selectData = getPool.ReturnValue, > = getPool.Parameters & QueryParameter< getPool.ReturnValue, getPool.ErrorType, selectData, getPool.QueryKey > export type ReturnValue< config extends Config, selectData = getPool.ReturnValue, > = QueryOptions< getPool.ReturnValue, getPool.ErrorType, selectData, getPool.QueryKey > } } /** * Gets the LP token balance for an account in a specific pool. * * @example * ```ts * import { createConfig, http } from '@wagmi/core' * import { tempo } from '@wagmi/core/chains' * import { Actions } from '@wagmi/core/tempo' * * const config = createConfig({ * chains: [tempoModerato], * transports: { * [tempo.id]: http(), * }, * }) * * const poolId = await Actions.amm.getPoolId(config, { * userToken: '0x...', * validatorToken: '0x...', * }) * * const balance = await Actions.amm.getLiquidityBalance(config, { * poolId, * address: '0x...', * }) * ``` * * @param config - Config. * @param parameters - Parameters. * @returns The LP token balance. */ export function getLiquidityBalance( config: config, parameters: getLiquidityBalance.Parameters, ): Promise { const { chainId, ...rest } = parameters const client = config.getClient({ chainId }) return Actions.amm.getLiquidityBalance(client, rest) } export namespace getLiquidityBalance { export type Parameters = ChainIdParameter & Actions.amm.getLiquidityBalance.Parameters export type ReturnValue = Actions.amm.getLiquidityBalance.ReturnValue export type ErrorType = BaseErrorType export function queryKey( parameters: Parameters, ) { return ['getLiquidityBalance', filterQueryOptions(parameters)] as const } export type QueryKey = ReturnType< typeof queryKey > export function queryOptions( config: Config, parameters: queryOptions.Parameters, ): queryOptions.ReturnValue { const { query, ...rest } = parameters return { ...query, enabled: Boolean( rest.address && (rest.poolId || (rest.userToken !== undefined && rest.validatorToken !== undefined)) && (query?.enabled ?? true), ), queryKey: queryKey(rest), async queryFn({ queryKey }) { const [, parameters] = queryKey return await getLiquidityBalance(config, parameters as any) }, } } export declare namespace queryOptions { export type Parameters< config extends Config, selectData = getLiquidityBalance.ReturnValue, > = getLiquidityBalance.Parameters & QueryParameter< getLiquidityBalance.ReturnValue, getLiquidityBalance.ErrorType, selectData, getLiquidityBalance.QueryKey > export type ReturnValue< config extends Config, selectData = getLiquidityBalance.ReturnValue, > = QueryOptions< getLiquidityBalance.ReturnValue, getLiquidityBalance.ErrorType, selectData, getLiquidityBalance.QueryKey > } } /** * Performs a rebalance swap from validator token to user token. * * @example * ```ts * import { createConfig, http } from '@wagmi/core' * import { tempo } from '@wagmi/core/chains' * import { Actions } from '@wagmi/core/tempo' * * const config = createConfig({ * chains: [tempoModerato], * transports: { * [tempo.id]: http(), * }, * }) * * const hash = await Actions.amm.rebalanceSwap(config, { * userToken: '0x...', * validatorToken: '0x...', * amountOut: 100n, * to: '0x...', * }) * ``` * * @param config - Config. * @param parameters - Parameters. * @returns The transaction hash. */ export async function rebalanceSwap( config: config, parameters: rebalanceSwap.Parameters, ): Promise { const { account, chainId, connector } = parameters const client = await getConnectorClient(config, { account, assertChainId: false, chainId, connector, }) return Actions.amm.rebalanceSwap(client, parameters as never) } export declare namespace rebalanceSwap { export type Parameters = ChainIdParameter & ConnectorParameter & UnionLooseOmit< Actions.amm.rebalanceSwap.Parameters, 'chain' > export type ReturnValue = Actions.amm.rebalanceSwap.ReturnValue export type ErrorType = BaseErrorType } /** * Performs a rebalance swap from validator token to user token. * * @example * ```ts * import { createConfig, http } from '@wagmi/core' * import { tempo } from '@wagmi/core/chains' * import { Actions } from '@wagmi/core/tempo' * * const config = createConfig({ * chains: [tempoModerato], * transports: { * [tempo.id]: http(), * }, * }) * * const result = await Actions.amm.rebalanceSwapSync(config, { * userToken: '0x...', * validatorToken: '0x...', * amountOut: 100n, * to: '0x...', * }) * ``` * * @param config - Config. * @param parameters - Parameters. * @returns The transaction receipt and event data. */ export async function rebalanceSwapSync( config: config, parameters: rebalanceSwapSync.Parameters, ): Promise { const { account, chainId, connector } = parameters const client = await getConnectorClient(config, { account, assertChainId: false, chainId, connector, }) return Actions.amm.rebalanceSwapSync(client, parameters as never) } export declare namespace rebalanceSwapSync { export type Parameters = ChainIdParameter & ConnectorParameter & UnionLooseOmit< Actions.amm.rebalanceSwapSync.Parameters< config['chains'][number], Account >, 'chain' > export type ReturnValue = Actions.amm.rebalanceSwapSync.ReturnValue export type ErrorType = BaseErrorType } /** * Adds liquidity to a pool. * * @example * ```ts * import { createConfig, http } from '@wagmi/core' * import { tempo } from '@wagmi/core/chains' * import { Actions } from '@wagmi/core/tempo' * * const config = createConfig({ * chains: [tempoModerato], * transports: { * [tempo.id]: http(), * }, * }) * * const hash = await Actions.amm.mint(config, { * userTokenAddress: '0x20c0...beef', * validatorTokenAddress: '0x20c0...babe', * validatorTokenAmount: 100n, * to: '0xfeed...fede', * }) * ``` * * @param config - Config. * @param parameters - Parameters. * @returns The transaction hash. */ export async function mint( config: config, parameters: mint.Parameters, ): Promise { const { account, chainId, connector } = parameters const client = await getConnectorClient(config, { account, assertChainId: false, chainId, connector, }) return Actions.amm.mint(client, parameters as never) } export declare namespace mint { export type Parameters = ChainIdParameter & ConnectorParameter & UnionLooseOmit< Actions.amm.mint.Parameters, 'chain' > export type ReturnValue = Actions.amm.mint.ReturnValue export type ErrorType = BaseErrorType } /** * Adds liquidity to a pool. * * @example * ```ts * import { createConfig, http } from '@wagmi/core' * import { tempo } from '@wagmi/core/chains' * import { Actions } from '@wagmi/core/tempo' * * const config = createConfig({ * chains: [tempoModerato], * transports: { * [tempo.id]: http(), * }, * }) * * const result = await Actions.amm.mintSync(config, { * userTokenAddress: '0x20c0...beef', * validatorTokenAddress: '0x20c0...babe', * validatorTokenAmount: 100n, * to: '0xfeed...fede', * }) * ``` * * @param config - Config. * @param parameters - Parameters. * @returns The transaction receipt and event data. */ export async function mintSync( config: config, parameters: mintSync.Parameters, ): Promise { const { account, chainId, connector } = parameters const client = await getConnectorClient(config, { account, assertChainId: false, chainId, connector, }) return Actions.amm.mintSync(client, parameters as never) } export declare namespace mintSync { export type Parameters = ChainIdParameter & ConnectorParameter & UnionLooseOmit< Actions.amm.mintSync.Parameters, 'chain' > export type ReturnValue = Actions.amm.mintSync.ReturnValue export type ErrorType = BaseErrorType } /** * Removes liquidity from a pool. * * @example * ```ts * import { createConfig, http } from '@wagmi/core' * import { tempo } from '@wagmi/core/chains' * import { Actions } from '@wagmi/core/tempo' * * const config = createConfig({ * chains: [tempoModerato], * transports: { * [tempo.id]: http(), * }, * }) * * const hash = await Actions.amm.burn(config, { * userToken: '0x20c0...beef', * validatorToken: '0x20c0...babe', * liquidity: 50n, * to: '0xfeed...fede', * }) * ``` * * @param config - Config. * @param parameters - Parameters. * @returns The transaction hash. */ export async function burn( config: config, parameters: burn.Parameters, ): Promise { const { account, chainId, connector } = parameters const client = await getConnectorClient(config, { account, assertChainId: false, chainId, connector, }) return Actions.amm.burn(client, parameters as never) } export declare namespace burn { export type Parameters = ChainIdParameter & ConnectorParameter & UnionLooseOmit< Actions.amm.burn.Parameters, 'chain' > export type ReturnValue = Actions.amm.burn.ReturnValue export type ErrorType = BaseErrorType } /** * Removes liquidity from a pool. * * @example * ```ts * import { createConfig, http } from '@wagmi/core' * import { tempo } from '@wagmi/core/chains' * import { Actions } from '@wagmi/core/tempo' * * const config = createConfig({ * chains: [tempoModerato], * transports: { * [tempo.id]: http(), * }, * }) * * const result = await Actions.amm.burnSync(config, { * userToken: '0x20c0...beef', * validatorToken: '0x20c0...babe', * liquidity: 50n, * to: '0xfeed...fede', * }) * ``` * * @param config - Config. * @param parameters - Parameters. * @returns The transaction receipt and event data. */ export async function burnSync( config: config, parameters: burnSync.Parameters, ): Promise { const { account, chainId, connector } = parameters const client = await getConnectorClient(config, { account, assertChainId: false, chainId, connector, }) return Actions.amm.burnSync(client, parameters as never) } export declare namespace burnSync { export type Parameters = ChainIdParameter & ConnectorParameter & UnionLooseOmit< Actions.amm.burnSync.Parameters, 'chain' > export type ReturnValue = Actions.amm.burnSync.ReturnValue export type ErrorType = BaseErrorType } /** * Watches for rebalance swap events. * * @example * ```ts * import { createConfig, http } from '@wagmi/core' * import { tempo } from '@wagmi/core/chains' * import { Actions } from '@wagmi/core/tempo' * * const config = createConfig({ * chains: [tempoModerato], * transports: { * [tempo.id]: http(), * }, * }) * * const unwatch = Actions.amm.watchRebalanceSwap(config, { * onRebalanceSwap: (args, log) => { * console.log('Rebalance swap:', args) * }, * }) * ``` * * @param config - Config. * @param parameters - Parameters. * @returns A function to unsubscribe from the event. */ export function watchRebalanceSwap( config: config, parameters: watchRebalanceSwap.Parameters, ) { const { chainId, ...rest } = parameters const client = config.getClient({ chainId }) return Actions.amm.watchRebalanceSwap(client, rest) } export declare namespace watchRebalanceSwap { export type Parameters = ChainIdParameter & Actions.amm.watchRebalanceSwap.Parameters } /** * Watches for liquidity mint events. * * @example * ```ts * import { createConfig, http } from '@wagmi/core' * import { tempo } from '@wagmi/core/chains' * import { Actions } from '@wagmi/core/tempo' * * const config = createConfig({ * chains: [tempoModerato], * transports: { * [tempo.id]: http(), * }, * }) * * const unwatch = Actions.amm.watchMint(config, { * onMint: (args, log) => { * console.log('Liquidity added:', args) * }, * }) * ``` * * @param config - Config. * @param parameters - Parameters. * @returns A function to unsubscribe from the event. */ export function watchMint( config: config, parameters: watchMint.Parameters, ) { const { chainId, ...rest } = parameters const client = config.getClient({ chainId }) return Actions.amm.watchMint(client, rest) } export declare namespace watchMint { export type Parameters = ChainIdParameter & Actions.amm.watchMint.Parameters } /** * Watches for liquidity burn events. * * @example * ```ts * import { createConfig, http } from '@wagmi/core' * import { tempo } from '@wagmi/core/chains' * import { Actions } from '@wagmi/core/tempo' * * const config = createConfig({ * chains: [tempoModerato], * transports: { * [tempo.id]: http(), * }, * }) * * const unwatch = Actions.amm.watchBurn(config, { * onBurn: (args, log) => { * console.log('Liquidity removed:', args) * }, * }) * ``` * * @param config - Config. * @param parameters - Parameters. * @returns A function to unsubscribe from the event. */ export function watchBurn( config: config, parameters: watchBurn.Parameters, ) { const { chainId, ...rest } = parameters const client = config.getClient({ chainId }) return Actions.amm.watchBurn(client, rest) } export declare namespace watchBurn { export type Parameters = ChainIdParameter & Actions.amm.watchBurn.Parameters }