Esempio n. 1
0
 def execute(cls, stack: MichelsonStack, stdout: List[str],
             context: AbstractContext):
     a, b = cast(
         Tuple[Union[IntType, NatType, MutezType, TimestampType,
                     BLS12_381_G1Type, BLS12_381_G2Type, BLS12_381_FrType],
               ...], stack.pop2())
     res_type, = dispatch_types(
         type(a),
         type(b),
         mapping={
             (NatType, NatType): (NatType, ),
             (NatType, IntType): (IntType, ),
             (IntType, NatType): (IntType, ),
             (IntType, IntType): (IntType, ),
             (TimestampType, IntType): (TimestampType, ),
             (IntType, TimestampType): (TimestampType, ),
             (MutezType, MutezType): (MutezType, ),
             (BLS12_381_FrType, BLS12_381_FrType): (BLS12_381_FrType, ),
             (BLS12_381_G1Type, BLS12_381_G1Type): (BLS12_381_G1Type, ),
             (BLS12_381_G2Type, BLS12_381_G2Type): (BLS12_381_G2Type, )
         })
     res_type = cast(
         Union[Type[IntType], Type[NatType], Type[TimestampType],
               Type[MutezType], Type[BLS12_381_G1Type],
               Type[BLS12_381_G2Type], Type[BLS12_381_FrType]], res_type)
     if issubclass(res_type, IntType):
         res = res_type.from_value(int(a) + int(b))  # type: ignore
     else:
         res = res_type.from_point(bls12_381.add(
             a.to_point(), b.to_point()))  # type: ignore
     stack.push(res)
     stdout.append(format_stdout(cls.prim, [a, b], [res]))  # type: ignore
     return cls(stack_items_added=1)
Esempio n. 2
0
 def execute(cls, stack: MichelsonStack, stdout: List[str], context: AbstractContext):
     key, src = cast(Tuple[MichelsonType, Union[SetType, MapType, BigMapType]], stack.pop2())
     src.assert_type_in(MapType, BigMapType, SetType)
     res = BoolType.from_value(src.contains(key))
     stack.push(res)
     stdout.append(format_stdout(cls.prim, [key, src], [res]))
     return cls()
Esempio n. 3
0
 def execute(cls, stack: MichelsonStack, stdout: List[str],
             context: AbstractContext):
     a, b = stack.pop2()
     stack.push(a)
     stack.push(b)
     stdout.append(format_stdout(cls.prim, [a, b], [b, a]))
     return cls()
Esempio n. 4
0
 def execute(cls, stack: MichelsonStack, stdout: List[str], context: AbstractContext):
     elt, lst = cast(Tuple[MichelsonType, ListType], stack.pop2())
     lst.assert_type_in(ListType)
     res = lst.prepend(elt)
     stack.push(res)
     stdout.append(format_stdout(cls.prim, [elt, lst], [res]))
     return cls()
Esempio n. 5
0
 def execute(cls, stack: MichelsonStack, stdout: List[str],
             context: AbstractContext):
     a, b = cast(
         Tuple[Union[IntType, NatType, MutezType, TimestampType], ...],
         stack.pop2())
     q_type, r_type = dispatch_types(
         type(a),
         type(b),
         mapping={  # type: ignore
             (NatType, NatType): (NatType, NatType),
             (NatType, IntType): (IntType, NatType),
             (IntType, NatType): (IntType, NatType),
             (IntType, IntType): (IntType, NatType),
             (MutezType, NatType): (MutezType, MutezType),
             (MutezType, MutezType): (NatType, MutezType)
         }
     )  # type: Union[Type[IntType], Type[NatType], Type[TimestampType], Type[MutezType]]
     if int(b) == 0:
         res = OptionType.none(PairType.create_type(args=[q_type, r_type]))
     else:
         q, r = divmod(int(a), int(b))
         if r < 0:
             r += abs(int(b))
             q += 1
         items = [q_type.from_value(q), r_type.from_value(r)]
         res = OptionType.from_some(PairType.from_comb(items))
     stack.push(res)
     stdout.append(format_stdout(cls.prim, [a, b], [res]))  # type: ignore
     return cls(stack_items_added=1)
