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

Commit ca647d4

Browse files
committed
Setting up files for Issuance Orders. Made issueInternal function that issue and fxns in other extensions can call.
1 parent 8c7ca7a commit ca647d4

File tree

4 files changed

+273
-82
lines changed

4 files changed

+273
-82
lines changed

contracts/core/extensions/CoreIssuance.sol

Lines changed: 85 additions & 81 deletions
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
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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 { CoreModifiers } from "../lib/CoreSharedModifiers.sol";
22+
23+
/**
24+
* @title CoreIssuanceOrder
25+
* @author Set Protocol
26+
*
27+
* The Core Issuance Order extension houses all functions related to the filling and
28+
* canceling issuance orders.
29+
*
30+
*/
31+
32+
contract CoreIssuanceOrder is
33+
CoreModifiers
34+
{
35+
using SafeMath for uint256;
36+
37+
function fillOrder(
38+
address _maker,
39+
address _setAddress,
40+
uint _quantity
41+
)
42+
public
43+
isValidSet(_setAddress)
44+
isPositiveQuantity(_quantity)
45+
isNaturalUnitMultiple(_quantity, _setAddress)
46+
{
47+
//Issue Set
48+
//issueInternal(_maker, _setAddress, _quantity);
49+
}
50+
}

contracts/core/lib/CoreSharedModifiers.sol

Lines changed: 12 additions & 1 deletion
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)