Back to glossary

Reentrancy Lock (Vyper Code Example)

Table of Contents

Vyper has a handy way to secure your contract from reentrancy.

A reentrancy lock can be created on a function with @nonreentrant.

Example Vyper contract with reentrancy lock

# pragma version ^0.4.0

@external
@nonreentrant
def func():
    raw_call(msg.sender, b"", value=0)

Example Vyper contract without reentrancy lock

# pragma version ^0.4.0

interface ILock:
    def func(): nonpayable

count: public(uint256)

@external
@payable
def __default__():
    if self.count < 2:
        self.count += 1
        extcall ILock(msg.sender).func()

@external
def reset():
    self.count = 0

@external
def test_lock(to: address):
    extcall ILock(to).func()

Related Terms

No items found.