Skip to content

Dex Berachain

Git Source

Note: Link to existing contract ABI's can be found on Github here.

Interface of the dex module's precompiled contract

Functions

getPreviewSwapExact

previews a single swap into a pool.

solidity
function getPreviewSwapExact(SwapKind kind, string memory pool, Cosmos.Coin memory baseAsset, string memory quoteAsset)
    external
    view
    returns (Cosmos.Coin memory);
function getPreviewSwapExact(SwapKind kind, string memory pool, Cosmos.Coin memory baseAsset, string memory quoteAsset)
    external
    view
    returns (Cosmos.Coin memory);

Parameters

NameTypeDescription
kindSwapKindThe swap kind. (GIVEN_IN vs GIVEN_OUT)
poolstringThe address of the pool.
baseAssetCosmos.CoinThe amount of tokens that would have been sent to the pool.
quoteAssetstringThe amount of tokens that would have been received from the pool.

Returns

NameTypeDescription
<none>Cosmos.Coinout The amount of tokens that would have been received from the pool.

getPreviewBatchSwap

previews a batch swap into a series of pools.

solidity
function getPreviewBatchSwap(SwapKind kind, BatchSwapStep[] memory swaps, Cosmos.Coin[] memory coins)
    external
    view
    returns (Cosmos.Coin memory);
function getPreviewBatchSwap(SwapKind kind, BatchSwapStep[] memory swaps, Cosmos.Coin[] memory coins)
    external
    view
    returns (Cosmos.Coin memory);

Parameters

NameTypeDescription
kindSwapKindThe swap kind. (GIVEN_IN vs GIVEN_OUT)
swapsBatchSwapStep[]The swaps to perform.
coinsCosmos.Coin[]The coins to use for the swaps.

Returns

NameTypeDescription
<none>Cosmos.Coinout The amount of tokens that would have been received from the pool.

getLiquidity

previews the balance of tokens currently in the liquidity pool.

solidity
function getLiquidity(string memory pool) external view returns (Cosmos.Coin[] memory);
function getLiquidity(string memory pool) external view returns (Cosmos.Coin[] memory);

Parameters

NameTypeDescription
poolstringThe address of the pool.

Returns

NameTypeDescription
<none>Cosmos.Coin[]tokens The amount of tokens in the LP.

getTotalShares

previews the total amount of shares of the liquidity pool.

solidity
function getTotalShares(string memory pool) external view returns (Cosmos.Coin[] memory);
function getTotalShares(string memory pool) external view returns (Cosmos.Coin[] memory);

Parameters

NameTypeDescription
poolstringThe address of the pool.

Returns

NameTypeDescription
<none>Cosmos.Coin[]shares The current amount of shares of the LP.

getExchangeRate

previews the exchange rate between two assets in a pool. Note: the returned uint is represented as a value of up to 18 decimal precision

solidity
function getExchangeRate(string memory pool, string memory baseAsset, string memory quoteAsset)
    external
    view
    returns (uint256);
function getExchangeRate(string memory pool, string memory baseAsset, string memory quoteAsset)
    external
    view
    returns (uint256);

Parameters

NameTypeDescription
poolstringThe address of the pool.
baseAssetstringThe base asset to get the exchange rate for.
quoteAssetstringThe quote asset to get the exchange rate for.

Returns

NameTypeDescription
<none>uint256rate The exchange rate between the two assets.

getPreviewSharesForLiquidity

previews the amount of LP tokens that will be received for adding liquidity to a pool.

solidity
function getPreviewSharesForLiquidity(string memory pool, Cosmos.Coin[] memory coins)
    external
    view
    returns (Cosmos.Coin[] memory, Cosmos.Coin[] memory);
function getPreviewSharesForLiquidity(string memory pool, Cosmos.Coin[] memory coins)
    external
    view
    returns (Cosmos.Coin[] memory, Cosmos.Coin[] memory);

Parameters

NameTypeDescription
poolstringThe address of the pool.
coinsCosmos.Coin[]The amount of tokens to add to the pool.

Returns

NameTypeDescription
<none>Cosmos.Coin[]shares The amount of LP tokens that will be received for adding liquidity to the pool.
<none>Cosmos.Coin[]liquidity The new liquidity in the pool.

