Git Product home page Git Product logo

py-etherdelta's Introduction

EtherDelta Python Client

Python client for interacting with the EtherDelta API and Smart Contracts.

Install

Dependencies

  • gcc
  • git
  • Python 3.5+

Install via pip

pip3 install etherdelta

# or

python3 -m pip install etherdelta

or (always latest)

pip3 install git+git://github.com/coincircle/py-etherdelta.git

Getting Started

# import package
import etherdelta

# initialize client
client = etherdelta.Client()

# call methods
token_addr = '0x0d8775f648430679a709e98d2b0cb6250d2887ef'
orders = client.get_orderbook(token_addr)
print(orders)

A ForkDelta client is also available

import etherdelta

# initialize ForkDelta client
client = etherdelta.ForkDeltaClient()

Documentation

Examples

Get account ETH balance

client = etherdelta.Client()
account = '0x85E4B84D784eE9eEB7489F0B0c66B343AF2a0BE5'
bal = client.get_eth_balance(account)
print(bal) # 0.053658783

Get account token balance

client = etherdelta.Client()
account = '0x85E4B84D784eE9eEB7489F0B0c66B343AF2a0BE5'
token_addr = '0x0d8775f648430679a709e98d2b0cb6250d2887ef'
bal = client.get_token_balance(account, token_addr)
print(bal) # 71.464571009031715384

Get account ETH balance on EtherDelta

client = etherdelta.Client()
account = '0x85E4B84D784eE9eEB7489F0B0c66B343AF2a0BE5'
bal = client.get_etherdelta_eth_balance(account)
print(bal) # 0.060271757614136072

Get account token balance on EtherDelta

client = etherdelta.Client()
account = '0x85E4B84D784eE9eEB7489F0B0c66B343AF2a0BE5'
token_addr = '0x0d8775f648430679a709e98d2b0cb6250d2887ef'
bal = client.get_etherdelta_token_balance(account, token_addr)
print(bal) # 0

Get highest block number

client = etherdelta.Client()
number = client.get_block_number()
print(number) # 5018972

Get token address of token symbol

client = etherdelta.Client()
symbol = 'BAT'
token_addr = client.get_token_address(symbol)
print(token_addr) # 0x0d8775f648430679a709e98d2b0cb6250d2887ef

Get ticker data for all tokens

client = etherdelta.Client()
tickers = client.get_tickers()
print(tickers)
# {'ETH_0xec46': {'ask': 0.01, 'quoteVolume': 193.809, 'last': 0.000300001, 'baseVolume': 0.058, 'bid': 0.000300002, 'percentChange': 0, 'tokenAddr': '0xec46f8207d766012454c408de210bcbc2243e71c'}, 'ETH_EMV': {'ask': 0.0004579, 'quoteVolume': 11451.47, 'last': 0.0004579, 'baseVolume': 4.647, 'bid': 0.0004586, 'percentChange': 0.2376, 'tokenAddr': '0xb802b24e0637c2b87d2e8b7784c055bbe921011a'}}

Get ticker data for a token

client = etherdelta.Client()
symbol = 'BAT'
ticker = client.get_ticker(symbol)
print(ticker)
# {'quoteVolume': 2603.107, 'bid': 0.000421, 'baseVolume': 1.135, 'tokenAddr': '0x0d8775f648430679a709e98d2b0cb6250d2887ef', 'last': 0.000477839, 'ask': 0.000477838, 'percentChange': 0.0607}

Get orderbook (all buy and sell orders) for token

client = etherdelta.Client()
token_addr = '0x0d8775f648430679a709e98d2b0cb6250d2887ef'
orders = client.get_orderbook(token_addr)
print(orders)
# {'buys': [{'s': '0x36728e74f49ed1ef7f85e603c8ea4b149f5c427b9f0bfc6bc072ad780486dd7b', 'user': '0x0f8aa39a58adcc3df98d826ac798ab837cc0833c', 'ethAvailableVolume': '6755.671999999999', 'amount': '6755671999999999213568', 'availableVolumeBase': '2876979662150982000', 'tokenGive': '0x0000000000000000000000000000000000000000', 'expires': '5019005', 'amountFilled': None, 'updated': '2018-02-02T19:42:59.089Z', 'nonce': '4500026492', 'id': '9f365bafc972b2e21ed52569075647c4778385c0ce9283cb16630df78ed99f72_buy', 'availableVolume': '6.755671999999999213568e+21', 'r': '0xa3ca2a8fb3773271b073bc45a1b81138d8bf95c82ca877bf407ec3250a934b2a', 'tokenGet': '0x0d8775f648430679a709e98d2b0cb6250d2887ef', 'amountGet': '6755671999999999213568', 'v': 28, 'price': '0.00042586135948444247', 'ethAvailableVolumeBase': '2.876979662150982', 'amountGive': '2876979662150982144'}}

