Skip to content
This repository was archived by the owner on Mar 19, 2019. It is now read-only.

Commit 3c28eb4

Browse files
authored
V1.6 broker (#317)
1 parent 8b0a7f5 commit 3c28eb4

File tree

5 files changed

+287
-1
lines changed

5 files changed

+287
-1
lines changed

contracts/BrokerRegistry.sol

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
3+
Copyright 2017 Loopring Project Ltd (Loopring Foundation).
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
*/
17+
pragma solidity 0.4.23;
18+
pragma experimental "v0.5.0";
19+
pragma experimental "ABIEncoderV2";
20+
21+
22+
/// @title Trade Broker
23+
/// @dev A broker is an account that can submit order on behalf of other
24+
/// accounts. When register a broker, the owner can also specify a
25+
/// pre-deployed BrokageTracker to manage the allowance of for the
26+
/// specific broker.
27+
/// @author Daniel Wang - <[email protected]>.
28+
contract BrokerRegistry {
29+
event BrokerRegistered(
30+
address owner,
31+
address broker,
32+
address tracker
33+
);
34+
35+
event BrokerUnregistered(
36+
address owner,
37+
address broker
38+
);
39+
40+
function getBroker(
41+
address owner,
42+
address broker
43+
)
44+
external
45+
view
46+
returns(
47+
bool authenticated,
48+
address tracker
49+
);
50+
51+
function getBrokers(
52+
uint start,
53+
uint count
54+
)
55+
public
56+
view
57+
returns (
58+
address[] brokers,
59+
address[] trackers
60+
);
61+
62+
function registerBroker(
63+
address broker,
64+
address tracker // 0x0 allowed
65+
)
66+
external;
67+
68+
function unregisterBroker(
69+
address broker
70+
)
71+
external;
72+
}

contracts/BrokerRegistryImpl.sol

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
/*
2+
3+
Copyright 2017 Loopring Project Ltd (Loopring Foundation).
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
*/
17+
pragma solidity 0.4.23;
18+
pragma experimental "v0.5.0";
19+
pragma experimental "ABIEncoderV2";
20+
21+
import "./BrokerRegistry.sol";
22+
23+
24+
/// @title An Implementation of BrokerRegistry.
25+
/// @author Daniel Wang - <[email protected]>.
26+
contract BrokerRegistryImpl is BrokerRegistry {
27+
struct Broker {
28+
uint pos; // 0 mens unregistered; if > 0, pos - 1 is the
29+
// token's position in `addresses`.
30+
address owner;
31+
address addr;
32+
address tracker;
33+
}
34+
35+
mapping(address => Broker[]) public brokerageMap;
36+
mapping(address => mapping(address => Broker)) public brokerMap;
37+
38+
function getBroker(
39+
address owner,
40+
address broker
41+
)
42+
external
43+
view
44+
returns(
45+
bool authenticated,
46+
address tracker
47+
)
48+
{
49+
Broker storage b = brokerMap[owner][broker];
50+
authenticated = (b.addr != 0x0);
51+
tracker = b.tracker;
52+
}
53+
54+
function getBrokers(
55+
uint start,
56+
uint count
57+
)
58+
public
59+
view
60+
returns (
61+
address[] brokers,
62+
address[] trackers
63+
)
64+
{
65+
Broker[] storage _brokers = brokerageMap[msg.sender];
66+
uint num = _brokers.length;
67+
68+
if (start >= num) {
69+
return;
70+
}
71+
72+
uint end = start + count;
73+
if (end > num) {
74+
end = num;
75+
}
76+
77+
if (start == num) {
78+
return;
79+
}
80+
81+
brokers = new address[](end - start);
82+
trackers = new address[](end - start);
83+
for (uint i = start; i < end; i++) {
84+
brokers[i - start] = _brokers[i].addr;
85+
trackers[i - start] = _brokers[i].tracker;
86+
}
87+
}
88+
89+
function registerBroker(
90+
address broker,
91+
address tracker // 0x0 allowed
92+
)
93+
external
94+
{
95+
require(0x0 != broker,"bad broker");
96+
require(
97+
0 == brokerMap[msg.sender][broker].pos,
98+
"broker already exists"
99+
);
100+
101+
Broker[] storage brokers = brokerageMap[msg.sender];
102+
Broker memory b = Broker(
103+
brokers.length + 1,
104+
msg.sender,
105+
broker,
106+
tracker
107+
);
108+
109+
brokers.push(b);
110+
brokerMap[msg.sender][broker] = b;
111+
112+
emit BrokerRegistered(
113+
msg.sender,
114+
broker,
115+
tracker
116+
);
117+
}
118+
119+
function unregisterBroker(
120+
address broker
121+
)
122+
external
123+
{
124+
require(0x0 != broker, "bad broker");
125+
require(
126+
brokerMap[msg.sender][broker].addr == broker,
127+
"broker not found"
128+
);
129+
130+
Broker storage b = brokerMap[msg.sender][broker];
131+
delete brokerMap[msg.sender][broker];
132+
133+
Broker[] storage brokers = brokerageMap[msg.sender];
134+
Broker storage lastBroker = brokers[brokers.length - 1];
135+
136+
if (lastBroker.addr != broker) {
137+
// Swap with the last token and update the pos
138+
lastBroker.pos = b.pos;
139+
brokers[b.pos - 1] = lastBroker;
140+
brokerMap[lastBroker.owner][lastBroker.addr] = lastBroker;
141+
}
142+
143+
brokers.length--;
144+
145+
emit BrokerUnregistered(msg.sender, broker);
146+
}
147+
}

contracts/BrokerTracker.sol

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
3+
Copyright 2017 Loopring Project Ltd (Loopring Foundation).
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
*/
17+
pragma solidity 0.4.23;
18+
pragma experimental "v0.5.0";
19+
pragma experimental "ABIEncoderV2";
20+
21+
22+
/// @title BrokerTracker
23+
contract BrokerTracker {
24+
/// @dev Returns the max amount the broker can buy or sell.
25+
function getAllowance(
26+
address owner,
27+
address broker,
28+
address tokenB,
29+
address tokenS
30+
)
31+
public
32+
view
33+
returns (
34+
uint allowanceB,
35+
uint allowanceS,
36+
uint lrcAllowance
37+
);
38+
39+
/// @dev This method will be called from TokenTransferDelegateImpl, so
40+
/// it must check `msg.sender` is the address of LoopringProtocol.
41+
/// Check https://github.com/Loopring/token-listing/blob/master/ethereum/deployment.md
42+
/// for the current address of LoopringProtocol deployment.
43+
function onSettlement(
44+
address owner,
45+
address broker,
46+
address tokenB,
47+
uint amountB,
48+
address tokenS,
49+
uint amountS,
50+
uint lrcFee,
51+
uint lrcReward
52+
)
53+
public;
54+
}

contracts/LoopringProtocolImpl.sol

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ pragma experimental "ABIEncoderV2";
2121
import "./lib/AddressUtil.sol";
2222
import "./lib/ERC20.sol";
2323
import "./lib/MathUint.sol";
24+
import "./BrokerRegistry.sol";
25+
import "./BrokerTracker.sol";
2426
import "./LoopringProtocol.sol";
2527
import "./TokenRegistry.sol";
2628
import "./TokenTransferDelegate.sol";

contracts/TokenRegistryImpl.sol

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ contract TokenRegistryImpl is TokenRegistry, Claimable {
3434
mapping (string => address) symbolMap;
3535

3636
struct TokenInfo {
37-
uint pos; // 0 mens unregistered; if > 0, pos + 1 is the
37+
uint pos; // 0 mens unregistered; if > 0, pos - 1 is the
3838
// token's position in `addresses`.
3939
string symbol; // Symbol of the token
4040
}
@@ -170,6 +170,17 @@ contract TokenRegistryImpl is TokenRegistry, Claimable {
170170
}
171171
}
172172

173+
// address[] public addresses;
174+
// mapping (address => TokenInfo) addressMap;
175+
// mapping (string => address) symbolMap;
176+
177+
// struct TokenInfo {
178+
// uint pos; // 0 mens unregistered; if > 0, pos - 1 is the
179+
// // token's position in `addresses`.
180+
// string symbol; // Symbol of the token
181+
// }
182+
183+
173184
function registerTokenInternal(
174185
address addr,
175186
string symbol

0 commit comments

Comments
 (0)