Exemplo n.º 1
0
    def setScript(self,
                  scriptSource,
                  txFee=yuwaves.DEFAULT_SCRIPT_FEE,
                  timestamp=0):
        script = yuwaves.wrapper('/utils/script/compile',
                                 scriptSource)['script'][7:]
        if not self.privateKey:
            logging.error('Private key required')
        else:
            compiledScript = base64.b64decode(bytes(script))
            scriptLength = len(compiledScript)
            if timestamp == 0:
                timestamp = int(time.time() * 1000)
            sData = b'\x0d' + \
                b'\1' + \
                crypto.str2bytes(bytes(yuwaves.CHAIN_ID)) + \
                base58.b58decode(bytes(self.publicKey)) + \
                b'\1' + \
                struct.pack(">H", scriptLength) + \
                compiledScript + \
                struct.pack(">Q", txFee) + \
                struct.pack(">Q", timestamp)
            signature = crypto.sign(self.privateKey, sData)

            data = json.dumps({
                "type": 13,
                "version": 1,
                "senderPublicKey": self.publicKey,
                "fee": txFee,
                "timestamp": timestamp,
                "script": 'base64:' + script,
                "proofs": [signature]
            })

            return yuwaves.wrapper('/transactions/broadcast', data)
Exemplo n.º 2
0
 def issueSmartAsset(self,
                     name,
                     description,
                     quantity,
                     scriptSource,
                     decimals=0,
                     reissuable=False,
                     txFee=yuwaves.DEFAULT_ASSET_FEE):
     script = yuwaves.wrapper('/utils/script/compile',
                              scriptSource)['script'][7:]
     if not self.privateKey:
         msg = 'Private key required'
         logging.error(msg)
         yuwaves.throw_error(msg)
     elif len(name) < 4 or len(name) > 16:
         msg = 'Asset name must be between 4 and 16 characters long'
         logging.error(msg)
         yuwaves.throw_error(msg)
     else:
         compiledScript = base64.b64decode(script)
         scriptLength = len(compiledScript)
         timestamp = int(time.time() * 1000)
         sData = b'\3' + \
                 b'\2' + \
                 crypto.str2bytes(str(yuwaves.CHAIN_ID)) + \
                 base58.b58decode(self.publicKey) + \
                 struct.pack(">H", len(name)) + \
                 crypto.str2bytes(name) + \
                 struct.pack(">H", len(description)) + \
                 crypto.str2bytes(description) + \
                 struct.pack(">Q", quantity) + \
                 struct.pack(">B", decimals) + \
                 (b'\1' if reissuable else b'\0') + \
                 struct.pack(">Q", txFee) + \
                 struct.pack(">Q", timestamp) + \
                 b'\1' + \
                 struct.pack(">H", scriptLength) + \
                 compiledScript
         signature = crypto.sign(self.privateKey, sData)
         data = json.dumps({
             "type": 3,
             "senderPublicKey": self.publicKey,
             "name": name,
             "version": 2,
             "quantity": quantity,
             "timestamp": timestamp,
             "description": description,
             "decimals": decimals,
             "reissuable": reissuable,
             "fee": txFee,
             "proofs": [signature],
             "script": 'base64:' + script
         })
         req = yuwaves.wrapper('/transactions/broadcast', data)
         if yuwaves.OFFLINE:
             return req
         else:
             return req
Exemplo n.º 3
0
 def balance(self, assetId='', confirmations=0):
     try:
         if assetId:
             return yuwaves.wrapper('/assets/balance/%s/%s' %
                                    (self.address, assetId))['balance']
         else:
             return yuwaves.wrapper(
                 '/addresses/balance/%s%s' %
                 (self.address, '' if confirmations == 0 else '/%d' %
                  confirmations))['balance']
     except:
         return 0
