Skip to content

Commit

Permalink
feat: First working version of vega simulator (#5)
Browse files Browse the repository at this point in the history
* feat: First working version of vega simulator
  • Loading branch information
TomMcL authored Apr 1, 2022
1 parent 1fa8a08 commit 7dc8bdc
Show file tree
Hide file tree
Showing 57 changed files with 2,130 additions and 0 deletions.
33 changes: 33 additions & 0 deletions .github/workflows/python.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
---

name: Python

"on":
pull_request:
branches:
- main

jobs:

build:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.7", "3.8", "3.9"]
steps:
- name: Check out code
uses: actions/checkout@v3

- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v3
with:
python-version: ${{ matrix.python-version }}

- name: Lint with black
run: |
command -v black 1>/dev/null || pip install black
make blackcheck
- name: Test with pytest
run: |
command -v pipenv 1>/dev/null || pip install pipenv
make test
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ share/python-wheels/
.installed.cfg
*.egg
MANIFEST
extern/*

# PyInstaller
# Usually these files are written by a python script from a template
Expand Down Expand Up @@ -127,3 +128,8 @@ dmypy.json

# Pyre type checker
.pyre/


.DS_Store
.vscode/*
vega_sim/bin/*
1 change: 1 addition & 0 deletions CODEOWNERS
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* @vegaprotocol/research
44 changes: 44 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
VEGA_TAG := tags/v0.49.6
WALLET_TAG := tags/v0.13.1
DATA_NODE_TAG := tags/v0.49.3
EXTERN_DIR := "./extern"

all: pull_deps build_deps

pull_deps:
@mkdir -p ./extern/{vega,vegawallet,data-node}
@echo "Downloading Git dependencies into " ${EXTERN_DIR}
@echo "Downloading Vega"
@git clone https://github.com/vegaprotocol/vega ${EXTERN_DIR}/vega && git -C ${EXTERN_DIR}/vega checkout -b ${VEGA_TAG}
@echo "Downloading data-node"
@git clone https://github.com/vegaprotocol/data-node ${EXTERN_DIR}/data-node && git -C ${EXTERN_DIR}/data-node checkout -b ${DATA_NODE_TAG}
@echo "Downloading Vegawallet"
@git clone https://github.com/vegaprotocol/vegawallet ${EXTERN_DIR}/vegawallet && git -C ${EXTERN_DIR}/vegawallet checkout -b ${WALLET_TAG}

build_deps:
@mkdir -p ./vega_sim/bin
cd ${EXTERN_DIR}/vega && go build -o ../../vega_sim/bin/ ./...
cd ${EXTERN_DIR}/vegawallet && go build -o ../../vega_sim/bin/ ./...
cd ${EXTERN_DIR}/data-node && go build -o ../../vega_sim/bin/ ./...

.PHONY: black
black:
@black .

.PHONY: blackcheck
blackcheck:
@black --check .

.PHONY: clean
clean:
@rm -rf "$(GENERATED_DIR)"

.PHONY: flake8
flake8:
@flake8

.PHONY: test
test:
@pipenv --bare install --dev # 1>/dev/null 2>&1
@pipenv run pip install -e .
@env PYTHONPATH=. pipenv run pytest tests/
18 changes: 18 additions & 0 deletions Pipfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[[source]]
url = "https://pypi.org/simple"
verify_ssl = true
name = "pypi"

[packages]
requests = "*"
vega-api-client = "*"
toml = "*"

[dev-packages]
black = "*"
flake8 = "*"
pytest = "*"
requests-mock = "*"

[requires]
python_version = "3"
391 changes: 391 additions & 0 deletions Pipfile.lock

Large diffs are not rendered by default.

Empty file added examples/__init__.py
Empty file.
36 changes: 36 additions & 0 deletions examples/nullchain.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from collections import namedtuple

from vega_sim.null_service import VegaServiceNull


WalletConfig = namedtuple("WalletConfig", ["name", "passphrase"])

# Set up parties in the market/ Submit liquidity provision/ Control midprice
MM_WALLET = WalletConfig("wwestgarth", "pin")

# The party to send selling/buying MOs to hit LP orders
TRADER_WALLET = WalletConfig("Zl3pLs6Xk6SwIK7Jlp2x", "bJQDDVGAhKkj3PVCc7Rr")

# The party randomly post LOs at buy/sell side to simulate real Market situation
RANDOM_WALLET = WalletConfig("OJpVLvU5fgLJbhNPdESa", "GmJTt9Gk34BHDlovB7AJ")

# The party to terminate the market and send settlment price
TERMINATE_WALLET = WalletConfig("FJMKnwfZdd48C8NqvYrG", "bY3DxwtsCstMIIZdNpKs")

wallets = [MM_WALLET, TRADER_WALLET, RANDOM_WALLET, TERMINATE_WALLET]

if __name__ == "__main__":
vega = VegaServiceNull()
vega.start()
import time

time.sleep(10)
for wallet in wallets:
vega.create_wallet(wallet.name, wallet.passphrase)

vega.mint(
MM_WALLET.name,
asset="6d9d35f657589e40ddfb448b7ad4a7463b66efb307527fedd2aa7df1bbd5ea61",
amount=10000000000,
)
# vega.propose_market()
37 changes: 37 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import os
import setuptools


VERSION = "0.0.1"


def package_files(directory):
paths = []
for (path, directories, filenames) in os.walk(directory):
for filename in filenames:
paths.append(os.path.join("..", path, filename))
return paths


extra_files = package_files("vega_sim/vegahome")


setuptools.setup(
name="vega-market-sim",
version=VERSION,
author="Vega",
author_email="[email protected]",
description="Simulator for running self-contained Vega chain on local PC",
url="https://github.com/vegaprotocol/vega-market-sim",
packages=setuptools.find_packages(exclude=["tests", "examples"]),
package_data={"": extra_files, "vega_sim": ["bin/*"]},
classifiers=[
"Programming Language :: Python :: 3",
"License :: OSI Approved :: MIT License",
"Operating System :: OS Independent",
],
python_requires=">=3.7",
install_requires=[],
setup_requires=["wheel"],
zip_safe=True,
)
Empty file added tests/__init__.py
Empty file.
76 changes: 76 additions & 0 deletions tests/test_service.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import json
import pytest
import requests_mock

from vega_sim.service import VegaService
from vega_sim.api.wallet import WALLET_CREATION_URL, WALLET_KEY_URL, WALLET_LOGIN_URL


class StubService(VegaService):
def __init__(self, wallet_url: str, data_url: str, faucet_url: str):
super().__init__()
self.wallet_url_str = wallet_url
self.data_url_str = data_url
self.faucet_url_str = faucet_url

def wallet_url(self) -> str:
return self.wallet_url_str

def data_node_rest_url(self) -> str:
return self.data_url_str

def faucet_url(self) -> str:
return self.faucet_url_str


@pytest.fixture
def stub_service():
return StubService(
"localhost:TEST_WALLET", "localhost:TEST_DATA", "localhost:TEST_FAUCET"
)


def test_base_service_wallet_creation(stub_service: StubService):
with requests_mock.Mocker() as req_mocker:
req_mocker.post(
WALLET_CREATION_URL.format(wallet_server_url="localhost:TEST_WALLET"),
json=lambda req, _: {
"token": req.json()["wallet"] + req.json()["passphrase"]
},
)
req_mocker.post(
WALLET_KEY_URL.format(wallet_server_url="localhost:TEST_WALLET"),
json=lambda req, _: {
"key": {
"pub": req.json()["passphrase"] + json.dumps(req.json()["meta"])
}
},
)
stub_service.create_wallet("TEST_NAME", "TEST_PHRASE")
assert stub_service.login_tokens["TEST_NAME"] == "TEST_NAMETEST_PHRASE"
assert (
stub_service.pub_keys["TEST_NAME"] == 'TEST_PHRASE[{"name": "default_key"}]'
)


def test_base_service_wallet_login(stub_service: StubService):
with requests_mock.Mocker() as req_mocker:
req_mocker.post(
WALLET_LOGIN_URL.format(wallet_server_url="localhost:TEST_WALLET"),
json=lambda req, _: {
"token": req.json()["wallet"] + req.json()["passphrase"]
},
)
req_mocker.post(
WALLET_KEY_URL.format(wallet_server_url="localhost:TEST_WALLET"),
json=lambda req, _: {
"key": {
"pub": req.json()["passphrase"] + json.dumps(req.json()["meta"])
}
},
)
stub_service.login("TEST_NAME", "TEST_PHRASE")
assert stub_service.login_tokens["TEST_NAME"] == "TEST_NAMETEST_PHRASE"
assert (
stub_service.pub_keys["TEST_NAME"] == 'TEST_PHRASE[{"name": "default_key"}]'
)
4 changes: 4 additions & 0 deletions vega_sim/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from pkg_resources import resource_filename

vega_home_path = resource_filename(__name__, "vegahome")
vega_bin_path = resource_filename(__name__, "bin")
Empty file added vega_sim/api/__init__.py
Empty file.
16 changes: 16 additions & 0 deletions vega_sim/api/faucet.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import requests

BASE_MINT_URL = "{faucet_url}/api/v1/mint"


def mint(pub_key: str, asset: str, amount: int, faucet_url: str) -> None:
url = BASE_MINT_URL.format(faucet_url=faucet_url)
payload = {
"party": pub_key,
"amount": str(amount),
# "asset": "VOTE",
"asset": asset,
}

r = requests.post(url, json=payload)
r.raise_for_status()
Loading

0 comments on commit 7dc8bdc

Please sign in to comment.