Dex Berachain
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.
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
Name | Type | Description |
---|---|---|
kind | SwapKind | The swap kind. (GIVEN_IN vs GIVEN_OUT) |
pool | string | The address of the pool. |
baseAsset | Cosmos.Coin | The amount of tokens that would have been sent to the pool. |
quoteAsset | string | The amount of tokens that would have been received from the pool. |
Returns
Name | Type | Description |
---|---|---|
<none> | Cosmos.Coin | out The amount of tokens that would have been received from the pool. |
getPreviewBatchSwap
previews a batch swap into a series of pools.
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
Name | Type | Description |
---|---|---|
kind | SwapKind | The swap kind. (GIVEN_IN vs GIVEN_OUT) |
swaps | BatchSwapStep[] | The swaps to perform. |
coins | Cosmos.Coin[] | The coins to use for the swaps. |
Returns
Name | Type | Description |
---|---|---|
<none> | Cosmos.Coin | out The amount of tokens that would have been received from the pool. |
getLiquidity
previews the balance of tokens currently in the liquidity pool.
function getLiquidity(string memory pool) external view returns (Cosmos.Coin[] memory);
function getLiquidity(string memory pool) external view returns (Cosmos.Coin[] memory);
Parameters
Name | Type | Description |
---|---|---|
pool | string | The address of the pool. |
Returns
Name | Type | Description |
---|---|---|
<none> | Cosmos.Coin[] | tokens The amount of tokens in the LP. |
getTotalShares
previews the total amount of shares of the liquidity pool.
function getTotalShares(string memory pool) external view returns (Cosmos.Coin[] memory);
function getTotalShares(string memory pool) external view returns (Cosmos.Coin[] memory);
Parameters
Name | Type | Description |
---|---|---|
pool | string | The address of the pool. |
Returns
Name | Type | Description |
---|---|---|
<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
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
Name | Type | Description |
---|---|---|
pool | string | The address of the pool. |
baseAsset | string | The base asset to get the exchange rate for. |
quoteAsset | string | The quote asset to get the exchange rate for. |
Returns
Name | Type | Description |
---|---|---|
<none> | uint256 | rate The exchange rate between the two assets. |
getPreviewSharesForLiquidity
previews the amount of LP tokens that will be received for adding liquidity to a pool.
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
Name | Type | Description |
---|---|---|
pool | string | The address of the pool. |
coins | Cosmos.Coin[] | The amount of tokens to add to the pool. |
Returns
Name | Type | Description |
---|---|---|
<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.
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
Name | Type | Description |
---|---|---|
pool | string | The address of the pool. |
liquidity | Cosmos.Coin[] | The amount of tokens to add to the pool. |
Returns
Name | Type | Description |
---|---|---|
<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.
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
Name | Type | Description |
---|---|---|
pool | string | The address of the pool. |
coin | Cosmos.Coin | The token to be added into the liquidity pool. |
Returns
Name | Type | Description |
---|---|---|
<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.
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
Name | Type | Description |
---|---|---|
pool | string | The address of the pool. |
liquidity | Cosmos.Coin[] | The tokens to be added into the liquidity pool. |
Returns
Name | Type | Description |
---|---|---|
<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.
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
Name | Type | Description |
---|---|---|
pool | string | The address of the pool. |
shares | Cosmos.Coin | The amount of LP tokens to be burned. |
Returns
Name | Type | Description |
---|---|---|
<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.
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
Name | Type | Description |
---|---|---|
pool | string | The address of the pool. |
asset | Cosmos.Coin | The target amount and asset to be received from burning LP tokens. |
Returns
Name | Type | Description |
---|---|---|
<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.
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
Name | Type | Description |
---|---|---|
pool | string | The address of the pool. |
denom | string | The target asset to be received from burning LP tokens. |
sharesIn | uint256 | The amount of LP tokens to be burned. |
Returns
Name | Type | Description |
---|---|---|
<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.
function getPoolName(string memory pool) external view returns (string memory);
function getPoolName(string memory pool) external view returns (string memory);
Parameters
Name | Type | Description |
---|---|---|
pool | string | The address of the pool. |
Returns
Name | Type | Description |
---|---|---|
<none> | string | name The name of the pool. |
getPoolOptions
gets the pool options for a given pool address.
function getPoolOptions(string memory pool) external view returns (PoolOptions memory);
function getPoolOptions(string memory pool) external view returns (PoolOptions memory);
Parameters
Name | Type | Description |
---|---|---|
pool | string | The address of the pool. |
Returns
Name | Type | Description |
---|---|---|
<none> | PoolOptions | options The options of the pool. |
getPoolAddress
converts the bech32 address of a pool to the hex address.
function getPoolAddress(string memory pool) external view returns (address);
function getPoolAddress(string memory pool) external view returns (address);
Parameters
Name | Type | Description |
---|---|---|
pool | string | The bech32 address of the pool. |
Returns
Name | Type | Description |
---|---|---|
<none> | address | poolAddress 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.
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
Name | Type | Description |
---|---|---|
singleSwap | SingleSwap | the data for the single swap. |
limit | uint256 | the limit for the swap / tolerable slippage. |
deadline | uint256 | the deadline for the swap. |
Returns
Name | Type | Description |
---|---|---|
<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.
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
Name | Type | Description |
---|---|---|
kind | SwapKind | The swap kind. (GIVEN_IN vs GIVEN_OUT) |
swaps | BatchSwapStep[] | The swaps to perform. |
coins | Cosmos.Coin[] | The assets to use for the swaps, and the corresponding amounts. |
deadline | uint256 | The deadline for the swaps. |
Returns
Name | Type | Description |
---|---|---|
<none> | Cosmos.Coin[] | out The amount of tokens that were received from the pool. |
createPool
Creates a new pool.
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
Name | Type | Description |
---|---|---|
name | string | The name of the pool. |
coins | Cosmos.Coin[] | The initial liquidity of the pool. |
poolType | string | The type of the pool. (Currently only balancer style pools are supported) |
options | bytes | The options of the pool. This contains the asset weights, and swap fee. |
creator | address | The address of the pool creator. |
Returns
Name | Type | Description |
---|---|---|
<none> | string | The address of the new pool. |
addLiquidity
Adds liquidity to a pool.
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
Name | Type | Description |
---|---|---|
pool | string | The address of the pool. |
account | address | The address of the liquidity provider. |
receiver | address | The address of who will receive the LP tokens. |
coins | Cosmos.Coin[] | The amount of tokens to add to the pool. |
Returns
Name | Type | Description |
---|---|---|
<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.
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
Name | Type | Description |
---|---|---|
pool | string | The address of the pool. |
account | address | The address of the liquidity provider. |
receiver | address | The address of who will receive the liquidity tokens. |
coin | Cosmos.Coin | The amount of LP tokens to be burned. |
Returns
Name | Type | Description |
---|---|---|
<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.
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
Name | Type | Description |
---|---|---|
pool | string | The address of the pool. |
account | address | The address of the liquidity provider. |
receiver | address | The address of who will receive the liquidity tokens. |
coin | Cosmos.Coin | The desired amount of tokens to be received from burning the LP tokens. |
maxShares | Cosmos.Coin | The maximum amount of LP tokens to be burned. |
Returns
Name | Type | Description |
---|---|---|
<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.
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
Name | Type | Description |
---|---|---|
pool | string | The address of the new pool. |
poolName | string | The name of the new pool. |
poolShareDenom | string | The denom of the pool shares / LP token. |
poolOptions | PoolOptions | The configs of the new pool. This contains the asset weights, and swap fee. |
liquidity | Cosmos.Coin[] | The initial liquidity of the new pool. |
Swap
Emitted whenever a swap is performed.
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
Name | Type | Description |
---|---|---|
pool | string | The address of the pool. |
swapIn | Cosmos.Coin | The amount of tokens sent to the pool. |
swapOut | Cosmos.Coin | The amount of tokens received from the pool. |
sender | address | The address of the swap sender. |
AddLiquidity
Emitted whenever liquidity is added to a pool.
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
Name | Type | Description |
---|---|---|
pool | string | The address of the pool. |
liquidityIn | Cosmos.Coin[] | The amount of tokens sent to the pool. |
sharesOut | Cosmos.Coin | The amount of pool shares / LP tokens received for adding liquidity. |
sender | address | The address of the liquidity provider. |
RemoveLiquidity
Emitted whenever liquidity is removed from a pool.
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
Name | Type | Description |
---|---|---|
pool | string | The address of the pool. |
sharesIn | Cosmos.Coin | The amount of pool shares / LP tokens being burnt. |
liquidityOut | Cosmos.Coin[] | The amount of tokens received for burning the shares. |
sender | address | The 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%)
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.
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.
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
Name | Type | Description |
---|---|---|
poolId | string | The address of the pool. |
kind | SwapKind | The swap kind. (GIVEN_IN vs GIVEN_OUT) |
assetIn | string | The target input asset. |
assetOut | string | The target output asset. |
amount | uint256 | The amount of tokens to be sent to the pool, or received from the pool based on kind . |
userData | bytes | The 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.
struct BatchSwapStep {
string poolId;
uint256 assetInIndex;
uint256 assetOutIndex;
bytes userData;
}
struct BatchSwapStep {
string poolId;
uint256 assetInIndex;
uint256 assetOutIndex;
bytes userData;
}
Properties
Name | Type | Description |
---|---|---|
poolId | string | The address of the pool. |
assetInIndex | uint256 | The index of the input asset in the coins array. |
assetOutIndex | uint256 | The index of the output asset in the coins array. |
userData | bytes | The 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.*
enum SwapKind {
GIVEN_IN,
GIVEN_OUT
}
enum SwapKind {
GIVEN_IN,
GIVEN_OUT
}