Skip to content

ERC20 Dex Berachain

Git Source

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

Interface of the erc20 dex module's precompiled contract

Functions

getPreviewSwapExact

previews a single swap into a pool.

solidity
function getPreviewSwapExact(
    SwapKind kind,
    address pool,
    address baseAsset,
    uint256 baseAssetAmount,
    address quoteAsset
) external view returns (address asset, uint256 amount);
function getPreviewSwapExact(
    SwapKind kind,
    address pool,
    address baseAsset,
    uint256 baseAssetAmount,
    address quoteAsset
) external view returns (address asset, uint256 amount);

Parameters

NameTypeDescription
kindSwapKindThe swap kind. (GIVEN_IN vs GIVEN_OUT)
pooladdressThe address of the pool.
baseAssetaddressThe base asset.
baseAssetAmountuint256The amount of base asset.
quoteAssetaddressThe quote asset.

Returns

NameTypeDescription
assetaddressThe token to be received from the pool.
amountuint256The amount of tokens to be received from the pool.

getPreviewBatchSwap

previews a batch swap.

solidity
function getPreviewBatchSwap(SwapKind kind, BatchSwapStep[] memory swaps)
    external
    view
    returns (address asset, uint256 amount);
function getPreviewBatchSwap(SwapKind kind, BatchSwapStep[] memory swaps)
    external
    view
    returns (address asset, uint256 amount);

Parameters

NameTypeDescription
kindSwapKindThe swap kind. (GIVEN_IN vs GIVEN_OUT)
swapsBatchSwapStep[]The swaps to be executed.

Returns

NameTypeDescription
assetaddressThe token to be received from the pool.
amountuint256The amount of tokens to be received from the pool.

getLiquidity

previews the balance of tokens currently in the liquidity pool.

solidity
function getLiquidity(address pool) external view returns (address[] memory asset, uint256[] memory amounts);
function getLiquidity(address pool) external view returns (address[] memory asset, uint256[] memory amounts);

Parameters

NameTypeDescription
pooladdressThe address of the pool.

Returns

NameTypeDescription
assetaddress[]The tokens in the pool.
amountsuint256[]The amount of tokens in the pool.

getTotalShares

previews the total amount of shares of the liquidity pool.

solidity
function getTotalShares(address pool) external view returns (address[] memory assets, uint256[] memory amounts);
function getTotalShares(address pool) external view returns (address[] memory assets, uint256[] memory amounts);

Parameters

NameTypeDescription
pooladdressThe address of the pool.

Returns

NameTypeDescription
assetsaddress[]The share tokens / LP tokens of the pool.
amountsuint256[]The amount share tokens / LP tokens of the pool.

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(address pool, address baseAsset, address quoteAsset) external view returns (uint256);
function getExchangeRate(address pool, address baseAsset, address quoteAsset) external view returns (uint256);

Parameters

NameTypeDescription
pooladdressThe address of the pool.
baseAssetaddressThe base asset to get the exchange rate for.
quoteAssetaddressThe 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(address pool, address[] memory assets, uint256[] memory amounts)
    external
    view
    returns (
        address[] memory shares,
        uint256[] memory shareAmounts,
        address[] memory liquidity,
        uint256[] memory liquidityAmounts
    );
function getPreviewSharesForLiquidity(address pool, address[] memory assets, uint256[] memory amounts)
    external
    view
    returns (
        address[] memory shares,
        uint256[] memory shareAmounts,
        address[] memory liquidity,
        uint256[] memory liquidityAmounts
    );

Parameters

NameTypeDescription
pooladdressThe address of the pool.
assetsaddress[]The assets to add to the pool.
amountsuint256[]The amounts of assets to add to the pool.

Returns

NameTypeDescription
sharesaddress[]The LP tokens that will be received for adding liquidity to the pool.
shareAmountsuint256[]The amount of LP tokens that will be received for adding liquidity to the pool.
liquidityaddress[]The liquidity in the pool.
liquidityAmountsuint256[]The amount of 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(address pool, address[] memory liquidity, uint256[] memory amounts)
    external
    view
    returns (
        address[] memory shares,
        uint256[] memory shareAmounts,
        address[] memory liqOut,
        uint256[] memory liquidityAmounts
    );
