• Stars
    star
    136
  • Rank 267,670 (Top 6 %)
  • Language
    Go
  • License
    Apache License 2.0
  • Created about 4 years ago
  • Updated almost 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Cosmos SDK Liquidity module

codecov PkgGoDev

Liquidity Module

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.

Key features

new-amm-model

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.

Installation

Requirements

Requirement Notes
Go version Go1.15 or higher
Cosmos SDK v0.45.1 or higher

Get Liquidity Module source code

$ git clone https://github.com/tendermint/liquidity.git
$ cd liquidity
$ go mod tidy

Build

# The `liquidityd` binary is in the build directory.
$ make build

Install

$ make install

Usage of CLI Commands

With the exception of creating the liquidity pool, all commands are implemented to execute on the batch.

Transactions

$ 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

Queries

$ 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

Development

Test

$ make test-all

1. Setup local testnet using script

# 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

1.1 Broadcast transactions using CLI command-line interface

Sample scripts are provided in scripts folder to help you to test the liquidity module interface.

2. Manually set up a local testnet

# 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

2.1 Broadcast transactions using CLI commands

# An example of creating liquidity pool 1
$BINARY tx liquidity create-pool 1 1000000000uatom,50000000000uusd --from user1 --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 --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 --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 --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)

2.2 Broadcast transactions using REST APIs

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

2.3 Export Genesis State

$ $BINARY export

Export empty state case

{
  "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": []
  }
}

Export when some states exist

{
  "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": []
      }
    ]
  }
}

Protobuf and Swagger

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

Resources

To learn more about the liquidity module, check out the following resources:

More Repositories

1

tendermint

⟁ Tendermint Core (BFT Consensus) in Go
Go
5,705
star
2

go-amino

Protobuf3 with Interface support - Designed for blockchains (deterministic, upgradeable, fast, and compact)
Go
259
star
3

abci

DEPRECATED: Merged into https://github.com/tendermint/tendermint under `abci`
Go
254
star
4

signatory

Multi-provider digital signature library for Rust
Rust
141
star
5

tmkms

Key Management service for Tendermint Validator nodes
Rust
140
star
6

spec

TeX
130
star
7

go-p2p

DEPRECATED (moved to tendermint/tendermint): Golang P2P library
Go
121
star
8

rust-abci

A rust implementation of the ABCI protocol for tendermint core
Rust
116
star
9

spn

A blockchain to launch blockchains.
Go
112
star
10

awesome

Collection of resources for all things Tendermint
97
star
11

tm-db

Common database interface for various database backends for Tendermint Core and Cosmos SDK
Go
89
star
12

js-abci

Javascript ABCI libraries
JavaScript
87
star
13

yubihsm-rs

Pure Rust client for YubiHSM2 devices
70
star
14

dex-demo

DeX Demo
HTML
48
star
15

clearchain

Cosmos app for clearing and settlements
Go
47
star
16

merkleeyes

DEPRECATED: Merkle-ized data store
Go
46
star
17

tmlibs

DEPRECATED: Merged into https://github.com/tendermint/tendermint under `libs`
Go
45
star
18

basecoin

DEPRECATED: see https://github.com/cosmos/cosmos-sdk
Go
44
star
19

go-crypto

DEPRECATED: Merged into https://github.com/tendermint/tendermint under `crypto`
Go
44
star
20

cns

Chain Name System
Go
39
star
21

tools

DEPRECATED: Merged into https://github.com/tendermint/tendermint under `tools`
Go
38
star
22

faucet

A faucet for cosmos-sdk apps that uses the app binary only
Go
30
star
23

sig

A signing library for Cosmos.
TypeScript
29
star
24

tendermint2

Tendermint2
Go
29
star
25

atom_one

ATOM ONE CONSTITUTION
27
star
26

amino_rs

Rust implementation of Amino
Rust
26
star
27

cosmos-rosetta-gateway

Rosetta Libraries for Cosmos SDK apps
Go
24
star
28

flutter

Dart
23
star
29

fundraising

Go
22
star
30

budget

Budget is a Cosmos SDK module that implements budget functionality.
Go
21
star
31

farming

Farming is a Cosmos SDK module that implements farming functionality
Go
20
star
32

cosmos-ui

UI component library
Vue
18
star
33

testnets

Config files for connecting to testnets
Python
17
star
34

governmint

DEPRECATED: TMSP Governance App
Go
17
star
35

light-client

DEPRECATED: A light client for tendermint, supporting signatures, proofs, and validation (see github.com/tendermint/tendermint/lite)
Go
17
star
36

coding

Shell
14
star
37

interchange

Interchain Exchange Module
TypeScript
14
star
38

basecoin-examples

DEPRECATED: Example code, showing how you can build your own cryptocurrency on top of basecoin
Go
10
star
39

nomnomcoin

DEPRECATED: A TMSP cryptocurrency in NodeJS
JavaScript
9
star
40

ecosystem

Tendermint forks geneology and directory
9
star
41

classic

canonical blockchain infrastructure
Go
8
star
42

dither

Decentralized messaging for all Cosmos blockchains
7
star
43

alpha

Tiny web app to help you form a genesis file
Go
6
star
44

networks

Deploying and testing Tendermint networks
Go
6
star
45

network_testing

DEPRECATED: Benchmarking tendermint networks
Shell
6
star
46

ledger-validator-app

C
5
star
47

fault-tolerance-demo

DEPRECATED: A demo of tendermint's fault tolerance
Shell
4
star
48

homebrew-tendermint

Homebrew Tap for Tendermint, Ethermint, Basecoin and all related projects.
Ruby
4
star
49

images

Shell
4
star
50

docs

Docs deployment
3
star
51

spm

Go
3
star
52

devdoc

Docker environment for development
Makefile
3
star
53

backend

Backend stuff for design
JavaScript
3
star
54

go-amino-x

Go
3
star
55

js-wire

Javascript library for go-wire codec
JavaScript
2
star
56

go-process

DEPRECATED (moved to tendermint/tmlibs): Process libraries for Golang
Go
2
star
57

identity

Identity and authentication for the blockchain. Public domain work.
2
star
58

js-merkleeyes

DEPRECATED: Javascript library for MerkleEyes
JavaScript
1
star
59

go-alert

DEPRECATED (moved to tendermint/tmlibs)
Go
1
star
60

belt

A utility belt for TypeScript + JavaScript.
TypeScript
1
star
61

mintkey

DEPRECATED: Tendermint key management
Go
1
star
62

lunie-ng

Lunie Next Generation
Vue
1
star
63

spm-extras

Additional packages that we can not keep under tendermint/spm because of incompatibilities.
Go
1
star
64

fuzz

Go
1
star
65

homebrew-tap

Homebrew tap for Cosmos
Ruby
1
star
66

go-keys

DEPRECATED (moved to tendermint/go-crypto and tendermint/basecoin): Key manager for tendermint clients
Go
1
star