Esempio n. 6
0
 def execute(cls, stack: MichelsonStack, stdout: List[str],
             context: AbstractContext):
     left, right = stack.pop2()
     res = PairType.from_comb([left, right])
     stack.push(res)
     stdout.append(format_stdout(cls.prim, [left, right],
                                 [res]))  # type: ignore
     return cls(stack_items_added=1)
Esempio n. 7
0
 def execute(cls, stack: MichelsonStack, stdout: List[str], context: AbstractContext):
     element, pair = cast(Tuple[MichelsonType, PairType], stack.pop2())
     pair.assert_type_in(PairType)
     index = cls.args[0].get_int()
     res = pair.update_comb(index, element)
     stack.push(res)
     stdout.append(format_stdout(cls.prim, [element, pair], [res], index))
     return cls()
Esempio n. 8
0
 def execute(cls, stack: MichelsonStack, stdout: List[str], context: AbstractContext):
     item, amount = cast(Tuple[MichelsonType, NatType], stack.pop2())
     amount.assert_type_equal(NatType)
     address = context.get_self_address()
     res = TicketType.create(address, item, int(amount))
     stack.push(res)
     stdout.append(format_stdout(cls.prim, [item, amount], [res]))
     return cls()
Esempio n. 9
0
 def execute(cls, stack: MichelsonStack, stdout: List[str],
             context: AbstractContext):
     a, b = stack.pop2()
     a.assert_type_equal(type(b))
     res = IntType.from_value(compare(a, b))
     stack.push(res)
     stdout.append(format_stdout(cls.prim, [a, b], [res]))  # type: ignore
     return cls(stack_items_added=1)
Esempio n. 10
0
def execute_boolean_add(prim: str, stack: MichelsonStack, stdout: List[str], add: Callable):
    a, b = cast(Tuple[Union[BoolType, NatType], ...], stack.pop2())
    res_type, convert = dispatch_types(type(a), type(b), mapping={
        (BoolType, BoolType): (BoolType, bool),
        (NatType, NatType): (NatType, int)
    })
    val = add((convert(a), convert(b)))
    res = res_type.from_value(val)
    stack.push(res)
    stdout.append(format_stdout(prim, [a, b], [res]))
Esempio n. 11
0
def execute_shift(prim: str, stack: MichelsonStack, stdout: List[str],
                  shift: Callable[[Tuple[int, int]], int]):
    a, b = cast(Tuple[NatType, NatType], stack.pop2())
    a.assert_type_equal(NatType)
    b.assert_type_equal(NatType)
    assert int(b) < 257, f'shift overflow {int(b)}, should not exceed 256'
    c = shift((int(a), int(b)))
    res = NatType.from_value(c)
    stack.push(res)
    stdout.append(format_stdout(prim, [a, b], [res]))
Esempio n. 12
0
 def execute(cls, stack: MichelsonStack, stdout: List[str], context: AbstractContext):
     key, src = cast(Tuple[MichelsonType, Union[MapType, BigMapType]], stack.pop2())
     src.assert_type_in(MapType, BigMapType)
     val = src.get(key, dup=True)
     if val is None:
         res = OptionType.none(src.args[0])
     else:
         res = OptionType.from_some(val)
     stack.push(res)
     stdout.append(format_stdout(cls.prim, [key, src], [res]))
     return cls()
