How to use vyper - 10 common examples

To help you get started, we’ve selected a few vyper examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github vyperlang / vyper / vyper / parser / expr.py View on Github external
pos=getpos(self.expr),
                )
            # Literal is large enough (mostly likely) becomes uint256.
            else:
                return LLLnode.from_list(
                    self.expr.n,
                    typ=BaseType('uint256', unit={}, is_literal=True),
                    pos=getpos(self.expr),
                )

        elif isinstance(self.expr.n, float):
            numstring, num, den = get_number_as_fraction(self.expr, self.context)
            # if not SizeLimits.in_bounds('decimal', num // den):
            # if not SizeLimits.MINDECIMAL * den <= num <= SizeLimits.MAXDECIMAL * den:
            if not (SizeLimits.MINNUM * den < num < SizeLimits.MAXNUM * den):
                raise InvalidLiteralException("Number out of range: " + numstring, self.expr)
            if DECIMAL_DIVISOR % den:
                raise InvalidLiteralException(
                    "Type 'decimal' has maximum 10 decimal places",
                    self.expr
                )
            return LLLnode.from_list(
                num * DECIMAL_DIVISOR // den,
                typ=BaseType('decimal', unit=None, is_literal=True),
                pos=getpos(self.expr),
            )

        # Binary literal.
        elif orignum[:2] == '0b':
            str_val = orignum[2:]
            total_bits = len(orignum[2:])
            total_bits = (
github vyperlang / vyper / vyper / parser / parser_utils.py View on Github external
def make_byte_slice_copier(destination, source, length, max_length, pos=None):
    # Special case: memory to memory
    if source.location == "memory" and destination.location == "memory":
        return LLLnode.from_list([
            'with', '_l', max_length,
            [
                'pop',
                ['call', 18 + max_length // 10, 4, 0, source, '_l', destination, '_l']
            ]
        ], typ=None, annotation='copy byte slice dest: %s' % str(destination))
    # Copy over data
    if isinstance(source.typ, NullType):
        loader = 0
    elif source.location == "memory":
        loader = ['mload', ['add', '_pos', ['mul', 32, ['mload', MemoryPositions.FREE_LOOP_INDEX]]]]
    elif source.location == "storage":
        loader = ['sload', ['add', '_pos', ['mload', MemoryPositions.FREE_LOOP_INDEX]]]
    else:
        raise Exception("Unsupported location:" + source.location)
    # Where to paste it?
    if destination.location == "memory":
        setter = [
            'mstore',
            ['add', '_opos', ['mul', 32, ['mload', MemoryPositions.FREE_LOOP_INDEX]]],
            loader
        ]
    elif destination.location == "storage":
        setter = ['sstore', ['add', '_opos', ['mload', MemoryPositions.FREE_LOOP_INDEX]], loader]
    else:
        raise Exception("Unsupported location:" + destination.location)
    # Check to see if we hit the length
github vyperlang / vyper / vyper / parser / parser_utils.py View on Github external
'pop',
                ['call', 18 + max_length // 10, 4, 0, source, '_l', destination, '_l']
            ]
        ], typ=None, annotation=f'copy byte slice dest: {str(destination)}')

    # special case: rhs is zero
    if source.value is None:

        if destination.location == 'memory':
            return mzero(destination, max_length)

        else:
            loader = 0
    # Copy over data
    elif source.location == "memory":
        loader = ['mload', ['add', '_pos', ['mul', 32, ['mload', MemoryPositions.FREE_LOOP_INDEX]]]]
    elif source.location == "storage":
        loader = ['sload', ['add', '_pos', ['mload', MemoryPositions.FREE_LOOP_INDEX]]]
    else:
        raise CompilerPanic(f'Unsupported location: {source}')
    # Where to paste it?
    if destination.location == "memory":
        setter = [
            'mstore',
            ['add', '_opos', ['mul', 32, ['mload', MemoryPositions.FREE_LOOP_INDEX]]],
            loader
        ]
    elif destination.location == "storage":
        setter = ['sstore', ['add', '_opos', ['mload', MemoryPositions.FREE_LOOP_INDEX]], loader]
    else:
        raise CompilerPanic("Unsupported location:" + destination.location)
    # Check to see if we hit the length