Exemplo n.º 4
0
    def cancelOrder(self, assetPair, order):
        if not yuwaves.OFFLINE:
            if order.status() == 'Filled':
                msg = "Order already filled"
                logging.error(msg)
                yuwaves.throw_error(msg)

            elif not order.status():
                msg = "Order not found"
                logging.error(msg)
                yuwaves.throw_error(msg)
        sData = base58.b58decode(bytes(self.publicKey)) + \
                base58.b58decode(bytes(order.orderId))
        signature = crypto.sign(self.privateKey, sData)
        data = json.dumps({
            "sender": self.publicKey,
            "orderId": order.orderId,
            "signature": signature
        })
        req = yuwaves.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=yuwaves.MATCHER)
        if yuwaves.OFFLINE:
            return req
        else:
            id = -1
            if req['status'] == 'OrderCanceled':
                id = req['orderId']
                logging.info('Order Cancelled - ID: %s' % id)
            return id
Exemplo n.º 5
0
 def leaseCancel(self,
                 leaseId,
                 txFee=yuwaves.DEFAULT_LEASE_FEE,
                 timestamp=0):
     if not self.privateKey:
         msg = 'Private key required'
         logging.error(msg)
         yuwaves.throw_error(msg)
     elif not yuwaves.OFFLINE and self.balance() < txFee:
         msg = 'Insufficient Waves balance'
         logging.error(msg)
         yuwaves.throw_error(msg)
     else:
         if timestamp == 0:
             timestamp = int(time.time() * 1000)
         sData = b'\x09' + \
                 base58.b58decode(bytes(self.publicKey)) + \
                 struct.pack(">Q", txFee) + \
                 struct.pack(">Q", timestamp) + \
                 base58.b58decode(bytes(leaseId))
         signature = crypto.sign(self.privateKey, sData)
         data = json.dumps({
             "senderPublicKey": self.publicKey,
             "txId": leaseId,
             "fee": txFee,
             "timestamp": timestamp,
             "signature": signature
         })
         req = yuwaves.wrapper('/leasing/broadcast/cancel', data)
         if yuwaves.OFFLINE:
             return req
         elif 'leaseId' in req:
             return req['leaseId']
Exemplo n.º 6
0
 def createAlias(self, alias, txFee=yuwaves.DEFAULT_ALIAS_FEE, timestamp=0):
     aliasWithNetwork = b'\x02' + crypto.str2bytes(bytes(
         yuwaves.CHAIN_ID)) + struct.pack(
             ">H", len(alias)) + crypto.str2bytes(alias)
     if not self.privateKey:
         msg = 'Private key required'
         logging.error(msg)
         yuwaves.throw_error(msg)
     else:
         if timestamp == 0:
             timestamp = int(time.time() * 1000)
         sData = b'\x0a' + \
                 base58.b58decode(bytes(self.publicKey)) + \
                 struct.pack(">H", len(aliasWithNetwork)) + \
                 crypto.str2bytes(bytes(aliasWithNetwork)) + \
                 struct.pack(">Q", txFee) + \
                 struct.pack(">Q", timestamp)
         signature = crypto.sign(self.privateKey, sData)
         data = json.dumps({
             "alias": alias,
             "senderPublicKey": self.publicKey,
             "fee": txFee,
             "timestamp": timestamp,
             "signature": signature
         })
         return yuwaves.wrapper('/alias/broadcast/create', data)
Exemplo n.º 7
0
    def sponsorAsset(self,
                     assetId,
                     minimalFeeInAssets,
                     txFee=yuwaves.DEFAULT_SPONSOR_FEE,
                     timestamp=0):
        if not self.privateKey:
            logging.error('Private key required')
        else:
            if timestamp == 0:
                timestamp = int(time.time() * 1000)
            sData = b'\x0e' + \
                b'\1' + \
                base58.b58decode(bytes(self.publicKey)) + \
                base58.b58decode(bytes(assetId)) + \
                struct.pack(">Q", minimalFeeInAssets) + \
                struct.pack(">Q", txFee) + \
                struct.pack(">Q", timestamp)
            signature = crypto.sign(self.privateKey, sData)

            data = json.dumps({
                "type": 14,
                "version": 1,
                "senderPublicKey": self.publicKey,
                "assetId": assetId,
                "fee": txFee,
                "timestamp": timestamp,
                "minSponsoredAssetFee": minimalFeeInAssets,
                "proofs": [signature]
            })

            return yuwaves.wrapper('/transactions/broadcast', data)
