GithubHelp home page GithubHelp logo

v2-sdk-python's Introduction

Python SDK for HMXv2

This library is tested against Python versions 2.7, 3.4, 3.5, 3.9, and 3.11.

Installation

This package is available on PyPI. To install run the command below:

$ pip install hmx-v2-python

Getting Started

The Client object contains two major attributes: Public and Private. As the names suggest, Public is for public functions that don't require an Ethereum private key, and Private is for functions specifically for the given key. For more comprehensive examples, please refer to the examples directory as well as the tests.

Public functions

from hmx2.hmx_client import Client
from hmx2.constants.markets import MARKET_ETH_USD
from hmx2.enum import Action

#
# Using publicly access functions
#
hmx_client = Client(
    rpc_url=RPC_URL
)
# Get oracle price, adaptive price, and price impact of a new position
hmx_client.public.get_price(MARKET_ETH_USD, Action.SELL, 1000)
# Get market information
hmx_client.public.get_market_info(MARKET_ETH_USD)
# Get sub account in address format
hmx_client.public.get_sub_account(1)
# Get position ID
hmx_client.public.get_position_id(some_account, some_sub_account_id, MARKET_ETH_USD)
# Get position info
hmx_client.public.get_position_info(some_account, some_sub_account_id, MARKET_ETH_USD)

Private function

from hmx2.hmx_client import Client
from hmx2.constants.markets import MARKET_ETH_USD
from hmx2.constants.tokens import COLLATERAL_USDCe
from hmx2.enum import Action

#
# Initailized client with private key
#
hmx_client = Client(
    eth_private_key=PRIVATE_KEY,
    rpc_url=RPC_URL
)
# Get public address of the ethereum key
hmx_client.private.get_public_address()
# Deposit ETH as collateral
hmx_client.private.deposit_eth_collateral(sub_account_id=0, amount=10.123)
# Deposit ERC20 as collateral. This function will automatically
# approve CrossMarginHandler if needed.
hmx_client.private.deposit_erc20_collateral(sub_account_id=0, token_address=COLLATERAL_USDCe, amount=100.10)
# Create a market order
create_market_order = hmx_client.private.create_market_order(
  sub_account_id=0, market_index=MARKET_ETH_USD, buy=Action.BUY, size=100, reduce_only=False
)
print(create_market_order)
# Create a trigger order
# trigger_above_threshold = The current price must go above (if True) or below (if False)
# the trigger price in order for the order to be executed
create_order = hmx_client.private.create_trigger_order(
  sub_account_id=0,
  market_index=MARKET_ETH_USD,
  buy=Action.BUY,
  size=100,
  trigger_price=1800,
  trigger_above_threshold=True,
  reduce_only=False)
print(create_order)
# Update the order
update_order = hmx_client.private.update_trigger_order(
  0, create_order["order"]["orderIndex"], Action.SELL, 50, 1700, True, False)
print(update_order)
# Cancel the order
cancel_order = hmx_client.private.cancel_trigger_order(
  0, update_order["order"]["orderIndex"])

Running Tests

To run tests, you will need have to clone the repo, update .env, and run:

$ make test

Please note that to run tests, Tenderly account is required.

License

The primary license for HMXOrg/v2-sdk-python is the MIT License, see here.

v2-sdk-python's People

Contributors

chenciao8 avatar shutsuwei avatar kangdaiyu avatar kim-hera avatar john-xina-p88 avatar 50shadesofgwei avatar miccwan avatar

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.