Skip to content

Instantly share code, notes, and snippets.

@pcaversaccio
Last active March 16, 2024 10:34
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pcaversaccio/cebfca2e26646bb619a52b1ff9ab2c07 to your computer and use it in GitHub Desktop.
Save pcaversaccio/cebfca2e26646bb619a52b1ff9ab2c07 to your computer and use it in GitHub Desktop.
Guide to get started with Vyper modules.

🐍 How to get started with Vyper modules

1. Create a new, clean testing directory test

mkdir test
cd test

2. Set up pyenv

  • Linux
sudo apt update -y
sudo apt install -y make build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev libncursesw5-dev xz-utils tk-dev libffi-dev liblzma-dev python-openssl git
curl https://pyenv.run | bash
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc
echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc
echo -e 'if command -v pyenv 1>/dev/null 2>&1; then\n eval "$(pyenv init -)"\nfi' >> ~/.bashrc
exec "$SHELL"

3. Install Python version 3.11

pyenv install 3.11.8
pyenv global 3.11.8 # If you only want to set the local Python version, you can invoke `pyenv local 3.11.8` instead.

4. Create a virtual environment

python -m venv venv
source venv/bin/activate # If you use Windows, you can active the virtual environment by invoking `venv\Scripts\Activate.ps1` instead.

5. Install the latest Vyper and 🐍 snekmate versions

pip install git+https://github.com/vyperlang/vyper.git@master
pip install git+https://github.com/pcaversaccio/snekmate.git@modules

6. Create a Vyper contract and import modules

vi Test.vy

Paste the following snippet into Test.vy:

# pragma version ~=0.4.0b5

from snekmate.utils import ECDSA

@external
@pure
def recover_sig(hash: bytes32, signature: Bytes[65]) -> address:
    assert len(signature) == 65, "ECDSA: only 65-byte signatures supported"
    r: uint256 = extract32(signature, empty(uint256), output_type=uint256)
    s: uint256 = extract32(signature, 32, output_type=uint256)
    v: uint256 = convert(slice(signature, 64, 1), uint256)
    return ECDSA._try_recover_vrs(hash, v, r, s)

Important

You can import all 🐍 snekmate logic via the namespace snekmate, i.e. snekmate.auth, snekmate.tokens etc.

7. Compile Test.vy

vyper Test.vy

This will output:

0x6102e9610011610000396102e9610000f35f3560e01c6325d869828118610194576064361034176102e55760243560040160418135116102e5576020813501808261018037505060416101805118156100cb576028610200527f45434453413a206f6e6c792036352d62797465207369676e6174757265732073610220527f7570706f727465640000000000000000000000000000000000000000000000006102405261020050610200518061022001601f825f031636823750506308c379a06101c05260206101e052601f19601f6102005101166044016101dcfd5b6101807fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff602082510313600116156102e5575f60051c60051b602001810151905061020052610180601f602082510313600116156102e557602060051c60051b602001810151905061022052610180516041116102e5576101e051610280526001610260526102606020810151815160200360031b1c905061024052602060043560405261024051606052610200516080526102205160a05261018f610260610198565b610260f35b5f5ffd5b7f7fffffffffffffffffffffffffffffff5d576e7357a4501ddfe92f46681b20a060a051111561024357602260c0527f45434453413a20696e76616c6964207369676e6174757265206073602076616c60e0527f75650000000000000000000000000000000000000000000000000000000000006101005260c05060c0518060e001601f825f031636823750506308c379a0608052602060a052601f19601f60c0510116604401609cfd5b5f6101605260405160e052606051610100526080516101205260a051610140526020610160608060e060015afa506101605160c05260c0516102dd57601860e0527f45434453413a20696e76616c6964207369676e617475726500000000000000006101005260e05060e0518061010001601f825f031636823750506308c379a060a052602060c052601f19601f60e051011660440160bcfd5b60c051815250565b5f80fd841902e98000a16576797065728300030b0013

πŸŽ‰ Congratulations, you just compiled a Vyper contract using modules!

πŸ“š Further References

@0xClandestine
Copy link

🀝

@iFrostizz
Copy link

🐍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment