def create_new_event(self, create_call: ast.Call) -> Event: event = Event('') event_args = create_call.args if len(event_args) < 0: self._log_error( CompilerError.UnfilledArgument(line=create_call.lineno, col=create_call.col_offset, param=list( Builtin.NewEvent.args)[0])) elif len(event_args) > 0: args_type = self.get_type(event_args[0]) if not Type.list.is_type_of(args_type): self._log_error( CompilerError.MismatchedTypes( line=event_args[0].lineno, col=event_args[0].col_offset, expected_type_id=Type.list.identifier, actual_type_id=args_type.identifier)) else: for value in event_args[0].elts: if not isinstance(value, ast.Tuple): CompilerError.MismatchedTypes( line=value.lineno, col=value.col_offset, expected_type_id=Type.tuple.identifier, actual_type_id=self.get_type(value).identifier) elif len(value.elts) < 2: self._log_error( CompilerError.UnfilledArgument( line=value.lineno, col=value.col_offset, param=list(Builtin.NewEvent.args)[0])) elif not (isinstance(value.elts[0], ast.Str) and (isinstance(value.elts[1], ast.Name) and isinstance( self.get_symbol(value.elts[1].id), IType))): CompilerError.MismatchedTypes( line=value.lineno, col=value.col_offset, expected_type_id=Type.tuple.identifier, actual_type_id=self.get_type(value).identifier) else: arg_name = value.elts[0].s arg_type = self.get_symbol(value.elts[1].id) event.args[arg_name] = Variable(arg_type) if len(event_args) > 1: if not isinstance(event_args[1], ast.Str): name_type = self.get_type(event_args[1]) CompilerError.MismatchedTypes( line=event_args[1].lineno, col=event_args[1].col_offset, expected_type_id=Type.str.identifier, actual_type_id=name_type.identifier) else: event.name = event_args[1].s return event
def validate_values(self, *params: Any) -> List[Any]: values = [] if len(params) != 2: return values origin, visitor = params values.append(self.contract_hash) from boa3.analyser.astanalyser import IAstAnalyser if not isinstance(visitor, IAstAnalyser): return values from boa3.exception import CompilerError if not isinstance(origin, ast.Call) or len(origin.args) < 1: visitor._log_error( CompilerError.UnfilledArgument(origin.lineno, origin.col_offset, list(self.args.keys())[0])) return values argument_hash = visitor.visit(origin.args[0]) try: if isinstance(argument_hash, str): from boa3.neo import from_hex_str argument_hash = from_hex_str(argument_hash) if isinstance(argument_hash, bytes): values[0] = UInt160(argument_hash) except BaseException: visitor._log_error( CompilerError.InvalidUsage( origin.lineno, origin.col_offset, "Only literal values are accepted for 'script_hash' argument" )) return values
def validate_callable_arguments(self, call: ast.Call, callable_target: Callable) -> bool: if (callable_target.allow_starred_argument and not hasattr(call, 'checked_starred_args') and len(call.args) > len(callable_target.args_without_default)): args = self.parse_to_node(str(Type.sequence.default_value), call) args.elts = call.args call.args = [args] call.checked_starred_args = True len_call_args = len(call.args) callable_required_args = len(callable_target.args_without_default) if len_call_args > len(callable_target.args): unexpected_arg = call.args[len(callable_target.args)] self._log_error( CompilerError.UnexpectedArgument(unexpected_arg.lineno, unexpected_arg.col_offset) ) return False elif len_call_args < callable_required_args: missed_arg = list(callable_target.args)[len(call.args)] self._log_error( CompilerError.UnfilledArgument(call.lineno, call.col_offset, missed_arg) ) return False if isinstance(callable_target, IBuiltinMethod) and callable_target.requires_reordering: if not hasattr(call, 'was_reordered') or not call.was_reordered: callable_target.reorder(call.args) call.was_reordered = True if callable_required_args <= len_call_args < len(callable_target.args): included_args = len_call_args - callable_required_args call.args.extend(callable_target.defaults[included_args:]) return True
def validate_values(self, *params: Any) -> List[Any]: values = [] if len(params) != 2: return values origin, visitor = params values.append(self.external_name) from boa3.analyser.astanalyser import IAstAnalyser if not isinstance(visitor, IAstAnalyser): return values from boa3.exception import CompilerError if not isinstance(origin, ast.Call): visitor._log_error( CompilerError.UnfilledArgument(origin.lineno, origin.col_offset, list(self.args.keys())[0])) return values args_names = list(self.args.keys()) if len(origin.args) > len(args_names): visitor._log_error( CompilerError.UnexpectedArgument(origin.lineno, origin.col_offset)) return values # read the called arguments args_len = min(len(origin.args), len(args_names)) values.clear() for x in range(args_len): values.append(visitor.visit(origin.args[x])) if len(values) < 1: values.append(self.external_name) # read the called keyword arguments for kwarg in origin.keywords: if kwarg.arg in args_names: x = args_names.index(kwarg.arg) value = visitor.visit(kwarg.value) values[x] = value else: visitor._log_error( CompilerError.UnexpectedArgument(kwarg.lineno, kwarg.col_offset)) if not isinstance(values[0], str): visitor._log_error( CompilerError.UnfilledArgument(origin.lineno, origin.col_offset, list(self.args.keys())[0])) return values if visitor.get_symbol(values[0]) is not None: visitor._log_error( CompilerError.InvalidUsage( origin.lineno, origin.col_offset, "Only literal values are accepted for 'name' argument")) values[0] = self.external_name return values