1
+ pragma solidity 0.4.24 ;
2
+
3
+ import "./open-zeppelin/contracts/ownership/Ownable.sol " ;
4
+ import "./token.sol " ;
5
+
6
+ contract ExposureCrowdSale is Ownable {
7
+ using SafeMath for uint256 ;
8
+
9
+ // The address of the token contract which we'll be interacting with.
10
+ ExposureToken public exposure;
11
+
12
+ // Amount of Exposure you get for every 1 ETH
13
+ uint256 public exchangeRate;
14
+
15
+ // Amount of Exposure sold via this contract
16
+ uint256 public exposureSold;
17
+
18
+ // Amount of ETH collected via this contract
19
+ uint256 public ethCollected;
20
+
21
+ // The address which this contract forwards its ETH to.
22
+ address public fundsWallet;
23
+
24
+ /**
25
+ * @dev The constructor sets the original exchange rate of the contract and the address of the token.
26
+ */
27
+ constructor (uint256 _exchangeRate , address _fundsWallet ) public {
28
+ exchangeRate = _exchangeRate;
29
+ fundsWallet = _fundsWallet;
30
+
31
+ exposureSold = 0 ;
32
+ ethCollected = 0 ;
33
+ }
34
+
35
+ /**
36
+ * @dev The default function purchases Exposure.
37
+ */
38
+ function () external payable {
39
+ purchase ();
40
+ }
41
+
42
+ // -----------------------------
43
+ // VIEWS
44
+ // -----------------------------
45
+
46
+ /**
47
+ * @dev Allow users to easily query the amount available for purchase.
48
+ */
49
+ function availableExposure () external view returns (uint256 ) {
50
+ return exposure.balanceOf (this );
51
+ }
52
+
53
+ // -----------------------------
54
+ // SETTERS
55
+ // -----------------------------
56
+
57
+ /**
58
+ * @dev The owner is able to change the exchange rate.
59
+ */
60
+ function setExchangeRate (uint256 _newExchangeRate ) external onlyOwner {
61
+ exchangeRate = _newExchangeRate;
62
+
63
+ emit ExchangeRateChanged (exchangeRate);
64
+ }
65
+
66
+ /**
67
+ * @dev The owner is able to change the funds wallet, which is where any ETH we
68
+ * receive goes.
69
+ */
70
+ function setFundsWallet (address _newFundsWallet ) external onlyOwner {
71
+ fundsWallet = _newFundsWallet;
72
+
73
+ emit FundsWalletChanged (fundsWallet);
74
+ }
75
+
76
+ /**
77
+ * @dev The owner is able to withdraw any Exposure the contract has.
78
+ */
79
+ function withdraw (uint256 amount ) external onlyOwner {
80
+ exposure.transfer (owner, amount);
81
+ }
82
+
83
+ // -----------------------------
84
+ // PURCHASE
85
+ // -----------------------------
86
+
87
+ /**
88
+ * @dev The purchase function allows users to purchase Exposure when they aren't
89
+ * concerned about the owner front running the exchange rate.
90
+ *
91
+ * For more about front running, read here:
92
+ * https://consensys.github.io/smart-contract-best-practices/known_attacks/#transaction-ordering-dependence-tod-front-running
93
+ */
94
+ function purchase () public payable returns (bool ) {
95
+ uint256 amountPurchased = msg .value .mul (exchangeRate);
96
+
97
+ // Send the ETH to our funds wallet.
98
+ fundsWallet.transfer (msg .value );
99
+
100
+ // Send the Exposure to the purchaser.
101
+ exposure.transfer (msg .sender , amountPurchased);
102
+
103
+ // Note: No emits here because we're expecting you to watch the transfer events on the
104
+ // token contract if you want to see when people purchase.
105
+ return true ;
106
+ }
107
+
108
+ /**
109
+ * @dev The purchaseWithExchangeRate function allows users to purchase Exposure and
110
+ * guarantee that they will receive a specific amount of Exposure in return.
111
+ * If the owner of the contract tries to front run their transaction, the call
112
+ * will revert.
113
+ *
114
+ * For more about front running, read here:
115
+ * https://consensys.github.io/smart-contract-best-practices/known_attacks/#transaction-ordering-dependence-tod-front-running
116
+ */
117
+ function purchaseWithExchangeRate (uint256 guaranteedExchangeRate ) external payable returns (bool ) {
118
+ require (exchangeRate == guaranteedExchangeRate);
119
+
120
+ return purchase ();
121
+ }
122
+
123
+ // Emitted whenever the owner of the contract changes the exchange rate.
124
+ event ExchangeRateChanged (uint256 newExchangeRate );
125
+
126
+ // Emitted whenever the owner of the contract changes the funds wallet.
127
+ event FundsWalletChanged (address newFundsWallet );
128
+
129
+ // Note, there is no event for purchase as you can subscribe to transfer events on the
130
+ // actual token contract if you want to monitor purchases.
131
+ }
0 commit comments