getPreviewAddLiquidityStaticPrice

previews the amount of tokens that can be added to a pool without impacting the exchange rate.

solidity
function getPreviewAddLiquidityStaticPrice(string memory pool, Cosmos.Coin[] memory liquidity)
    external
    view
    returns (Cosmos.Coin[] memory, Cosmos.Coin[] memory);
function getPreviewAddLiquidityStaticPrice(string memory pool, Cosmos.Coin[] memory liquidity)
    external
    view
    returns (Cosmos.Coin[] memory, Cosmos.Coin[] memory);

Parameters

NameTypeDescription
poolstringThe address of the pool.
liquidityCosmos.Coin[]The amount of tokens to add to the pool.

Returns

NameTypeDescription
<none>Cosmos.Coin[]shares The shares out.
<none>Cosmos.Coin[]liquidity The remaining coins that were not spent.

getPreviewSharesForSingleSidedLiquidityRequest

previews the amount of shares that will be received from adding one sided liquidity to a pool.

solidity
function getPreviewSharesForSingleSidedLiquidityRequest(string memory pool, Cosmos.Coin memory coin)
    external
    view
    returns (Cosmos.Coin[] memory);
function getPreviewSharesForSingleSidedLiquidityRequest(string memory pool, Cosmos.Coin memory coin)
    external
    view
    returns (Cosmos.Coin[] memory);

Parameters

NameTypeDescription
poolstringThe address of the pool.
coinCosmos.CoinThe token to be added into the liquidity pool.

Returns

NameTypeDescription
<none>Cosmos.Coin[]shares The amount of tokens that will be received for adding liquidity to the pool.

getPreviewAddLiquidityNoSwap

previews the amount of tokens that will be received from adding liquidity without swapping.

solidity
function getPreviewAddLiquidityNoSwap(string memory pool, Cosmos.Coin[] memory liquidity)
    external
    view
    returns (Cosmos.Coin[] memory, Cosmos.Coin[] memory);
function getPreviewAddLiquidityNoSwap(string memory pool, Cosmos.Coin[] memory liquidity)
    external
    view
    returns (Cosmos.Coin[] memory, Cosmos.Coin[] memory);

Parameters

NameTypeDescription
poolstringThe address of the pool.
liquidityCosmos.Coin[]The tokens to be added into the liquidity pool.

Returns

NameTypeDescription
<none>Cosmos.Coin[]shares The amount of LP tokens that will be received for adding liquidity to the pool.
<none>Cosmos.Coin[]liquidity The amount of liquidity added.

getPreviewBurnShares

previews the amount of tokens that will be received from burning LP tokens to remove liquidity.

solidity
function getPreviewBurnShares(string memory pool, Cosmos.Coin memory shares)
    external
    view
    returns (Cosmos.Coin[] memory);
function getPreviewBurnShares(string memory pool, Cosmos.Coin memory shares)
    external
    view
    returns (Cosmos.Coin[] memory);

Parameters

NameTypeDescription
poolstringThe address of the pool.
sharesCosmos.CoinThe amount of LP tokens to be burned.

Returns

NameTypeDescription
<none>Cosmos.Coin[]liquidity The amount of liquidity tokens to be received for burning LP shares.

getRemoveLiquidityExactAmountOut

previews the amount of LP tokens required to be removed to withdraw a specific amount of one asset from the pool.

solidity
function getRemoveLiquidityExactAmountOut(string memory pool, Cosmos.Coin memory asset)
    external
    view
    returns (Cosmos.Coin[] memory);
function getRemoveLiquidityExactAmountOut(string memory pool, Cosmos.Coin memory asset)
    external
    view
    returns (Cosmos.Coin[] memory);

Parameters

NameTypeDescription
poolstringThe address of the pool.
assetCosmos.CoinThe target amount and asset to be received from burning LP tokens.

Returns

NameTypeDescription
<none>Cosmos.Coin[]shares The amount of LP tokens required.

getRemoveLiquidityOneSideOut

previews the amount of one asset that will be received for burning LP tokens.

solidity
function getRemoveLiquidityOneSideOut(string memory pool, string memory denom, uint256 sharesIn)
    external
    view
    returns (Cosmos.Coin[] memory);
