Vega API client for gRPC
Project description
Vega API client
This is the Vega API client, which talks to a Vega node using gRPC, and to a Vega Wallet server using REST.
Disclaimer
This is a community effort. It is not supported by Vega, nor is it guaranteed to be up to date or compatible with new Vega releases.
Examples
Connect to a Vega Wallet server
The methods of WalletClient each return a plain requests.Response object.
See Python requests for details.
import vegaapiclient as vac
walletclient = vac.WalletClient("https://wallet.example.com")
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
# Print key information. Note that the private key is *not* returned.
print("Key: {}".format(response.json()["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 finished with the wallet, log out.
response = walletclient.logout()
assert response.status_code == 200
Connect to a Vega node
In order to interact with a Vega node, create:
VegaTradingClient: for trading, e.g. submitting, amending, cancelling ordersVegaTradingDataClient: for accessing public data
import vegaapiclient as vac
# Create client for accessing public data
datacli = vac.VegaTradingDataClient("veganode.example.com:1234")
# Create client for trading (e.g. submitting orders)
tradingcli = vac.VegaTradingClient("veganode.example.com:1234")
Get market data
from google.protobuf.empty_pb2 import Empty
import vegaapiclient as vac
# Create client for accessing public data
datacli = vac.VegaTradingDataClient("veganode.example.com:1234")
# Get a list of markets
markets = datacli.Markets(Empty()).markets
print("{}".format(markets))
# Get a specific market by ID
req = vac.api.trading.MarketByIDRequest(marketID="MARKETID")
market = datacli.MarketByID(req)
print("{}: {}".format(market.id, market.name))
Submit a signed SubmitOrder transaction
The flow to submit a signed transaction to a Vega node is as follows:
- Wallet server: Create a new wallet, or log in to an existing one.
- Wallet server: Create a keypair, if one has not already been created.
- Vega node: Call
PrepareSubmitOrder. Send the order details. Receive aPreparedOrderobject. - Wallet server: Call
SignTx. Send thePreparedOrderobject and a public key associated with the logged-in wallet. Receive aSignedBundleobject. - Vega node: Call
SubmitTransaction. Send theSignedBundleobject.
import base64, binascii
import vegaapiclient as vac
# Vega node: Create client for trading (e.g. submitting orders)
tradingcli = vac.VegaTradingClient(nodeurl)
# Wallet server: Create a walletclient (see above for details)
walletclient = vac.WalletClient(walleturl)
walletclient.login(wallet_name, wallet_passphrase)
marketID = "MARKETID"
myPubKey = "1122aabb..."
# Vega node: Prepare the SubmitOrder
order = vac.api.trading.SubmitOrderRequest(
submission=vac.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.vega.Side.Buy,
size=100,
timeInForce=vac.vega.Order.TimeInForce.GTC,
type=vac.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 response.status_code == 200
signedTx = response.json()["signedTx"]
# Vega node: Submit the signed transaction
request = vac.api.trading.SubmitTransactionRequest(
tx=vac.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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file Vega API client-0.20.0.tar.gz.
File metadata
- Download URL: Vega API client-0.20.0.tar.gz
- Upload date:
- Size: 44.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/47.1.1 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.6.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5bf47f71c0da4bbbd7b41ef0f9aaaba1a5b872ab035aae600171f421456ae305
|
|
| MD5 |
97a2c019890d5a271e41cba9336f73d2
|
|
| BLAKE2b-256 |
d634189a6b7fe2082b859b3c0393bf5bbc87b8103b0e4ac28fce7949fad71f45
|
File details
Details for the file Vega_API_client-0.20.0-py3-none-any.whl.
File metadata
- Download URL: Vega_API_client-0.20.0-py3-none-any.whl
- Upload date:
- Size: 100.1 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/47.1.1 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.6.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f7588dcfe46379ec4860732402a3ebe103a325adc0c7d2ebbdb2e5a7cce8725c
|
|
| MD5 |
22a6e3aacbd10d2f70fb062da19da6c9
|
|
| BLAKE2b-256 |
7fba7f901c961fddeee769d8e8a4b14cdf41386b614c365527292fe4e7a2af58
|