Get order from ID

client = etherdelta.Client()
token_addr = '0x0d8775f648430679a709e98d2b0cb6250d2887ef'
order_id = '6e40fdfc81d58a70405431599a1d5c76d502b3cf02e1936bc36f1e8583c0d2b9_sell'
order = client.get_order(token_addr, order_id)
print(order)
# {'ethAvailableVolumeBase': '4.495', 'availableVolume': '100000000000000', 'expires': '1004639969', 'user': '0x2C34973C4c46f13534C81A893645F347B65c89d6', 'amount': '-100000000000000', 'r': '0xff456276e336d37bee3e59f9c8e46e9dffa4dea73dfe85dcad73df543ebc9ec2', 'price': '44950', 'nonce': '1384390526', 'v': 27, 'id': '6e40fdfc81d58a70405431599a1d5c76d502b3cf02e1936bc36f1e8583c0d2b9_sell', 'tokenGive': '0x0d8775f648430679a709e98d2b0cb6250d2887ef', 'updated': '2017-11-28T21:15:11.423Z', 'availableVolumeBase': '4495000000000000000', 'amountFilled': None, 'ethAvailableVolume': '0.0001', 'amountGive': '100000000000000', 's': '0x166c467778a5f89aa3d9283eca77d5e4d857a6de365da9f25b8ec30446ae08e5', 'tokenGet': '0x0000000000000000000000000000000000000000', 'amountGet': '4495000000000000000'}

Get buy (bids) orderbook for a token

client = etherdelta.Client()
token_addr = '0x0d8775f648430679a709e98d2b0cb6250d2887ef'
orders = client.get_buy_orderbook(token_addr)
print(orders)
# [{'availableVolumeBase': '6115302748878024', 'user': '0x9b3e7f46e4e8894d4bc84bb3200161cc6f0fa0f2', 'tokenGet': '0x0d8775f648430679a709e98d2b0cb6250d2887ef', 'amountGive': '189450000000000000', 'amount': '450000000000000000000', 's': '0x07ec06b8e95e84530755a55aa20d8e16d978592a76d1990ba38f96c35be930af', 'ethAvailableVolumeBase': '0.006115302748878024', 'updated': '2018-02-02T17:40:30.400Z', 'price': '0.000421', 'expires': '5028370', 'id': '0f97c3f4c9d78eb44f20395adfa4ed85b4a5f69389853faa6f2cad2b0c24931e_buy', 'nonce': '2687232062', 'amountFilled': None, 'r': '0xb47fce6954b181d656629f62f7c9ff6a7bd11604a5e26bc7a00a099e396c5138', 'availableVolume': '14525659736052313539', 'amountGet': '450000000000000000000', 'tokenGive': '0x0000000000000000000000000000000000000000', 'ethAvailableVolume': '14.525659736052313', 'v': 27}]

Get sell (asks) orderbook for a token

client = etherdelta.Client()
token_addr = '0x0d8775f648430679a709e98d2b0cb6250d2887ef'
orders = client.get_sell_orderbook(token_addr)
print(orders)
# [{'user': '0x955051F2cF3bA245ae8Ee9057458836eAe3b1FeC', 'expires': '5018717', 'amount': '-2.1572374771676692e+21', 'ethAvailableVolumeBase': '1.0308100416148447', 'tokenGet': '0x0000000000000000000000000000000000000000', 'ethAvailableVolume': '2157.237477167669', 'updated': '2018-02-02T18:15:21.791Z', 'price': '0.000477838', 'r': '0xe3129e0ec2110063d16d84ac4770f402555614d077b6cfd1ba9d701839f0691d', 'availableVolumeBase': '1030810041614844700', 'v': 28, 'availableVolume': '2.15723747716766907792023752118211285330018e+21', 'amountGet': '1030810041614844700', 'id': 'b66abf9a645756ef32aff132d6dde19ad7d7b2c5c026475c60140da266186a01_sell', 'nonce': '26698014251852476', 'tokenGive': '0x0d8775f648430679a709e98d2b0cb6250d2887ef', 's': '0x38a06acd697cb5cf91f9c8d19389904331b1014a0713a11f775f632d7e7e4dc3', 'amountFilled': None, 'amountGive': '2.157237477167669064104e+21'}]

Get amount filled for an order

client = etherdelta.Client()
token_addr = '0x0d8775f648430679a709e98d2b0cb6250d2887ef'
order_id = '6e40fdfc81d58a70405431599a1d5c76d502b3cf02e1936bc36f1e8583c0d2b9_sell'
filled = client.get_amount_filled(token_addr, order_id)
print(filled) # 0

