Consider this simplified ERC-20 transfer function snippet:
function transfer(address to, uint256 amount) public returns (bool) {
require(balances[msg.sender] >= amount, "Insufficient balance");
balances[msg.sender] -= amount;
balances[to] += amount;
emit Transfer(msg.sender, to, amount);
return true;
}If balances[msg.sender] is 100 and amount is 50, what does transfer return?
function transfer(address to, uint256 amount) public returns (bool) { require(balances[msg.sender] >= amount, "Insufficient balance"); balances[msg.sender] -= amount; balances[to] += amount; emit Transfer(msg.sender, to, amount); return true; }
Think about what the function returns after successfully transferring tokens.
The transfer function returns true after successfully transferring tokens. It reverts only if the sender has insufficient balance.
In ERC-20 token contracts, which event is required to be emitted when tokens are transferred?
Think about the event that signals token movement between addresses.
The Transfer event must be emitted on every token transfer, including zero value transfers.
Examine this approve function snippet:
function approve(address spender, uint256 amount) public returns (bool) {
allowances[msg.sender][spender] = amount;
emit Approval(msg.sender, spender, amount);
return true
}What error will this code cause when compiled?
function approve(address spender, uint256 amount) public returns (bool) { allowances[msg.sender][spender] = amount; emit Approval(msg.sender, spender, amount); return true }
Check the end of the return statement carefully.
The return true line is missing a semicolon, causing a syntax error.
In an ERC-20 contract, how do you correctly declare a mapping to store token balances?
Remember the correct syntax for mappings and visibility keywords.
The correct syntax uses mapping(address => uint) public balances; to declare a public mapping.
Given this snippet of an ERC-20 token contract:
uint256 public totalSupply;
function mint(address to, uint256 amount) public {
totalSupply += amount;
balances[to] += amount;
emit Transfer(address(0), to, amount);
}If totalSupply was initially 5000, what is its value after calling mint(0x123..., 1000)?
Think about how minting affects total supply.
Minting adds the minted amount to the total supply, so 5000 + 1000 = 6000.