Example #1
0
    def __init__(self,
                 asset_id=None,
                 asset_type=None,
                 name=None,
                 amount=Fixed8(0),
                 available=Fixed8(0),
                 precision=0,
                 fee_mode=0,
                 fee=Fixed8(0),
                 fee_addr=UInt160(data=bytearray(20)),
                 owner=None,
                 admin=None,
                 issuer=None,
                 expiration=None,
                 is_frozen=False):
        self.AssetId = asset_id
        self.AssetType = asset_type
        self.Name = name

        self.Amount = amount
        self.Available = available
        self.Precision = precision
        self.FeeMode = fee_mode
        self.Fee = fee
        self.FeeAddress = fee_addr

        if owner is not None and type(owner) is not EllipticCurve.ECPoint:
            raise Exception("Owner must be ECPoint Instance")

        self.Owner = owner
        self.Admin = admin
        self.Issuer = issuer
        self.Expiration = expiration
        self.IsFrozen = is_frozen
Example #2
0
    def test_fixed8_div(self):
        f1 = Fixed8(27)
        f2 = Fixed8(3)

        f3 = f1 / f2

        self.assertIsInstance(f3, Fixed8)
        self.assertEqual(f3.value, 9)
Example #3
0
    def test_fixed8_pow(self):
        f1 = Fixed8(2)
        f2 = Fixed8(3)

        f3 = pow(f1, f2)

        self.assertIsInstance(f3, Fixed8)
        self.assertEqual(f3.value, 8)
Example #4
0
    def test_fixed8_sub(self):
        f1 = Fixed8(100)
        f2 = Fixed8(300)

        f3 = f1 - f2

        self.assertIsInstance(f3, Fixed8)
        self.assertEqual(f3.value, -200)
Example #5
0
    def test_fixed8_mul(self):
        f1 = Fixed8(3)
        f2 = Fixed8(9)

        f3 = f1 * f2

        self.assertIsInstance(f3, Fixed8)
        self.assertEqual(f3.value, 27)
Example #6
0
    def test_fixed8_add(self):
        f1 = Fixed8(100)
        f2 = Fixed8(300)

        f3 = f1 + f2

        self.assertIsInstance(f3, Fixed8)
        self.assertEqual(f3.value, 400)
Example #7
0
    def test_fixed8_mod(self):

        f1 = Fixed8(10)
        f2 = Fixed8(5)

        f3 = f1 % f2

        self.assertIsInstance(f3, Fixed8)
        self.assertEqual(f3.value, 0)

        f4 = Fixed8(7)

        f5 = f1 % f4

        self.assertEqual(f5.value, 3)
Example #8
0
    def test_fixed8_neg(self):
        f1 = Fixed8(2)

        f1 = -f1
        self.assertIsInstance(f1, Fixed8)

        self.assertEqual(f1.value, -2)
Example #9
0
    def __init__(self,
                 inputs=None,
                 outputs=None,
                 assettype=AssetType.GoverningToken,
                 assetname='',
                 amount=Fixed8(0),
                 precision=0,
                 owner=None,
                 admin=None):

        super(RegisterTransaction, self).__init__(inputs, outputs)
        self.Type = TransactionType.RegisterTransaction  # 0x40
        self.AssetType = assettype
        self.Name = assetname
        self.Amount = amount  # Unlimited Mode: -0.00000001

        if inputs is not None:
            self.inputs = inputs
        else:
            self.inputs = []

        if outputs is not None:
            self.outputs = outputs
        else:
            self.outputs = []

        if owner is not None and type(owner) is not EllipticCurve.ECPoint:
            raise Exception("Invalid owner, must be ECPoint instance")

        self.Owner = owner
        self.Admin = admin
        self.Precision = precision
Example #10
0
 def SubtractFromBalance(self, assetId, fixed8_val):
     found = False
     for key, balance in self.Balances.items():
         if key == assetId:
             self.Balances[assetId] = self.Balances[assetId] - fixed8_val
     if not found:
         self.Balances[assetId] = fixed8_val * Fixed8(-1)
Example #11
0
    def GetBalance(self, asset_id, watch_only=0):
        """
        Get the balance of a specific token by its asset id.

        Args:
            asset_id (NEP5Token|TransactionOutput): an instance of type neo.Wallets.NEP5Token or neo.Core.TX.Transaction.TransactionOutput to get the balance from.
            watch_only (bool): True, to limit to watch only wallets.

        Returns:
            Fixed8: total balance.
        """
        total = Fixed8(0)

        if type(asset_id) is NEP5Token:
            return self.GetTokenBalance(asset_id, watch_only)

        for coin in self.GetCoins():
            if coin.Output.AssetId == asset_id:
                if coin.State & CoinState.Confirmed > 0 and \
                        coin.State & CoinState.Spent == 0 and \
                        coin.State & CoinState.Locked == 0 and \
                        coin.State & CoinState.Frozen == 0 and \
                        coin.State & CoinState.WatchOnly == watch_only:

                    total = total + coin.Output.Value

        return total
Example #12
0
    def ReadFixed8(self, unsigned=False):

        if unsigned:
            fval = self.ReadUInt64()
        else:
            fval = self.ReadInt64()

        return Fixed8(fval)
Example #13
0
 def TotalFees(self):
     amount=0
     for tx in self.Transactions:
         if type(tx.SystemFee()) is int:
             raise Exception("TX %s is baddddddd %s %s" % (tx, tx.Type))
         elif type(tx.SystemFee().value) is Fixed8:
             raise Exception("TX ISSS BADD:::: %s %s" % (tx, tx.Type))
         amount += tx.SystemFee().value
     return Fixed8(amount)
Example #14
0
    def GetBalance(self, asset_id):
        total = Fixed8(0)
        for coin in self.GetCoins():
            if coin.State & CoinState.Spent == 0 \
                and coin.Output.AssetId == asset_id:

                total = total + coin.Output.Value

        return total
Example #15
0
class TransactionResult():
    AssetId = None
    Amount = Fixed8(0)

    def __init__(self, asset_id, amount):
        self.AssetId = asset_id
        self.Amount = amount

    def ToString(self):
        return "%s -> %s " % (self.AssetId.ToString(), self.Amount.value)
Example #16
0
    def DeserializeExclusiveData(self, reader):
        self.Type = TransactionType.RegisterTransaction
        self.AssetType = reader.ReadByte()
        self.Name = reader.ReadVarString().decode('utf-8')
        self.Amount = Fixed8(reader.ReadInt64())
        self.Precision = reader.ReadByte()

        pkey = reader.ReadBytes(33)
        ecdsa = ECDSA.decode_secp256r1(pkey)
        self.Owner = ecdsa.G
        self.Admin = reader.ReadUInt160()
Example #17
0
    def FindUnspentCoinsByAssetAndTotal(self, asset_id, amount):

        coins = self.FindUnspentCoinsByAsset(asset_id)

        sum = Fixed8(0)

        for coin in coins:
            sum = sum + coin.Output.Value

        if sum < amount:
            return None

        sorted(coins, key=lambda coin: coin.Output.Value.value)

        total = Fixed8(0)

        for index, coin in enumerate(coins):
            total = total + coin.Output.Value
            if total >= amount:
                return coins[0:index + 1]
Example #18
0
    def test_account_state(self):
        hash = UInt160(data=self.shash)
        account = AccountState(script_hash=hash)

        addr = account.Address

        self.assertEqual(addr, self.saddr)

        input = binascii.unhexlify(self.assset)

        asset = AssetState.DeserializeFromDB(input)

        account.AddToBalance(asset.AssetId, Fixed8(2440000000))

        self.assertEqual(account.BalanceFor(asset.AssetId), Fixed8(2440000000))

        account.SubtractFromBalance(asset.AssetId, Fixed8(1220000000))

        self.assertEqual(account.BalanceFor(asset.AssetId), Fixed8(1220000000))

        self.assertEqual(account.HasBalance(asset.AssetId), True)

        sshare_hash = Blockchain.SystemShare().Hash

        account.SubtractFromBalance(sshare_hash, Fixed8(800000000))

        self.assertFalse(account.AllBalancesZeroOrLess())

        acct_json = account.ToJson()

        stream = StreamManager.GetStream()
        writer = BinaryWriter(stream)

        account.Serialize(writer)
        out = stream.ToArray()
        StreamManager.ReleaseStream(stream)

        input = binascii.unhexlify(out)
        newaccount = AccountState.DeserializeFromDB(input)

        self.assertEqual(acct_json, newaccount.ToJson())
Example #19
0
    def NetworkFee(self):

        if self.__network_fee is None:

            input = Fixed8(0)

            for coin_ref in self.References.values():
                if coin_ref.AssetId == GetBlockchain().SystemCoin().Hash:
                    input = input + coin_ref.Value

            output = Fixed8(0)

            for tx_output in self.outputs:
                if tx_output.AssetId == GetBlockchain().SystemCoin().Hash:
                    output = output + tx_output.Value

            self.__network_fee = input - output - self.SystemFee()

#            print("Determined network fee to be %s " % (self.__network_fee.value))

        return self.__network_fee
Example #20
0
    def test_account_2(self):

        hash = UInt160(data=self.ac2_h)
        account2 = AccountState(script_hash=hash)

        addr = account2.Address

        self.assertEqual(addr, self.ac2_a)

        input = binascii.unhexlify(self.assset)
        asset = AssetState.DeserializeFromDB(input)

        account2.AddToBalance(asset.AssetId, Fixed8(97800000000))

        #        print("account 2 %s " % json.dumps(account2.ToJson(), indent=4))

        self.assertEqual(account2.BalanceFor(asset.AssetId),
                         Fixed8(97800000000))

        input = binascii.unhexlify(self.ac1_out)
        account1 = AccountState.DeserializeFromDB(input)
Example #21
0
    def GetTransactionResults(self):
        if self.References is None: return None

        results = []
        realresults = []
        for ref_output in self.References.values():
            results.append(TransactionResult(ref_output.AssetId, ref_output.Value))

        for output in self.outputs:
            results.append(TransactionResult(output.AssetId, output.Value * Fixed8(-1)))

        for key, group in groupby(results, lambda x: x.AssetId):
            sum=Fixed8(0)
            for item in group:
                sum = sum + item.Amount

            if sum != Fixed8.Zero():

                realresults.append( TransactionResult(key, sum))

        return realresults
Example #22
0
    def FindUnspentCoinsByAssetAndTotal(self,
                                        asset_id,
                                        amount,
                                        from_addr=None,
                                        use_standard=False,
                                        watch_only_val=0):
        """
        Finds unspent coin objects totalling a requested value in the wallet limited to those of a certain asset type.

        Args:
            asset_id (UInt256): a bytearray (len 32) representing an asset on the blockchain.
            amount (int): the amount of unspent coins that are being requested.
            from_addr (UInt160): a bytearray (len 20) representing an address.
            use_standard (bool): whether or not to only include standard contracts ( i.e not a smart contract addr ).
            watch_only_val (int): a flag ( 0 or 64 ) indicating whether or not to find coins that are in 'watch only' addresses.

        Returns:
            list: a list of ``neo.Wallet.Coin`` in the wallet that are not spent. this list is empty if there are not enough coins to satisfy the request.
        """
        coins = self.FindUnspentCoinsByAsset(asset_id,
                                             from_addr=from_addr,
                                             use_standard=use_standard,
                                             watch_only_val=watch_only_val)

        sum = Fixed8(0)

        for coin in coins:
            sum = sum + coin.Output.Value

        if sum < amount:
            return None

        sorted(coins, key=lambda coin: coin.Output.Value.value)

        total = Fixed8(0)

        for index, coin in enumerate(coins):
            total = total + coin.Output.Value
            if total >= amount:
                return coins[0:index + 1]
Example #23
0
    def LoadCoins(self):
        coins = {}

        try:
            for coin in Coin.select():
                reference = CoinReference(prev_hash=UInt256(coin.TxId), prev_index=coin.Index)
                output = TransactionOutput(UInt256(coin.AssetId), Fixed8(coin.Value), UInt160(coin.ScriptHash))
                walletcoin = WalletCoin.CoinFromRef(reference, output, coin.State)
                coins[reference] = walletcoin
        except Exception as e:
            print("could not load coins %s " % e)

        return coins
Example #24
0
    def DeserializeExclusiveData(self, reader):

        if self.Version > 1:
            raise Exception('Invalid format')

        self.Script = reader.ReadVarBytes()

        if len(self.Script) == 0:
            raise Exception('Invalid Format')

        if self.Version >= 1:
            self.Gas = reader.ReadFixed8()
        else:
            self.Gas = Fixed8(0)
Example #25
0
    def SystemFee(self):

        if self.Version >= 1:
            return Fixed8.Zero()

        # if all outputs are NEO or gas, return 0
        all_neo_gas = True
        for output in self.outputs:
            if output.AssetId != GetSystemCoin().Hash and output.AssetId != GetSystemShare().Hash:
                all_neo_gas = False
        if all_neo_gas:
            return Fixed8.Zero()

        return Fixed8(int(settings.ISSUE_TX_FEE))