function getRemoveLiquidityOneSideOut(string memory pool, string memory denom, uint256 sharesIn)
    external
    view
    returns (Cosmos.Coin[] memory);

Parameters

NameTypeDescription
poolstringThe address of the pool.
denomstringThe target asset to be received from burning LP tokens.
sharesInuint256The amount of LP tokens to be burned.

Returns

NameTypeDescription
<none>Cosmos.Coin[]liquidity The amount of the target asset that will be received for burning the LP tokens.

getPoolName

gets the pool name for a given pool address.

solidity
function getPoolName(string memory pool) external view returns (string memory);
function getPoolName(string memory pool) external view returns (string memory);

Parameters

NameTypeDescription
poolstringThe address of the pool.

Returns

NameTypeDescription
<none>stringname The name of the pool.

getPoolOptions

gets the pool options for a given pool address.

solidity
function getPoolOptions(string memory pool) external view returns (PoolOptions memory);
function getPoolOptions(string memory pool) external view returns (PoolOptions memory);

Parameters

NameTypeDescription
poolstringThe address of the pool.

Returns

NameTypeDescription
<none>PoolOptionsoptions The options of the pool.

getPoolAddress

converts the bech32 address of a pool to the hex address.

solidity
function getPoolAddress(string memory pool) external view returns (address);
function getPoolAddress(string memory pool) external view returns (address);

Parameters

NameTypeDescription
poolstringThe bech32 address of the pool.

Returns

NameTypeDescription
<none>addresspoolAddress The hex address of the pool.

swap

Performs a swap with a single Pool. NOTE: If the limit is set as 0, there is no maximum slippage set. NOTE: The type of swap (GIVEN_IN vs GIVEN_OUT) determines if the limit is a max input, or a min output.

solidity
function swap(SingleSwap memory singleSwap, uint256 limit, uint256 deadline)
    external
    payable
    returns (Cosmos.Coin[] memory);
function swap(SingleSwap memory singleSwap, uint256 limit, uint256 deadline)
    external
    payable
    returns (Cosmos.Coin[] memory);

Parameters

NameTypeDescription
singleSwapSingleSwapthe data for the single swap.
limituint256the limit for the swap / tolerable slippage.
deadlineuint256the deadline for the swap.

Returns

NameTypeDescription
<none>Cosmos.Coin[]out The amount of tokens that were received from the pool.

batchSwap

Performs a series of swaps with one or multiple Pools. NOTE: Swaps are executed sequentially, in the order specified by the swaps array. NOTE: The amounts in and amounts out are determined by the values set in the coins argument. NOTE: Similar to single swaps, the limit is determined by the swap kind. GIVEN_IN means the limit is a min output, GIVEN_OUT means the limit is a max input. NOTE: If no limits are set, then there is no max slippage.

solidity
function batchSwap(SwapKind kind, BatchSwapStep[] memory swaps, Cosmos.Coin[] memory coins, uint256 deadline)
    external
    payable
    returns (Cosmos.Coin[] memory);
function batchSwap(SwapKind kind, BatchSwapStep[] memory swaps, Cosmos.Coin[] memory coins, uint256 deadline)
    external
    payable
    returns (Cosmos.Coin[] memory);

Parameters

NameTypeDescription
kindSwapKindThe swap kind. (GIVEN_IN vs GIVEN_OUT)
swapsBatchSwapStep[]The swaps to perform.
coinsCosmos.Coin[]The assets to use for the swaps, and the corresponding amounts.
deadlineuint256The deadline for the swaps.

Returns

NameTypeDescription
<none>Cosmos.Coin[]out The amount of tokens that were received from the pool.

createPool

Creates a new pool.

solidity
function createPool(
    string memory name,
    Cosmos.Coin[] memory coins,
    string memory poolType,
    bytes memory options,
    address creator
) external payable returns (string memory);
function createPool(
    string memory name,
    Cosmos.Coin[] memory coins,
    string memory poolType,
    bytes memory options,
    address creator
) external payable returns (string memory);

Parameters

NameTypeDescription
namestringThe name of the pool.
coinsCosmos.Coin[]The initial liquidity of the pool.
poolTypestringThe type of the pool. (Currently only balancerstyle pools are supported)
optionsbytesThe options of the pool. This contains the asset weights, and swap fee.
creatoraddressThe address of the pool creator.

Returns

NameTypeDescription
<none>stringThe address of the new pool.

addLiquidity

Adds liquidity to a pool.

solidity
function addLiquidity(string memory pool, address account, address receiver, Cosmos.Coin[] memory coins)
    external
    payable
    returns (Cosmos.Coin[] memory, Cosmos.Coin[] memory);
function addLiquidity(string memory pool, address account, address receiver, Cosmos.Coin[] memory coins)
    external
    payable
    returns (Cosmos.Coin[] memory, Cosmos.Coin[] memory);

Parameters

NameTypeDescription
poolstringThe address of the pool.
accountaddressThe address of the liquidity provider.
receiveraddressThe address of who will receive the LP tokens.
coinsCosmos.Coin[]The amount of tokens to add to the pool.

Returns

NameTypeDescription
<none>Cosmos.Coin[]The amount of LP tokens that were received for adding liquidity to the pool.
<none>Cosmos.Coin[]

removeLiquidityBurningShares

Removes liquidity from a pool by burning shares.

solidity
function removeLiquidityBurningShares(string memory pool, address account, address receiver, Cosmos.Coin memory coin)
    external
    payable
    returns (Cosmos.Coin[] memory);
function removeLiquidityBurningShares(string memory pool, address account, address receiver, Cosmos.Coin memory coin)
    external
    payable
    returns (Cosmos.Coin[] memory);

Parameters

NameTypeDescription
poolstringThe address of the pool.
accountaddressThe address of the liquidity provider.
receiveraddressThe address of who will receive the liquidity tokens.
coinCosmos.CoinThe amount of LP tokens to be burned.

Returns

NameTypeDescription
<none>Cosmos.Coin[]liquidity The amount of tokens that were received for burning the LP tokens.

removeLiquidityExactAmount

Removes a specific amount of liquidity from the pool, with a maximum number of shares to be burned.

solidity
function removeLiquidityExactAmount(
    string memory pool,
    address account,
    address receiver,
    Cosmos.Coin memory coin,
    Cosmos.Coin memory maxShares
) external payable returns (Cosmos.Coin[] memory, Cosmos.Coin[] memory);
function removeLiquidityExactAmount(
    string memory pool,
    address account,
    address receiver,
    Cosmos.Coin memory coin,
    Cosmos.Coin memory maxShares
) external payable returns (Cosmos.Coin[] memory, Cosmos.Coin[] memory);

Parameters

NameTypeDescription
poolstringThe address of the pool.
accountaddressThe address of the liquidity provider.
receiveraddressThe address of who will receive the liquidity tokens.
coinCosmos.CoinThe desired amount of tokens to be received from burning the LP tokens.
maxSharesCosmos.CoinThe maximum amount of LP tokens to be burned.

Returns

NameTypeDescription
<none>Cosmos.Coin[]liquidity The amount of tokens that were received for burning the LP tokens, and the amount of LP tokens burned.
<none>Cosmos.Coin[]

Events

PoolCreated

Emitted whenever a new pool is created.

solidity
event PoolCreated(
    string pool, string poolName, string poolShareDenom, PoolOptions poolOptions, Cosmos.Coin[] liquidity
);
event PoolCreated(
    string pool, string poolName, string poolShareDenom, PoolOptions poolOptions, Cosmos.Coin[] liquidity
);

Parameters

NameTypeDescription
poolstringThe address of the new pool.
poolNamestringThe name of the new pool.
poolShareDenomstringThe denom of the pool shares / LP token.
poolOptionsPoolOptionsThe configs of the new pool. This contains the asset weights, and swap fee.
liquidityCosmos.Coin[]The initial liquidity of the new pool.

Swap

Emitted whenever a swap is performed.

solidity
event Swap(string pool, Cosmos.Coin swapIn, Cosmos.Coin swapOut, address sender);
event Swap(string pool, Cosmos.Coin swapIn, Cosmos.Coin swapOut, address sender);

Parameters

NameTypeDescription
poolstringThe address of the pool.
swapInCosmos.CoinThe amount of tokens sent to the pool.
swapOutCosmos.CoinThe amount of tokens received from the pool.
senderaddressThe address of the swap sender.

AddLiquidity

Emitted whenever liquidity is added to a pool.

solidity
event AddLiquidity(string pool, Cosmos.Coin[] liquidityIn, Cosmos.Coin sharesOut, address sender);
event AddLiquidity(string pool, Cosmos.Coin[] liquidityIn, Cosmos.Coin sharesOut, address sender);

Parameters

NameTypeDescription
poolstringThe address of the pool.
liquidityInCosmos.Coin[]The amount of tokens sent to the pool.
sharesOutCosmos.CoinThe amount of pool shares / LP tokens received for adding liquidity.
senderaddressThe address of the liquidity provider.

RemoveLiquidity

Emitted whenever liquidity is removed from a pool.

solidity
event RemoveLiquidity(string pool, Cosmos.Coin sharesIn, Cosmos.Coin[] liquidityOut, address sender);
event RemoveLiquidity(string pool, Cosmos.Coin sharesIn, Cosmos.Coin[] liquidityOut, address sender);

Parameters

NameTypeDescription
poolstringThe address of the pool.
sharesInCosmos.CoinThe amount of pool shares / LP tokens being burnt.
liquidityOutCosmos.Coin[]The amount of tokens received for burning the shares.
senderaddressThe address of who burnt the LP tokens.

Structs

PoolOptions

The configuration options for a pool. This contains asset weights, and swap fees. NOTE: The swap fees must be one of the following options: (0.05%, 0.3%, 1%)

solidity
struct PoolOptions {
    AssetWeight[] weights;
    uint256 swapFee;
}
struct PoolOptions {
    AssetWeight[] weights;
    uint256 swapFee;
}

AssetWeight

An asset weight to be used for pool options. NOTE: The weights do not have to add up to any specific number. The weight given here is normalized against the total weight.

solidity
struct AssetWeight {
    string denom;
    uint256 weight;
}
struct AssetWeight {
    string denom;
    uint256 weight;
}

SingleSwap

Data for a single swap executed by swap. NOTE: amount is either amountIn or amountOut depending on the kind value.

solidity
struct SingleSwap {
    string poolId;
    SwapKind kind;
    string assetIn;
    string assetOut;
    uint256 amount;
    bytes userData;
}
struct SingleSwap {
    string poolId;
    SwapKind kind;
    string assetIn;
    string assetOut;
    uint256 amount;
    bytes userData;
}

Properties

NameTypeDescription
poolIdstringThe address of the pool.
kindSwapKindThe swap kind. (GIVEN_IN vs GIVEN_OUT)
assetInstringThe target input asset.
assetOutstringThe target output asset.
amountuint256The amount of tokens to be sent to the pool, or received from the pool based on kind.
userDatabytesThe user data to be passed to the pool.

BatchSwapStep

Data for a single swap executed by batchSwap. NOTE: The steps are executed sequentially, so the order in which they're passed in must logically make sense. Example: Given pools of (A,B) (B,C) (C,D), the following steps would be valid: A-B, B->C, C->D. The following steps would be invalid: A->B, C->D, B->C.

solidity
struct BatchSwapStep {
    string poolId;
    uint256 assetInIndex;
    uint256 assetOutIndex;
    bytes userData;
}
struct BatchSwapStep {
    string poolId;
    uint256 assetInIndex;
    uint256 assetOutIndex;
    bytes userData;
}

Properties

NameTypeDescription
poolIdstringThe address of the pool.
assetInIndexuint256The index of the input asset in the coins array.
assetOutIndexuint256The index of the output asset in the coins array.
userDatabytesThe user data to be passed to the pool.

Enums

SwapKind

*SwapKind is an enum which represents what type of swap it is. There are two swap kinds:

  • 'GIVEN_IN' swaps, where the amount of tokens in (sent to the Pool) is known, and the Pool determines the amount of tokens out.
  • 'GIVEN_OUT' swaps, where the amount of tokens out (received from the Pool) is known, and the Pool determines the amount of tokens in.*
solidity
enum SwapKind {
    GIVEN_IN,
    GIVEN_OUT
}
enum SwapKind {
    GIVEN_IN,
    GIVEN_OUT
}