This repository has been archived by the owner on Dec 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathBasketRegistry.sol
197 lines (169 loc) · 7.33 KB
/
BasketRegistry.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*
Copyright 2018 CoinAlpha, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
pragma solidity 0.4.21;
import "./zeppelin/SafeMath.sol";
contract IBasketRegistry {
// Called by BasketFactory
function registerBasket(address, address, string, string, address[], uint[]) public returns (uint) {}
function checkBasketExists (address) public returns (bool) {}
function getBasketArranger (address) public returns (address) {}
// Called by Basket
function incrementBasketsMinted (uint, address) public returns (bool) {}
function incrementBasketsBurned (uint, address) public returns (bool) {}
}
/**
* @title BasketRegistry -- Storage contract to keep track of all baskets created
* @author CoinAlpha, Inc. <[email protected]>
*/
contract BasketRegistry {
using SafeMath for uint;
// Constants set at contract inception
address public admin;
mapping(address => bool) public basketFactoryMap;
uint public basketIndex; // Baskets index starting from index = 1
address[] public basketList;
mapping(address => BasketStruct) public basketMap;
mapping(address => uint) public basketIndexFromAddress;
uint public arrangerIndex; // Arrangers register starting from index = 1
address[] public arrangerList;
mapping(address => uint) public arrangerBasketCount;
mapping(address => uint) public arrangerIndexFromAddress;
// Structs
struct BasketStruct {
address basketAddress;
address arranger;
string name;
string symbol;
address[] tokens;
uint[] weights;
uint totalMinted;
uint totalBurned;
}
// Modifiers
modifier onlyBasket {
require(basketIndexFromAddress[msg.sender] > 0); // Check: "Only a basket can call this function"
_;
}
modifier onlyBasketFactory {
require(basketFactoryMap[msg.sender] == true); // Check: "Only a basket factory can call this function"
_;
}
// Events
event LogWhitelistBasketFactory(address basketFactory);
event LogBasketRegistration(address basketAddress, uint basketIndex);
event LogIncrementBasketsMinted(address basketAddress, uint quantity, address sender);
event LogIncrementBasketsBurned(address basketAddress, uint quantity, address sender);
/// @dev BasketRegistry constructor
function BasketRegistry() public {
basketIndex = 1;
arrangerIndex = 1;
admin = msg.sender;
}
/// @dev Set basket factory address after deployment
/// @param _basketFactory Basket factory address
/// @return success Operation successful
function whitelistBasketFactory(address _basketFactory) public returns (bool success) {
require(msg.sender == admin); // Check: "Only an admin can call this function"
basketFactoryMap[_basketFactory] = true;
emit LogWhitelistBasketFactory(_basketFactory);
return true;
}
/// @dev Add new basket to registry after being created in the basketFactory
/// @param _basketAddress Address of deployed basket
/// @param _arranger Address of basket admin
/// @param _name Basket name
/// @param _symbol Basket symbol
/// @param _tokens Token address array
/// @param _weights Weight ratio array
/// @return basketIndex Index of basket in registry
function registerBasket(
address _basketAddress,
address _arranger,
string _name,
string _symbol,
address[] _tokens,
uint[] _weights
)
public
onlyBasketFactory
returns (uint index)
{
basketMap[_basketAddress] = BasketStruct(
_basketAddress, _arranger, _name, _symbol, _tokens, _weights, 0, 0
);
basketList.push(_basketAddress);
basketIndexFromAddress[_basketAddress] = basketIndex;
if (arrangerBasketCount[_arranger] == 0) {
arrangerList.push(_arranger);
arrangerIndexFromAddress[_arranger] = arrangerIndex;
arrangerIndex = arrangerIndex.add(1);
}
arrangerBasketCount[_arranger] = arrangerBasketCount[_arranger].add(1);
emit LogBasketRegistration(_basketAddress, basketIndex);
basketIndex = basketIndex.add(1);
return basketIndex.sub(1);
}
/// @dev Check if basket exists in registry
/// @param _basketAddress Address of basket to check
/// @return basketExists
function checkBasketExists(address _basketAddress) public view returns (bool basketExists) {
return basketIndexFromAddress[_basketAddress] > 0;
}
/// @dev Retrieve basket info from the registry
/// @param _basketAddress Address of basket to check
/// @return basketDetails
function getBasketDetails(address _basketAddress)
public
view
returns (
address basketAddress,
address arranger,
string name,
string symbol,
address[] tokens,
uint[] weights,
uint totalMinted,
uint totalBurned
)
{
BasketStruct memory b = basketMap[_basketAddress];
return (b.basketAddress, b.arranger, b.name, b.symbol, b.tokens, b.weights, b.totalMinted, b.totalBurned);
}
/// @dev Look up a basket's arranger
/// @param _basketAddress Address of basket to check
/// @return arranger
function getBasketArranger(address _basketAddress) public view returns (address) {
return basketMap[_basketAddress].arranger;
}
/// @dev Increment totalMinted from BasketStruct
/// @param _quantity Quantity to increment
/// @param _sender Address that bundled tokens
/// @return success Operation successful
function incrementBasketsMinted(uint _quantity, address _sender) public onlyBasket returns (bool) {
basketMap[msg.sender].totalMinted = basketMap[msg.sender].totalMinted.add(_quantity);
emit LogIncrementBasketsMinted(msg.sender, _quantity, _sender);
return true;
}
/// @dev Increment totalBurned from BasketStruct
/// @param _quantity Quantity to increment
/// @param _sender Address that debundled tokens
/// @return success Operation successful
function incrementBasketsBurned(uint _quantity, address _sender) public onlyBasket returns (bool) {
basketMap[msg.sender].totalBurned = basketMap[msg.sender].totalBurned.add(_quantity);
emit LogIncrementBasketsBurned(msg.sender, _quantity, _sender);
return true;
}
/// @dev Fallback to reject any ether sent to contract
// CHeck: "BasketRegistry does not accept ETH transfers"
function () public payable { revert(); }
}