Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
PyWaves authored Apr 2, 2017
1 parent 2b85987 commit 9e0ee09
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 13 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ __pywaves.Address(address, publicKey, privateKey, seed)__ _Creates a new Address

`cancelOrder(assetPair, order)` cancel an order

`buy(assetPair, price, amount, maxLifetime=30*86400, matcherFee=DEFAULT_MATCHER_FEE)` post a buy order
`buy(assetPair, amount price, maxLifetime=30*86400, matcherFee=DEFAULT_MATCHER_FEE)` post a buy order

`sell(assetPair, price, amount, maxLifetime=30*86400, matcherFee=DEFAULT_MATCHER_FEE)` post a sell order
`sell(assetPair, amount, price, maxLifetime=30*86400, matcherFee=DEFAULT_MATCHER_FEE)` post a sell order

`lease(recipient, amount, txFee=DEFAULT_LEASE_FEE)` post a lease transaction

Expand Down Expand Up @@ -232,18 +232,18 @@ pw.setMatcher(node = 'http://127.0.0.1:6886')
BTC = pw.Asset('4ZzED8WJXsvuo2MEm2BmZ87Azw8Sx7TVC6ufSUA5LyTV')
USD = pw.Asset('6wuo2hTaDyPQVceETj1fc5p4WoMVCGMYNASN8ym4BGiL')
BTC_USD = pw.AssetPair(BTC, USD)
myOrder = myAddress.buy(assetPair = BTC_USD, price = 950.75, amount = 15)
myOrder = myAddress.buy(assetPair = BTC_USD, amount = 15, price = 950.75)

# post a sell order
WCT = pw.Asset('6wuo2hTaDyPQVceETj1fc5p4WoMVCGMYNASN8ym4BGiL')
Incent = pw.Asset('FLbGXzrpqkvucZqsHDcNxePTkh2ChmEi4GdBfDRRJVof')
WCT_Incent = pw.AssetPair(WCT, Incent)
myOrder = myAddress.sell(assetPair = WCT_Incent, price = 2.50, amount = 100)
myOrder = myAddress.sell(assetPair = WCT_Incent, amount = 100, price = 2.50)

# post a buy order using Waves as price asset
BTC = pw.Asset('4ZzED8WJXsvuo2MEm2BmZ87Azw8Sx7TVC6ufSUA5LyTV')
BTC_WAVES = pw.AssetPair(BTC, pw.WAVES)
myOrder = myAddress.buy(assetPair = BTC_WAVES, price = 5000, amount = 1)
myOrder = myAddress.buy(assetPair = BTC_WAVES, amount = 1, price = 5000)

# cancel an order
myOrder.cancel()
Expand Down
36 changes: 30 additions & 6 deletions address.py
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ def sendAsset(self, recipient, asset, amount, attachment='', txFee=pywaves.DEFAU
})
return pywaves.wrapper('/assets/broadcast/transfer', data)

def _postOrder(self, amountAsset, priceAsset, orderType, price, amount, maxLifetime=30*86400, matcherFee=pywaves.DEFAULT_MATCHER_FEE):
def _postOrder(self, amountAsset, priceAsset, orderType, amount, price, maxLifetime=30*86400, matcherFee=pywaves.DEFAULT_MATCHER_FEE):
timestamp = int(time.time() * 1000) -1000
expiration = timestamp + maxLifetime * 1000
asset1 = b'\0' if amountAsset.assetId=='' else b'\1' + base58.b58decode(amountAsset.assetId)
Expand Down Expand Up @@ -464,26 +464,26 @@ def cancelOrder(self, assetPair, order):
"orderId": order.orderId,
"signature": signature
})
req = pywaves.wrapper('/matcher/orderbook/%s/%s/cancel' % (assetPair.asset1.assetId, assetPair.asset2.assetId), data, host=pywaves.MATCHER)
req = pywaves.wrapper('/matcher/orderbook/%s/%s/cancel' % ('WAVES' if assetPair.asset1.assetId=='' else assetPair.asset1.assetId, 'WAVES' if assetPair.asset2.assetId=='' else assetPair.asset2.assetId), data, host=pywaves.MATCHER)
id = -1
if req['status'] == 'OrderCanceled':
id = req['orderId']
logging.info('Order Cancelled - ID: %s' % id)
return id

def buy(self, assetPair, price, amount, maxLifetime=30 * 86400, matcherFee=pywaves.DEFAULT_MATCHER_FEE):
def buy(self, assetPair, amount, price, maxLifetime=30 * 86400, matcherFee=pywaves.DEFAULT_MATCHER_FEE):
assetPair.refresh()
normPrice = int(10. ** assetPair.asset2.decimals * price)
normAmount = int(10. ** assetPair.asset1.decimals * amount)
id = self._postOrder(assetPair.asset1, assetPair.asset2, b'\0', normPrice, normAmount, maxLifetime, matcherFee)
id = self._postOrder(assetPair.asset1, assetPair.asset2, b'\0', normAmount, normPrice, maxLifetime, matcherFee)
if id != -1:
return pywaves.Order(id, assetPair, self)

def sell(self, assetPair, price, amount, maxLifetime=30 * 86400, matcherFee=pywaves.DEFAULT_MATCHER_FEE):
def sell(self, assetPair, amount, price, maxLifetime=30 * 86400, matcherFee=pywaves.DEFAULT_MATCHER_FEE):
assetPair.refresh()
normPrice = int(10. ** assetPair.asset2.decimals * price)
normAmount = int(10. ** assetPair.asset1.decimals * amount)
id = self._postOrder(assetPair.asset1, assetPair.asset2, b'\1', normPrice, normAmount, maxLifetime, matcherFee)
id = self._postOrder(assetPair.asset1, assetPair.asset2, b'\1', normAmount, normPrice, maxLifetime, matcherFee)
if id!=-1:
return pywaves.Order(id, assetPair, self)

Expand Down Expand Up @@ -537,5 +537,29 @@ def leaseCancel(self, leaseId, txFee=pywaves.DEFAULT_LEASE_FEE):
if 'leaseId' in req:
return req['leaseId']

def createAlias(self, alias, txFee=pywaves.DEFAULT_LEASE_FEE):
if not self.privateKey:
logging.error('Private key required')
elif self.balance() < txFee:
logging.error('Insufficient Waves balance')
else:
timestamp = int(time.time() * 1000)
sData = b'\x0A' + \
base58.b58decode(self.publicKey) + \
struct.pack(">H", len(alias)) + \
crypto.str2bytes(alias) + \
struct.pack(">Q", txFee) + \
struct.pack(">Q", timestamp)
signature = crypto.sign(self.privateKey, sData)
data = json.dumps({
"senderPublicKey": self.publicKey,
"alias": alias,
"fee": txFee,
"timestamp": timestamp,
"signature": signature
})
print data
req = pywaves.wrapper('/alias/broadcast/create', data)
print req


2 changes: 1 addition & 1 deletion asset.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def __str__(self):
'name = %s\n' \
'description = %s\n' \
'quantity = %d\n' \
'ecimals = %d\n' \
'decimals = %d\n' \
'reissuable = %s' % (self.status(), self.assetId, self.issuer, self.name, self.description, self.quantity, self.decimals, self.reissuable)

__repr__ = __str__
Expand Down
2 changes: 1 addition & 1 deletion order.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def __str__(self):

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

0 comments on commit 9e0ee09

Please sign in to comment.