Testing a Contract

The following example demonstrates how to compile and deploy your vyper contract. It requires pyethereum>=2.0.0 for the tester module

from vyper import compiler
from ethereum.tools import tester

# Get a new chain
chain = tester.Chain()
# Set the vyper compiler to run when the vyper language is requested
tester.languages['vyper'] = compiler.Compiler()

with open('my_contract.vy' 'r') as f:
    source_code = f.read()
# Compile and Deploy contract to provisioned testchain
# (e.g. run __init__ method) with given args (e.g. init_args)
# from msg.sender = t.k1 (private key of address 1 in test acconuts)
# and supply 1000 wei to the contract
init_args = ['arg1', 'arg2', 3]
contract = chain.contract(source_code, language="vyper",
        init_args, sender=t.k1, value=1000)

contract.myMethod() # Executes myMethod on the tester "chain"
chain.mine() # Mines the above transaction (and any before it) into a block

Note: We are working on integration with ethereum-tester, so this example will change.

Testing Using vyper-run Command

To allow quickly testing contracts, Vyper provides a command line tool for instantly executing a function:

vyper-run yourFileName.vy "yourFunction();" -i some_init_param, another_init_param

The vyper-run command is composed of 4 parts:

  • vyper-run
  • the name of the contract file you want to execute (for example: coolContract.vy)
  • a string (wrapped in double quotes) with the function you want to trigger, you can trigger multiple functions by adding a semicolon at the end of each function and then call the next function (for example: "myFunction1(100,4);myFunction2()") +
  • (Optional) the parameters for the __init__ function of the contract (for example: given __init__(a: int128, b: int128) the syntax would be -i 8,27).

Putting it all together:

vyper-run myContract.vy "myFunction1();myFunction2()" -i 1,3

The vyper-run command will print out the returned value of the called functions as well as all the logged events emitted during the function’s execution.

Deploying a Contract

You have several options to deploy a Vyper contract to the public testnets.

One option is to take the bytecode generated by the vyper compiler and manually deploy it through mist or geth:

vyper yourFileName.vy
# returns bytecode

Or deploy it with current browser on myetherwallet contract menu.

We are working on integration with populus, this will be the preferred way of deploying vyper contracts in the future.