Skip to main content

python SDK for Perpetual Protocol

Project description

PyPerp

A python SDK for Perpetual Protocol.

Installation

From PyPi:

pip install pyperp

Documentaion

Example Code

This example code is written to demonstrate simple trading on the testnet.

1.) creating a Trader object

import web3
from pyperp import Trader, Providers

l2provider = Web3(Web3.HTTPProvider("https://rpc.xdaichain.com/"))
l1provider = Web3(Web3.HTTPProvider("https://rinkeby.infura.io/v3/YOUR_INFURA_PROJECT_ID"))
l1provider.middleware_onion.inject(geth_poa_middleware, layer=0) #rinkeby used poa
provider = Providers.Providers(l1provider,l2provider,True) #The third argument specifies whether the providers are in testnet or mainnet. True means testnet, False means Mainnet

l1wallet = provider.l1.eth.account.privateKeyToAccount("PRIVATE_KEY_HERE")
l2wallet = provider.l2.eth.account.privateKeyToAccount("PRIVATE_KEY_HERE")

trader = Trader.Trader(provider, l1wallet, l2wallet)

2.) using the USDC faucet in rinkeby

from pyperp import Faucets

response = Faucets.getUsdcL1(trader.layer1wallet.address)
print(response)

This USDC faucet is hosted by the Perpetual Protocol team, and it can be used only once per address.

3.) moving USDC from layer1 to layer2

allow layer1 bridge to use your USDC funds if not done already

receipt = trader.approveL1BridgetoUseUSDC()
print(receipt)

Then, call depositUsdcToXdai()

receipt = trader.depositUsdcToxDai(100) #move 100 USDC to xDai 

4.) open a position

allow ClearingHouse contract to use the USDC in your xDai if not done already.

receipt = trader.approveClearingHouseToUseUSDC()
print(receipt)

Open a position with a pair of your choice using the openPosition() method in Trader class. Here, the ETH/USDC pair is used

from perpy import constants

receipt = trader.openPosition("ETHUSDC",constants.Side.SHORT.value,100,2,0)
print(receipt)

the openPosition() function takes the following parameters:

  1. The pair: the pair has to chosen from the available pairs in AvailableAmms dict in constants.py file
  2. side: Has to be either constants.Side.SHORT.value (0) or constants.Side.LONG.value (1)
  3. quoteAssetAmount
  4. leverage
  5. baseAssetAmountLimit

5.) get position information

print(trader.getPersonalPositionWithFundingPayment("ETHUSDC"))
print(trader.getUnrealizedPnl("ETHUSDC",constants.PnlCalcOption.SPOT_PRICE))

The second parameter of the getUnrealizedPnl function is the PnlCalcOption which can be any of these three values : constants.PnlCalcOption.SPOT_PRICE(0), constants.PnlCalcOption.TWAP(1), constants.PnlCalcOption.ORACLE(2)

6.) add margin to position:

receipt = trader.addMargin("ETHUSDC", 10)
print(receipt)

7.) remove margin:

receipt = trader.removeMargin("ETHUSDC", 10)
print(receipt)

8.) close a position:

receipt = trader.closePosition("ETHUSDC",0) #the second parameter is quote asset amount limit
print(receipt)

9.) withdrawing USDC to L1

allow Layer 2 Bridge to use the USDC if not done already

reciept = trader.approveL2BridgeToUseUSDC()
print(receipt)

then, withdraw USDC to L1 using withdrawUsdcToEthereum() function in Trader class

receipt = trader.withdrawUsdcToEthereum(100)
print(receipt)

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-0.0.1.tar.gz (9.5 kB view details)

Uploaded Source

Built Distribution

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

pyperp-0.0.1-py3-none-any.whl (9.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pyperp-0.0.1.tar.gz
  • Upload date:
  • Size: 9.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.22.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.10

File hashes

Hashes for pyperp-0.0.1.tar.gz
Algorithm Hash digest
SHA256 e1d12bab76e9d7b853b8a0523f79d338ac4134f4789c2bedcb3a35e27e3a1b23
MD5 6e827ca38366af456b25e584a722d24c
BLAKE2b-256 26bd502ef2555ccbc8ee37a9c76983355fa2f0303dd53256f2a97e170f007ffb

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pyperp-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 9.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.22.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.10

File hashes

Hashes for pyperp-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 6d4cd01fd088d52589964d98cabeba412b2922e4e5e8f9a4708cee7bbbabff6c
MD5 f62432148d4c7617a92e9ffbfeec146f
BLAKE2b-256 b24354f3e7d5c41c3461f9952dae58811c34f25e560f98028c7ca34e6e373977

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