Skip to content

Instantly share code, notes, and snippets.

@charles-cooper
Created October 15, 2021 21:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save charles-cooper/d85a8ac0536b6d0741694949e144e62c to your computer and use it in GitHub Desktop.
Save charles-cooper/d85a8ac0536b6d0741694949e144e62c to your computer and use it in GitHub Desktop.
compare erc20 in sol and vyper
/*
# @version 0.3.0
# @dev Implementation of ERC-20 token standard, vyper/examples/tokens/ERC20.vy at tag v0.3.0
# @author Takayuki Jimba (@yudetamago)
# https://github.com/ethereum/EIPs/blob/master/EIPS/eip-20.md
# compiled using `vyper -f bytecode_runtime`: 1579 bytes
from vyper.interfaces import ERC20
implements: ERC20
*/
// compiled using `solc --optimize --optimize-runs=1 --bin-runtime`: 1970 bytes
pragma solidity ^0.8.9;
contract ERC20 {
/*
event Transfer:
sender: indexed(address)
receiver: indexed(address)
value: uint256
*/
event Transfer(address indexed sender, address indexed received, uint256 value);
/*
event Approval:
owner: indexed(address)
spender: indexed(address)
value: uint256
*/
event Approval(address indexed owner, address indexed spender, uint256 value);
//name: public(String[64])
string public name;
//symbol: public(String[32])
string public symbol;
//decimals: public(uint256)
uint256 public decimals;
/*
# 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(HashMap[address, uint256])
*/
mapping(address => uint256) public balanceOf;
/*
# By declaring `allowance` as public, vyper automatically generates the `allowance()` getter
allowance: public(HashMap[address, HashMap[address, uint256]])
*/
mapping(address => mapping(address => uint256)) public allowance;
/*
# By declaring `totalSupply` as public, we automatically create the `totalSupply()` getter
totalSupply: public(uint256)
*/
uint256 public totalSupply;
//minter: address
address minter;
/*
@external
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.totalSupply = init_supply
self.minter = msg.sender
log Transfer(ZERO_ADDRESS, msg.sender, init_supply)
*/
constructor(string memory _name, string memory _symbol, uint256 _decimals, uint256 _supply) {
uint256 init_supply = _supply * 10 ** _decimals;
require(bytes(_name).length <= 64);
name = _name;
require(bytes(_symbol).length <= 32);
symbol = _symbol;
decimals = _decimals;
balanceOf[msg.sender] = init_supply;
totalSupply = init_supply;
minter = msg.sender;
emit Transfer(address(0), msg.sender, init_supply);
}
/*
@external
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
*/
function transfer(address _to, uint256 _value) external returns(bool) {
balanceOf[msg.sender] -= _value;
balanceOf[_to] += _value;
emit Transfer(msg.sender, _to, _value);
return true;
}
/*
@external
def transferFrom(_from : address, _to : address, _value : uint256) -> bool:
"""
@dev Transfer tokens from one address to another.
@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.allowance[_from][msg.sender] -= _value
log Transfer(_from, _to, _value)
return True
*/
function transferFrom(address _from, address _to, uint256 _value) external returns(bool) {
balanceOf[_from] -= _value;
balanceOf[_to] += _value;
allowance[_from][msg.sender] -= _value;
emit Transfer(_from, _to, _value);
return true;
}
/*
@external
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.allowance[msg.sender][_spender] = _value
log Approval(msg.sender, _spender, _value)
return True
*/
function approve(address _spender, uint256 _value) external returns (bool) {
allowance[msg.sender][_spender] = _value;
emit Approval(msg.sender, _spender, _value);
return true;
}
/*
@external
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.totalSupply += _value
self.balanceOf[_to] += _value
log Transfer(ZERO_ADDRESS, _to, _value)
*/
function mint(address _to, uint256 _value) external {
require(msg.sender == minter);
require(_to != address(0));
totalSupply += _value;
balanceOf[_to] += _value;
emit Transfer(address(0), _to, _value);
}
/*
@internal
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.totalSupply -= _value
self.balanceOf[_to] -= _value
log Transfer(_to, ZERO_ADDRESS, _value)
*/
function _burn(address _to, uint256 _value) internal {
require(_to != address(0));
totalSupply -= _value;
balanceOf[_to] -= _value;
emit Transfer(_to, address(0), _value);
}
/*
@external
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)
*/
function burn(uint256 _value) external {
_burn(msg.sender, _value);
}
/*
@external
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.allowance[_to][msg.sender] -= _value
self._burn(_to, _value)
*/
function burnFrom(address _to, uint256 _value) external {
allowance[_to][msg.sender] -= _value;
_burn(_to, _value);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment