Beispiel #1
0
    def callback_view(self):
        """Get return value of an on-chain callback method.

        :returns: Decoded parameters of a callback
        """
        if self.address:
            initial_storage = self.shell.blocks[
                self.context.block_id].context.contracts[
                    self.address].storage()
        else:
            storage_ty = StorageSection.match(self.context.storage_expr)
            initial_storage = storage_ty.dummy(
                self.context).to_micheline_value(lazy_diff=True)

        operations, _, stdout, error = Interpreter.run_view(
            parameter=self.parameters['value'],
            entrypoint=self.parameters['entrypoint'],
            storage=initial_storage,
            context=self.context,
        )
        if not len(operations) == 1:
            raise Exception(
                'Multiple internal operations, not sure which one to pick')
        if error:
            logger.debug('\n'.join(stdout))
            raise error
        return operations[0]
Beispiel #2
0
    def interpret(
        self,
        storage=None,
        source=None,
        sender=None,
        amount=None,
        balance=None,
        chain_id=None,
        level=None,
        now=None,
        self_address=None,
    ) -> ContractCallResult:
        """Run code in the builtin REPL (WARNING! Not recommended for critical tasks).

        :param storage: initial storage as Python object, leave None if you want to generate a dummy one
        :param source: patch SOURCE
        :param sender: patch SENDER
        :param amount: patch AMOUNT
        :param balance: patch BALANCE
        :param chain_id: patch CHAIN_ID
        :param level: patch LEVEL
        :param now: patch NOW
        :param self_address: patch SELF/SELF_ADDRESS
        :rtype: pytezos.contract.result.ContractCallResult
        """
        storage_ty = StorageSection.match(self.context.storage_expr)
        if storage is None:
            initial_storage = storage_ty.dummy(
                self.context).to_micheline_value(lazy_diff=True)
        else:
            initial_storage = storage_ty.from_python_object(
                storage).to_micheline_value(lazy_diff=True)
        assert self.context.script
        operations, storage, lazy_diff, stdout, error = Interpreter.run_code(
            parameter=self.parameters['value'],
            entrypoint=self.parameters['entrypoint'],
            storage=initial_storage,
            script=self.context.script['code'],
            source=source,
            sender=sender or source,
            amount=amount or self.amount,
            balance=balance,
            chain_id=chain_id,
            level=level,
            now=now,
            address=self_address,
        )
        if error:
            logger.debug('\n'.join(stdout))
            raise error
        res = {
            'operations': operations,
            'storage': storage,
            'lazy_diff': lazy_diff,
        }
        return ContractCallResult.from_run_code(
            res,
            parameters=self.parameters,
            context=self.context,
        )
Beispiel #3
0
 def load(context: AbstractContext, with_code=False):
     parameter = ParameterSection.match(context.get_parameter_expr())
     storage = StorageSection.match(context.get_storage_expr())
     code = CodeSection.match(context.get_code_expr() if with_code else [])
     cls = type(MichelsonProgram.__name__, (MichelsonProgram,), dict(parameter=parameter,
                                                                     storage=storage,
                                                                     code=code))
     return cast(Type['MichelsonProgram'], cls)
Beispiel #4
0
 def load(context: ExecutionContext, with_code=False):
     cls = type(
         MichelsonProgram.__name__,
         (MichelsonProgram, ),
         dict(
             parameter=ParameterSection.match(context.get_parameter_expr()),
             storage=StorageSection.match(context.get_storage_expr()),
             code=CodeSection.match(
                 context.get_code_expr() if with_code else []),
         ),
     )
     return cast(Type['MichelsonProgram'], cls)
Beispiel #5
0
    def run_code(
        self,
        storage=None,
        source=None,
        sender=None,
        amount=None,
        balance=None,
        chain_id=None,
        gas_limit=None,
    ) -> ContractCallResult:
        """Execute using RPC interpreter.

        :param storage: initial storage as Python object, leave None if you want to generate a dummy one
        :param source: patch SOURCE
        :param sender: patch SENDER
        :param amount: patch AMOUNT
        :param balance: patch BALANCE
        :param chain_id: patch CHAIN_ID
        :param gas_limit: restrict max consumed gas
        :rtype: ContractCallResult
        """
        storage_ty = StorageSection.match(self.context.storage_expr)
        if storage is None:
            initial_storage = storage_ty.dummy(
                self.context).to_micheline_value(lazy_diff=True)
        else:
            initial_storage = storage_ty.from_python_object(
                storage).to_micheline_value(lazy_diff=True)
        script = [
            self.context.parameter_expr, self.context.storage_expr,
            self.context.code_expr
        ]
        query = skip_nones(
            script=script,
            storage=initial_storage,
            entrypoint=self.parameters['entrypoint'],
            input=self.parameters['value'],
            amount=format_mutez(amount or self.amount),
            chain_id=chain_id or self.context.get_chain_id(),
            source=sender,
            payer=source,
            balance=str(balance or 0),
            gas=str(gas_limit) if gas_limit is not None else None,
        )
        res = self.shell.blocks[self.block_id].helpers.scripts.run_code.post(
            query)
        return ContractCallResult.from_run_code(res,
                                                parameters=self.parameters,
                                                context=self.context)
Beispiel #6
0
    def view(self):
        """ Get return value of a view method.

        :returns: Decoded parameters of a callback
        """
        if self.address:
            initial_storage = self.shell.blocks[
                self.context.block_id].context.contracts[
                    self.address].storage()
        else:
            storage_ty = StorageSection.match(self.context.storage_expr)
            initial_storage = storage_ty.dummy(
                self.context).to_micheline_value(lazy_diff=True)

        res, stdout, error = Interpreter.run_view(
            parameter=self.parameters['value'],
            entrypoint=self.parameters['entrypoint'],
            storage=initial_storage,
            context=self.context)
        if error:
            print('\n'.join(stdout))
            raise error
        return res