Get available volume of an order

client = etherdelta.Client()
token_addr = '0x0d8775f648430679a709e98d2b0cb6250d2887ef'
order_id = '6e40fdfc81d58a70405431599a1d5c76d502b3cf02e1936bc36f1e8583c0d2b9_sell'
volume = client.get_available_volume(token_addr, order_id)
print(volume) # 4495000000000000000

Make a trade

import etherdelta

client = etherdelta.Client()

# Get Buy Order Book
token_addr = '0x0d8775f648430679a709e98d2b0cb6250d2887ef'
orders = client.get_buy_orderbook(token_addr)

print(orders)

# Make a Trade
order_id = '959690a3d665ce5e77c76df4273170bae5e57f990436d4b909e9656f64e221f7_buy'
order = client.get_order(token_addr, order_id)

expires = int(order['expires'])
token_price = 0.0001
token_amount = 1
token_address = order['tokenGet']
priv_key = '123...'
randomseed = True

signed_order = client.create_order('buy', expires, token_price, token_amount, token_address, randomseed, priv_key)

eth_amount = 0.0001
tx = client.trade(order, eth_amount, priv_key)
print(tx)

Other methods available

  • withdraw()
  • withdraw_token()
  • deposit()
  • deposit_token()
  • approve_deposit()

Development

Install Web3.py

sudo pip3 install git+git://github.com/ethereum/web3.py.git

Install dependencies

pip3 install -r requirements.txt

FAQ

  • Q: Why do I get empty results sometimes?

    • A: Unfortunately, the EtherDelta websocket client is unreliable.
  • Q: Why doesn't get_ticker() return any data?

    • A: For some reason the EtherDelta API doesn't return this data anymore.
  • Q: Why doesn't get_token_address() work?

    • A: This method is dependent on the get_ticker() method. See above question.
  • Q: Help! I'm getting a bunch of install errors!

    • A: Please report it in the Issues.

Resources

License

MIT

py-etherdelta's People

Contributors

markguo40 avatar miguelmota avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

py-etherdelta's Issues

Convert Scientific Notation to Int Precision Problem

I was using the client.trade() function to buy Cobinhood, here is the sell order:

  "sells": {
    "amount": "-2.367e+21", 
    "amountFilled": null, 
    "amountGet": "447363000000000000", 
    "amountGive": "2.367e+21", 
    "availableVolume": "2.362e+21", 
    "availableVolumeBase": "446418000000000000", 
    "ethAvailableVolume": "2362", 
    "ethAvailableVolumeBase": "0.446418", 
    "expires": "5292321", 
    "id": "11fe2d956845bb5fc2ca05b6d655f04c43b5caaaac798a95f157d3d1964a853e_sell", 
    "nonce": "3592513099", 
    "price": "0.000189", 
    "r": "0x16a39aa3d4c1494799f5130530425b989a52b82d48e007f8daa1a49e991e528c", 
    "s": "0x3084e4f66ac78795ce688451fda68840e5373e9e2fbcbc613f3208f9e03b766a", 
    "tokenGet": "0x0000000000000000000000000000000000000000", 
    "tokenGive": "0xb2f7eb1f2c37645be61d73953035360e768d81e6", 
    "updated": "2018-03-19T11:02:08.951Z", 
    "user": "0xa330a750abca19b79640ee7db1b9d06226b6486d", 
    "v": 28
  }

As you can see the amount give is 2.367e+21 which should be 2367 cobinhood (18 decimal). However when you convert this scientific notation to int. Here is result:

>>> int(float("2.367e+21"))
2367000000000000262144

This will end up with hex value: 00000000000000000000000000000000000000000000008050b94718b5a00000

The correct amount give should be 2367000000000000000000 with hex value: 00000000000000000000000000000000000000000000008050b94718b59c0000

This leads to a transaction failure.

I do the following precision test:

>>> int(float("2.367e+6"))
2367000
>>> int(float("2.367e+10"))
23670000000
>>> int(float("2.367e+20"))
236700000000000000000
>>> int(float("2.367e+21"))
2367000000000000262144
>>> int(float("2.367e+26"))
236700000000000012188647424

As you can see that starting from 10 to 21 power, the results are wrong.

FileNoteFoundError: Can't find token.json

Am I missing a dependency or something here when calling the Client function? Here is the error message:

Traceback (most recent call last):
File "C:\Users\Vik\eclipse-workspace\LedgerCapital\EtherDelta_init_.py", line 2, in
client = etherdelta.Client()
File "C:\Users\Vik\AppData\Local\Programs\Python\Python36-32\lib\site-packages\etherdelta_init_.py", line 33, in init
with open(os.path.join(os.path.dirname(file), '../contracts/token.json'), 'r') as token_abi_definition:
FileNotFoundError: [Errno 2] No such file or directory: 'C:\Users\Vik\AppData\Local\Programs\Python\Python36-32\lib\site-packages\etherdelta\../contracts/token.json'

