Skip to main content

Vega API client for gRPC and REST

Project description

Vega API client

Python tests

This is the Vega API client, which can talk to a Vega node using gRPC or REST, and to a Vega Wallet server using REST.

Examples

Connect to a Vega node

In order to interact with a Vega node, create:

  • VegaTradingClient: for trading, e.g. submitting, amending, cancelling orders
  • VegaTradingDataClient: for accessing public data
import vegaapiclient as vac

nodeurl = "veganode.example.com:1234"  # gRPC
# nodeurl = "https://veganode.example.com"  # REST

# Create client for accessing public data
datacli = vac.VegaTradingDataClient(nodeurl)

# Create client for trading (e.g. submitting orders)
tradingcli = vac.VegaTradingClient(nodeurl)

Get market data

from google.protobuf.empty_pb2 import Empty

import vegaapiclient as vac

# Create client for accessing public data
datacli = vac.VegaTradingDataClient(nodeurl)

# Get a list of markets
markets = datacli.Markets(Empty()).markets
for mkt in markets:
	print("{}: {}".format(mkt.id, mkt.name))

# Get a specific market by ID
req = vac.grpc.api.trading.MarketByIDRequest(marketID="MARKETID")
market = datacli.MarketByID(req)
print("{}: {}".format(market.id, market.name))

Connect to a Vega Wallet server

In order to submit signed transactions to a Vega node, connect to a Vega Wallet server, which handles your keypairs for you.

On Vega mainnet, everyone will run their own private wallet server: Your private keys, under your control, your responsibility.

But for testnet demonstration purposes, Vega provides a centralised wallet server. The URL for this is communicated separately.

import vegaapiclient as vac

walleturl = "https://wallet.example.com"
walletclient = vac.WalletClient(walleturl)

wname = "my_vega_wallet"
wpass = "supersecret_passphrase"

# If this is your first time: create a new wallet.
response = walletclient.create(wname, wpass)
assert response.status_code == 200
# If this is *not* your first time: log in to an existing wallet.
response = walletclient.login(wname, wpass)
assert response.status_code == 200

# If you don't already have a keypair, generate one.
response = walletclient.generatekey(wpass, [])
assert response.status_code == 200
key = response.json()["key"]
# Print key information. Note that the private key is *not* returned.
print("Key: {}".format(key))

# List keypairs
response = walletclient.listkeys()
assert response.status_code == 200
for key in response.json()["keys"]:
	print("Key: {}".format(key))

myPubKey = "1122aabb..."  # hex-encoded public key

# Get one keypair
response = walletclient.getkey(myPubKey)
assert response.status_code == 200
print("Key: {}".format(response.json()["key"]))

# Sign a transaction
blob = b"data returned from a Vega node 'Prepare' call"
tx = base64.b64encode(blob).decode("ascii")
response = walletclient.signtx(tx, myPubKey)
assert response.status_code == 200
print("signed tx: {}".format(response.json()["signedTx"]))

# When you're finished with the wallet, log out.
response = walletclient.logout()
assert response.status_code == 200

Submit a transaction

The flow to submit a signed transaction to a Vega node is as follows:

  1. Wallet server: Create a new wallet, or log in to an existing one.
  2. Wallet server: Create a keypair, if one has not already been created.
  3. Vega node: Call PrepareSubmitOrder. Send the order details. Receive a PreparedOrder object.
  4. Wallet server: Call SignTx. Send the PreparedOrder object and a public key associated with the logged-in wallet. Receive a SignedBundle object.
  5. Vega node: Call SubmitTransaction. Send the SignedBundle object.
import base64, binascii
import vegaapiclient as vac

# Vega node: Create client for trading (e.g. submitting orders)
tradingcli = vac.VegaTradingClient(url)

# Wallet server: Create a walletclient (see above for details)
walletclient = vac.WalletClient(walleturl)
walletclient.login(wallet_name, wallet_passphrase)

myPubKey = "1122aabb..."

# Vega node: Prepare the SubmitOrder
order = vac.grpc.api.trading.SubmitOrderRequest(
    submission=vac.grpc.vega.OrderSubmission(
        marketID='MARKETID',
        partyID=myPubKey,
        # price is an integer. For example 123456 is a price of 1.23456,
        # assuming 5 decimal places.
        price=100000,
        side=vac.grpc.vega.Side.Buy,
        size=100,
        timeInForce=vac.grpc.vega.Order.TimeInForce.GTC,
        type=vac.grpc.vega.Order.Type.LIMIT
    )
)
response = tradingcli.PrepareSubmitOrder(order)

# Wallet server: Sign the prepared transaction
blob_base64 = base64.b64encode(response.blob).decode("ascii")
response = walletclient.signtx(blob_base64, myPubKey)
assert r.status_code == 200
signedTx = response.json()["signedTx"]

# Vega node: Submit the signed transaction
request = vac.grpc.api.trading.SubmitTransactionRequest(
    tx=vac.grpc.vega.SignedBundle(
        data=base64.b64decode(signedTx["data"]),
        sig=base64.b64decode(signedTx["sig"]),
        pubKey=binascii.unhexlify(signedTx["pubKey"])
    )
)
response = tradingcli.SubmitTransaction(request)
assert response.success

Project details


Release history Release notifications | RSS feed

Download files

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

Source Distribution

Vega API client-0.0.13.tar.gz (39.7 kB view details)

Uploaded Source

Built Distribution

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

Vega_API_client-0.0.13-py3-none-any.whl (49.4 kB view details)

Uploaded Python 3

File details

Details for the file Vega API client-0.0.13.tar.gz.

File metadata

  • Download URL: Vega API client-0.0.13.tar.gz
  • Upload date:
  • Size: 39.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.44.1 CPython/3.6.9

File hashes

Hashes for Vega API client-0.0.13.tar.gz
Algorithm Hash digest
SHA256 1b6cc3fcbb5e46a9faae924823b900c2b29b72fbd6f7a815fb8042af51cb34a8
MD5 1285db80c963104b2ac74216e6c451d3
BLAKE2b-256 6a1650ae8dcf61d42d42c49f4ec1e4cc6cbb742e8e399eca790e5230d532838d

See more details on using hashes here.

File details

Details for the file Vega_API_client-0.0.13-py3-none-any.whl.

File metadata

  • Download URL: Vega_API_client-0.0.13-py3-none-any.whl
  • Upload date:
  • Size: 49.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.44.1 CPython/3.6.9

File hashes

Hashes for Vega_API_client-0.0.13-py3-none-any.whl
Algorithm Hash digest
SHA256 1b129ade86230cb59f99258614a73fc95eb3c73cb56cea024bfee95d41c80d40
MD5 4379dbc8d999c9a9b85079dbca28f75e
BLAKE2b-256 ae4adbac1fefe13185f2d95c0a27ad90c9ea979b40f3ae4a9a9142dfba9daf60

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