Exemplo n.º 8
0
 def reissueAsset(self,
                  Asset,
                  quantity,
                  reissuable=False,
                  txFee=yuwaves.DEFAULT_TX_FEE):
     timestamp = int(time.time() * 1000)
     sData = b'\5' + \
             base58.b58decode(bytes(self.publicKey)) + \
             base58.b58decode(bytes(Asset.assetId)) + \
             struct.pack(">Q", quantity) + \
             (b'\1' if reissuable else b'\0') + \
             struct.pack(">Q",txFee) + \
             struct.pack(">Q", timestamp)
     signature = crypto.sign(self.privateKey, sData)
     data = json.dumps({
         "senderPublicKey": self.publicKey,
         "assetId": Asset.assetId,
         "quantity": quantity,
         "timestamp": timestamp,
         "reissuable": reissuable,
         "fee": txFee,
         "signature": signature
     })
     req = yuwaves.wrapper('/assets/broadcast/reissue', data)
     if yuwaves.OFFLINE:
         return req
     else:
         return req.get('id', 'ERROR')
Exemplo n.º 9
0
    def __init__(self,
                 address='',
                 publicKey='',
                 privateKey='',
                 seed='',
                 alias='',
                 nonce=0):
        if nonce < 0 or nonce > 4294967295:
            raise ValueError('Nonce must be between 0 and 4294967295')

        if alias and not yuwaves.OFFLINE:
            address = yuwaves.wrapper('/alias/by-alias/%s' % alias).get(
                "address", "")

        if not alias and not address and not publicKey and not privateKey and not seed:
            from seed import generate_seed
            seed = generate_seed()

        self.seed = seed
        self.nonce = nonce

        self.address, self.publicKey, self.privateKey = crypto.generate_key(
            address=address,
            public_key=publicKey,
            private_key=privateKey,
            seed=seed,
            nonce=nonce)

        if self.address and not yuwaves.validateAddress(self.address):
            raise ValueError("Invalid address")

        if not yuwaves.OFFLINE:
            self.aliases = self.aliases()
Exemplo n.º 10
0
 def deleteOrderByID(self, assetPair, orderId):
     sData = base58.b58decode(bytes(self.publicKey)) + \
             base58.b58decode(bytes(orderId))
     signature = crypto.sign(self.privateKey, sData)
     data = json.dumps({
         "sender": self.publicKey,
         "orderId": orderId,
         "signature": signature
     })
     yuwaves.wrapper(
         '/matcher/orderbook/%s/%s/delete' %
         ('WAVES' if assetPair.asset1.assetId == '' else
          assetPair.asset1.assetId, 'WAVES'
          if assetPair.asset2.assetId == '' else assetPair.asset2.assetId),
         data,
         host=yuwaves.MATCHER)
Exemplo n.º 11
0
 def cancelOpenOrders(self, assetPair):
     orders = self.getOrderHistory(assetPair)
     for order in orders:
         status = order['status']
         orderId = order['id']
         if status == 'Accepted' or status == 'PartiallyFilled':
             sData = base58.b58decode(bytes(self.publicKey)) + \
                     base58.b58decode(bytes(orderId))
             signature = crypto.sign(self.privateKey, sData)
             data = json.dumps({
                 "sender": self.publicKey,
                 "orderId": orderId,
                 "signature": signature
             })
             yuwaves.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=yuwaves.MATCHER)
Exemplo n.º 12
0
    def sendWaves(self,
                  recipient,
                  amount,
                  attachment='',
                  txFee=yuwaves.DEFAULT_TX_FEE,
                  timestamp=0):
        if not self.privateKey:
            msg = 'Private key required'
            logging.error(msg)
            yuwaves.throw_error(msg)

        elif amount <= 0:
            msg = 'Amount must be > 0'
            logging.error(msg)
            yuwaves.throw_error(msg)
        elif not yuwaves.OFFLINE and self.balance() < amount + txFee:
            msg = 'Insufficient Waves balance'
            logging.error(msg)
            yuwaves.throw_error(msg)

        else:
            if timestamp == 0:
                timestamp = int(time.time() * 1000)
            sData = b'\4' + \
                    base58.b58decode(bytes(self.publicKey)) + \
                    b'\0\0' + \
                    struct.pack(">Q", timestamp) + \
                    struct.pack(">Q", amount) + \
                    struct.pack(">Q", txFee) + \
                    base58.b58decode(bytes(recipient.address)) + \
                    struct.pack(">H", len(attachment)) + \
                    crypto.str2bytes(attachment)
            signature = crypto.sign(self.privateKey, sData)
            data = json.dumps({
                "senderPublicKey":
                self.publicKey,
                "recipient":
                recipient.address,
                "amount":
                amount,
                "fee":
                txFee,
                "timestamp":
                timestamp,
                "attachment":
                base58.b58encode(crypto.str2bytes(attachment)),
                "signature":
                signature
            })

            return yuwaves.wrapper('/assets/broadcast/transfer', data)
Exemplo n.º 13
0
 def issueAsset(self,
                name,
                description,
                quantity,
                decimals=0,
                reissuable=False,
                txFee=yuwaves.DEFAULT_ASSET_FEE):
     if not self.privateKey:
         msg = 'Private key required'
         logging.error(msg)
         yuwaves.throw_error(msg)
     elif len(name) < 4 or len(name) > 16:
         msg = 'Asset name must be between 4 and 16 characters long'
         logging.error(msg)
         yuwaves.throw_error(msg)
     else:
         timestamp = int(time.time() * 1000)
         sData = b'\3' + \
                 base58.b58decode(bytes(self.publicKey)) + \
                 struct.pack(">H", len(name)) + \
                 crypto.str2bytes(name) + \
                 struct.pack(">H", len(description)) + \
                 crypto.str2bytes(description) + \
                 struct.pack(">Q", quantity) + \
                 struct.pack(">B", decimals) + \
                 (b'\1' if reissuable else b'\0') + \
                 struct.pack(">Q", txFee) + \
                 struct.pack(">Q", timestamp)
         signature = crypto.sign(self.privateKey, sData)
         data = json.dumps({
             "senderPublicKey": self.publicKey,
             "name": name,
             "quantity": quantity,
             "timestamp": timestamp,
             "description": description,
             "decimals": decimals,
             "reissuable": reissuable,
             "fee": txFee,
             "signature": signature
         })
         req = yuwaves.wrapper('/assets/broadcast/issue', data)
         if yuwaves.OFFLINE:
             return req
         else:
             return yuwaves.Asset(req['assetId'])
Exemplo n.º 14
0
 def tradableBalance(self, assetPair):
     try:
         req = yuwaves.wrapper(
             '/matcher/orderbook/%s/%s/tradableBalance/%s' %
             ('WAVES' if assetPair.asset1.assetId == '' else
              assetPair.asset1.assetId, 'WAVES' if assetPair.asset2.assetId
              == '' else assetPair.asset2.assetId, self.address),
             host=yuwaves.MATCHER)
         if yuwaves.OFFLINE:
             return req
         amountBalance = req['WAVES' if assetPair.asset1.assetId ==
                             '' else assetPair.asset1.assetId]
         priceBalance = req['WAVES' if assetPair.asset2.assetId ==
                            '' else assetPair.asset2.assetId]
     except:
         amountBalance = 0
         priceBalance = 0
     if not yuwaves.OFFLINE:
         return amountBalance, priceBalance
Exemplo n.º 15
0
    def update(self):
        if self.assetId == '':
            self._quantity = 100000000e8
            self._decimals = 8
            self._issuer = ''
            self._name = ''
            self._description = ''
            self._reissuable = False
            self._script = ''
            return

        req = yuwaves.wrapper('/assets/details/%s' % self.assetId)
        self._name = req['name'].encode('ascii', 'ignore')
        self._description = req['description'].encode('ascii', 'ignore')
        self._quantity = req['quantity']
        self._decimals = req['decimals']
        self._issuer = req['issuer']
        self._reissuable = req['reissuable']
        self._scripted = req['scripted']
Exemplo n.º 16
0
 def cancelOrderByID(self, assetPair, orderId):
     sData = base58.b58decode(bytes(self.publicKey)) + \
             base58.b58decode(bytes(orderId))
     signature = crypto.sign(self.privateKey, sData)
     data = json.dumps({
         "sender": self.publicKey,
         "orderId": orderId,
         "signature": signature
     })
     req = yuwaves.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=yuwaves.MATCHER)
     if 'message' in req:
         raise Exception(req['message'])
     if req['status'] == 'OrderCanceled':
         logging.info('Order Cancelled - ID: %s' % orderId)
         return
     raise Exception('Order was not canceled')
Exemplo n.º 17
0
    def burnAsset(self, Asset, quantity, txFee=yuwaves.DEFAULT_TX_FEE):
        timestamp = int(time.time() * 1000)

        sData = '\6' + \
                crypto.bytes2str(base58.b58decode(bytes(self.publicKey))) + \
                crypto.bytes2str(base58.b58decode(bytes(Asset.assetId))) + \
                crypto.bytes2str(struct.pack(">Q", quantity)) + \
                crypto.bytes2str(struct.pack(">Q", txFee)) + \
                crypto.bytes2str(struct.pack(">Q", timestamp))
        signature = crypto.sign(self.privateKey, crypto.str2bytes(sData))
        data = json.dumps({
            "senderPublicKey": self.publicKey,
            "assetId": Asset.assetId,
            "quantity": quantity,
            "timestamp": timestamp,
            "fee": txFee,
            "signature": signature
        })
        req = yuwaves.wrapper('/assets/broadcast/burn', data)
        if yuwaves.OFFLINE:
            return req
        else:
            return req.get('id', 'ERROR')
Exemplo n.º 18
0
 def getOrderHistory(self, assetPair, timestamp=0):
     if timestamp == 0:
         timestamp = int(time.time() * 1000)
     sData = base58.b58decode(bytes(self.publicKey)) + \
             struct.pack(">Q", timestamp)
     signature = crypto.sign(self.privateKey, sData)
     data = {
         "Accept": "application/json",
         "Timestamp": bytes(timestamp),
         "Signature": signature
     }
     req = yuwaves.wrapper(
         '/matcher/orderbook/%s/%s/publicKey/%s' %
         ('WAVES' if assetPair.asset1.assetId == '' else
          assetPair.asset1.assetId, 'WAVES' if assetPair.asset2.assetId
          == '' else assetPair.asset2.assetId, self.publicKey),
         headers=data,
         host=yuwaves.MATCHER)
     if 'message' in req:
         msg = req['message']
         logging.error(msg)
         yuwaves.throw_error(msg)
     return req
Exemplo n.º 19
0
 def lease(self,
           recipient,
           amount,
           txFee=yuwaves.DEFAULT_LEASE_FEE,
           timestamp=0):
     if not self.privateKey:
         msg = 'Private key required'
         logging.error(msg)
         yuwaves.throw_error(msg)
     elif amount <= 0:
         msg = 'Amount must be > 0'
         logging.error(msg)
         yuwaves.throw_error(msg)
     elif not yuwaves.OFFLINE and self.balance() < amount + txFee:
         msg = 'Insufficient Waves balance'
         logging.error(msg)
         yuwaves.throw_error(msg)
     else:
         if timestamp == 0:
             timestamp = int(time.time() * 1000)
         sData = b'\x08' + \
                 base58.b58decode(bytes(self.publicKey)) + \
                 base58.b58decode(bytes(recipient.address)) + \
                 struct.pack(">Q", amount) + \
                 struct.pack(">Q", txFee) + \
                 struct.pack(">Q", timestamp)
         signature = crypto.sign(self.privateKey, sData)
         data = json.dumps({
             "senderPublicKey": self.publicKey,
             "recipient": recipient.address,
             "amount": amount,
             "fee": txFee,
             "timestamp": timestamp,
             "signature": signature
         })
         req = yuwaves.wrapper('/leasing/broadcast/lease', data)
         return req
Exemplo n.º 20
0
 def status(self):
     try:
         req = yuwaves.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
Exemplo n.º 21
0
 def _getMarketData(self, method, params):
     return yuwaves.wrapper('%s/%s/%s/%s' %
                            (method, self.a1, self.a2, params),
                            host=yuwaves.DATAFEED)
