Skip to content

Instantly share code, notes, and snippets.

View bartubozkurt's full-sized avatar
🎯
Focusing

Bartu Bozkurt bartubozkurt

🎯
Focusing
View GitHub Profile
/* 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");
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);
/* Bad */
contract BadVault{
function receive() payable public{
}
}
/* Better */
contract GoodVault{
// add withdraw function.
function withdraw() public onlyOwner {
/* 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){
/* 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);
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";
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";
/* Bad */
function unlock(
address _to,
uint256 _amount,
uint8[] _v,
bytes32[] _r,
bytes32[] _s
)
external
{
/* 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) {
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);