On the following simple code:

import etherdelta
client = etherdelta.Client()
account = '0x85E4B84D784eE9eEB7489F0B0c66B343AF2a0BE5'
bal = client.get_eth_balance(account)
print(bal)

Thanks for the help. Appreciate the work.

Getting error when installing etherdelta module

All goes well, but at some moment I am getting error. Any ideas?

copying cytoolz\tests\test_utils.py -> build\lib.win-amd64-3.5\cytoolz\tests
running build_ext
building 'cytoolz.dicttoolz' extension
error: [WinError 2] The system cannot find the file specified

----------------------------------------

Command ""c:\program files\python 3.5\python.exe" -u -c "import setuptools, tokenize;file='C:\Users\Admin\AppData\Local\Temp\pip-install-kwqc1u6s\cytoolz\setup.py';f=getattr(tokenize, 'open', open)(file);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, file, 'exec'))" install --record C:\Users\Admin\AppData\Local\Temp\pip-record-qo01u2xc\install-record.txt --single-version-externally-managed --compile" failed with error code 1 in C:\Users\Admin\AppData\Local\Temp\pip-install-kwqc1u6s\cytoolz\

packages incompatible

When install this package I recieve a incompatiblity error:

rlp 1.0.1 has requirement eth-utils<2,>=1.0.2, but you'll have eth-utils 1.0.1 which is incompatible. eth-account 0.2.2 has requirement eth-utils<2,>=1.0.2, but you'll have eth-utils 1.0.1 which is incompatible.

Documentation?

Hello,

Will any documentation be added, this looks to be a very promising library and I would love to know how to use it (as an ameteur).

Thanks,
Joseph.

DeprecationWarning because of the rename from ethereum-utils to eth-utils

Traceback (most recent call last):
File "app.py", line 2, in
import etherdelta
File "/anaconda3/lib/python3.6/site-packages/etherdelta/init.py", line 12, in
import web3
File "/anaconda3/lib/python3.6/site-packages/web3/init.py", line 7, in
from eth_account import Account # noqa: E402
File "/anaconda3/lib/python3.6/site-packages/eth_account/init.py", line 1, in
from eth_account.account import Account # noqa: F401
File "/anaconda3/lib/python3.6/site-packages/eth_account/account.py", line 10, in
from eth_keyfile import (
File "/anaconda3/lib/python3.6/site-packages/eth_keyfile/init.py", line 7, in
from eth_keyfile.keyfile import ( # noqa: F401
File "/anaconda3/lib/python3.6/site-packages/eth_keyfile/keyfile.py", line 10, in
from eth_keys import keys
File "/anaconda3/lib/python3.6/site-packages/eth_keys/init.py", line 15, in
from .main import ( # noqa: F401
File "/anaconda3/lib/python3.6/site-packages/eth_keys/main.py", line 3, in
from eth_keys.datatypes import (
File "/anaconda3/lib/python3.6/site-packages/eth_keys/datatypes.py", line 8, in
from eth_utils import (
File "/anaconda3/lib/python3.6/site-packages/eth_utils/init.py", line 2, in
"This library has been renamed to eth-utils. The ethereum-utils "
DeprecationWarning: This library has been renamed to eth-utils. The ethereum-utils package will no longer recieve updates. Please update your dependencies accordingly.

Stop Print Output

Is there any way to disable this library from printing to console?

<<< Did not place order because available volume too low. >>>

I spend few hours trying to post order with
client.create_order + client.post
All I get back is "Did not place order because available volume too low." message.
googling this message shows that few ppl have such error as well. Anyone was able to get past this one ?

Requirements

Hello,
I struggle with the requirements/installation process. Cant find big-red and setupfiles also not goong through or is it the wrong requirements file I am using (python 2.7, 3.5, 3,6 trief? I am on mac, but fresh ubuntu available as well, what do I need from a fresh install to get it running? Help would be very appreciated!

Unable to use ForkDeltaClient

Code:
client = etherdelta.ForkDeltaClient()
token_addr = '0x0d8775f648430679a709e98d2b0cb6250d2887ef'
orders = client.get_buy_orderbook(token_addr)
print(orders)

However this gives error:
Traceback (most recent call last):
File "c:/FunProjects/Crypto/A_ForkDelta - Copy.py", line 33, in
client = etherdelta.ForkDeltaClient()
AttributeError: module 'etherdelta' has no attribute 'ForkDeltaClient'

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.