Exemplo n.º 22
0
 def ticker(self):
     return yuwaves.wrapper('/api/ticker/%s/%s' % (self.a1, self.a2),
                            host=yuwaves.DATAFEED)
Exemplo n.º 23
0
 def orderbook(self):
     req = yuwaves.wrapper('/matcher/orderbook/%s/%s' % (self.a1, self.a2),
                           host=yuwaves.MATCHER)
     return req
Exemplo n.º 24
0
    def sendAsset(self,
                  recipient,
                  asset,
                  amount,
                  attachment='',
                  feeAsset='',
                  txFee=yuwaves.DEFAULT_TX_FEE,
                  timestamp=0):
        if not self.privateKey:
            msg = 'Asset not issued'
            logging.error(msg)
            yuwaves.throw_error(msg)
        elif not yuwaves.OFFLINE and asset and not asset.status():
            msg = 'Asset not issued'
            logging.error(msg)
            yuwaves.throw_error(msg)
        elif amount <= 0:
            msg = 'Amount must be > 0'
            logging.error(msg)
            yuwaves.throw_error(msg)
        elif not yuwaves.OFFLINE and asset and self.balance(
                asset.assetId) < amount:
            msg = 'Insufficient asset balance'
            logging.error(msg)
            yuwaves.throw_error(msg)
        elif not yuwaves.OFFLINE and not asset and self.balance() < amount:
            msg = 'Insufficient Waves balance'
            logging.error(msg)
            yuwaves.throw_error(msg)
        elif not yuwaves.OFFLINE and not feeAsset and self.balance() < txFee:
            msg = 'Insufficient Waves balance'
            logging.error(msg)
            yuwaves.throw_error(msg)
        elif not yuwaves.OFFLINE and feeAsset and self.balance(
                feeAsset.assetId) < txFee:
            msg = 'Insufficient asset balance'
            logging.error(msg)
            yuwaves.throw_error(msg)
        else:
            if feeAsset:
                feeInfos = yuwaves.wrapper('/assets/details/' +
                                           feeAsset.assetId)
                if feeInfos['minSponsoredAssetFee']:
                    txFee = feeInfos['minSponsoredAssetFee']
            if timestamp == 0:
                timestamp = int(time.time() * 1000)
            sData = b'\4' + \
                    base58.b58decode(bytes(self.publicKey)) + \
                    (b'\1' + base58.b58decode(bytes(asset.assetId)) if asset else b'\0') + \
                    (b'\1' + base58.b58decode(bytes(feeAsset.assetId)) if feeAsset else b'\0') + \
                    struct.pack(">Q", timestamp) + \
                    struct.pack(">Q", amount) + \
                    struct.pack(">Q", txFee) + \
                    base58.b58decode(bytes(recipient.address)) + \
                    struct.pack(">H", len(attachment)) + \
                    crypto.str2bytes(attachment)
            signature = crypto.sign(self.privateKey, sData)
            data = json.dumps({
                "assetId": (asset.assetId if asset else ""),
                "feeAssetId": (feeAsset.assetId if feeAsset else ""),
                "senderPublicKey":
                self.publicKey,
                "recipient":
                recipient.address,
                "amount":
                amount,
                "fee":
                txFee,
                "timestamp":
                timestamp,
                "attachment":
                base58.b58encode(crypto.str2bytes(attachment)),
                "signature":
                signature
            })

            return yuwaves.wrapper('/assets/broadcast/transfer', data)
