Skip to content

Commit

Permalink
Contracts: make UUIDs iterable. Relates to #89
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason Banks committed Feb 2, 2018
1 parent 8adbf90 commit 35f4d28
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 54 deletions.
86 changes: 41 additions & 45 deletions contracts/OpenSTUtility.sol
Expand Up @@ -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;

/*
Expand All @@ -44,6 +44,9 @@ contract OpenSTUtility is Hasher, OpsManaged {
struct RegisteredToken {
UtilityTokenInterface token;
address registrar;
string symbol;
string name;
uint256 conversionRate;
}

struct Mint {
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
*/
Expand Down Expand Up @@ -571,30 +557,36 @@ 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
*/
/// @dev for v0.9.1 tracking Ethereum mainnet on the utility chain
/// 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 */
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions contracts/STPrime.sol
Expand Up @@ -72,8 +72,8 @@ contract STPrime is UtilityTokenAbstract, STPrimeConfig {
public
UtilityTokenAbstract(
_uuid,
TOKEN_SYMBOL,
TOKEN_NAME,
STPRIME_SYMBOL,
STPRIME_NAME,
_chainIdValue,
_chainIdUtility,
_conversionRate)
Expand Down
12 changes: 7 additions & 5 deletions contracts/STPrimeConfig.sol
@@ -1,3 +1,4 @@
/* solhint-disable-next-line compiler-fixed */
pragma solidity ^0.4.17;

// Copyright 2017 OpenST Ltd.
Expand All @@ -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;
Expand Down
3 changes: 3 additions & 0 deletions test/OpenSTUtility.js
Expand Up @@ -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]);
})
})
Expand Down
4 changes: 2 additions & 2 deletions test/STPrime_utils.js
Expand Up @@ -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 });
Expand Down

0 comments on commit 35f4d28

Please sign in to comment.