Saturn.py - source


#1

Saturn.py

Port of Saturn.js to Python.

NOTE

SATURN.PY IS ALREADY WORKING!
DECIMAL PRECISION IS NOT PERFECT,
FEEL FREE TO CONTRIBUTE AND FIX PRECISION.
PLEASE USE AT YOUR OWN RISK WITH SMALL TRADING AMOUNT!

Depencies

  • python3+ https://www.python.org/downloads/
  • pip3 install web3
  • pip3 install eth_account
  • pip3 install ethereum
  • pip3 install pandas
  • pip3 install base58
  • pip3 install ecdsa

Sample Use

NOTE: The biggest difference to saturn.js is, that you have to create 2 instances if you want to use eth and etc! More Code & Examples will follow soon™

#!/usr/bin/env python3
from saturnpy import Saturn

# EDIT / LOAD etcprofile.json or create settings-obj in file.

settings = {
    "blockchain": "ETC",
    "apiUrl": "https://ticker.saturn.network/api/v2/",
    "providerUrl": "https://etc-rpc.binancechain.io",
    "mnemonicKey": "legal winner thank year wave sausage worth useful legal winner thank yellow",
    "testMode": False
}

#you can also add privateKey to settings
settingsETH = {
    "blockchain": "ETH",
    "apiUrl": "https://ticker.saturn.network/api/v2/",
    "providerUrl": "http://localhost:8545",
    "privateKey": "0xKeyToShuttle",
    "testMode": True
}

# Saturn instances
etc = Saturn(settings)
eth = Saturn(settingsETH)

orderbook_saturnetc = etc.query.orderbook("0xac55641cbb734bdf6510d1bbd62e240c2409040f")
orderbook_saturneth = eth.query.orderbook("0xb9440022a095343b440d590fcd2d7a3794bd76c8")

print(orderbook_saturnetc, orderbook_saturneth)

myorders = etc.exchange.ordersForAddress(etc.myaddress)
print(myorders)

etc.exchange.newOrder("0xac55641cbb734bdf6510d1bbd62e240c2409040f", "buy", 5000, 0.0001)

PRIMITIVE BUY-IN BOT EXAMPLE

This primitive sample bot, is buying a token once per hour if
price is lower than buyInMax
this is just an example how to use saturn.py,
trading bots will follow soon™

#!/usr/bin/env python3

import time
from saturnpy import Saturn


settings = {
    "blockchain": "ETC",
    "apiUrl": "https://ticker.saturn.network/api/v2/",
    "providerUrl": "https://etc-rpc.binancechain.io",
    "mnemonicKey": "legal winner thank year wave sausage worth useful legal winner thank yellow",
    "testMode": True
}

tradeset = {
    "token": "0xac55641cbb734bdf6510d1bbd62e240c2409040f",
    "buyAtMax": "0.00019",
    "buyamount": "1000",
    "sleepTime": "3600"
} 

saturn = Saturn(settings)


def checkBuyIn():
    info = saturn.query.getTokenInfo(tradeset["token"])
    bestBuyPrice = float(info["best_sell_price"])
    bestBuyHash = info["best_sell_order_tx"]
    buyAtMax = float(tradeset["buyAtMax"])
    buyAmount = float(tradeset["buyamount"])

    print("bestbuyprice", bestBuyPrice)
    if bestBuyPrice <= buyAtMax:
        print("ORDER IN RANGE, VERIFY")
        order = saturn.query.getOrderByTx(bestBuyHash)
        orderBalance = float(order["balance"])
        awaitedEtherAmount = buyAmount * bestBuyPrice
        print ("AWAITED ETHER AMOUNT", awaitedEtherAmount)

        finalBuyAmount = None

        if buyAmount <= orderBalance:
            #print("ORDER BALANCE OK")
            finalBuyAmount = buyAmount
        elif buyAmount > orderBalance and orderBalance != 0:
            #print("BUYING REST", orderBalance)
            finalBuyAmount = orderBalance

        try:
            newtxhash = saturn.exchange.newTrade(float(finalBuyAmount), bestBuyHash)
            print('NEW BUY @ ' + newtxhash)
            saturn.query.awaitTradeTx(newtxhash)
            
        except Exception as e:
            print(e, bestBuyHash)
    else:
        print("ORDER TOO EXPENSIVE")
    return True

# LOOP
while True:
    checkBuyIn()
    time.sleep(int(tradeset["sleepTime"]))

NOTE: if you want to test this script, you have to set testMode to False or remove they key.

TEST RESULTS


https://blockscout.com/etc/mainnet/tx/0x9b1af87133bc1b21373b5049bff56356e56f7ff92579c5473d6b82e500c0e498/token_transfers
https://blockscout.com/etc/mainnet/tx/0x489514c9eb3a7476e5ade01a41d64a38d7ed6140fca7f1c14fb53355ab6113e3/token_transfers


#2

Please improve documentation, such as pip install instructions, and maybe make a simple trading bot to demo saturn.py’s usage?

Also DM me your ethereum address :sunny:


#3

More code, and ports will follow soon™, its currently still “alpha” version, so be aware.

documentation is also on the way!

edit:

@Neuron
request_manager.py is currently using following origin headers:

payload = {‘Origin’: ‘saturnjs’}

would be cool to have an own origin with saturnpy? what you think?


#4

:snake::snake::snake: DO IT :snake: :snake::snake:


#5

Hurrah hurrah!