Skip to main content

A concise PancakeSwap V2/V3 Python library

Project description

pancankeswap_py_logo

PyPI version Python License

pancakeswap-py 🍪🐍

A Python library based on web3.py, Asyncio, and PancakeSwap V2/V3 contracts, featuring pool creation, liquidity management, liquidity removal, liquidity viewing, swap trading, WBNB/BNB conversion, and more. V2/V3 use consistent syntax as much as possible, making it convenient for beginners to perform DeFi operations on the BSC network.

中文文档

⚡ Quick Start

Installation

pip install pancakeswap-py
uv add pancakeswap-py

V3 Examples

Prerequisites

💡 It's recommended to use private or privacy-focused RPC nodes to prevent sandwich attacks

from pancakeswap_py.v3 import PancakeSwapV3, Fee
pancakeswap_v3 = PancakeSwapV3(
    json_rpc=JSON_RPC,
    private_key=PRIVATE_KEY,
)
WBNB = "0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c"
# 💡 Test token with no value
TOKEN = "0x7e216C64f5954758d747e61C3b58062509eE40B8"
amountA = 0.001
amountB = 100

💡 All methods with the auto prefix are wrapped by the author for ease of use by beginners
💡 All slippage defaults to 0.05
💡 Both V2/V3 classes include WBNB/BNB conversion methods: wrap_bnb/unwrap_wbnb

Create Pool

sqrt_price_x96 = await pancakeswap_v3.calculate_sqrt_price_x96(
    tokenA=WBNB,
    tokenB=TOKEN,
    amountA=amountA,
    amountB=amountB,
)
tx_hash = await pancakeswap_v3.auto_create_pool(
    tokenA=WBNB,
    tokenB=TOKEN,
    sqrt_price_x96=sqrt_price_x96,
    fee=Fee.NORMAL,
)
#sqrt_price_x96:250541448375047936131727360
#tx_hash:24bed0415fd2d83f53f682f1d8a5780c973662827f29af9115146a3afc832bfe

Create Liquidity

tx_hash = await pancakeswap_v3.auto_create_liquidity(
    tokenA=WBNB,
    tokenB=TOKEN,
    amountA=amountA,
    amountB=amountB,
)
#tx_hash:b8ccee2cfc258979f93d6fcae672692b2ceaad9b7e35d981a26e7cc9ec2feed1

Get All Token IDs for Address

all_tokenID = await pancakeswap_v3.get_all_tokenID()
#all_tokenID:[2823169]

Add Liquidity

tx_hash = await pancakeswap_v3.auto_increase_liquidity(
    tokenA=WBNB,
    tokenB=TOKEN,
    amountA=amountA,
    amountB=amountB,
    token_id=token_id,
)
#tx_hash:36ff8bda68c9e959e49f3ef798cea4c6996c12bd0197de6bf26f54b16a14fe7f

Swap Trading

tx_hash = await pancakeswap_v3.auto_swap(
        tokenIn=WBNB,
        tokenOut=TOKEN,
        amountIn=0.0001,
        fee=Fee.NORMAL,
    )
#tx_hash:2abc0a2e19d8792b852c11aebdce185dba0348b8018f397c9acf937c64cca70e

View Liquidity

pool_liquidity = await pancakeswap_v3.get_pool_liquidity(
    tokenA=WBNB,
    tokenB=TOKEN,
    fee=Fee.NORMAL,
)
#pool_liquidity:62026147965412118004

View Position

position_info = await pancakeswap_v3.get_position_info(
    token_id = token_id,
)
#position_info:[0, '0x0000000000000000000000000000000000000000', '0x7e216C64f5954758d747e61C3b58062509eE40B8', '0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c', 500, -115340, -114940, 62026147965412118004, 0, 0, 0, 0]

Remove Liquidity

tx_hash = await pancakeswap_v3.auto_decrease_liquidity(
    token_id=token_id,
    liquidity_percentage=1,  # 1 means remove all
)
#tx_hash:5c59426baf1662824c33208d13ca9459da445458e02414e33657a4ab95c48f4f

Collect Fees

tx_hash = await pancakeswap_v3.auto_collect_fees(
    token_id = 2823169,
)
#tx_hash:c396af7ec749edf88a02cfb2af230978053061e28642f0728b08066971011448

Burn Position

