Skip to content

Commit

Permalink
Add basic test pytest setup.
Browse files Browse the repository at this point in the history
  • Loading branch information
jacqueswww committed Mar 18, 2019
1 parent a2ecc0f commit 68c32ce
Show file tree
Hide file tree
Showing 7 changed files with 363 additions and 31 deletions.
19 changes: 19 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
.PHONY: make_env test lint


env:
python3 -m venv env


init: make_env
source ./env/bin/activate; \
pip install --upgrade pip; \
pip install -r requirements.txt; \

test:
source ./env/bin/activate; \
pytest; \

lint:
source ./env/bin/activate; \
flake8 tests; \
4 changes: 4 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
vyper==0.1.0b9
https://github.com/status-im/vyper-debug/archive/master.zip
pytest==4.3.1
flake8==3.7.7
140 changes: 140 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
import os

import pytest

from vyper import compile_code

from eth_tester import (
EthereumTester,
)
from web3.providers.eth_tester import (
EthereumTesterProvider,
)
from web3 import (
Web3,
)
from web3.contract import (
ConciseContract,
)
from vdb.vdb import (
VyperDebugCmd
)
from vdb.eth_tester_debug_backend import (
PyEVMDebugBackend,
set_debug_info
)
from vdb.source_map import (
produce_source_map
)


@pytest.fixture()
def tester():
t = EthereumTester(backend=PyEVMDebugBackend())
return t


def zero_gas_price_strategy(web3, transaction_params=None):
return 0 # zero gas price makes testing simpler.


@pytest.fixture()
def w3(tester):
w3 = Web3(EthereumTesterProvider(tester))
w3.eth.setGasPriceStrategy(zero_gas_price_strategy)
w3.eth.defaultAccount = w3.eth.accounts[0]
return w3


def _get_contract(w3, source_code, *args, **kwargs):
interface_codes = kwargs.get('interface_codes')
compiler_output = compile_code(
source_code,
['bytecode', 'abi'],
interface_codes=interface_codes,
)
abi = compiler_output['abi']
bytecode = compiler_output['bytecode']
contract = w3.eth.contract(abi=abi, bytecode=bytecode)

# Enable vdb.
source_map = produce_source_map(source_code, interface_codes=interface_codes)
set_debug_info(source_code, source_map)
import vdb
setattr(vdb.debug_computation.DebugComputation, 'enable_debug', True)
constructor_args = kwargs.get('constructor_args', [])
value = kwargs.pop('value', 0)
value_in_eth = kwargs.pop('value_in_eth', 0)
value = value_in_eth * 10**18 if value_in_eth else value # Handle deploying with an eth value.
gasPrice = kwargs.pop('gasPrice', 0)
deploy_transaction = {
'from': w3.eth.accounts[0],
'data': contract._encode_constructor_data(constructor_args),
'value': value,
'gasPrice': gasPrice,
}
tx = w3.eth.sendTransaction(deploy_transaction)
tx_receipt = w3.eth.getTransactionReceipt(tx)
if tx_receipt['status'] == 0:
import ipdb; ipdb.set_trace()
raise Exception('Could not deploy contract! {}'.format(tx_receipt))
address = tx_receipt['contractAddress']
contract = w3.eth.contract(address, abi=abi, bytecode=bytecode)
# Filter logs.
contract._logfilter = w3.eth.filter({
'fromBlock': w3.eth.blockNumber - 1,
'address': contract.address
})
return ConciseContract(contract)


@pytest.fixture
def get_logs(w3):
def get_logs(tx_hash, c, event_name):
tx_receipt = w3.eth.getTransactionReceipt(tx_hash)
logs = c._classic_contract.events[event_name]().processReceipt(tx_receipt)
return logs
return get_logs


@pytest.fixture
def get_contract(w3):
def get_contract(source_code, *args, **kwargs):
return _get_contract(w3, source_code, *args, **kwargs)
return get_contract


def create_contract(w3, get_contract, path, constructor_args, interface_codes=None):
wd = os.path.dirname(os.path.realpath(__file__))
with open(os.path.join(wd, os.pardir, path)) as f:
source_code = f.read()
return get_contract(source_code, constructor_args=constructor_args, interface_codes=interface_codes)


@pytest.fixture
def SNT_token(w3, get_contract):
return create_contract(
w3=w3,
get_contract=get_contract,
path='vyper/ERC20.vy',
constructor_args=['Status Network Token', 'SNT', 18, 3470483788]
)


@pytest.fixture
def DappStore(w3, get_contract, SNT_token):
wd = os.path.dirname(os.path.realpath(__file__))
with open(os.path.join(wd, os.pardir, 'vyper/ApproveAndCallFallBack.vy')) as f:
interface_codes = {
'ApproveAndCallFallBackInterface': {
'type': 'vyper',
'code': f.read()
}
}
return create_contract(
w3=w3,
get_contract=get_contract,
path='vyper/DAppStore.vy',
constructor_args=[SNT_token.address],
interface_codes=interface_codes
)
12 changes: 12 additions & 0 deletions tests/token/test_token_init.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@


def test_erc20_deploy(w3, SNT_token):
a0 = w3.eth.accounts[0]
assert SNT_token.name() == "Status Network Token"
assert SNT_token.symbol() == "SNT"
assert SNT_token.decimals() == 18
assert SNT_token.total_supply() == 3470483788*10**18


def test_dappstore_deploy(w3, SNT_token, DappStore):
assert DappStore.SNT() == SNT_token.address
34 changes: 3 additions & 31 deletions vyper/DAppStore.vy
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import ApproveAndCallFallBack as ApproveAndCallFallBackInterface

implements: ApproveAndCallFallBackInterface
# implements: ApproveAndCallFallBackInterface

struct Data:
developer: address
Expand Down Expand Up @@ -46,7 +46,7 @@ total: public(uint256)
ceiling: public(uint256)
maxStake: public(uint256)

SNT: MiniMeTokenInterface
SNT: public(MiniMeTokenInterface)

#Constant functions
@public
Expand Down Expand Up @@ -242,32 +242,4 @@ def withdraw(_id: bytes32, _amount: uint256):

log.Withdraw(_id, _amount, dapp.effective_balance)

@public
def receiveApproval(_from: address, _amount: uint256, _token: address, _data: bytes[132]):
"""
@notice Support for "approveAndCall".
@param _from Who approved.
@param _amount Amount being approved, needs to be equal `_amount` or `cost`
@param _token Token being approved, needs to be `SNT`
@param _data Abi encoded data with selector of `register(bytes32,address,bytes32,bytes32)`
"""
assert _token == msg.sender, "Wrong account"
assert _token == self.SNT, "Wrong token"

#decode signature
sig: bytes[4] = slice(_data, start=0, len=4)
#decode id
id: bytes32 = extract32(_data, 4, type=bytes32)
#decode amount
amount: uint256 = convert(extract32(_data, 32, type=bytes32), uint256)

assert amount == _amount, "Wrong amount"

if (sig == b"\x1a\x21\x4f\x43"):
self._createDapp(_from, id, amount)
elif (sig == b"\xac\x76\x90\x90"):
self._downvote(_from, id, amount)
elif (sig == b"\x2b\x3d\xf6\x90"):
self._upvote(_from, id, amount)
else:
assert False, "Wrong method selector"
# Snipped: receiveApproval
154 changes: 154 additions & 0 deletions vyper/ERC20.vy
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
# @dev Implementation of ERC-20 token standard.
# @author Takayuki Jimba (@yudetamago)
# https://github.com/ethereum/EIPs/blob/master/EIPS/eip-20.md

Transfer: event({_from: indexed(address), _to: indexed(address), _value: uint256})
Approval: event({_owner: indexed(address), _spender: indexed(address), _value: uint256})

name: public(string[64])
symbol: public(string[32])
decimals: public(uint256)

# NOTE: By declaring `balanceOf` as public, vyper automatically generates a 'balanceOf()' getter
# method to allow access to account balances.
# The _KeyType will become a required parameter for the getter and it will return _ValueType.
# See: https://vyper.readthedocs.io/en/v0.1.0-beta.8/types.html?highlight=getter#mappings
balanceOf: public(map(address, uint256))
allowances: map(address, map(address, uint256))
total_supply: public(uint256)
minter: address


