Skip to content
This repository was archived by the owner on Jan 18, 2023. It is now read-only.

Commit 0620e36

Browse files
authored
Merge pull request #74 from SetProtocol/brian/core_issuance_order
Brian/core issuance order
2 parents 8c7ca7a + c4704f1 commit 0620e36

File tree

6 files changed

+325
-84
lines changed

6 files changed

+325
-84
lines changed

contracts/core/Core.sol

+5-2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ import { CoreAccounting } from "./extensions/CoreAccounting.sol";
2020
import { CoreFactory } from "./extensions/CoreFactory.sol";
2121
import { CoreInternal } from "./extensions/CoreInternal.sol";
2222
import { CoreIssuance } from "./extensions/CoreIssuance.sol";
23+
import { CoreIssuanceOrder } from "./extensions/CoreIssuanceOrder.sol";
24+
2325

2426

2527
/**
@@ -30,8 +32,9 @@ import { CoreIssuance } from "./extensions/CoreIssuance.sol";
3032
* creating Sets, as well as all collateral flows throughout the system.
3133
*/
3234
contract Core is
35+
CoreIssuanceOrder,
3336
CoreAccounting,
34-
CoreIssuance,
3537
CoreInternal,
36-
CoreFactory
38+
CoreFactory,
39+
CoreIssuance
3740
{}

contracts/core/extensions/CoreIssuance.sol

+85-81
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,6 @@ contract CoreIssuance is
3737
// Use SafeMath library for all uint256 arithmetic
3838
using SafeMath for uint256;
3939

40-
/* ============ Constants ============ */
41-
42-
string constant INVALID_QUANTITY = "Quantity must be multiple of the natural unit of the set.";
43-
string constant INVALID_SET = "Set token is disabled or does not exist.";
44-
string constant ZERO_QUANTITY = "Quantity must be greater than zero.";
45-
4640
/* ============ Events ============ */
4741

4842
event IssuanceComponentDeposited(
@@ -51,17 +45,6 @@ contract CoreIssuance is
5145
uint _quantity
5246
);
5347

54-
/* ============ Modifiers ============ */
55-
56-
// Validate quantity is multiple of natural unit
57-
modifier isNaturalUnitMultiple(uint _quantity, address _setToken) {
58-
require(
59-
_quantity % ISetToken(_setToken).naturalUnit() == 0,
60-
INVALID_QUANTITY
61-
);
62-
_;
63-
}
64-
6548
/* ============ Public Functions ============ */
6649