Exemplo n.º 25
0
    def massTransferAssets(self, transfers, asset, attachment='', timestamp=0):
        txFee = 100000 + (math.ceil((len(transfers) + 1) / 2 - 0.5)) * 100000

        if (asset.scripted):
            txFee += 400000

        totalAmount = 0

        if not self.privateKey:
            logging.error('Private key required')
        elif len(transfers) > 100:
            logging.error('Too many recipients')
        elif not yuwaves.OFFLINE and self.balance() < txFee:
            logging.error('Insufficient Waves balance')
        elif not yuwaves.OFFLINE and self.balance(
                assetId=asset.assetId) < totalAmount:
            logging.error('Insufficient %s balance' % asset.name)
        else:
            if timestamp == 0:
                timestamp = int(time.time() * 1000)
            transfersData = b''
            for i in range(0, len(transfers)):
                transfersData += base58.b58decode(
                    bytes(transfers[i]['recipient']) +
                    struct.pack(">Q", transfers[i]['amount']))
            sData = b'\x0b' + \
                    b'\1' + \
                    base58.b58decode(bytes(self.publicKey)) + \
                    b'\1' + \
                    base58.b58decode(bytes(asset.assetId)) + \
                    struct.pack(">H", len(transfers)) + \
                    transfersData + \
                    struct.pack(">Q", timestamp) + \
                    struct.pack(">Q", txFee) + \
                    struct.pack(">H", len(attachment)) + \
                    crypto.str2bytes(attachment)

            signature = crypto.sign(self.privateKey, sData)

            data = json.dumps({
                "type":
                11,
                "version":
                1,
                "assetId":
                asset.assetId,
                "senderPublicKey":
                self.publicKey,
                "fee":
                txFee,
                "timestamp":
                timestamp,
                "transfers":
                transfers,
                "attachment":
                base58.b58encode(crypto.str2bytes(attachment)),
                "signature":
                signature,
                "proofs": [signature]
            })

            return yuwaves.wrapper('/transactions/broadcast', data)
Exemplo n.º 26
0
 def aliases(self):
     a = yuwaves.wrapper('/alias/by-address/%s' % self.address)
     if type(a) == list:
         for i in range(len(a)):
             a[i] = a[i][8:]
     return a
Exemplo n.º 27
0
    def dataTransaction(self, data, timestamp=0):
        if not self.privateKey:
            logging.error('Private key required')
        else:
            if timestamp == 0:
                timestamp = int(time.time() * 1000)
            dataObject = {
                "type": 12,
                "version": 1,
                "senderPublicKey": self.publicKey,
                "data": data,
                "fee": 0,
                "timestamp": timestamp,
                "proofs": ['']
            }
            dataBinary = b''
            for i in range(0, len(data)):
                d = data[i]
                keyBytes = crypto.str2bytes(d['key'])
                dataBinary += struct.pack(">H", len(keyBytes))
                dataBinary += keyBytes
                if d['type'] == 'binary':
                    dataBinary += b'\2'
                    valueAsBytes = d['value']
                    dataBinary += struct.pack(">H", len(valueAsBytes))
                    dataBinary += crypto.str2bytes(valueAsBytes)
                elif d['type'] == 'boolean':
                    if d['value']:
                        dataBinary += b'\1\1'
                    else:
                        dataBinary += b'\1\0'
                elif d['type'] == 'integer':
                    dataBinary += b'\0'
                    dataBinary += struct.pack(">Q", d['value'])
                elif d['type'] == 'string':
                    dataBinary += b'\3'
                    dataBinary += struct.pack(">H", len(d['value']))
                    dataBinary += crypto.str2bytes(d['value'])
            # check: https://stackoverflow.com/questions/2356501/how-do-you-round-up-a-number-in-python
            txFee = (int((
                (len(crypto.str2bytes(json.dumps(data))) + 2 + 64)) / 1000.0) +
                     1) * 100000
            dataObject['fee'] = txFee
            sData = b'\x0c' + \
                    b'\1' + \
                    base58.b58decode(bytes(self.publicKey)) + \
                    struct.pack(">H", len(data)) + \
                    dataBinary + \
                    struct.pack(">Q", timestamp) + \
                    struct.pack(">Q", txFee)

            dataObject['proofs'] = [crypto.sign(self.privateKey, sData)]

            for entry in dataObject['data']:
                if entry['type'] == 'binary':
                    base64Encoded = base64.b64encode(
                        crypto.str2bytes(entry['value']))
                    entry['value'] = 'base64:' + crypto.bytes2str(
                        base64Encoded)
            dataObjectJSON = json.dumps(dataObject)
            return yuwaves.wrapper('/transactions/broadcast', dataObjectJSON)