@public
def __init__(_name: string[64], _symbol: string[32], _decimals: uint256, _supply: uint256):
init_supply: uint256 = _supply * 10 ** _decimals
self.name = _name
self.symbol = _symbol
self.decimals = _decimals
self.balanceOf[msg.sender] = init_supply
self.total_supply = init_supply
self.minter = msg.sender
log.Transfer(ZERO_ADDRESS, msg.sender, init_supply)


@public
@constant
def totalSupply() -> uint256:
"""
@dev Total number of tokens in existence.
"""
return self.total_supply


@public
@constant
def allowance(_owner : address, _spender : address) -> uint256:
"""
@dev Function to check the amount of tokens that an owner allowed to a spender.
@param _owner The address which owns the funds.
@param _spender The address which will spend the funds.
@return An uint256 specifying the amount of tokens still available for the spender.
"""
return self.allowances[_owner][_spender]


@public
def transfer(_to : address, _value : uint256) -> bool:
"""
@dev Transfer token for a specified address
@param _to The address to transfer to.
@param _value The amount to be transferred.
"""
# NOTE: vyper does not allow unterflows
# so the following subtraction would revert on insufficient balance
self.balanceOf[msg.sender] -= _value
self.balanceOf[_to] += _value
log.Transfer(msg.sender, _to, _value)
return True


@public
def transferFrom(_from : address, _to : address, _value : uint256) -> bool:
"""
@dev Transfer tokens from one address to another.
Note that while this function emits a Transfer event, this is not required as per the specification,
and other compliant implementations may not emit the event.
@param _from address The address which you want to send tokens from
@param _to address The address which you want to transfer to
@param _value uint256 the amount of tokens to be transferred
"""
# NOTE: vyper does not allow unterflows
# so the following subtraction would revert on insufficient balance
self.balanceOf[_from] -= _value
self.balanceOf[_to] += _value
# NOTE: vyper does not allow underflows
# so the following subtraction would revert on insufficient allowance
self.allowances[_from][msg.sender] -= _value
log.Transfer(_from, _to, _value)
return True


@public
def approve(_spender : address, _value : uint256) -> bool:
"""
@dev Approve the passed address to spend the specified amount of tokens on behalf of msg.sender.
Beware that changing an allowance with this method brings the risk that someone may use both the old
and the new allowance by unfortunate transaction ordering. One possible solution to mitigate this
race condition is to first reduce the spender's allowance to 0 and set the desired value afterwards:
https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729
@param _spender The address which will spend the funds.
@param _value The amount of tokens to be spent.
"""
self.allowances[msg.sender][_spender] = _value
log.Approval(msg.sender, _spender, _value)
return True


@public
def mint(_to: address, _value: uint256):
"""
@dev Mint an amount of the token and assigns it to an account.
This encapsulates the modification of balances such that the
proper events are emitted.
@param _to The account that will receive the created tokens.
@param _value The amount that will be created.
"""
assert msg.sender == self.minter
assert _to != ZERO_ADDRESS
self.total_supply += _value
self.balanceOf[_to] += _value
log.Transfer(ZERO_ADDRESS, _to, _value)


@private
def _burn(_to: address, _value: uint256):
"""
@dev Internal function that burns an amount of the token of a given
account.
@param _to The account whose tokens will be burned.
@param _value The amount that will be burned.
"""
assert _to != ZERO_ADDRESS
self.total_supply -= _value
self.balanceOf[_to] -= _value
log.Transfer(_to, ZERO_ADDRESS, _value)


@public
def burn(_value: uint256):
"""
@dev Burn an amount of the token of msg.sender.
@param _value The amount that will be burned.
"""
self._burn(msg.sender, _value)


@public
def burnFrom(_to: address, _value: uint256):
"""
@dev Burn an amount of the token from a given account.
@param _to The account whose tokens will be burned.
@param _value The amount that will be burned.
"""
self.allowances[_to][msg.sender] -= _value
self._burn(_to, _value)
Loading

0 comments on commit 68c32ce

Please sign in to comment.