Skip to content

Commit

Permalink
Rewritten to class
Browse files Browse the repository at this point in the history
  • Loading branch information
iammortimer committed Jan 10, 2020
1 parent cd729c1 commit 6df01b4
Show file tree
Hide file tree
Showing 7 changed files with 434 additions and 161 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,6 @@ ENV/

# Rope project settings
.ropeproject
arbitrage.py
bot.cfg
bot.py
297 changes: 172 additions & 125 deletions address.py

Large diffs are not rendered by default.

42 changes: 22 additions & 20 deletions asset.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import pywaves
#import pywaves
import logging

class Asset(object):
def __init__(self, assetId):
self.assetId='' if assetId == pywaves.DEFAULT_CURRENCY else assetId
def __init__(self, assetId, pycwaves):
self.pycwaves = pycwaves
self.assetId='' if assetId == self.pycwaves.DEFAULT_CURRENCY else assetId
self.issuer = self.name = self.description = ''
self.quantity = self.decimals = 0
self.reissuable = False
Expand All @@ -26,9 +27,9 @@ def __str__(self):
__repr__ = __str__

def status(self):
if self.assetId!=pywaves.DEFAULT_CURRENCY:
if self.assetId!=self.pycwaves.DEFAULT_CURRENCY:
try:
req = pywaves.wrapper('/transactions/info/%s' % self.assetId)
req = self.pycwaves.wrapper('/transactions/info/%s' % self.assetId)
if req['type'] == 3:
self.issuer = req['sender']
self.quantity = req['quantity']
Expand All @@ -41,19 +42,20 @@ def status(self):
pass

def isSmart(self):
req = pywaves.wrapper('/transactions/info/%s' % self.assetId)
req = self.pycwaves.wrapper('/transactions/info/%s' % self.assetId)

if ('script' in req and req['script']):
return True
else:
return False

class AssetPair(object):
def __init__(self, asset1, asset2):
def __init__(self, asset1, asset2, pycwaves):
self.pycwaves = pycwaves
self.asset1 = asset1
self.asset2 = asset2
self.a1 = pywaves.DEFAULT_CURRENCY if self.asset1.assetId == '' else self.asset1.assetId
self.a2 = pywaves.DEFAULT_CURRENCY if self.asset2.assetId == '' else self.asset2.assetId
self.a1 = self.pycwaves.DEFAULT_CURRENCY if self.asset1.assetId == '' else self.asset1.assetId
self.a2 = self.pycwaves.DEFAULT_CURRENCY if self.asset2.assetId == '' else self.asset2.assetId

def __str__(self):
return 'asset1 = %s\nasset2 = %s' % (self.asset1.assetId, self.asset2.assetId)
Expand All @@ -80,11 +82,11 @@ def second(self):
return self.asset1

def orderbook(self):
req = pywaves.wrapper('/matcher/orderbook/%s/%s' % (self.a1, self.a2), host=pywaves.MATCHER)
req = self.pycwaves.wrapper('/matcher/orderbook/%s/%s' % (self.a1, self.a2), host=self.pycwaves.MATCHER)
return req

def ticker(self):
return pywaves.wrapper('/api/ticker/%s/%s' % (self.a1, self.a2), host=pywaves.DATAFEED)
return self.pycwaves.wrapper('/api/ticker/%s/%s' % (self.a1, self.a2), host=self.pycwaves.DATAFEED)

def last(self):
return str(self.ticker()['24h_close'])
Expand All @@ -111,16 +113,16 @@ def priceVolume(self):
return str(self.ticker()['24h_priceVolume'])

def _getMarketData(self, method, params):
return pywaves.wrapper('%s/%s/%s/%s' % (method, self.a1, self.a2, params), host=pywaves.DATAFEED)
return self.pycwaves.wrapper('%s/%s/%s/%s' % (method, self.a1, self.a2, params), host=self.pycwaves.DATAFEED)

def trades(self, *args):
if len(args)==1:
limit = args[0]
if limit > 0 and limit <= pywaves.MAX_WDF_REQUEST:
if limit > 0 and limit <= self.pycwaves.MAX_WDF_REQUEST:
return self._getMarketData('/api/trades/', '%d' % limit)
else:
msg = 'Invalid request. Limit must be >0 and <= 100'
pywaves.throw_error(msg)
self.pycwaves.throw_error(msg)
return logging.error(msg)
elif len(args)==2:
fromTimestamp = args[0]
Expand All @@ -131,23 +133,23 @@ def candles(self, *args):
if len(args)==2:
timeframe = args[0]
limit = args[1]
if timeframe not in pywaves.VALID_TIMEFRAMES:
if timeframe not in self.pycwaves.VALID_TIMEFRAMES:
msg = 'Invalid timeframe'
pywaves.throw_error(msg)
self.pycwaves.throw_error(msg)
return logging.error(msg)
elif limit > 0 and limit <= pywaves.MAX_WDF_REQUEST:
elif limit > 0 and limit <= self.pycwaves.MAX_WDF_REQUEST:
return self._getMarketData('/api/candles', '%d/%d' % (timeframe, limit))
else:
msg = 'Invalid request. Limit must be >0 and <= 100'
pywaves.throw_error(msg)
self.pycwaves.throw_error(msg)
return logging.error(msg)
elif len(args)==3:
timeframe = args[0]
fromTimestamp = args[1]
toTimestamp = args[2]
if timeframe not in pywaves.VALID_TIMEFRAMES:
if timeframe not in self.pycwaves.VALID_TIMEFRAMES:
msg = 'Invalid timeframe'
pywaves.throw_error(msg)
self.pycwaves.throw_error(msg)
return logging.error(msg)
else:
return self._getMarketData('/api/candles', '%d/%d/%d' % (timeframe, fromTimestamp, toTimestamp))
Expand Down
9 changes: 5 additions & 4 deletions contract.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import requests
import pywaves as pw
#import pywaves as pw

