Giter Site home page Giter Site logo

pyperp's Introduction

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
)

pyperp's People

Contributors

guplersaxanoid avatar

Stargazers

 avatar  avatar  avatar  avatar supernoveau avatar Scott Li avatar Brendan Wenzel avatar WhichWayzUp avatar  avatar Gabriel Fior avatar  avatar  avatar  avatar dim3003 avatar rockybi avatar  avatar Shao avatar  avatar  avatar longcpp avatar Dendi Suhubdy avatar Bakuchi avatar Tyler Martin avatar Tobias Oberstein avatar Michael  Martin avatar 江祐宏 Alex avatar Shreyas Papinwar avatar

Watchers

 avatar  avatar

pyperp's Issues

Streaming WebSocket API support

Hi there! First, thanks for your project, appreciated that you share as OSS!

Q: Do you plan to support the streaming WebSocket API? As in https://docs.dydx.exchange/#v3-websocket-api


Other notes:

And this additional WebSocket URL wss://jp7ymdkf7jcz5hni62kofoxapm.appsync-realtime-api.ap-southeast-1.amazonaws.com/graphql?header=eyJob3N0IjoianA3eW1ka2Y3amN6NWhuaTYya29mb3hhcG0uYXBwc3luYy1hcGkuYXAtc291dGhlYXN0LTEuYW1hem9uYXdzLmNvbSIsIngtYW16LWRhdGUiOiIyMDIxMTEyMFQwNTEzMDJaIiwieC1hcGkta2V5IjoiZGEyLWVhMmx1eWc3eGZhcnZpaDJwM240N2xjbno0In0=&payload=e30= used by the Web UI, apparently built using https://aws.amazon.com/appsync/:
Bildschirmfoto von 2021-11-20 06-18-20

Wrong Addresses in /abi/optimism

Many addresses in the abi/optimism folder are wrong, for example in AccountBalance.json the correct address is :

"address": "0xA7f3FC32043757039d5e13d790EE43edBcBa8b7c"

in vBTC.json "address": "0x86f1e0420c26a858fc203A3645dD1A36868F18e5"

and many more to correct,

I've always used the one I found in https://metadata.perp.exchange/v2/optimism.json and they are workin great

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.