tx_hash = await pancakeswap_v3.auto_burn_position(
    token_id=2823169,
)
#tx_hash:f1746719e0ee85d0bcab108bef8353b1df12060840df022a7815d904d48fcef8

On-Chain Screenshot

pancankeswap_py_logo

V2 Examples

Prerequisites

from pancakeswap_py.v2 import PancakeSwapV2

JSON_RPC = os.getenv("JSON_RPC")
PRIVATE_KEY = os.getenv("PRIVATE_KEY")

pancakeswap_v2 = PancakeSwapV2(
    json_rpc=JSON_RPC,
    private_key=PRIVATE_KEY,
)

WBNB = "0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c"
# 💡 Test token with no value!
TOKEN = "0xDD6A7a67466e4c207188Ad3A1E7D0E667C9F00B1"
amountA = 0.001
amountB = 100

Create Pool

tx_hash = await pancakeswap_v2.auto_create_pair(
    tokenA=WBNB,
    tokenB=TOKEN,
)
#tx_hash:dfc36bab549723f9cb90203d52b0450c7c830765328af1f062c067ebe67ae2d5

Create Liquidity

tx_hash = await pancakeswap_v2.auto_create_liquidity(
    tokenA=WBNB,
    tokenB=TOKEN,
    amountA=amountA,
    amountB=amountB,
)
#tx_hash:0b256eef837c8801fc7243a2158a7100239fddb7290ea305642a6ed2469c4138

Add Liquidity

tx_hash = await pancakeswap_v2.auto_increase_liquidity(
    tokenA=WBNB,
    tokenB=TOKEN,
    amountA=amountA,
    amountB=amountB,
)
#tx_hash:cab6bb030664588bfb1ad51e688c4c05a12fa72ba09464475ece94f7e3996d9b

Get Pool Address

pair_address = await pancakeswap_v2.get_pair(
    tokenA=WBNB,
    tokenB=TOKEN,
)
#pair_address:0x002693E9eb3B6787034a8C09acB7cb90AFba1C5F

Get Token Reserves

reserves = await pancakeswap_v2.get_reserves(
    tokenA=WBNB,
    tokenB=TOKEN,
)
#reserves:(2000000000000000, 200000000000000000000, 1751778446)

Swap Trading

tx_hash = await pancakeswap_v2.auto_swap(
    tokenIn=TOKEN,
    tokenOut=WBNB,
    amountIn=10,
)
#tx_hash:a3a0f07b5535d32d876eb2d5716be0aa9835f49e78a9added254246f3d2c48ec

Remove Liquidity

tx_hash = await pancakeswap_v2.auto_decrease_liquidity(
    tokenA=TOKEN,
    tokenB=WBNB,
    liquidity_percentage=1,  # 1 means remove all
)
#tx_hash:d38f04a4d87d7ffe2d3b2f24be9011bc1c2394083e5c65a6ce042273537ccfcd

On-Chain Screenshot

pancankeswap_py_logo

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pancakeswap_py-0.1.6.tar.gz (1.1 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pancakeswap_py-0.1.6-py3-none-any.whl (21.0 kB view details)

Uploaded Python 3

File details

Details for the file pancakeswap_py-0.1.6.tar.gz.

File metadata

  • Download URL: pancakeswap_py-0.1.6.tar.gz
  • Upload date:
  • Size: 1.1 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.7.13

File hashes

Hashes for pancakeswap_py-0.1.6.tar.gz
Algorithm Hash digest
SHA256 f9d5f61cdf475012a7db434709c8f33070ad183a68d9a17a1768476dbb3c38dc
MD5 82ba6642857ed60e542ca483e651897a
BLAKE2b-256 d7b96f5a74826b6e8d775861ca9e66282fdc83f10b326bbc7b3bc37a0e7fc960

See more details on using hashes here.

File details

Details for the file pancakeswap_py-0.1.6-py3-none-any.whl.

File metadata

File hashes

Hashes for pancakeswap_py-0.1.6-py3-none-any.whl
Algorithm Hash digest
SHA256 67856a4d5eba5df4f67c698ee5479a58d0546eb901484b0ec2341447daa51b4e
MD5 2277ba3c863cefcd2a438c52f1ebbbaf
BLAKE2b-256 4afce34c424cd245d4da7d071bcf3c5274e1685a0046a85722eb3e189b67305b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page