6750
/**
@@ -79,70 +62,8 @@ contract CoreIssuance is
7962
isPositiveQuantity(_quantity)
8063
isNaturalUnitMultiple(_quantity, _setAddress)
8164
{
82-
// Fetch set token components
83-
address[] memory components = ISetToken(_setAddress).getComponents();
84-
// Fetch set token component units
85-
uint[] memory units = ISetToken(_setAddress).getUnits();
86-
87-
// Inspect vault for required component quantity
88-
for (uint16 i = 0; i < components.length; i++) {
89-
address component = components[i];
90-
uint unit = units[i];
91-
92-
// Calculate required component quantity
93-
uint requiredComponentQuantity = calculateTransferValue(
94-
unit,
95-
ISetToken(_setAddress).naturalUnit(),
96-
_quantity
97-
);
98-
99-
// Fetch component quantity in vault
100-
uint vaultBalance = IVault(state.vaultAddress).getOwnerBalance(msg.sender, component);
101-
if (vaultBalance >= requiredComponentQuantity) {
102-
// Decrement vault balance by the required component quantity
103-
IVault(state.vaultAddress).decrementTokenOwner(
104-
msg.sender,
105-
component,
106-
requiredComponentQuantity
107-
);
108-
} else {
109-
// User has less than required amount, decrement the vault by full balance
110-
if (vaultBalance > 0) {
111-
IVault(state.vaultAddress).decrementTokenOwner(
112-
msg.sender,
113-
component,
114-
vaultBalance
115-
);
116-
}
117-
118-
// Calculate remainder to deposit
119-
uint amountToDeposit = requiredComponentQuantity.sub(vaultBalance);
120-
121-
// Transfer the remainder component quantity required to vault
122-
ITransferProxy(state.transferProxyAddress).transferToVault(
123-
msg.sender,
124-
component,
125-
requiredComponentQuantity.sub(vaultBalance)
126-
);
127-
128-
// Log transfer of component from issuer waller
129-
emit IssuanceComponentDeposited(
130-
_setAddress,
131-
component,
132-
amountToDeposit
133-
);
134-
}
135-
136-
// Increment the vault balance of the set token for the component
137-
IVault(state.vaultAddress).incrementTokenOwner(
138-
_setAddress,
139-
component,
140-
requiredComponentQuantity
141-
);
142-
}
143-
144-
// Issue set token
145-
ISetToken(_setAddress).mint(msg.sender, _quantity);
65+
// Run issueInternal
66+
issueInternal(msg.sender, _setAddress, _quantity);
14667
}
14768

14869
/**
@@ -214,4 +135,87 @@ contract CoreIssuance is
214135
{
215136
return _quantity.div(_naturalUnit).mul(_componentUnits);
216137
}
138+
139+
140+
/* ============ Internal Functions ============ */
141+
142+
/**
143+
* Issue
144+
*
145+
* @param _owner Address to issue set to
146+
* @param _setAddress Address of set to issue
147+
* @param _quantity Quantity of set to issue
148+
*/
149+
function issueInternal(
150+
address _owner,
151+
address _setAddress,
152+
uint _quantity
153+
)
154+
internal
155+
{
156+
// Fetch set token components
157+
address[] memory components = ISetToken(_setAddress).getComponents();
158+
// Fetch set token component units
159+
uint[] memory units = ISetToken(_setAddress).getUnits();
160+
161+
// Inspect vault for required component quantity
162+
for (uint16 i = 0; i < components.length; i++) {
163+
address component = components[i];
164+
uint unit = units[i];
165+
166+
// Calculate required component quantity
167+
uint requiredComponentQuantity = calculateTransferValue(
168+
unit,
169+
ISetToken(_setAddress).naturalUnit(),
170+
_quantity
171+
);
172+
173+
// Fetch component quantity in vault
174+
uint vaultBalance = IVault(state.vaultAddress).getOwnerBalance(_owner, component);
175+
if (vaultBalance >= requiredComponentQuantity) {
176+
// Decrement vault balance by the required component quantity
177+
IVault(state.vaultAddress).decrementTokenOwner(
178+
_owner,
179+
component,
180+
requiredComponentQuantity
181+
);
182+
} else {
183+
// User has less than required amount, decrement the vault by full balance
184+
if (vaultBalance > 0) {
185+
IVault(state.vaultAddress).decrementTokenOwner(
186+
_owner,
187+
component,
188+
vaultBalance
189+
);
190+
}
191+
192+
// Calculate remainder to deposit
193+
uint amountToDeposit = requiredComponentQuantity.sub(vaultBalance);
194+
195+
// Transfer the remainder component quantity required to vault
196+
ITransferProxy(state.transferProxyAddress).transferToVault(
197+
_owner,
198+
component,
199+
requiredComponentQuantity.sub(vaultBalance)
200+
);
201+
202+
// Log transfer of component from issuer waller
203+
emit IssuanceComponentDeposited(
204+
_setAddress,
205+
component,
206+
amountToDeposit
207+
);
208+
}
209+
210+
// Increment the vault balance of the set token for the component
211+
IVault(state.vaultAddress).incrementTokenOwner(
212+
_setAddress,
213+
component,
214+
requiredComponentQuantity
215+
);
216+
}
217+
218+
// Issue set token
219+
ISetToken(_setAddress).mint(_owner, _quantity);
220+
}
217221
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
Copyright 2018 Set Labs Inc.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
pragma solidity 0.4.24;
18+
19+
20+
import { SafeMath } from "zeppelin-solidity/contracts/math/SafeMath.sol";
21+
import { ICoreIssuance } from "../interfaces/ICoreIssuance.sol";
22+
import { CoreModifiers } from "../lib/CoreSharedModifiers.sol";
23+
24+
/**
25+
* @title CoreIssuanceOrder
26+
* @author Set Protocol
27+
*
28+
* The Core Issuance Order extension houses all functions related to the filling and
29+
* canceling issuance orders.
30+
*
31+
*/
32+
33+
contract CoreIssuanceOrder is
34+
CoreModifiers,
35+
ICoreIssuance
36+
{
37+
using SafeMath for uint256;
38+
39+
function fillOrder(
40+
address _maker,
41+
address _setAddress,
42+
uint _quantity
43+
)
44+
public
45+
isValidSet(_setAddress)
46+
isPositiveQuantity(_quantity)
47+
isNaturalUnitMultiple(_quantity, _setAddress)
48+
{
49+
//Issue Set
50+
issueInternal(_maker, _setAddress, _quantity);
51+
}
52+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
Copyright 2018 Set Labs Inc.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
pragma solidity 0.4.24;
18+
19+
/**
20+
* @title ICoreIssuance
21+
* @author Set Protocol
22+
*
23+
* The ICoreIssuance Contract defines all the functions exposed in the CoreIssuance
24+
* extension.
25+
*/
26+
27+
contract ICoreIssuance {
28+
29+
/**
30+
* Issue internally. Can define who to issue to.
31+
*
32+
* @param _owner Address to issue set to
33+
* @param _setAddress Address of set to issue
34+
* @param _quantity Quantity of set to issue
35+
*/
36+
function issueInternal(
37+
address _owner,
38+
address _setAddress,
39+
uint _quantity
40+
)
41+
internal;
42+
}

contracts/core/lib/CoreSharedModifiers.sol

+12-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616

1717
pragma solidity 0.4.24;
1818

19-
import { CoreState } from "../lib/CoreState.sol";
19+
import { CoreState } from "./CoreState.sol";
20+
import { ISetToken } from "../interfaces/ISetToken.sol";
2021

2122
/**
2223
* @title Core Shared Modifiers
@@ -32,6 +33,7 @@ contract CoreModifiers is
3233

3334
/* ============ Constants ============ */
3435

36+
string constant INVALID_QUANTITY = "Quantity must be multiple of the natural unit of the set.";
3537
string constant ZERO_QUANTITY = "Quantity must be greater than zero.";
3638
string constant INVALID_SET = "Set token is disabled or does not exist.";
3739
string constant INVALID_FACTORY = "Factory is disabled or does not exist.";
@@ -64,4 +66,13 @@ contract CoreModifiers is
6466
);
6567
_;
6668
}
69+
70+
// Validate quantity is multiple of natural unit
71+
modifier isNaturalUnitMultiple(uint _quantity, address _setToken) {
72+
require(
73+
_quantity % ISetToken(_setToken).naturalUnit() == 0,
74+
INVALID_QUANTITY
75+
);
76+
_;
77+
}
6778
}

0 commit comments

Comments
 (0)