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 1, 2018
1 parent a7104c6 commit 4ff4b80
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 51 deletions.
84 changes: 40 additions & 44 deletions contracts/OpenSTUtility.sol
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import "./ProtocolVersioned.sol";


/// @title OpenST Utility
contract OpenSTUtility is Hasher, OpsManaged {
contract OpenSTUtility is Hasher, OpsManaged, STPrimeConfig {
using SafeMath for uint256;

/*
Expand All @@ -45,6 +45,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;
bytes32[] public uuids;
/// registered branded tokens
mapping(bytes32 /* uuid */ => RegisteredToken) internal registeredTokens;
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 @@ -468,23 +471,6 @@ contract OpenSTUtility is Hasher, OpsManaged {
return (uuid, redeemer, 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 @@ -559,30 +545,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 @@ -619,14 +611,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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,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

0 comments on commit 4ff4b80

Please sign in to comment.