Example #1
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)
Example #2
0
 def execute(cls, stack: MichelsonStack, stdout: List[str], context: AbstractContext):
     key, val, src = cast(Tuple[MichelsonType, OptionType, Union[MapType, BigMapType]], stack.pop3())
     src.assert_type_in(MapType, BigMapType)
     prev_val, dst = src.update(key, None if val.is_none() else val.get_some())
     res = OptionType.none(src.args[1]) if prev_val is None else OptionType.from_some(prev_val)
     stack.push(dst)
     stack.push(res)
     stdout.append(format_stdout(cls.prim, [key, val, src], [res, dst]))
     return cls()
Example #3
0
 def execute(cls, stack: MichelsonStack, stdout: List[str],
             context: AbstractContext):
     a = cast(IntType, stack.pop1())
     a.assert_type_equal(IntType)
     if int(a) >= 0:
         res = OptionType.from_some(NatType.from_value(int(a)))
     else:
         res = OptionType.none(NatType)
     stack.push(res)
     stdout.append(format_stdout(cls.prim, [a], [res]))  # type: ignore
     return cls(stack_items_added=1)
Example #4
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()
Example #5
0
 def execute(cls, stack: MichelsonStack, stdout: List[str],
             context: AbstractContext):
     a = cast(BytesType, stack.pop1())
     a.assert_type_equal(BytesType)
     try:
         some = cls.args[0].unpack(bytes(a))
         res = OptionType.from_some(some)
     except Exception as e:
         stdout.append(f'{cls.prim}: {e}')
         res = OptionType.none(cls.args[0])
     stack.push(res)
     stdout.append(format_stdout(cls.prim, [a], [res]))
     return cls()
Example #6
0
 def execute(cls, stack: MichelsonStack, stdout: List[str], context: AbstractContext):
     pair = cast(PairType, stack.pop1())
     pair.assert_type_in(PairType)
     left, right = tuple(pair)
     assert isinstance(left, TicketType), f'expected ticket on the left, got {left.prim}'
     assert isinstance(right, TicketType), f'expected ticket on the right, got {right.prim}'
     res = TicketType.join(left, right)
     if res is None:
         res = OptionType.none(type(left))
     else:
         res = OptionType.from_some(res)
     stack.push(res)
     stdout.append(format_stdout(cls.prim, [pair], [res]))
     return cls()
Example #7
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()
Example #8
0
    def execute(cls, stack: MichelsonStack, stdout: List[str],
                context: AbstractContext):
        sequence = cast(MichelineSequence, cls.args[0])
        assert len(sequence.args
                   ) == 3, f'expected 3 sections, got {len(sequence.args)}'
        assert {arg.prim
                for arg in sequence.args} == {'parameter', 'storage',
                                              'code'}, f'unexpected sections'
        storage_type = cast(
            Type[MichelsonType],
            next(arg.args[0] for arg in sequence.args
                 if arg.prim == 'storage'))

        delegate, amount, initial_storage = cast(
            Tuple[OptionType, MutezType, MichelsonType], stack.pop3())
        delegate.assert_type_equal(OptionType.create_type(args=[KeyHashType]))
        amount.assert_type_equal(MutezType)
        initial_storage.assert_type_equal(storage_type)

        originated_address = AddressType.from_value(
            context.get_originated_address())
        context.spend_balance(int(amount))
        origination = OperationType.origination(
            source=context.get_self_address(),
            script=cls.args[0],  # type: ignore
            storage=initial_storage,
            balance=int(amount),
            delegate=None if delegate.is_none() else str(delegate.get_some()))

        stack.push(originated_address)
        stack.push(origination)
        stdout.append(
            format_stdout(cls.prim, [delegate, amount, initial_storage],
                          [origination, originated_address]))  # type: ignore
        return cls(stack_items_added=2)
Example #9
0
 def execute(cls, stack: MichelsonStack, stdout: List[str],
             context: AbstractContext):
     offset, length, s = cast(
         Tuple[NatType, NatType, Union[StringType, BytesType]],
         stack.pop3())
     offset.assert_type_equal(NatType)
     length.assert_type_equal(NatType)
     s.assert_type_in(StringType, BytesType)
     start, stop = int(offset), int(offset) + int(length)
     if 0 <= start <= stop <= len(s):
         res = OptionType.from_some(s[start:stop])
     else:
         res = OptionType.none(type(s))
     stack.push(res)
     stdout.append(format_stdout(cls.prim, [offset, length, s], [res]))
     return cls()
Example #10
0
 def execute(cls, stack: MichelsonStack, stdout: List[str],
             context: AbstractContext):
     some = stack.pop1()
     res = OptionType.from_some(some)
     stack.push(res)
     stdout.append(format_stdout(cls.prim, [some], [res]))  # type: ignore
     return cls(stack_items_added=1)
Example #11
0
    def execute(cls, stack: MichelsonStack, stdout: List[str],
                context: AbstractContext):
        delegate = cast(OptionType, stack.pop1())
        delegate.assert_type_equal(OptionType.create_type(args=[KeyHashType]))

        delegation = OperationType.delegation(
            source=context.get_self_address(),
            delegate=None if delegate.is_none() else str(delegate.get_some()))
        stack.push(delegation)
        stdout.append(format_stdout(cls.prim, [delegate], [delegation]))
        return cls()
Example #12
0
 def execute(cls, stack: MichelsonStack, stdout: List[str],
             context: AbstractContext):
     entrypoint = next(iter(cls.field_names), 'default')
     address = cast(AddressType, stack.pop1())
     address.assert_type_in(AddressType)
     entrypoint_type = get_entrypoint_type(context,
                                           entrypoint,
                                           address=str(address))
     contract_type = ContractType.create_type(args=cls.args)
     try:
         if entrypoint_type is None:
             stdout.append(
                 f'{cls.prim}: skip type checking for {str(address)}')
         else:
             entrypoint_type.assert_type_equal(cls.args[0])
         res = OptionType.from_some(
             contract_type.from_value(f'{str(address)}%{entrypoint}'))
     except AssertionError:
         res = OptionType.none(contract_type)
     stack.push(res)
     stdout.append(format_stdout(cls.prim, [address], [res]))
     return cls()
Example #13
0
 def execute(cls, stack: MichelsonStack, stdout: List[str],
             context: AbstractContext):
     res = OptionType.none(cls.args[0])  # type: ignore
     stack.push(res)
     stdout.append(format_stdout(cls.prim, [], [res]))  # type: ignore
     return cls(stack_items_added=1)