class Contract(object):

def __init__(self, contractAddress, seed):
def __init__(self, contractAddress, seed, pycwaves):
self.pycwaves = pycwaves
self.contractAddress = contractAddress

metaInfo = self.parseContractAddress()
Expand All @@ -15,7 +16,7 @@ def __init__(self, contractAddress, seed):


def parseContractAddress(self):
metaInfo = requests.get(pw.NODE + '/addresses/scriptInfo/' + self.contractAddress + '/meta').json()
metaInfo = requests.get(self.pycwaves.NODE + '/addresses/scriptInfo/' + self.contractAddress + '/meta').json()

return metaInfo['meta']['callableFuncTypes']

Expand All @@ -37,7 +38,7 @@ def generateCode(self, method, parameters, seed):
if len(parameters.keys()) > 0:
callParameters = callParameters[0:len(callParameters) - 2] + '\n'
callParameters += '\t]'
call = 'pw.Address(seed = \'' + seed + '\').invokeScript(\'' + self.contractAddress + '\', \'' + method + '\', parameters, [])'
call = 'self.pycwaves.Address(seed = \'' + seed + '\').invokeScript(\'' + self.contractAddress + '\', \'' + method + '\', parameters, [])'

code = 'def ' + method + '(' + parameterList[0: len(parameterList) - 2] + '):\n\t' + callParameters + '\n\t' + call

Expand Down
15 changes: 8 additions & 7 deletions oracle.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import requests
import pywaves as pw
#import pywaves as pw

class Oracle(object):

def __init__(self, oracleAddress = None, seed = None):
def __init__(self, pycwaves, oracleAddress = None, seed = None):
self.pycwaves = pycwaves
if seed != None:
self.oracleAddress = pw.Address(seed=seed)
self.oracleAddress = self.pycwaves.Address(seed=seed)
else:
self.oracleAddress = oracleAddress

Expand All @@ -20,14 +21,14 @@ def getData(self, key = None, regex = None):
return result

def _getDataWithRegex(self, regex):
print(pw.NODE + '/addresses/data/' + self.oracleAddress + '?matches=' + regex)
return requests.get(pw.NODE + '/addresses/data/' + self.oracleAddress + '?matches=' + regex).json()
print(self.pycwaves.NODE + '/addresses/data/' + self.oracleAddress + '?matches=' + regex)
return requests.get(self.pycwaves.NODE + '/addresses/data/' + self.oracleAddress + '?matches=' + regex).json()

def _getDataWithoutKey(self):
return requests.get(pw.NODE + '/addresses/data/' + self.oracleAddress).json()
return requests.get(self.pycwaves.NODE + '/addresses/data/' + self.oracleAddress).json()

def _getDataWithKey(self, key):
return requests.get(pw.NODE + '/addresses/data/' + self.oracleAddress + '/' + key).json()['value']
return requests.get(self.pycwaves.NODE + '/addresses/data/' + self.oracleAddress + '/' + key).json()['value']

def storeData(self, key, type, dataEntry):
dataToStore = [{
Expand Down
11 changes: 6 additions & 5 deletions order.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import pywaves
#import pywaves

class Order(object):
def __init__(self, orderId, assetPair, address = ''):
def __init__(self, pycwaves, orderId, assetPair, address = ''):
self.pycwaves = pycwaves
self.orderId = orderId
self.assetPair = assetPair
self.address = address
self.matcher = pywaves.MATCHER
self.matcherPublicKey = pywaves.MATCHER_PUBLICKEY
self.matcher = self.pycwaves.MATCHER
self.matcherPublicKey = self.pycwaves.MATCHER_PUBLICKEY
self.status()

def __str__(self):
Expand All @@ -19,7 +20,7 @@ def __str__(self):

def status(self):
try:
req = pywaves.wrapper('/matcher/orderbook/%s/%s/%s' % (pywaves.DEFAULT_CURRENCY if self.assetPair.asset1.assetId=='' else self.assetPair.asset1.assetId, pywaves.DEFAULT_CURRENCY if self.assetPair.asset2.assetId=='' else self.assetPair.asset2.assetId, self.orderId), host=self.matcher)
req = self.pycwaves.wrapper('/matcher/orderbook/%s/%s/%s' % (self.pycwaves.DEFAULT_CURRENCY if self.assetPair.asset1.assetId=='' else self.assetPair.asset1.assetId, self.pycwaves.DEFAULT_CURRENCY if self.assetPair.asset2.assetId=='' else self.assetPair.asset2.assetId, self.orderId), host=self.matcher)
return req['status']
except:
pass
Expand Down
Loading

0 comments on commit 6df01b4

Please sign in to comment.