Exemplo n.º 28
0
 def assets(self):
     req = yuwaves.wrapper('/assets/balance/%s' % self.address)['balances']
     return [r['assetId'] for r in req]
Exemplo n.º 29
0
    def _postOrder(self,
                   amountAsset,
                   priceAsset,
                   orderType,
                   amount,
                   price,
                   maxLifetime=30 * 86400,
                   matcherFee=yuwaves.DEFAULT_MATCHER_FEE,
                   timestamp=0):

        from decimal import Decimal
        if not isinstance(amount, Decimal) or not isinstance(price, Decimal):
            raise Exception('Decimal type expected')
        amount = int((amount * Decimal(
            (0, (1, ), amountAsset.decimals))).to_integral_exact())
        price = int((price * Decimal(
            (0, (1, ), priceAsset.decimals))).to_integral_exact())

        if timestamp == 0:
            timestamp = int(time.time() * 1000)
        expiration = timestamp + maxLifetime * 1000
        asset1 = b'\0' if amountAsset.assetId == '' else b'\1' + base58.b58decode(
            bytes(amountAsset.assetId))
        asset2 = b'\0' if priceAsset.assetId == '' else b'\1' + base58.b58decode(
            bytes(priceAsset.assetId))
        sData = base58.b58decode(bytes(self.publicKey)) + \
                base58.b58decode(bytes(yuwaves.MATCHER_PUBLICKEY)) + \
                asset1 + \
                asset2 + \
                orderType + \
                struct.pack(">Q", price) + \
                struct.pack(">Q", amount) + \
                struct.pack(">Q", timestamp) + \
                struct.pack(">Q", expiration) + \
                struct.pack(">Q", matcherFee)
        signature = crypto.sign(self.privateKey, sData)
        otype = "buy" if orderType == b'\0' else "sell"
        data = json.dumps({
            "senderPublicKey": self.publicKey,
            "matcherPublicKey": yuwaves.MATCHER_PUBLICKEY,
            "assetPair": {
                "amountAsset": amountAsset.assetId,
                "priceAsset": priceAsset.assetId,
            },
            "orderType": otype,
            "price": price,
            "amount": amount,
            "timestamp": timestamp,
            "expiration": expiration,
            "matcherFee": matcherFee,
            "signature": signature
        })
        req = yuwaves.wrapper('/matcher/orderbook', data, host=yuwaves.MATCHER)
        """
        {u'status': u'OrderAccepted', u'message': {u'assetPair': {u'priceAsset': u'Ft8X1v1LTa1ABafufpaCWyVj8KkaxUWE6xBhW6sNFJck', u'amountAsset': None}, u'orderType': u'buy', u'price': 268, u'signature': u'4LzSfA4CDLbznAsykB8fHXtvLP6ZvaUYkSowrn6KXgouatSskHRQ1chiPdq6Hyo7yXi9cdfYdTZo6NFVk8APBXuz', u'matcherFee': 300000, u'senderPublicKey': u'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX', u'amount': 387444879, u'version': 1, u'expiration': 1553609813760, u'timestamp': 1551017813760, u'proofs': [u'4LzSfA4CDLbznAsykB8fHXtvLP6ZvaUYkSowrn6KXgouatSskHRQ1chiPdq6Hyo7yXi9cdfYdTZo6NFVk8APBXuz'], u'id': u'CGchfxnGKsySb36SPziRXo7AoijPUPvYReuXN9KczRwT', u'matcherPublicKey': u'7kPFrHDiGw1rCm7LPszuECwWYL3dMf6iMifLRDJQZMzy', u'sender': u'3XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'}}
        """
        id = -1
        if 'status' in req:
            if req['status'] == 'OrderRejected':
                msg = 'Order Rejected - %s' % req['message']
                logging.error(msg)
                yuwaves.throw_error(msg)
            elif req['status'] == 'OrderAccepted':
                id = str(req['message']['id'])
                logging.info('Order Accepted - ID: %s' % id)
        elif not yuwaves.OFFLINE:
            logging.error(req)
            yuwaves.throw_error(req)
        else:
            return req
        return id
Exemplo n.º 30
0
 def balances(self):
     return yuwaves.wrapper('/assets/balance/%s' % self.address)['balances']