function getPreviewAddLiquidityStaticPrice(address pool, address[] memory liquidity, uint256[] memory amounts)
    external
    view
    returns (
        address[] memory shares,
        uint256[] memory shareAmounts,
        address[] memory liqOut,
        uint256[] memory liquidityAmounts
    );

Parameters

NameTypeDescription
pooladdressThe address of the pool.
liquidityaddress[]The tokens to add to the pool.
amountsuint256[]The amounts of tokens to add to the pool.

Returns

NameTypeDescription
sharesaddress[]The LP tokens that will be received for adding liquidity to the pool.
shareAmountsuint256[]The amount of LP tokens that will be received for adding liquidity to the pool.
liqOutaddress[]The pool's asset tokens.
liquidityAmountsuint256[]The amount of liquidity assets not used.

getPreviewSharesForSingleSidedLiquidityRequest

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

solidity
function getPreviewSharesForSingleSidedLiquidityRequest(address pool, address asset, uint256 amount)
    external
    view
    returns (address[] memory assets, uint256[] memory amounts);
function getPreviewSharesForSingleSidedLiquidityRequest(address pool, address asset, uint256 amount)
    external
    view
    returns (address[] memory assets, uint256[] memory amounts);

Parameters

NameTypeDescription
pooladdressThe address of the pool.
assetaddressThe token to be added into the liquidity pool.
amountuint256The amount of token to be added into the liquidity pool.

Returns

NameTypeDescription
assetsaddress[]The share/LP token to be received
amountsuint256[]The amount of LP tokens to be received.

getPreviewAddLiquidityNoSwap

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

solidity
function getPreviewAddLiquidityNoSwap(address pool, address[] memory assets, uint256[] memory amounts)
    external
    view
    returns (
        address[] memory shares,
        uint256[] memory shareAmounts,
        address[] memory liqOut,
        uint256[] memory liquidityAmounts
    );
function getPreviewAddLiquidityNoSwap(address pool, address[] memory assets, uint256[] memory amounts)
    external
    view
    returns (
        address[] memory shares,
        uint256[] memory shareAmounts,
        address[] memory liqOut,
        uint256[] memory liquidityAmounts
    );

Parameters

NameTypeDescription
pooladdressThe address of the pool.
assetsaddress[]The tokens to be added into the liquidity pool.
amountsuint256[]The amounts of tokens to be added into the liquidity pool.

Returns

NameTypeDescription
sharesaddress[]The LP tokens that will be received for adding liquidity to the pool.
shareAmountsuint256[]The amount of LP tokens that will be received for adding liquidity to the pool.
liqOutaddress[]The pool's asset tokens.
liquidityAmountsuint256[]The amount of liquidity assets added.

getPreviewBurnShares

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

solidity
function getPreviewBurnShares(address pool, address asset, uint256 amount)
    external
    view
    returns (address[] memory assets, uint256[] memory amounts);
function getPreviewBurnShares(address pool, address asset, uint256 amount)
    external
    view
    returns (address[] memory assets, uint256[] memory amounts);

Parameters

NameTypeDescription
pooladdressThe address of the pool.
assetaddressThe LP token to be burned.
amountuint256The amount of LP tokens to be burned.

Returns

NameTypeDescription
assetsaddress[]The tokens to be received for burning shares/LP tokens.
amountsuint256[]The amount of tokens to be received for burning shares/LP tokens.

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(address pool, address assetIn, uint256 assetAmount)
    external
    view
    returns (address[] memory assets, uint256[] memory amounts);
function getRemoveLiquidityExactAmountOut(address pool, address assetIn, uint256 assetAmount)
    external
    view
    returns (address[] memory assets, uint256[] memory amounts);

Parameters

NameTypeDescription
pooladdressThe address of the pool.
assetInaddressThe target asset to be received from burning LP tokens.
assetAmountuint256The amount of target asset to be received from burning LP tokens.

Returns

NameTypeDescription
assetsaddress[]The asset received for burning the LP tokens.
amountsuint256[]The amount of asset received for burning the LP tokens.

getRemoveLiquidityOneSideOut

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

solidity
function getRemoveLiquidityOneSideOut(address pool, address assetOut, uint256 sharesIn)
    external
    view
    returns (address[] memory assets, uint256[] memory amounts);
function getRemoveLiquidityOneSideOut(address pool, address assetOut, uint256 sharesIn)
    external
    view
    returns (address[] memory assets, uint256[] memory amounts);

Parameters

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

Returns

NameTypeDescription
assetsaddress[]The asset received for burning the LP tokens.
amountsuint256[]The amount of target asset received for burning the LP tokens.

getPoolName

gets the pool name for a given pool address.

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

Parameters

NameTypeDescription
pooladdressThe 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(address pool) external view returns (PoolOptions memory);
function getPoolOptions(address pool) external view returns (PoolOptions memory);

Parameters

NameTypeDescription
pooladdressThe address of the pool.

Returns

NameTypeDescription
<none>PoolOptionsoptions The options 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(
    SwapKind kind,
    address poolId,
    address assetIn,
    uint256 amountIn,
    address assetOut,
    uint256 amountOut,
    uint256 deadline
) external payable returns (address[] memory assets, uint256[] memory amounts);
function swap(
    SwapKind kind,
    address poolId,
    address assetIn,
    uint256 amountIn,
    address assetOut,
    uint256 amountOut,
    uint256 deadline
) external payable returns (address[] memory assets, uint256[] memory amounts);

Parameters

NameTypeDescription
kindSwapKindThe type of swap.
poolIdaddressThe address of the pool.
assetInaddressThe asset to be sent to the pool.
amountInuint256The amount of asset in to be sent to the pool.
assetOutaddressThe asset to be received from the pool.
amountOutuint256The amount of asset out to be received from the pool.
deadlineuint256The deadline for the swap.

Returns

NameTypeDescription
assetsaddress[]The asset received from the swap.
amountsuint256[]The amount of asset received from the swap.

batchSwap

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 batchSwap(SwapKind kind, BatchSwapStep[] memory swaps, uint256 deadline)
    external
    payable
    returns (address[] memory assets, uint256[] memory amounts);
function batchSwap(SwapKind kind, BatchSwapStep[] memory swaps, uint256 deadline)
    external
    payable
    returns (address[] memory assets, uint256[] memory amounts);

Parameters

NameTypeDescription
kindSwapKindThe type of swap.
swapsBatchSwapStep[]The swap steps to be executed.
deadlineuint256The deadline for the swap.

Returns

NameTypeDescription
assetsaddress[]The asset received from the swap.
amountsuint256[]The amount of asset received from the swap.

createPool

Creates a new pool.

solidity
function createPool(
    string memory name,
    address[] memory assetsIn,
    uint256[] memory amountsIn,
    string memory poolType,
    PoolOptions memory options
) external payable returns (address);
function createPool(
    string memory name,
    address[] memory assetsIn,
    uint256[] memory amountsIn,
    string memory poolType,
    PoolOptions memory options
) external payable returns (address);

Parameters

NameTypeDescription
namestringThe name of the pool.
assetsInaddress[]The assets to be added to the pool.
amountsInuint256[]The amounts of assets to be added to the pool.
poolTypestringThe type of pool to be created. (Currently only balancerstyle pools are supported)
optionsPoolOptionsThe options for the pool.

Returns

NameTypeDescription
<none>addresspool The address of the newly created pool.

addLiquidity

Adds liquidity to a pool.

solidity
function addLiquidity(address pool, address receiver, address[] memory assetsIn, uint256[] memory amountsIn)
    external
    payable
    returns (
        address[] memory shares,
        uint256[] memory shareAmounts,
        address[] memory liquidity,
        uint256[] memory liquidityAmounts
    );
function addLiquidity(address pool, address receiver, address[] memory assetsIn, uint256[] memory amountsIn)
    external
    payable
    returns (
        address[] memory shares,
        uint256[] memory shareAmounts,
        address[] memory liquidity,
        uint256[] memory liquidityAmounts
    );

Parameters

NameTypeDescription
pooladdressThe address of the pool.
receiveraddressThe address to receive the LP tokens.
assetsInaddress[]The assets to be added to the pool.
amountsInuint256[]The amounts of assets to be added to the pool.

Returns

NameTypeDescription
sharesaddress[]The LP tokens that were received for adding liquidity to the pool.
shareAmountsuint256[]The amount of LP tokens that were received for adding liquidity to the pool.
liquidityaddress[]The liquidity in the pool.
liquidityAmountsuint256[]The amount of liquidity in the pool.

removeLiquidityBurningShares

Removes liquidity from a pool by burning shares.

solidity
function removeLiquidityBurningShares(address pool, address withdrawAddress, address assetIn, uint256 amountIn)
    external
    payable
    returns (address[] memory liquidity, uint256[] memory liquidityAmounts);
function removeLiquidityBurningShares(address pool, address withdrawAddress, address assetIn, uint256 amountIn)
    external
    payable
    returns (address[] memory liquidity, uint256[] memory liquidityAmounts);

Parameters

NameTypeDescription
pooladdressThe address of the pool.
withdrawAddressaddressThe address to receive the assets from burning the LP shares.
assetInaddressThe LP token to be burned.
amountInuint256The amount of LP tokens to be burned.

Returns

NameTypeDescription
liquidityaddress[]The tokens received from burning the LP tokens.
liquidityAmountsuint256[]The amount of tokens received from 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(
    address pool,
    address withdrawAddress,
    address assetOut,
    uint256 amountOut,
    address sharesIn,
    uint256 maxSharesIn
)
    external
    payable
    returns (
        address[] memory shares,
        uint256[] memory shareAmounts,
        address[] memory liquidity,
        uint256[] memory liquidityAmounts
    );
function removeLiquidityExactAmount(
    address pool,
    address withdrawAddress,
    address assetOut,
    uint256 amountOut,
    address sharesIn,
    uint256 maxSharesIn
)
    external
    payable
    returns (
        address[] memory shares,
        uint256[] memory shareAmounts,
        address[] memory liquidity,
        uint256[] memory liquidityAmounts
    );

Parameters

NameTypeDescription
pooladdressThe address of the pool.
withdrawAddressaddressThe address to receive the assets from burning the LP shares.
assetOutaddressThe target asset to be received from burning the LP shares.
amountOutuint256The target amount of asset to be received from burning the LP shares.
sharesInaddressThe LP token to be burned
maxSharesInuint256The maximum amount (limit) of LP tokens to be burned.

Returns

NameTypeDescription
sharesaddress[]The LP tokens that were burned.
shareAmountsuint256[]The amount of LP tokens that were burned.
liquidityaddress[]The tokens received from burning the LP tokens.
liquidityAmountsuint256[]The amount of tokens received from burning the LP tokens.

Structs

BatchSwapStep

A single swap step to be executed in a batch swap. 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 {
    address poolId;
    address assetIn;
    uint256 amountIn;
    address assetOut;
    uint256 amountOut;
    bytes userData;
}
struct BatchSwapStep {
    address poolId;
    address assetIn;
    uint256 amountIn;
    address assetOut;
    uint256 amountOut;
    bytes userData;
}

Properties

NameTypeDescription
poolIdaddressThe address of the pool.
assetInaddressThe input asset of the swap.
amountInuint256The amount of the input asset.
assetOutaddressThe output asset of the swap.
amountOutuint256The amount of the output asset.
userDatabytesThe user data to be passed to the pool.

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 {
    address asset;
    uint256 weight;
}
struct AssetWeight {
    address asset;
    uint256 weight;
}

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
}