Example #26
0
    def CalculateBonusInternal(unclaimed):

        amount_claimed = Fixed8.Zero()

        decInterval = Blockchain.DECREMENT_INTERVAL
        genAmount = Blockchain.GENERATION_AMOUNT
        genLen = len(genAmount)

        for coinheight, group in groupby(unclaimed, lambda x: x.Heights):
            amount = 0
            ustart = int(coinheight.start / decInterval)

            if ustart < genLen:

                istart = coinheight.start % decInterval
                uend = int(coinheight.end / decInterval)
                iend = coinheight.end % decInterval

                if uend >= genLen:
                    iend = 0

                if iend == 0:
                    uend -= 1
                    iend = decInterval

                while ustart < uend:

                    amount += (decInterval - istart) * genAmount[ustart]
                    ustart += 1
                    istart = 0

                amount += (iend - istart) * genAmount[ustart]

            endamount = Blockchain.Default().GetSysFeeAmountByHeight(
                coinheight.end - 1)
            startamount = 0 if coinheight.start == 0 else Blockchain.Default(
            ).GetSysFeeAmountByHeight(coinheight.start - 1)
            amount += endamount - startamount

            outputSum = 0

            for spentcoin in group:
                outputSum += spentcoin.Value.value

            outputSum = outputSum / 100000000
            outputSumFixed8 = Fixed8(int(outputSum * amount))
            amount_claimed += outputSumFixed8

        return amount_claimed
Example #27
0
 def GetUnavailableBonus(self):
     """
     Gets the total claimable amount of Gas in the wallet that is not available to claim
     because it has not yet been spent
     Returns:
         Fixed8: the amount of Gas unavailable to claim
     """
     height = Blockchain.Default().Height + 1
     unspents = self.FindUnspentCoinsByAsset(Blockchain.SystemShare().Hash)
     refs = [coin.Reference for coin in unspents]
     try:
         unavailable_bonus = Blockchain.CalculateBonus(refs,
                                                       height_end=height)
         return unavailable_bonus
     except Exception as e:
         pass
     return Fixed8(0)
Example #28
0
class InvocationTransaction(Transaction):

    Script = bytearray(0)
    Gas = Fixed8(0)

    def SystemFee(self):
        return self.Gas

    def __init__(self, *args, **kwargs):
        super(InvocationTransaction, self).__init__(*args, **kwargs)
        self.Type = TransactionType.InvocationTransaction

    def Size(self):
        return self.Size() + sys.getsizeof(int)

    def DeserializeExclusiveData(self, reader):

        if self.Version > 1:
            raise Exception('Invalid format')

        self.Script = reader.ReadVarBytes()

        if len(self.Script) == 0:
            raise Exception('Invalid Format')

        if self.Version >= 1:
            self.Gas = reader.ReadFixed8()
        else:
            self.Gas = Fixed8(0)

    def SerializeExclusiveData(self, writer):
        writer.WriteVarBytes(self.Script)
        if self.Version >= 1:
            writer.WriteFixed8(self.Gas)

    def Verify(self, mempool):
        if self.Gas.value % 100000000 != 0:
            return False
        return super(InvocationTransaction, self).Verify(mempool)

    def ToJson(self):
        jsn = super(InvocationTransaction, self).ToJson()
        jsn['script'] = self.Script.hex()
        jsn['gas'] = self.Gas.value
        return jsn
Example #29
0
    def __init__(self,
                 inputs=[],
                 outputs=[],
                 assettype=AssetType.AntShare,
                 assetname='',
                 amount=Fixed8(0),
                 precision=0,
                 owner=None,
                 admin=None):
        super(RegisterTransaction, self).__init__(inputs, outputs)
        self.Type = TransactionType.RegisterTransaction  # 0x40

        self.AssetType = assettype
        self.Name = assetname
        self.Amount = amount  # Unlimited Mode: -0.00000001
        self.Owner = owner
        self.Admin = admin
        self.Precision = precision
Example #30
0
    def Verify(self, mempool):

        if not super(ClaimTransaction, self).Verify(mempool):
            return False

        # wat does this do
        # get all claim transactinos from mempool list
        # that are not this claim
        # and gather all the claims of those claim transactions
        # and see if they intersect the claims of this transaction
        # and if that number is greater than zero that we do not verify
        # (now, to do that in python)
        # if (mempool.OfType < ClaimTransaction > ().Where(p => p != this).SelectMany(p= > p.Claims).Intersect(Claims).Count() > 0)
        # return false;

        # im sorry about the below
        otherclaimTxs = [
            tx for tx in mempool if tx is ClaimTransaction and tx is not self
        ]
        for other in otherclaimTxs:
            # check to see if the length of the intersection between this objects claim's and the other txs claims is > 0
            if len([
                    list(filter(lambda x: x in self.Claims, otherClaims))
                    for otherClaims in other.Claims
            ]):
                return False

        txResult = None
        for tx in self.GetTransactionResults():
            if tx.AssetId == Blockchain.SystemCoin().Hash:
                txResult = tx
                break

        if txResult is None or txResult.Amount > Fixed8(0):
            return False

        try:
            return Blockchain.CalculateBonusIgnoreClaimed(
                self.Claims, False) == -txResult.Amount

        except Exception as e:
            logger.error('couldnt calculate bonus: %s ' % e)

        return False