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

Commit 85e03a6

Browse files
authored
Merge pull request #65 from SetProtocol/felix/create-mixin
Split up Create function into its own Extension
2 parents ffb2e72 + 8694bd9 commit 85e03a6

File tree

5 files changed

+275
-141
lines changed

5 files changed

+275
-141
lines changed

contracts/core/Core.sol

Lines changed: 3 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import { Ownable } from "zeppelin-solidity/contracts/ownership/Ownable.sol";
2020
import { SafeMath } from "zeppelin-solidity/contracts/math/SafeMath.sol";
2121
import { CoreInternal } from "./extensions/CoreInternal.sol";
2222
import { CoreIssuance } from "./extensions/CoreIssuance.sol";
23+
import { CoreCreate } from "./extensions/CoreCreate.sol";
2324
import { CoreState } from "./lib/CoreState.sol";
2425
import { ISetFactory } from "./interfaces/ISetFactory.sol";
2526
import { ITransferProxy } from "./interfaces/ITransferProxy.sol";
@@ -35,7 +36,8 @@ import { IVault } from "./interfaces/IVault.sol";
3536
*/
3637
contract Core is
3738
CoreIssuance,
38-
CoreInternal
39+
CoreInternal,
40+
CoreCreate
3941
{
4042
// Use SafeMath library for all uint256 arithmetic
4143
using SafeMath for uint256;
@@ -44,31 +46,10 @@ contract Core is
4446

4547
string constant ADDRESSES_MISSING = "Addresses must not be empty.";
4648
string constant BATCH_INPUT_MISMATCH = "Addresses and quantities must be the same length.";
47-
string constant INVALID_FACTORY = "Factory is disabled or does not exist.";
4849
string constant QUANTITES_MISSING = "Quantities must not be empty.";
4950

50-
/* ============ Events ============ */
51-
52-
event SetTokenCreated(
53-
address indexed _setTokenAddress,
54-
address _factoryAddress,
55-
address[] _components,
56-
uint[] _units,
57-
uint _naturalUnit,
58-
string _name,
59-
string _symbol
60-
);
61-
6251
/* ============ Modifiers ============ */
6352

64-
modifier isValidFactory(address _factoryAddress) {
65-
require(
66-
state.validFactories[_factoryAddress],
67-
INVALID_FACTORY
68-
);
69-
_;
70-
}
71-
7253
// Confirm that all inputs are valid for batch transactions
7354
modifier isValidBatchTransaction(address[] _tokenAddresses, uint[] _quantities) {
7455
// Confirm an empty _addresses array is not passed
@@ -195,52 +176,4 @@ contract Core is
195176
_quantity
196177
);
197178
}
198-
199-
/**
200-
* Deploys a new Set Token and adds it to the valid list of SetTokens
201-
*
202-
* @param _factoryAddress address The address of the Factory to create from
203-
* @param _components address[] The address of component tokens
204-
* @param _units uint[] The units of each component token
205-
* @param _naturalUnit uint The minimum unit to be issued or redeemed
206-
* @param _name string The name of the new Set
207-
* @param _symbol string The symbol of the new Set
208-
* @return setTokenAddress address The address of the new Set
209-
*/
210-
function create(
211-
address _factoryAddress,
212-
address[] _components,
213-
uint[] _units,
214-
uint _naturalUnit,
215-
string _name,
216-
string _symbol
217-
)
218-
public
219-
isValidFactory(_factoryAddress)
220-
returns (address)
221-
{
222-
// Create the Set
223-
address newSetTokenAddress = ISetFactory(_factoryAddress).create(
224-
_components,
225-
_units,
226-
_naturalUnit,
227-
_name,
228-
_symbol
229-
);
230-
231-
// Add Set to the list of tracked Sets
232-
state.validSets[newSetTokenAddress] = true;
233-
234-
emit SetTokenCreated(
235-
newSetTokenAddress,
236-
_factoryAddress,
237-
_components,
238-
_units,
239-
_naturalUnit,
240-
_name,
241-
_symbol
242-
);
243-
244-
return newSetTokenAddress;
245-
}
246179
}
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
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+
import { SafeMath } from "zeppelin-solidity/contracts/math/SafeMath.sol";
20+
import { CoreState } from "../lib/CoreState.sol";
21+
import { ISetFactory } from "../interfaces/ISetFactory.sol";
22+
23+
24+
/**
25+
* @title Core Create
26+
* @author Set Protocol
27+
*x
28+
* The CoreCreate contract contains public set token operations
29+
*/
30+
contract CoreCreate is
31+
CoreState
32+
{
33+
// Use SafeMath library for all uint256 arithmetic
34+
using SafeMath for uint256;
35+
36+
/* ============ Constants ============ */
37+
38+
string constant INVALID_FACTORY = "Factory is disabled or does not exist.";
39+
40+
/* ============ Events ============ */
41+
42+
event SetTokenCreated(
43+
address indexed _setTokenAddress,
44+
address _factoryAddress,
45+
address[] _components,
46+
uint[] _units,
47+
uint _naturalUnit,
48+
string _name,
49+
string _symbol
50+
);
51+
52+
/* ============ Modifiers ============ */
53+
54+
modifier isValidFactory(address _factoryAddress) {
55+
require(
56+
state.validFactories[_factoryAddress],
57+
INVALID_FACTORY
58+
);
59+
_;
60+
}
61+
62+
/* ============ Public Functions ============ */
63+
64+
/**
65+
* Deploys a new Set Token and adds it to the valid list of SetTokens
66+
*
67+
* @param _factoryAddress address The address of the Factory to create from
68+
* @param _components address[] The address of component tokens
69+
* @param _units uint[] The units of each component token
70+
* @param _naturalUnit uint The minimum unit to be issued or redeemed
71+
* @param _name string The name of the new Set
72+
* @param _symbol string The symbol of the new Set
73+
* @return setTokenAddress address The address of the new Set
74+
*/
75+
function create(
76+
address _factoryAddress,
77+
address[] _components,
78+
uint[] _units,
79+
uint _naturalUnit,
80+
string _name,
81+
string _symbol
82+
)
83+
public
84+
isValidFactory(_factoryAddress)
85+
returns (address)
86+
{
87+
// Create the Set
88+
address newSetTokenAddress = ISetFactory(_factoryAddress).create(
89+
_components,
90+
_units,
91+
_naturalUnit,
92+
_name,
93+
_symbol
94+
);
95+
96+
// Add Set to the list of tracked Sets
97+
state.validSets[newSetTokenAddress] = true;
98+
99+
emit SetTokenCreated(
100+
newSetTokenAddress,
101+
_factoryAddress,
102+
_components,
103+
_units,
104+
_naturalUnit,
105+
_name,
106+
_symbol
107+
);
108+
109+
return newSetTokenAddress;
110+
}
111+
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
"clean": "rm -rf build; rm -rf transpiled; rm -rf types/generated",
2424
"compile": "truffle compile",
2525
"prepare-test": "yarn clean && truffle compile --all && yarn run generate-typings && yarn run transpile",
26-
"test": "yarn prepare-test && truffle test transpiled/test/core/**/*.js",
26+
"test": "yarn prepare-test && truffle test `find ./transpiled/test -name '*.spec.js'`",
2727
"transpile": "tsc",
2828
"generate-typings": "abi-gen --abis './build/contracts/*.json' --out './types/generated' --template './types/contract_templates/contract.mustache' --partials './types/contract_templates/partials/*.mustache' && yarn run rename-generated-abi",
2929
"rename-generated-abi": "mv types/generated/detailed_e_r_c20.ts types/generated/detailed_erc20.ts && mv types/generated/e_r_c20_basic.ts types/generated/erc20_basic.ts",

test/core/core.spec.ts

Lines changed: 0 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -559,74 +559,4 @@ contract("Core", (accounts) => {
559559
});
560560
});
561561
});
562-
563-
describe("#create", async () => {
564-
let factoryAddress: Address;
565-
let components: Address[];
566-
const units: BigNumber[] = [ONE];
567-
const naturalUnit: BigNumber = ONE;
568-
const name = "New Set";
569-
const symbol = "SET";
570-
571-
beforeEach(async () => {
572-
await deployCoreAndInitializeDependencies();
573-
mockToken = await coreWrapper.deployTokenAsync(ownerAccount);
574-
575-
factoryAddress = setTokenFactory.address;
576-
components = [mockToken.address];
577-
});
578-
579-
async function subject(): Promise<string> {
580-
return core.create.sendTransactionAsync(
581-
factoryAddress,
582-
components,
583-
units,
584-
naturalUnit,
585-
name,
586-
symbol,
587-
{ from: ownerAccount },
588-
);
589-
}
590-
591-
it("creates a new SetToken and tracks it", async () => {
592-
const txHash = await subject();
593-
594-
const logs = await getFormattedLogsFromTxHash(txHash);
595-
const newSetTokenAddress = extractNewSetTokenAddressFromLogs(logs);
596-
597-
const isSetTokenValid = await core.validSets.callAsync(newSetTokenAddress);
598-
expect(isSetTokenValid).to.be.true;
599-
});
600-
601-
it("emits a SetTokenCreated event", async () => {
602-
const txHash = await subject();
603-
const logs = await getFormattedLogsFromTxHash(txHash);
604-
const newSetTokenAddress = extractNewSetTokenAddressFromLogs(logs);
605-
606-
const expectedLogs: Log[] = [
607-
SetTokenCreated(
608-
core.address,
609-
newSetTokenAddress,
610-
factoryAddress,
611-
components,
612-
units,
613-
naturalUnit,
614-
name,
615-
symbol,
616-
),
617-
];
618-
619-
await assertLogEquivalence(expectedLogs, logs);
620-
});
621-
622-
describe("when the factory is not valid", async () => {
623-
beforeEach(async () => {
624-
factoryAddress = NULL_ADDRESS;
625-
});
626-
627-
it("should revert", async () => {
628-
await expectRevertError(subject());
629-
});
630-
});
631-
});
632562
});

0 commit comments

Comments
 (0)