examples and more contracts
This commit is contained in:
14
contracts/BadArrayUse.sol
Normal file
14
contracts/BadArrayUse.sol
Normal file
@ -0,0 +1,14 @@
|
||||
import './PullPaymentCapable.sol';
|
||||
|
||||
// UNSAFE CODE, DO NOT USE!
|
||||
|
||||
contract BadArrayUse is PullPaymentCapable {
|
||||
address[] employees;
|
||||
|
||||
function payroll() {
|
||||
for (var i = 0; i < employees.length; i++) {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
15
contracts/BadFailEarly.sol
Normal file
15
contracts/BadFailEarly.sol
Normal file
@ -0,0 +1,15 @@
|
||||
// UNSAFE CODE, DO NOT USE!
|
||||
|
||||
contract BadFailEarly {
|
||||
|
||||
uint constant DEFAULT_SALARY = 50000;
|
||||
mapping(string => uint) nameToSalary;
|
||||
|
||||
function getSalary(string name) constant returns (uint) {
|
||||
if (bytes(name).length != 0 && nameToSalary[name] != 0) {
|
||||
return nameToSalary[name];
|
||||
} else {
|
||||
return DEFAULT_SALARY;
|
||||
}
|
||||
}
|
||||
}
|
||||
18
contracts/BadPushPayments.sol
Normal file
18
contracts/BadPushPayments.sol
Normal file
@ -0,0 +1,18 @@
|
||||
// UNSAFE CODE, DO NOT USE!
|
||||
|
||||
contract BadPushPayments {
|
||||
|
||||
address highestBidder;
|
||||
uint highestBid;
|
||||
|
||||
function bid() {
|
||||
if (msg.value < highestBid) throw;
|
||||
|
||||
if (highestBidder != 0) {
|
||||
highestBidder.send(highestBid);
|
||||
}
|
||||
|
||||
highestBidder = msg.sender;
|
||||
highestBid = msg.value;
|
||||
}
|
||||
}
|
||||
11
contracts/GoodArrayUse.sol
Normal file
11
contracts/GoodArrayUse.sol
Normal file
@ -0,0 +1,11 @@
|
||||
|
||||
contract GoodArrayUse {
|
||||
address[] employees;
|
||||
|
||||
function payroll() {
|
||||
for (uint i = 0; i < employees.length; i++) {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
16
contracts/GoodFailEarly.sol
Normal file
16
contracts/GoodFailEarly.sol
Normal file
@ -0,0 +1,16 @@
|
||||
contract GoodFailEarly {
|
||||
|
||||
uint constant DEFAULT_SALARY = 50000;
|
||||
mapping(string => uint) nameToSalary;
|
||||
|
||||
function getSalary(string name) constant returns (uint) {
|
||||
if (bytes(name).length == 0) {
|
||||
throw;
|
||||
}
|
||||
if (nameToSalary[name] == 0) {
|
||||
throw;
|
||||
}
|
||||
|
||||
return nameToSalary[name];
|
||||
}
|
||||
}
|
||||
24
contracts/GoodPullPayments.sol
Normal file
24
contracts/GoodPullPayments.sol
Normal file
@ -0,0 +1,24 @@
|
||||
contract GoodPullPayments {
|
||||
address highestBidder;
|
||||
uint highestBid;
|
||||
mapping(address => uint) refunds;
|
||||
|
||||
function bid() external {
|
||||
if (msg.value < highestBid) throw;
|
||||
|
||||
if (highestBidder != 0) {
|
||||
refunds[highestBidder] += highestBid;
|
||||
}
|
||||
|
||||
highestBidder = msg.sender;
|
||||
highestBid = msg.value;
|
||||
}
|
||||
|
||||
function withdrawRefund() external {
|
||||
uint refund = refunds[msg.sender];
|
||||
refunds[msg.sender] = 0;
|
||||
if (!msg.sender.send(refund)) {
|
||||
refunds[msg.sender] = refund;
|
||||
}
|
||||
}
|
||||
}
|
||||
16
contracts/PullPaymentBid.sol
Normal file
16
contracts/PullPaymentBid.sol
Normal file
@ -0,0 +1,16 @@
|
||||
import './PullPaymentCapable.sol';
|
||||
|
||||
contract PullPaymentBid is PullPaymentCapable {
|
||||
address public highestBidder;
|
||||
uint public highestBid;
|
||||
|
||||
function bid() external {
|
||||
if (msg.value <= highestBid) throw;
|
||||
|
||||
if (highestBidder != 0) {
|
||||
asyncSend(highestBidder, highestBid);
|
||||
}
|
||||
highestBidder = msg.sender;
|
||||
highestBid = msg.value;
|
||||
}
|
||||
}
|
||||
15
contracts/PullPaymentCapable.sol
Normal file
15
contracts/PullPaymentCapable.sol
Normal file
@ -0,0 +1,15 @@
|
||||
contract PullPaymentCapable {
|
||||
mapping(address => uint) refunds;
|
||||
|
||||
function asyncSend(address dest, uint amount) {
|
||||
refunds[dest] += amount;
|
||||
}
|
||||
|
||||
function withdrawRefund() external {
|
||||
uint refund = refunds[msg.sender];
|
||||
refunds[msg.sender] = 0;
|
||||
if (!msg.sender.send(refund)) {
|
||||
refunds[msg.sender] = refund;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1,5 +1,5 @@
|
||||
module.exports = function(deployer) {
|
||||
deployer.deploy(ConvertLib);
|
||||
deployer.autolink();
|
||||
deployer.deploy(MetaCoin);
|
||||
deployer.deploy(BadFailEarly);
|
||||
deployer.deploy(GoodFailEarly);
|
||||
deployer.deploy(PullPaymentBid);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user