def Query(self):
        """
        Query the smart contract for its token information (name, symbol, decimals).

        Args:
            wallet (neo.Wallets.Wallet): a wallet instance.

        Returns:
            None: if the NEP5Token instance `Name` is already set.
            True: if all information was retrieved.
            False: if information retrieval failed.
        """
        if self.name is not None:
            # don't query twice
            return

        sb = ScriptBuilder()
        sb.EmitAppCallWithOperation(self.ScriptHash, 'name')
        sb.EmitAppCallWithOperation(self.ScriptHash, 'symbol')
        sb.EmitAppCallWithOperation(self.ScriptHash, 'decimals')

        engine = ApplicationEngine.Run(sb.ToArray())
        results = engine.EvaluationStack.Items

        try:
            self.name = results[0].GetString()
            self.symbol = results[1].GetString()
            self.decimals = results[2].GetBigInteger()
            if len(self.name) > 1 and self.name != 'Stack Item' \
                    and len(self.symbol) > 1 and self.symbol != 'Stack Item'\
                    and self.decimals < 10:
                return True
        except Exception as e:
            logger.info("could not query token %s " % e)
        return False
Beispiel #2
0
    def Query(self, wallet):
        """
        Query the smart contract for its token information (name, symbol, decimals).

        Args:
            wallet (neo.Wallets.Wallet): a wallet instance.

        Returns:
            None: if the NEP5Token instance `Name` is already set.
            True: if all information was retrieved.
            False: if information retrieval failed.
        """
        if self.name is not None:
            # don't query twice
            return

        sb = ScriptBuilder()
        sb.EmitAppCallWithOperation(self.ScriptHash, 'name')
        sb.EmitAppCallWithOperation(self.ScriptHash, 'symbol')
        sb.EmitAppCallWithOperation(self.ScriptHash, 'decimals')

        tx, fee, results, num_ops = test_invoke(sb.ToArray(), wallet, [])

        try:
            self.name = results[0].GetString()
            self.symbol = results[1].GetString()
            self.decimals = results[2].GetBigInteger()
            return True
        except Exception as e:
            logger.error("could not query token %s " % e)
        return False
Beispiel #3
0
    def Query(self):
        """
        Query the smart contract for its token information (name, symbol, decimals).

        Args:
            wallet (neo.Wallets.Wallet): a wallet instance.

        Returns:
            None: if the NEP5Token instance `Name` is already set.
            True: if all information was retrieved.
            False: if information retrieval failed.
        """
        if self.name is not None:
            # don't query twice
            return

        sb = ScriptBuilder()
        sb.EmitAppCallWithOperation(self.ScriptHash, 'name')
        sb.EmitAppCallWithOperation(self.ScriptHash, 'symbol')
        sb.EmitAppCallWithOperation(self.ScriptHash, 'decimals')

        snapshot = GetBlockchain().Default()._db.createSnapshot().Clone()
        engine = None
        try:
            engine = ApplicationEngine.Run(snapshot,
                                           sb.ToArray(),
                                           exit_on_error=True,
                                           gas=Fixed8.FromDecimal(10.0),
                                           test_mode=False)
        except Exception as e:
            traceback.print_exc()
            pass

        if engine and len(engine.ResultStack.Items) == 3:
            results = engine.ResultStack.Items

            try:
                self.name = results[0].GetString()
                self.symbol = results[1].GetString()
                self.decimals = results[2].GetBigInteger()
                if len(self.name) > 1 and self.name != 'Stack Item' \
                        and len(self.symbol) > 1 and self.symbol != 'Stack Item' \
                        and self.decimals < 10:
                    return True
            except Exception as e:
                pass
        return False
def example2():
    source_address = "AJQ6FoaSXDFzA6wLnyZ1nFN7SGSN2oNTc3"
    source_script_hash = address_to_scripthash(source_address)

    # start by creating a base InvocationTransaction
    # the inputs, outputs and Type do not have to be set anymore.
    invocation_tx = InvocationTransaction()

    # Since we are building a raw transaction, we will add the raw_tx flag

    invocation_tx.raw_tx = True

    # often times smart contract developers use the function ``CheckWitness`` to determine if the transaction is signed by somebody eligible of calling a certain method
    # in order to pass that check you want to add the corresponding script_hash as a transaction attribute (this is generally the script_hash of the public key you use for signing)
    # Note that for public functions like the NEP-5 'getBalance' and alike this would not be needed, but it doesn't hurt either
    invocation_tx.Attributes.append(
        TransactionAttribute(usage=TransactionAttributeUsage.Script,
                             data=source_script_hash))

    # next we need to build a 'script' that gets executed against the smart contract.
    # this is basically the script that calls the entry point of the contract with the necessary parameters
    smartcontract_scripthash = UInt160.ParseString(
        "31730cc9a1844891a3bafd1aa929a4142860d8d3")
    sb = ScriptBuilder()
    # call the NEP-5 `name` method on the contract (assumes contract address is a NEP-5 token)
    sb.EmitAppCallWithOperation(smartcontract_scripthash, 'name')
    invocation_tx.Script = binascii.unhexlify(sb.ToArray())

    # at this point we've build our unsigned transaction and it's time to sign it before we get the raw output that we can send to the network via RPC
    # we need to create a Wallet instance for helping us with signing
    wallet = UserWallet.Create('path',
                               to_aes_key('mypassword'),
                               generate_default_key=False)

    # if you have a WIF use the following
    # this WIF comes from the `neo-test1-w.wallet` fixture wallet
    private_key = KeyPair.PrivateKeyFromWIF(
        "Ky94Rq8rb1z8UzTthYmy1ApbZa9xsKTvQCiuGUZJZbaDJZdkvLRV")

    # if you have a NEP2 encrypted key use the following instead
    # private_key = KeyPair.PrivateKeyFromNEP2("NEP2 key string", "password string")

    # we add the key to our wallet
    wallet.CreateKey(private_key)

    # and now we're ready to sign
    context = ContractParametersContext(invocation_tx)
    wallet.Sign(context)

    invocation_tx.scripts = context.GetScripts()
    raw_tx = invocation_tx.ToArray()

    return raw_tx