github vyperlang / vyper / vyper / parser / parser_utils.py View on Github external
setter = ['sstore', ['add', '_opos', ['mload', MemoryPositions.FREE_LOOP_INDEX]], loader]
    else:
        raise Exception("Unsupported location:" + destination.location)
    # Check to see if we hit the length
    checker = [
        'if',
        ['gt', ['mul', 32, ['mload', MemoryPositions.FREE_LOOP_INDEX]], '_actual_len'],
        'break'
    ]
    # Make a loop to do the copying
    o = [
        'with', '_pos', source, [
            'with', '_opos', destination, [
                'with', '_actual_len', length, [
                    'repeat',
                    MemoryPositions.FREE_LOOP_INDEX,
                    0,
                    (max_length + 31) // 32,
                    ['seq', checker, setter]
                ]
            ]
        ]
    ]
    return LLLnode.from_list(
        o,
        typ=None,
        annotation='copy byte slice src: %s dst: %s' % (source, destination),
        pos=pos,
    )
github vyperlang / vyper / tests / parser / syntax / test_tuple_assign.py View on Github external
)
from vyper.exceptions import (
    ConstancyViolationException,
    TypeMismatchException,
    VariableDeclarationException,
)

fail_list = [
    ("""
@public
def test():
    a: int128
    b: int128
    c: int128
    a, b, c = 1, 2, 3
    """, VariableDeclarationException),
    """
@public
def out_literals() -> (int128, int128, bytes[10]):
    return 1, 2, b"3333"

@public
def test() -> (int128, address, bytes[10]):
    a: int128
    b: int128
    a, b, b = self.out_literals()  # incorrect bytes type
    return a, b, c
    """,
    """
@public
def out_literals() -> (int128, int128, bytes[10]):
    return 1, 2, b"3333"
github vyperlang / vyper / tests / parser / syntax / test_constants.py View on Github external
""", VariableDeclarationException),
    # signature variable with same name
    ("""
VAL: constant(bytes[4]) = b"t"

@public
def test(VAL: uint256):
    pass
    """, FunctionDeclarationException),
    ("""
VAL: constant(decimal) = 2e-8
    """, InvalidLiteralException),
    ("""
C1: constant(uint256) = block.number
C2: constant(uint256) = convert(C1, uint256)
    """, InvalidLiteralException),
]


@pytest.mark.parametrize('bad_code', fail_list)
def test_constants_fail(bad_code):
    if isinstance(bad_code, tuple):
        with raises(bad_code[1]):
            compiler.compile_code(bad_code[0])
    else:
        with raises(StructureException):
            compiler.compile_code(bad_code)


valid_list = [
    """
VAL: constant(uint256) = 123
github vyperlang / vyper / tests / parser / syntax / test_block.py View on Github external
def test_block_fail(bad_code):

    if isinstance(bad_code, tuple):
        with raises(bad_code[1]):
            compiler.compile(bad_code[0])
    else:
        with raises(TypeMismatchException):
            compiler.compile(bad_code)
github vyperlang / vyper / tests / parser / syntax / test_as_uint256.py View on Github external
def test_as_wei_fail(bad_code):

    with raises(TypeMismatchException):
        compiler.compile(bad_code)
github vyperlang / vyper / tests / compiler / test_sha3_32.py View on Github external
def test_sha3_32():
    lll = ['sha3_32', 0]
    evm = ['PUSH1', 0, 'PUSH1', 192, 'MSTORE', 'PUSH1', 32, 'PUSH1', 192, 'SHA3']
    assert compile_lll.compile_to_assembly(LLLnode.from_list(lll)) == evm
    assert compile_lll.compile_to_assembly(optimizer.optimize(LLLnode.from_list(lll))) == evm
github vyperlang / vyper / tests / parser / syntax / test_unbalanced_return.py View on Github external
def test_return_mismatch(bad_code):
    with raises(StructureException):
        compiler.compile_code(bad_code)