From 35f4d2829e7ec0e59d8fd3bda594a2440c487b1b Mon Sep 17 00:00:00 2001 From: Jason Banks Date: Wed, 31 Jan 2018 20:14:53 +0100 Subject: [PATCH] Contracts: make UUIDs iterable. Relates to #89 --- contracts/OpenSTUtility.sol | 86 ++++++++++++++++++------------------- contracts/STPrime.sol | 4 +- contracts/STPrimeConfig.sol | 12 +++--- test/OpenSTUtility.js | 3 ++ test/STPrime_utils.js | 4 +- 5 files changed, 55 insertions(+), 54 deletions(-) diff --git a/contracts/OpenSTUtility.sol b/contracts/OpenSTUtility.sol index bb7def82..186e5fc3 100644 --- a/contracts/OpenSTUtility.sol +++ b/contracts/OpenSTUtility.sol @@ -35,7 +35,7 @@ import "./ProtocolVersioned.sol"; /// @title OpenST Utility -contract OpenSTUtility is Hasher, OpsManaged { +contract OpenSTUtility is Hasher, OpsManaged, STPrimeConfig { using SafeMath for uint256; /* @@ -44,6 +44,9 @@ contract OpenSTUtility is Hasher, OpsManaged { struct RegisteredToken { UtilityTokenInterface token; address registrar; + string symbol; + string name; + uint256 conversionRate; } struct Mint { @@ -93,11 +96,6 @@ contract OpenSTUtility is Hasher, OpsManaged { /* * Constants */ - string public constant STPRIME_SYMBOL = "STP"; - string public constant STPRIME_NAME = "SimpleTokenPrime"; - uint256 public constant STPRIME_CONVERSION_RATE = 1; - uint8 public constant TOKEN_DECIMALS = 18; - uint256 public constant DECIMALSFACTOR = 10**uint256(TOKEN_DECIMALS); // ~2 weeks, assuming ~15s per block uint256 public constant BLOCKS_TO_WAIT_LONG = 80667; // ~1hour, assuming ~15s per block @@ -114,8 +112,9 @@ contract OpenSTUtility is Hasher, OpsManaged { /// chainId of the current utility chain uint256 public chainIdUtility; address public registrar; - /// registered branded tokens - mapping(bytes32 /* uuid */ => RegisteredToken) internal registeredTokens; + bytes32[] public uuids; + /// registered branded tokens + mapping(bytes32 /* uuid */ => RegisteredToken) public registeredTokens; /// name reservation is first come, first serve mapping(bytes32 /* hashName */ => address /* requester */) public nameReservation; /// symbol reserved for unique API routes @@ -170,10 +169,14 @@ contract OpenSTUtility is Hasher, OpsManaged { STPRIME_CONVERSION_RATE); registeredTokens[uuidSTPrime] = RegisteredToken({ - token: UtilityTokenInterface(simpleTokenPrime), - registrar: registrar + token: UtilityTokenInterface(simpleTokenPrime), + registrar: registrar, + symbol: STPRIME_SYMBOL, + name: STPRIME_NAME, + conversionRate: STPRIME_CONVERSION_RATE }); + uuids.push(uuidSTPrime); // lock name and symbol route for ST' bytes32 hashName = keccak256(STPRIME_NAME); nameReservation[hashName] = registrar; @@ -480,23 +483,6 @@ contract OpenSTUtility is Hasher, OpsManaged { return (uuid, redeemer, beneficiary, amountUT); } - /* - * External view functions - */ - function registeredTokenProperties( - bytes32 _uuid) - external - view - returns ( - address, /* token */ - address /* registrar */) - { - RegisteredToken storage registeredToken = registeredTokens[_uuid]; - return ( - address(registeredToken.token), - registeredToken.registrar); - } - /* * Public functions */ @@ -571,6 +557,12 @@ contract OpenSTUtility is Hasher, OpsManaged { return false; } + /// @dev Returns size of uuids + /// @return size + function getUuidsSize() public view returns (uint256) { + return uuids.length; + } + /* * Registrar functions */ @@ -578,23 +570,23 @@ contract OpenSTUtility is Hasher, OpsManaged { /// is not a required feature yet, so the core is simplified /// to uint256 valueChainId as storage on construction // function addCore( - // CoreInterface _core) - // public - // onlyRegistrar - // returns (bool /* success */) + // CoreInterface _core) + // public + // onlyRegistrar + // returns (bool /* success */) // { - // require(address(_core) != address(0)); - // // core constructed with same registrar - // require(registrar == _core.registrar()); - // // on utility chain core only tracks a remote value chain - // uint256 coreChainIdValue = _core.chainIdRemote(); - // require(chainIdUtility != 0); - // // cannot overwrite core for given chainId - // require(cores[coreChainIdValue] == address(0)); - - // cores[coreChainIdValue] = _core; - - // return true; + // require(address(_core) != address(0)); + // // core constructed with same registrar + // require(registrar == _core.registrar()); + // // on utility chain core only tracks a remote value chain + // uint256 coreChainIdValue = _core.chainIdRemote(); + // require(chainIdUtility != 0); + // // cannot overwrite core for given chainId + // require(cores[coreChainIdValue] == address(0)); + + // cores[coreChainIdValue] = _core; + + // return true; // } /* solhint-disable-next-line separate-by-one-line-in-contract */ @@ -631,14 +623,18 @@ contract OpenSTUtility is Hasher, OpsManaged { assert(address(registeredTokens[registeredUuid].token) == address(0)); registeredTokens[registeredUuid] = RegisteredToken({ - token: _brandedToken, - registrar: registrar + token: _brandedToken, + registrar: registrar, + symbol: _symbol, + name: _name, + conversionRate: _conversionRate }); // register name to registrar nameReservation[hashName] = registrar; // register symbol symbolRoute[hashSymbol] = _brandedToken; + uuids.push(registeredUuid); RegisteredBrandedToken(registrar, _brandedToken, registeredUuid, _symbol, _name, _conversionRate, _requester); diff --git a/contracts/STPrime.sol b/contracts/STPrime.sol index 0f1d2cca..74303023 100644 --- a/contracts/STPrime.sol +++ b/contracts/STPrime.sol @@ -72,8 +72,8 @@ contract STPrime is UtilityTokenAbstract, STPrimeConfig { public UtilityTokenAbstract( _uuid, - TOKEN_SYMBOL, - TOKEN_NAME, + STPRIME_SYMBOL, + STPRIME_NAME, _chainIdValue, _chainIdUtility, _conversionRate) diff --git a/contracts/STPrimeConfig.sol b/contracts/STPrimeConfig.sol index 96b1da2a..8e15b5ab 100644 --- a/contracts/STPrimeConfig.sol +++ b/contracts/STPrimeConfig.sol @@ -1,3 +1,4 @@ +/* solhint-disable-next-line compiler-fixed */ pragma solidity ^0.4.17; // Copyright 2017 OpenST Ltd. @@ -21,12 +22,13 @@ pragma solidity ^0.4.17; // // ---------------------------------------------------------------------------- - +/* solhint-disable-next-line two-lines-top-level-separator */ +/// @title STPrimeConfig contract STPrimeConfig { - - string public constant TOKEN_SYMBOL = "STP"; - string public constant TOKEN_NAME = "SimpleTokenPrime"; - uint8 public constant TOKEN_DECIMALS = 18; + string public constant STPRIME_SYMBOL = "STP"; + string public constant STPRIME_NAME = "SimpleTokenPrime"; + uint256 public constant STPRIME_CONVERSION_RATE = 1; + uint8 public constant TOKEN_DECIMALS = 18; uint256 public constant DECIMALSFACTOR = 10**uint256(TOKEN_DECIMALS); uint256 public constant TOKENS_MAX = 800000000 * DECIMALSFACTOR; diff --git a/test/OpenSTUtility.js b/test/OpenSTUtility.js index 52b0bd66..c252559c 100644 --- a/test/OpenSTUtility.js +++ b/test/OpenSTUtility.js @@ -200,8 +200,11 @@ contract('OpenSTUtility', function(accounts) { }) it('successfully registers', async () => { + assert.equal(await openSTUtility.getUuidsSize.call(), 1); // there is already 1 UUID in uuids for STPrime assert.equal(await openSTUtility.registerBrandedToken.call(symbol, name, conversionRate, accounts[0], brandedToken, checkBtUuid, { from: registrar }), checkBtUuid); result = await openSTUtility.registerBrandedToken(symbol, name, conversionRate, accounts[0], brandedToken, checkBtUuid, { from: registrar }); + assert.equal(await openSTUtility.getUuidsSize.call(), 2); + assert.equal((await openSTUtility.registeredTokens.call(checkBtUuid))[2], symbol); await OpenSTUtility_utils.checkRegisteredBrandedTokenEvent(result.logs[0], registrar, brandedToken, checkBtUuid, symbol, name, conversionRate, accounts[0]); }) }) diff --git a/test/STPrime_utils.js b/test/STPrime_utils.js index 26cc08b9..61afb082 100644 --- a/test/STPrime_utils.js +++ b/test/STPrime_utils.js @@ -35,8 +35,8 @@ module.exports.deploySTPrime = async (artifacts, accounts) => { const conversionRate = 10; const genesisChainIdValue = 3; const genesisChainIdUtility = 1410; - const stPrimeSymbol = await stPrimeConfig.TOKEN_SYMBOL.call(); - const stPrimeName = await stPrimeConfig.TOKEN_NAME.call(); + const stPrimeSymbol = await stPrimeConfig.STPRIME_SYMBOL.call(); + const stPrimeName = await stPrimeConfig.STPRIME_NAME.call(); const UUID = await hasher.hashUuid.call(stPrimeSymbol, stPrimeName, genesisChainIdValue, genesisChainIdUtility, openSTProtocol, conversionRate); const stPrime = await STPrime.new(UUID, genesisChainIdValue, genesisChainIdUtility, conversionRate, { from: openSTProtocol });