Skip to content
This repository has been archived by the owner on Sep 17, 2021. It is now read-only.

Updated Vyper to v0.1.0b14 #44

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
181 changes: 130 additions & 51 deletions contracts/test_contracts/ERC20.vy
Original file line number Diff line number Diff line change
@@ -1,81 +1,160 @@
# THIS CONTRACT IS FOR TESTING PURPOSES AND IS NOT PART OF THE PROJECT

# Modified from: https://github.com/ethereum/vyper/blob/master/examples/tokens/ERC20_solidity_compatible/ERC20.v.py
# @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(wei)})
Approval: event({_owner: indexed(address), _spender: indexed(address), _value: uint256(wei)})
from vyper.interfaces import ERC20

name: public(bytes32)
symbol: public(bytes32)
implements: ERC20

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)
balances: uint256(wei)[address]
allowances: (uint256(wei)[address])[address]
total_supply: uint256(wei)

# 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: uint256
minter: address


@public
def __init__(_name: bytes32, _symbol: bytes32, _decimals: uint256, _supply: uint256(wei)):
_sender: address = msg.sender
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.balances[_sender] = _supply
self.total_supply = _supply
log.Transfer(ZERO_ADDRESS, _sender, _supply)

@public
@payable
def deposit():
_value: uint256(wei) = msg.value
_sender: address = msg.sender
self.balances[_sender] = self.balances[_sender] + _value
self.total_supply = self.total_supply + _value
log.Transfer(ZERO_ADDRESS, _sender, _value)
self.balanceOf[msg.sender] = init_supply
self.total_supply = init_supply
self.minter = msg.sender
log.Transfer(ZERO_ADDRESS, msg.sender, init_supply)

@public
def withdraw(_value : uint256(wei)) -> bool:
_sender: address = msg.sender
self.balances[_sender] = self.balances[_sender] - _value
self.total_supply = self.total_supply - _value
send(_sender, _value)
log.Transfer(_sender, ZERO_ADDRESS, _value)
return True

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


@public
@constant
def balanceOf(_owner : address) -> uint256(wei):
return self.balances[_owner]
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(wei)) -> bool:
_sender: address = msg.sender
self.balances[_sender] = self.balances[_sender] - _value
self.balances[_to] = self.balances[_to] + _value
log.Transfer(_sender, _to, _value)
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 underflows
# 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(wei)) -> bool:
_sender: address = msg.sender
allowance: uint256(wei) = self.allowances[_from][_sender]
self.balances[_from] = self.balances[_from] - _value
self.balances[_to] = self.balances[_to] + _value
self.allowances[_from][_sender] = allowance - _value
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 underflows
# 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(wei)) -> bool:
_sender: address = msg.sender
self.allowances[_sender][_spender] = _value
log.Approval(_sender, _spender, _value)
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
@constant
def allowance(_owner : address, _spender : address) -> uint256(wei):
return self.allowances[_owner][_spender]
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)
31 changes: 16 additions & 15 deletions contracts/uniswap_exchange.vy
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# @title Uniswap Exchange Interface V1
# @notice Source code found at https://github.com/uniswap
# @notice Use at your own risk
from vyper.interfaces import ERC20

contract Factory():
def getExchange(token_addr: address) -> address: constant
Expand All @@ -21,18 +22,18 @@ name: public(bytes32) # Uniswap V1
symbol: public(bytes32) # UNI-V1
decimals: public(uint256) # 18
totalSupply: public(uint256) # total number of UNI in existence
balances: uint256[address] # UNI balance of an address
allowances: (uint256[address])[address] # UNI allowance of one address on another
token: address(ERC20) # address of the ERC20 token traded on this contract
balances: map(address,uint256) # UNI balance of an address
allowances: map(address, map(address, uint256)) # UNI allowance of one address on another
token: ERC20 # address of the ERC20 token traded on this contract
factory: Factory # interface for the factory that created this contract

# @dev This function acts as a contract constructor which is not currently supported in contracts deployed
# using create_with_code_of(). It is called once by the factory during contract creation.
@public
def setup(token_addr: address):
assert (self.factory == ZERO_ADDRESS and self.token == ZERO_ADDRESS) and token_addr != ZERO_ADDRESS
self.factory = msg.sender
self.token = token_addr
self.factory = Factory(msg.sender)
self.token = ERC20(token_addr)
self.name = 0x556e697377617020563100000000000000000000000000000000000000000000
self.symbol = 0x554e492d56310000000000000000000000000000000000000000000000000000
self.decimals = 18
Expand All @@ -57,7 +58,7 @@ def addLiquidity(min_liquidity: uint256, max_tokens: uint256, deadline: timestam
assert max_tokens >= token_amount and liquidity_minted >= min_liquidity
self.balances[msg.sender] += liquidity_minted
self.totalSupply = total_liquidity + liquidity_minted
assert self.token.transferFrom(msg.sender, self, token_amount)
assert_modifiable(self.token.transferFrom(msg.sender, self, token_amount))
log.AddLiquidity(msg.sender, msg.value, token_amount)
log.Transfer(ZERO_ADDRESS, msg.sender, liquidity_minted)
return liquidity_minted
Expand All @@ -68,7 +69,7 @@ def addLiquidity(min_liquidity: uint256, max_tokens: uint256, deadline: timestam
initial_liquidity: uint256 = as_unitless_number(self.balance)
self.totalSupply = initial_liquidity
self.balances[msg.sender] = initial_liquidity
assert self.token.transferFrom(msg.sender, self, token_amount)
assert_modifiable(self.token.transferFrom(msg.sender, self, token_amount))
log.AddLiquidity(msg.sender, msg.value, token_amount)
log.Transfer(ZERO_ADDRESS, msg.sender, initial_liquidity)
return initial_liquidity
Expand All @@ -91,7 +92,7 @@ def removeLiquidity(amount: uint256, min_eth: uint256(wei), min_tokens: uint256,
self.balances[msg.sender] -= amount
self.totalSupply = total_liquidity - amount
send(msg.sender, eth_amount)
assert self.token.transfer(msg.sender, token_amount)
assert_modifiable(self.token.transfer(msg.sender, token_amount))
log.RemoveLiquidity(msg.sender, eth_amount, token_amount)
log.Transfer(msg.sender, ZERO_ADDRESS, amount)
return eth_amount, token_amount
Expand Down Expand Up @@ -129,7 +130,7 @@ def ethToTokenInput(eth_sold: uint256(wei), min_tokens: uint256, deadline: times
token_reserve: uint256 = self.token.balanceOf(self)
tokens_bought: uint256 = self.getInputPrice(as_unitless_number(eth_sold), as_unitless_number(self.balance - eth_sold), token_reserve)
assert tokens_bought >= min_tokens
assert self.token.transfer(recipient, tokens_bought)
assert_modifiable(self.token.transfer(recipient, tokens_bought))
log.TokenPurchase(buyer, eth_sold, tokens_bought)
return tokens_bought

Expand Down Expand Up @@ -172,7 +173,7 @@ def ethToTokenOutput(tokens_bought: uint256, max_eth: uint256(wei), deadline: ti
eth_refund: uint256(wei) = max_eth - as_wei_value(eth_sold, 'wei')
if eth_refund > 0:
send(buyer, eth_refund)
assert self.token.transfer(recipient, tokens_bought)
assert_modifiable(self.token.transfer(recipient, tokens_bought))
log.TokenPurchase(buyer, as_wei_value(eth_sold, 'wei'), tokens_bought)
return as_wei_value(eth_sold, 'wei')

Expand Down Expand Up @@ -206,7 +207,7 @@ def tokenToEthInput(tokens_sold: uint256, min_eth: uint256(wei), deadline: times
wei_bought: uint256(wei) = as_wei_value(eth_bought, 'wei')
assert wei_bought >= min_eth
send(recipient, wei_bought)
assert self.token.transferFrom(buyer, self, tokens_sold)
assert_modifiable(self.token.transferFrom(buyer, self, tokens_sold))
log.EthPurchase(buyer, tokens_sold, wei_bought)
return wei_bought

Expand Down Expand Up @@ -241,7 +242,7 @@ def tokenToEthOutput(eth_bought: uint256(wei), max_tokens: uint256, deadline: ti
# tokens sold is always > 0
assert max_tokens >= tokens_sold
send(recipient, eth_bought)
assert self.token.transferFrom(buyer, self, tokens_sold)
assert_modifiable(self.token.transferFrom(buyer, self, tokens_sold))
log.EthPurchase(buyer, tokens_sold, eth_bought)
return tokens_sold

Expand Down Expand Up @@ -275,7 +276,7 @@ def tokenToTokenInput(tokens_sold: uint256, min_tokens_bought: uint256, min_eth_
eth_bought: uint256 = self.getInputPrice(tokens_sold, token_reserve, as_unitless_number(self.balance))
wei_bought: uint256(wei) = as_wei_value(eth_bought, 'wei')
assert wei_bought >= min_eth_bought
assert self.token.transferFrom(buyer, self, tokens_sold)
assert_modifiable(self.token.transferFrom(buyer, self, tokens_sold))
tokens_bought: uint256 = Exchange(exchange_addr).ethToTokenTransferInput(min_tokens_bought, deadline, recipient, value=wei_bought)
log.EthPurchase(buyer, tokens_sold, wei_bought)
return tokens_bought
Expand Down Expand Up @@ -317,7 +318,7 @@ def tokenToTokenOutput(tokens_bought: uint256, max_tokens_sold: uint256, max_eth
tokens_sold: uint256 = self.getOutputPrice(as_unitless_number(eth_bought), token_reserve, as_unitless_number(self.balance))
# tokens sold is always > 0
assert max_tokens_sold >= tokens_sold and max_eth_sold >= eth_bought
assert self.token.transferFrom(buyer, self, tokens_sold)
assert_modifiable(self.token.transferFrom(buyer, self, tokens_sold))
eth_sold: uint256(wei) = Exchange(exchange_addr).ethToTokenTransferOutput(tokens_bought, deadline, recipient, value=eth_bought)
log.EthPurchase(buyer, tokens_sold, eth_bought)
return tokens_sold
Expand Down Expand Up @@ -493,4 +494,4 @@ def approve(_spender : address, _value : uint256) -> bool:
@public
@constant
def allowance(_owner : address, _spender : address) -> uint256:
return self.allowances[_owner][_spender]
return self.allowances[_owner][_spender]
10 changes: 5 additions & 5 deletions contracts/uniswap_factory.vy
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ NewExchange: event({token: indexed(address), exchange: indexed(address)})

exchangeTemplate: public(address)
tokenCount: public(uint256)
token_to_exchange: address[address]
exchange_to_token: address[address]
id_to_token: address[uint256]
token_to_exchange: map(address, address)
exchange_to_token: map(address, address)
id_to_token: map(uint256, address)

@public
def initializeFactory(template: address):
Expand All @@ -20,7 +20,7 @@ def createExchange(token: address) -> address:
assert token != ZERO_ADDRESS
assert self.exchangeTemplate != ZERO_ADDRESS
assert self.token_to_exchange[token] == ZERO_ADDRESS
exchange: address = create_with_code_of(self.exchangeTemplate)
exchange: address = create_forwarder_to(self.exchangeTemplate)
Exchange(exchange).setup(token)
self.token_to_exchange[token] = exchange
self.exchange_to_token[exchange] = token
Expand All @@ -43,4 +43,4 @@ def getToken(exchange: address) -> address:
@public
@constant
def getTokenWithId(token_id: uint256) -> address:
return self.id_to_token[token_id]
return self.id_to_token[token_id]
Loading