Skip to content

Commit

Permalink
Merge a9c4eed into 9efbac9
Browse files Browse the repository at this point in the history
  • Loading branch information
KeithSSmith committed Aug 9, 2018
2 parents 9efbac9 + a9c4eed commit 23a696f
Show file tree
Hide file tree
Showing 8 changed files with 133 additions and 51 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ language: python

python:
- 3.6
- 3.7

install:
- pip install -r requirements.txt
Expand Down
11 changes: 7 additions & 4 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,21 @@ Switcheo Python API

Python Client for interacting with the Switcheo API.

.. image:: https://img.shields.io/pypi/v/switcheo.svg
:target: https://img.shields.io/pypi/v/switcheo.svg
:alt: PyPi
.. image:: https://readthedocs.org/projects/switcheo-python/badge/?version=latest
:target: https://switcheo-python.readthedocs.io/en/latest/?badge=latest
:target: https://switcheo-python.readthedocs.io/en/latest
:alt: ReadTheDocs
.. image:: https://travis-ci.org/KeithSSmith/switcheo-python.svg?branch=master
:target: https://travis-ci.org/KeithSSmith/switcheo-python
:alt: Travis CI
.. image:: https://coveralls.io/repos/github/KeithSSmith/switcheo-python/badge.svg?branch=master
:target: https://coveralls.io/github/KeithSSmith/switcheo-python?branch=master
:alt: Coveralls
.. image:: https://img.shields.io/pypi/v/switcheo.svg
:target: https://pypi.org/project/switcheo
:alt: PyPi
.. image:: https://img.shields.io/pypi/pyversions/switcheo.svg
:target: https://pypi.org/project/switcheo
:alt: PyPi
.. image:: https://img.shields.io/pypi/l/switcheo.svg
:target: https://img.shields.io/pypi/l/switcheo.svg
:alt: PyPi
Expand Down
Binary file added docs/images/switcheo-logo.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
58 changes: 28 additions & 30 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,37 +7,38 @@
Switcheo Python API
===================

.. image:: images/switcheo-logo.png
:width: 150px
:height: 150px
:alt: Switcheo Decentralized Exchange
:align: center

.. raw:: html
<p align="center">
<img
src="https://cdn.techinasia.com/data/images/77fcf2253e4cbdeb793944c2b4ef257c.png"
width="125px;">
</p>

<h1 align="center">switcheo-python</h1>

<p align="center">
Python API for the <b>Switcheo</b> decentralized exchange.
</p>

<p align="center">
<a href="https://img.shields.io/pypi/v/switcheo.svg">
<img src="https://img.shields.io/pypi/v/switcheo.svg">
</a>
<a href="https://travis-ci.org/KeithSSmith/switcheo-python">
<img src="https://travis-ci.org/KeithSSmith/switcheo-python.svg?branch=master">
</a>
<a href="https://switcheo-python.readthedocs.io/en/latest/?badge=latest" rel="nofollow">
<img src="https://readthedocs.org/projects/switcheo-python/badge/?version=latest">
</a>
<a href='https://coveralls.io/github/KeithSSmith/switcheo-python?branch=master'>
<img src='https://coveralls.io/repos/github/KeithSSmith/switcheo-python/badge.svg?branch=master' alt='Coverage Status' />
</a>
<a href="https://img.shields.io/pypi/l/switcheo.svg">
<img src="https://img.shields.io/pypi/l/switcheo.svg">
</a>
</p>

.. image:: https://readthedocs.org/projects/switcheo-python/badge/?version=latest
:target: https://switcheo-python.readthedocs.io/en/latest
:alt: ReadTheDocs
.. image:: https://travis-ci.org/KeithSSmith/switcheo-python.svg?branch=master
:target: https://travis-ci.org/KeithSSmith/switcheo-python
:alt: Travis CI
.. image:: https://coveralls.io/repos/github/KeithSSmith/switcheo-python/badge.svg?branch=master
:target: https://coveralls.io/github/KeithSSmith/switcheo-python?branch=master
:alt: Coveralls
.. image:: https://img.shields.io/pypi/v/switcheo.svg
:target: https://pypi.org/project/switcheo
:alt: PyPi
.. image:: https://img.shields.io/pypi/pyversions/switcheo.svg
:target: https://pypi.org/project/switcheo
:alt: PyPi
.. image:: https://img.shields.io/pypi/l/switcheo.svg
:target: https://img.shields.io/pypi/l/switcheo.svg
:alt: PyPi

Welcome to the Switcheo Python project. The goal of this project is to empower other developers and end users by offering a Python client that is able to interact with all aspects of the Switcheo Decentralized Exchange via its REST endpoints.
When using this package the developer does not have to worry about cryptographic signatures or interacting with the blockchain, that is handled within the client.
Allowing developers and end user to focus on **Trading**!

