The liquidity module serves Automated Market Maker (AMM)-style decentralized liquidity by providing liquidity activities and coin swap functions.
The module enables users to create a liquidity pool, make deposits and withdrawals, and request coin swaps from the liquidity pool.
This module can be used in the Cosmos Hub and any other Cosmos SDK-based blockchain projects.
- The Cosmos Hub AMM applies a strong philosophy of inclusiveness for users from different blockchains with its prime utility of inter-blockchain communication.
- To achieve heterogeneous blockchain adoption, the liquidity module provides convenient entry points for external users to come in and use the services that are provided by the Cosmos Hub.
- The liquidity module does not anticipate specific assets, such as ATOM, into the user workflow. Data shows that unnatural anticipation of native coin at unavoidable parts of the process results in poor user attraction.
Combination of traditional orderbook-based model and new AMM model
- With multiple advantages over order book-based models, the liquidity module combines a batch-based order book matching algorithm with AMM to create enriched utilities for more potential users.
- The liquidity module redefines the concept of a “swap order” in AMM as a “limit order with a short lifetime” in an order book-based exchange. By combining these concepts from two different models as one united model, the function supports both ways to participate in trading and liquidity-providing activities.
- Limit order options are not supported in the first version of the liquidity module, but the base structure of the codebase anticipates and supports feature expansion.
- Advantages of the combined model
- More freedom on ways to provide liquidity, planned expansion for limit orders
- The combination of pool liquidity and limit order liquidity provide users with a more enriched trading environment
For details, see the Liquidity Module Light Paper.
Requirement | Notes |
---|---|
Go version | Go1.18 or higher |
Cosmos SDK | v0.46.x or higher |
$ git clone https://github.com/tendermint/liquidity.git
$ cd liquidity
$ go mod tidy
# The `liquidityd` binary is in the build directory.
$ make build
$ make install
With the exception of creating the liquidity pool, all commands are implemented to execute on the batch.
$ liquidityd tx liquidity --help
Liquidity transaction subcommands
Usage:
liquidityd tx liquidity [flags]
liquidityd tx liquidity [command]
Available Commands:
create-pool Create liquidity pool and deposit coins
deposit Deposit coins to a liquidity pool
swap Swap offer coin with demand coin
withdraw Withdraw pool coin
$ liquidityd query liquidity --help
Querying commands for the liquidity module
Usage:
liquidityd query liquidity [flags]
liquidityd query liquidity [command]
Available Commands:
batch Query details of a liquidity pool batch
deposit Query the deposit messages on the liquidity pool batch
deposits Query all deposit messages of the liquidity pool batch
params Query the values set as liquidity parameters
pool Query details of a liquidity pool
pools Query for all liquidity pools
swap Query for the swap message on the batch of the liquidity pool specified pool-id and msg-index
swaps Query all swap messages in the liquidity pool batch
withdraw Query the withdraw messages in the liquidity pool batch
withdraws Query for all withdraw messages on the liquidity pool batch
A detailed document on client can be found here. client.md
$ make test-all
# This script bootstraps a single local testnet.
# Note that config, data, and keys are created in the ./data/localnet folder and
# RPC, GRPC, and REST ports are all open.
$ make localnet
Sample scripts are provided in scripts folder to help you to test the liquidity module interface.
# Build
make install
# Set Binary name of the app
# The basic simapp binary of the liquidity module is liquidityd, but set it differently depending on the situation such as gaiad.
BINARY=liquidityd
# Initialize and add keys
$BINARY init testing --chain-id testing
$BINARY keys add validator --keyring-backend test
$BINARY keys add user1 --keyring-backend test
# Add genesis accounts and provide coins to the accounts
$BINARY add-genesis-account $($BINARY keys show validator --keyring-backend test -a) 10000000000stake,10000000000uatom,500000000000uusd
$BINARY add-genesis-account $($BINARY keys show user1 --keyring-backend test -a) 10000000000stake,10000000000uatom,500000000000uusd
# Create gentx and collect
$BINARY gentx validator 1000000000stake --chain-id testing --keyring-backend test
$BINARY collect-gentxs
# Start
$BINARY start
# An example of creating liquidity pool 1
$BINARY tx liquidity create-pool 1 1000000000uatom,50000000000uusd --from user1 --gas 300000 --keyring-backend test --chain-id testing -b block -o json -y
# An example of creating liquidity pool 2
$BINARY tx liquidity create-pool 1 10000000stake,10000000uusd --from validator --gas 300000 --keyring-backend test --chain-id testing -b block -o json -y
# An example of requesting swap
$BINARY tx liquidity swap 1 1 50000000uusd uatom 0.019 0.003 --from validator --gas 300000 --chain-id testing --keyring-backend test -b block -o json -y
# An example of generating unsigned tx
validator=$($BINARY keys show validator --keyring-backend test -a)
$BINARY tx liquidity swap 1 1 50000000uusd uatom 0.019 0.003 --from $validator --gas 300000 --chain-id testing --generate-only &> tx_swap.json
cat tx_swap.json
# Sign the unsigned tx
$BINARY tx sign tx_swap.json --from validator --chain-id testing --keyring-backend test -y &> tx_swap_signed.json
cat tx_swap_signed.json
# Encode the signed tx
$BINARY tx encode tx_swap_signed.json
tx_bytes=$($BINARY tx encode tx_swap_signed.json)
For an example of broadcasting transactions using REST API (via gRPC-gateway), see Cosmos SDK Migrating to New REST Endpoints. Testing requires that the API server is enabled in $HOME/.liquidityapp/config/app.toml
.
curl --header "Content-Type: application/json" --request POST --data '{"tx_bytes":"'"$tx_bytes"'","mode":1}' localhost:1317/cosmos/tx/v1beta1/txs
$ $BINARY export
{
"liquidity": {
"params": {
"circuit_breaker_enabled": false,
"init_pool_coin_mint_amount": "1000000",
"max_order_amount_ratio": "0.100000000000000000",
"max_reserve_coin_amount": "0",
"min_init_deposit_amount": "1000000",
"pool_creation_fee": [
{
"amount": "40000000",
"denom": "stake"
}
],
"pool_types": [
{
"description": "Standard liquidity pool with pool price function X/Y, ESPM constraint, and two kinds of reserve coins",
"id": 1,
"max_reserve_coin_num": 2,
"min_reserve_coin_num": 2,
"name": "StandardLiquidityPool"
}
],
"swap_fee_rate": "0.003000000000000000",
"unit_batch_height": 1,
"withdraw_fee_rate": "0.000000000000000000"
},
"pool_records": []
}
}
{
"liquidity": {
"params": {
"circuit_breaker_enabled": false,
"init_pool_coin_mint_amount": "1000000",
"max_order_amount_ratio": "0.100000000000000000",
"max_reserve_coin_amount": "0",
"min_init_deposit_amount": "1000000",
"pool_creation_fee": [
{
"amount": "40000000",
"denom": "stake"
}
],
"pool_types": [
{
"description": "Standard liquidity pool with pool price function X/Y, ESPM constraint, and two kinds of reserve coins",
"id": 1,
"max_reserve_coin_num": 2,
"min_reserve_coin_num": 2,
"name": "StandardLiquidityPool"
}
],
"swap_fee_rate": "0.003000000000000000",
"unit_batch_height": 1,
"withdraw_fee_rate": "0.000000000000000000"
},
"pool_records": [
{
"deposit_msg_states": [],
"pool": {
"id": "1",
"pool_coin_denom": "pool96EF6EA6E5AC828ED87E8D07E7AE2A8180570ADD212117B2DA6F0B75D17A6295",
"reserve_account_address": "cosmos1jmhkafh94jpgakr735r70t32sxq9wzkayzs9we",
"reserve_coin_denoms": [
"uatom",
"uusd"
],
"type_id": 1
},
"pool_batch": {
"begin_height": "563",
"deposit_msg_index": "2",
"executed": false,
"index": "3",
"pool_id": "1",
"swap_msg_index": "2",
"withdraw_msg_index": "2"
},
"pool_metadata": {
"pool_coin_total_supply": {
"amount": "1089899",
"denom": "pool96EF6EA6E5AC828ED87E8D07E7AE2A8180570ADD212117B2DA6F0B75D17A6295"
},
"pool_id": "1",
"reserve_coins": [
{
"amount": "1088843820",
"denom": "uatom"
},
{
"amount": "54551075322",
"denom": "uusd"
}
]
},
"swap_msg_states": [],
"withdraw_msg_states": []
},
{
"deposit_msg_states": [],
"pool": {
"id": "2",
"pool_coin_denom": "poolA4648A10F8D43B8EE4D915A35CB292618215D9F60CE3E2E29216489CF1FAE049",
"reserve_account_address": "cosmos153jg5y8c6sacaexezk34ev5jvxpptk0kscrx0x",
"reserve_coin_denoms": [
"stake",
"uusd"
],
"type_id": 1
},
"pool_batch": {
"begin_height": "0",
"deposit_msg_index": "1",
"executed": false,
"index": "1",
"pool_id": "2",
"swap_msg_index": "1",
"withdraw_msg_index": "1"
},
"pool_metadata": {
"pool_coin_total_supply": {
"amount": "1000000",
"denom": "poolA4648A10F8D43B8EE4D915A35CB292618215D9F60CE3E2E29216489CF1FAE049"
},
"pool_id": "2",
"reserve_coins": [
{
"amount": "10000000",
"denom": "stake"
},
{
"amount": "10000000",
"denom": "uusd"
}
]
},
"swap_msg_states": [],
"withdraw_msg_states": []
}
]
}
}
The API documentation for the liquidity module is available on http://localhost:1317/swagger-liquidity/
after you successfully boostrap a testnet in your local computer.
You must set swagger
config to true
in $HOME/.liquidityapp/config/app.toml
. The public Swagger API docs are also available on Cosmos SDK Liquidity Module - REST and gRPC Gateway docs.
# Generate `*.pb.go`, `*.pb.gw.go` files from `proto/*.proto`
$ make proto-gen
# Generate `swagger.yaml` from `proto/*.proto`
$ make proto-swagger-gen
To learn more about the liquidity module, check out the following resources: