This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Bad */ | |
modifier isNotContract(address _a){ | |
uint len; | |
assembly { len := extcodesize(_a) } | |
require(len == 0); | |
_; | |
} | |
/* Better */ | |
modifier isNotContract(address _a){ | |
(tx.origin == msg.sender,"No Contract"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
address owner; | |
/* Bad */ | |
function sendTo(address receiver, uint amount) public { | |
require(tx.origin == owner); | |
receiver.transfer(amount); | |
} | |
/* Better */ | |
function sendTo(address receiver, uint amount) public { | |
require(msg.sender == owner); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Bad */ | |
contract BadVault{ | |
function receive() payable public{ | |
} | |
} | |
/* Better */ | |
contract GoodVault{ | |
// add withdraw function. | |
function withdraw() public onlyOwner { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Bad */ | |
contract Bad{ | |
function fund_reached() public returns(bool){ | |
return this.balance == 100 ether; // strict equalities | |
} | |
} | |
/* Better */ | |
contract Good{ | |
function fund_reached() public returns(bool){ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Bad */ | |
uint256 constant private targetEther = 1000 ether; | |
function join() public payable { | |
require(msg.value == 5 ether); // each play is 5 ether | |
...doSomething; | |
} | |
function claimReward(address _to) public { | |
require(this.balance == targetEther); | |
_to.transfer(targetEther); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity =0.4.22; | |
/* Bad */ | |
contract BadERC721Token{ | |
function ownerOf(uint256 _tokenId) external view returns (bool); | |
//... | |
} | |
/* Better */ | |
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC721/SafeERC721.sol"; | |
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC721/IERC721.sol"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity =0.4.22; | |
/* Bad */ | |
contract BadERC20Token{ | |
function transfer(address to, uint value) external; | |
//... | |
} | |
/* Better */ | |
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC20/SafeERC20.sol"; | |
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC20/IERC20.sol"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Bad */ | |
function unlock( | |
address _to, | |
uint256 _amount, | |
uint8[] _v, | |
bytes32[] _r, | |
bytes32[] _s | |
) | |
external | |
{ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Bad */ | |
function approve(address _spender, uint256 _value) public returns (bool success) { | |
_allowances[msg.sender][_spender] = _value | |
} | |
/* Better */ | |
import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol"; | |
SafeERC20 for IERC20; | |
IERC20 token; | |
function addAllowance(IERC20 _token,address _spender, uint256 _value) public returns (bool success) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
address bob = 0x237C8Aea434dE4784d23d145069c6D0Bef629A19 // Give Me Ether | |
IERC20 token; | |
uint N = 3 ether | |
uint M = 1 ether | |
/* 👩 Alice, 👨 Bob */ | |
/* Bad */ | |
/* FROM | 🕛 | ORDER | BLOCK | GAS */ | |
/* 👩 | 0s | 1 | N | 100,000 */ token.approve(bob,N); | |
/* 👩 | 20s | 2 | N+1 | 100,000 */ token.approve(bob,M); |
NewerOlder