예제 #1
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)
예제 #2
0
    def execute(cls, stack: MichelsonStack, stdout: List[str],
                context: AbstractContext):
        parameter, amount, destination = cast(
            Tuple[MichelsonType, MutezType, ContractType], stack.pop3())
        amount.assert_type_equal(MutezType)
        assert isinstance(
            destination,
            ContractType), f'expected contract, got {destination.prim}'
        parameter.assert_type_equal(destination.args[0])

        ep_type = get_entrypoint_type(context,
                                      destination.get_entrypoint(),
                                      address=destination.get_address())
        if ep_type:
            parameter.assert_type_equal(
                ep_type, message='destination contract parameter')

        transaction = OperationType.transaction(
            source=context.get_self_address(),
            destination=destination.get_address(),
            amount=int(amount),
            entrypoint=destination.get_entrypoint(),
            parameter=parameter)
        stack.push(transaction)
        stdout.append(
            format_stdout(cls.prim, [parameter, amount, destination],
                          [transaction]))  # type: ignore
        return cls(stack_items_added=1)
예제 #3
0
파일: tezos.py 프로젝트: kengkiat/pytezos
    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()
예제 #4
0
    def test_execute_contract_operations(self) -> None:
        # Arrange
        interpreter = Interpreter()
        code = """
            PATCH SENDER "tz1VSUr8wwNhLAzempoch5d6hLRiTh8Cjcjb" ;
            PATCH BALANCE 200 ;
            
            parameter mutez ;
            storage unit ;
            
            BEGIN 100 Unit ;
            
                CAR ;
                DUP ;
                BALANCE ;
                IFCMPLT
                    { FAIL }
                    {
                        SENDER ;
                        CONTRACT unit ;
                        IF_NONE
                            { FAIL }
                            {
                                SWAP ;
                                UNIT ;
                                TRANSFER_TOKENS ;
                                NIL operation ;
                                SWAP ;
                                CONS ;
                                UNIT ;
                                SWAP ;
                                PAIR
                            } ;
                    };
            COMMIT
        """

        # Act
        result = interpreter.execute(code)

        # Assert
        self.assertEqual(None, result.error)
        self.assertEqual(
            [
                "parameter: updated",
                "storage: updated",
                "BEGIN %default / _ => (0.0001 * Unit)",
                "CAR / (0.0001 * Unit) => 0.0001",
                "DUP / 0.0001 => 0.0001 : 0.0001",
                "BALANCE / _ => 0.0002",
                "COMPARE / 0.0002 : 0.0001 => 1",
                "LT / 1 => False",
                "IF / False => _",
                "SENDER / _ => tz1VSU…cjb",
                "CONTRACT: skip type checking for tz1VSUr8wwNhLAzempoch5d6hLRiTh8Cjcjb",
                "CONTRACT / tz1VSU…cjb => tz1VSU…cjb%default?",
                "IF_NONE / tz1VSU…cjb%default? => tz1VSU…cjb%default",
                "SWAP / tz1VSU…cjb%default : 0.0001 => 0.0001 : tz1VSU…cjb%default",
                "UNIT / _ => Unit",
                "TRANSFER_TOKENS / Unit : 0.0001 : tz1VSU…cjb%default => transaction",
                "NIL / _ => []",
                "SWAP / [] : transaction => transaction : []",
                "CONS / transaction : [] => [transaction]",
                "UNIT / _ => Unit",
                "SWAP / Unit : [transaction] => [transaction] : Unit",
                "PAIR / [transaction] : Unit => ([transaction] * Unit)",
                "END %default / ([transaction] * Unit) => _",
            ],
            result.stdout,
        )
        commit_instruction = next((i for i in result.instructions.items[::-1]
                                   if isinstance(i, CommitInstruction)))
        self.assertEqual(
            PairType((
                ListType([
                    OperationType({
                        "kind": "transaction",
                        "source": "KT1BEqzn5Wx8uJrZNvuS9DVHmLvG9td3fDLi",
                        "destination": "tz1VSUr8wwNhLAzempoch5d6hLRiTh8Cjcjb",
                        "amount": "100",
                        "parameters": {
                            "entrypoint": "default",
                            "value": {
                                "prim": "Unit"
                            },
                        },
                    })
                ]),
                UnitType(),
            )),
            commit_instruction.result,
        )