Esempio n. 13
0
 def execute(cls, stack: MichelsonStack, stdout: List[str], context: AbstractContext):
     a, b = cast(Tuple[Union[BoolType, NatType, IntType], ...], stack.pop2())
     res_type, convert = dispatch_types(type(a), type(b), mapping={
         (BoolType, BoolType): (BoolType, bool),
         (NatType, NatType): (NatType, int),
         (NatType, IntType): (NatType, int),
         (IntType, NatType): (NatType, int),
     })
     res = res_type.from_value(convert(a) & convert(b))
     stack.push(res)
     stdout.append(format_stdout(cls.prim, [a, b], [res]))  # type: ignore
     return cls(stack_items_added=1)
Esempio n. 14
0
 def execute(cls, stack: MichelsonStack, stdout: List[str], context: AbstractContext):
     param, lambda_ = cast(Tuple[MichelsonType, LambdaType], stack.pop2())
     assert isinstance(lambda_, LambdaType), f'expected lambda, got {lambda_.prim}'
     param.assert_type_equal(lambda_.args[0])
     stdout.append(format_stdout(cls.prim, [param, lambda_], []))  # type: ignore
     lambda_stack = MichelsonStack.from_items([param])
     lambda_body = cast(MichelsonInstruction, lambda_.value)
     item = lambda_body.execute(lambda_stack, stdout, context=context)
     res = lambda_stack.pop1()
     res.assert_type_equal(lambda_.args[1])
     assert len(lambda_stack) == 0, f'lambda stack is not empty {lambda_stack}'
     stack.push(res)
     return cls(item)
Esempio n. 15
0
 def execute(cls, stack: MichelsonStack, stdout: List[str], context: AbstractContext):
     ticket, amounts = cast(Tuple[TicketType, PairType], stack.pop2())
     ticket.assert_type_in(TicketType)
     amounts.assert_type_in(PairType)
     a, b = tuple(amounts)  # type: NatType, NatType
     a.assert_type_equal(NatType)
     b.assert_type_equal(NatType)
     res = ticket.split(int(a), int(b))
     if res is None:
         res = OptionType.none(PairType.create_type(args=[type(ticket), type(ticket)]))
     else:
         res = OptionType.from_some(PairType.from_comb(list(res)))
     stack.push(res)
     stdout.append(format_stdout(cls.prim, [ticket, amounts], [res]))
     return cls()
Esempio n. 16
0
    def execute(cls, stack: MichelsonStack, stdout: List[str], context: AbstractContext):
        left, lambda_ = cast(Tuple[MichelsonType, LambdaType], stack.pop2())
        lambda_.assert_type_in(LambdaType)
        lambda_.args[0].assert_type_in(PairType)
        left_type, right_type = lambda_.args[0].args
        left.assert_type_equal(left_type)

        new_value = MichelineSequence.create_type(args=[
            PushInstruction.create_type(args=[left_type, left.to_literal()]),
            PairInstruction,
            lambda_.value
        ])
        res = LambdaType.create_type(args=[right_type, lambda_.args[1]])(new_value)  # type: ignore
        stack.push(res)
        stdout.append(format_stdout(cls.prim, [left, lambda_], [res]))  # type: ignore
        return cls(stack_items_added=1)
Esempio n. 17
0
 def execute(cls, stack: MichelsonStack, stdout: List[str],
             context: AbstractContext):
     a, b = cast(
         Tuple[Union[IntType, NatType, MutezType, TimestampType], ...],
         stack.pop2())
     res_type, = dispatch_types(
         type(a),
         type(b),
         mapping={  # type: ignore
             (NatType, NatType): (IntType, ),
             (NatType, IntType): (IntType, ),
             (IntType, NatType): (IntType, ),
             (IntType, IntType): (IntType, ),
             (TimestampType, IntType): (TimestampType, ),
             (TimestampType, TimestampType): (IntType, ),
             (MutezType, MutezType): (MutezType, )
         }
     )  # type: Union[Type[IntType], Type[NatType], Type[TimestampType], Type[MutezType]]
     res = res_type.from_value(int(a) - int(b))
     stack.push(res)
     stdout.append(format_stdout(cls.prim, [a, b], [res]))  # type: ignore
     return cls(stack_items_added=1)