.. toctree::
:maxdepth: 2
Expand All @@ -47,7 +48,4 @@ Switcheo Python API
usage
clients




.. _MIT: https://github.com/KeithSSmith/switcheo-python/blob/master/LICENSE.md
3 changes: 1 addition & 2 deletions switcheo/neo/test_neo_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@
'outputs': [{'assetId': '602c79718b16e442de58778e148d0b1084e3b2dffd5de6b7b16cee7969282de7', 'scriptHash': 'e707714512577b42f9a011f8b870625429f93573', 'value': 1e-08}],
'scripts': [],
'script': '0800e1f505000000001432e125258b7db0a0dffde5bd03b2b859253538ab14592c8a46a0d06c600f06c994d1f25e7283b8a2fe53c1076465706f73697467823b63e7c70a795a7615a38d1ba67d9e54c195a1',
'gas': 0
}
'gas': 0}

transaction_array = [{
'id': 'e30a7fdf-779c-4623-8f92-8a961450d843',
Expand Down
75 changes: 75 additions & 0 deletions switcheo/neo/test_transactions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import unittest
from switcheo.neo.transactions import serialize_transaction, serialize_transaction_attribute,\
serialize_transaction_input, serialize_transaction_output, serialize_witness, serialize_claim_exclusive,\
serialize_contract_exclusive, serialize_invocation_exclusive


transaction_dict = {'hash': '72b74c96b9174e9b9e1b216f7e8f21a6475e6541876a62614df7c1998c6e8376',
'sha256': '2109cbb5eea67a06f5dd8663e10fcd1128e28df5721a25d993e05fe2097c34f3',
'type': 209,
'version': 1,
'attributes': [{'usage': 32, 'data': '592c8a46a0d06c600f06c994d1f25e7283b8a2fe'},
{'usage': 32, 'data': '6a3d9b359fc17d711017daa6c0e14d6172a791ed'}],
'inputs': [{'prevHash': 'f09b3b697c580d1730cd360da5e1f0beeae00827eb2f0055cbc85a5a4dadd8ea', 'prevIndex': 0},
{'prevHash': 'c858e4d2af1e1525fa974fb2b1678caca1f81a5056513f922789594939ff713d', 'prevIndex': 31}],
'outputs': [{'assetId': '602c79718b16e442de58778e148d0b1084e3b2dffd5de6b7b16cee7969282de7', 'scriptHash': 'e707714512577b42f9a011f8b870625429f93573', 'value': 1e-08}],
'scripts': [],
'script': '0800e1f505000000001432e125258b7db0a0dffde5bd03b2b859253538ab14592c8a46a0d06c600f06c994d1f25e7283b8a2fe53c1076465706f73697467823b63e7c70a795a7615a38d1ba67d9e54c195a1',
'gas': 0}


class TestTransactions(unittest.TestCase):

def test_serialize_transaction(self):
serialized_transaction = 'd101520800e1f505000000001432e125258b7db0a0dffde5bd03b2b859253538ab14592c8a46a0d06c600f06c994d1f25e7283b8a2fe53c1076465706f73697467823b63e7c70a795a7615a38d1ba67d9e54c195a100000000000000000220592c8a46a0d06c600f06c994d1f25e7283b8a2fe206a3d9b359fc17d711017daa6c0e14d6172a791ed02ead8ad4d5a5ac8cb55002feb2708e0eabef0e1a50d36cd30170d587c693b9bf000003d71ff3949598927923f5156501af8a1ac8c67b1b24f97fa25151eafd2e458c81f0001e72d286979ee6cb1b7e65dfddfb2e384100b8d148e7758de42e4168b71792c6001000000000000007335f929546270b8f811a0f9427b5712457107e7'
self.assertEqual(serialize_transaction(transaction=transaction_dict, signed=False), serialized_transaction)

def test_serialize_transaction_attribute(self):
serialized_attributes = []
serialized_attribute_expected_list = ['20592c8a46a0d06c600f06c994d1f25e7283b8a2fe',
'206a3d9b359fc17d711017daa6c0e14d6172a791ed']
for attribute in transaction_dict['attributes']:
serialized_attributes.append(serialize_transaction_attribute(attr=attribute))
self.assertListEqual(serialized_attributes, serialized_attribute_expected_list)

def test_serialize_transaction_input(self):
serialized_inputs = []
serialized_input_expected_list = ['ead8ad4d5a5ac8cb55002feb2708e0eabef0e1a50d36cd30170d587c693b9bf00000',
'3d71ff3949598927923f5156501af8a1ac8c67b1b24f97fa25151eafd2e458c81f00']
for txn_input in transaction_dict['inputs']:
serialized_inputs.append(serialize_transaction_input(txn_input=txn_input))
self.assertListEqual(serialized_inputs, serialized_input_expected_list)

def test_serialize_transaction_output(self):
serialized_outputs = []
serialized_output_expected_list = ['e72d286979ee6cb1b7e65dfddfb2e384100b8d148e7758de42e4168b71792c6001000000000000007335f929546270b8f811a0f9427b5712457107e7']
for txn_output in transaction_dict['outputs']:
serialized_outputs.append(serialize_transaction_output(txn_output=txn_output))
self.assertListEqual(serialized_outputs, serialized_output_expected_list)

def test_serialize_witness(self):
# This is not used by Switcheo and I can't find a good test transaction for this, will pass for now.
pass

def test_serialize_claim_exclusive(self):
with self.assertRaises(ValueError):
serialize_claim_exclusive(transaction=transaction_dict)
# Switcheo will not be allowing for GAS claims so this should never be necessary.
# transaction_claim_dict = transaction_dict.copy()
# transaction_claim_dict['type'] = 2
# self.assertEqual(serialize_claim_exclusive(transaction=transaction_claim_dict), '')

def test_serialize_contract_exclusive(self):
with self.assertRaises(ValueError):
serialize_contract_exclusive(transaction=transaction_dict)
transaction_contract_dict = transaction_dict.copy()
transaction_contract_dict['type'] = 128
self.assertEqual(serialize_contract_exclusive(transaction=transaction_contract_dict), '')

def test_serialize_invocation_exclusive(self):
serialized_invocation = '520800e1f505000000001432e125258b7db0a0dffde5bd03b2b859253538ab14592c8a46a0d06c600f06c994d1f25e7283b8a2fe53c1076465706f73697467823b63e7c70a795a7615a38d1ba67d9e54c195a10000000000000000'
self.assertEqual(serialize_invocation_exclusive(transaction=transaction_dict), serialized_invocation)
transaction_invocation_dict = transaction_dict.copy()
transaction_invocation_dict['type'] = 128
with self.assertRaises(ValueError):
serialize_invocation_exclusive(transaction=transaction_invocation_dict)
18 changes: 13 additions & 5 deletions switcheo/neo/transactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,20 +63,28 @@ def serialize_witness(witness):
return invocation_len + witness['invocationScript'] + verification_len + witness['verificationScript']


# Switcheo does not allow for GAS claims so this function should not be used.
def serialize_claim_exclusive(transaction):
print("serialize claim exclusive")
# pass
if transaction['type'] != 0x02:
raise ValueError(
'The transaction type {} does not match the claim exclusive method.'.format(transaction['type']))
out = num2varint(len(transaction['claims']))
for claim in transaction['claims']:
out += serialize_transaction_input(claim)
return out


def serialize_contract_exclusive(transaction):
if hex(transaction['type']) != 0x80:
exit(4)
if transaction['type'] != 0x80:
raise ValueError(
'The transaction type {} does not match the contract exclusive method.'.format(transaction['type']))
return ''


def serialize_invocation_exclusive(transaction):
if transaction['type'] != 0xd1:
exit(4)
raise ValueError(
'The transaction type {} does not match the invocation exclusive method.'.format(transaction['type']))
out = num2varint(int(len(transaction['script'])/2))
out += transaction['script']
if transaction['version'] >= 1:
Expand Down
18 changes: 8 additions & 10 deletions switcheo/public_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ def get_orders(self, address):
}
return self.request.get(path='/orders', params=order_params)

def get_balance(self, address):
def get_balance(self, addresses, contracts):
"""
Function to fetch the current account balance for the given address in the Switcheo smart contract (i.e. deposited to the trading contract balance).
Execution of this function is as follows::
Expand All @@ -408,16 +408,14 @@ def get_balance(self, address):
'locked': {}
}
:param address: The ScriptHash of the address to retrieve its Smart Contract balance.
:type address: str
:return: Dictionary containing the smart contract account balance by state of deposit.
:param addresses: The ScriptHash of the address(es) to retrieve its Smart Contract balance.
:type addresses: list
:param contracts: The contract hash(es) to retrieve all addresses Smart Contract balance.
:type contracts: list
:return: Dictionary containing the sum of all addresses smart contract balances by processing state.
"""
balance_params = {
"addresses": [
address
],
"contract_hashes": [
self.get_contracts()["NEO"]["V2"]
]
"addresses": addresses,
"contract_hashes": contracts
}
return self.request.get(path='/balances', params=balance_params)

0 comments on commit 23a696f

Please sign in to comment.