Skip to main content

python SDK for Perpetual Protocol

Project description

PyPerp

A python SDK for Perpetual Protocol.

Installation

From PyPi:

pip install pyperp

Documentation

The complete documentation is available here

Example Code

This example code is written to demonstrate simple trading on Optimism Kovan.

Create an ApiProvider object

from eth_account import Account
from pyperp.providers import OptimismKovanProvider

account = Account.from_key('<PRIVATE_KEY_HERE>')
provider = OptimismKovanProvider(
  '<RPC_ENDPOINT_URL>',
  account
)

Approve Vault to use USDC

from pyperp.contracts import Vault
from pyperp.common.types import GasParams

gas_params = GasParams(
  gas=1000000,
  gas_price=100000
)

vault = Vault(provider)

receipt = vault.approve_vault_to_use_usdc(
  gas_params
)

deposit USDC to vault

receipt = vault.deposit(
  vault.usdc.address,
  10000000000,
  gas_params
)

Vault.deposit() takes the following positional arguments:

token: contract address of collateral token

amount: amount of token to deposit

gas_params: GasParams object denoting gas paramateres.

check how much collateral can be withdrawn

print(
  vault.get_free_collateral(
    account.address
  )
)

withdraw from vault

receipt = vault.withdraw(
  vault.usdc.address,
  <AMOUNT_HERE>,
  gas_params
)

open a position

You can open a position for either vBTC or vETH. In this example we will open a position for vBTC

from pyperp.contracts import ClearingHouse
from pyperp.contracts.types import OpenPositionParams
from pyperp.commom.utils import getDeadline

clearing_house = ClearingHouse(provider)

params = OpenPositionParams(
  base_token = clearing_house.vbtc.address,
  is_base_to_quote = True,
  is_exact_input = True,
  amount = int(0.002*10**18),
  opposite_amount_bound = 0,
  deadline = getDeadline(120), #deadline is 120 secs from now
  sqrt_price_limit_x96 = 0
)

receipt = clearing_house.open_position(
  params, gas_params
)

get account value

print(
  clearing_house.get_account_value(account.address)
)

Close Position

from pyperp.contract.types import ClosePositionParams

params = ClosePositionParams(
  base_token=clearing_house.vbtc.address,
  sqrt_price_limit_x96=0,
  oppposite_amount_bound=0,
  deadline=getDeadline(120)
)

receipt = clearing_house.close_position(
  params, gas_params
)

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

pyperp-2.0.0.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.

pyperp-2.0.0-py3-none-any.whl (1.1 MB view details)

Uploaded Python 3

File details

Details for the file pyperp-2.0.0.tar.gz.

File metadata

  • Download URL: pyperp-2.0.0.tar.gz
  • Upload date:
  • Size: 1.1 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.24.0 requests-toolbelt/0.9.1 tqdm/4.47.0 CPython/3.8.3

File hashes

Hashes for pyperp-2.0.0.tar.gz
Algorithm Hash digest
SHA256 14ab5d8bb1d94839c3f5db4d957586fec750370bdb76400169d6b91e5f71ad04
MD5 6dc647bb4a35ff7a02f6cd0d10985e42
BLAKE2b-256 9da399ed955adcf3da23a774e8e36f3cf986d1b1baa4413017a23976802f489a

See more details on using hashes here.

File details

Details for the file pyperp-2.0.0-py3-none-any.whl.

File metadata

  • Download URL: pyperp-2.0.0-py3-none-any.whl
  • Upload date:
  • Size: 1.1 MB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.24.0 requests-toolbelt/0.9.1 tqdm/4.47.0 CPython/3.8.3

File hashes

Hashes for pyperp-2.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 52e35148e9956ba7d28f40a204048085a06b47a60712631ed6bddc65cc248ac7
MD5 81badf0d1f7cfe9423acf1372332ff4d
BLAKE2b-256 4da426d7e76ff8b2db9fc09ced27316da88dcf84550d135d5c7f629891e2e131

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