maker registry compiles, untested

This commit is contained in:
Greg Hysen
2019-06-01 00:15:43 -07:00
parent 1c14948f8a
commit fae14a755f
6 changed files with 254 additions and 37 deletions

View File

@@ -20,13 +20,15 @@ pragma solidity ^0.5.9;
import "./core/MixinStorage.sol";
import "./core/MixinStake.sol";
import "./core/MixinMakerRegistry.sol";
contract Staking is
//IStaking,
//IStakingEvents,
MixinStorage,
MixinStake
MixinStake,
MixinMakerRegistry
{
constructor()
public
@@ -116,6 +118,54 @@ contract Staking is
return balance;
}
///// MAKERS /////
function createMakerId()
external
returns (bytes32 makerId)
{
makerId = _createMakerId(msg.sender);
return makerId;
}
function addMakerAddress(
bytes32 makerId,
address makerAddress,
bytes calldata makerSignature
)
external
{
_addMakerAddress(
makerId,
makerAddress,
makerSignature,
msg.sender
);
}
function removeMakerAddress(bytes32 makerId, address makerAddress)
external
{
_removeMakerAddress(makerId, makerAddress, msg.sender);
}
function getMakerId(address makerAddress)
external
view
returns (bytes32 makerId)
{
makerId = _getMakerId(makerAddress);
return makerId;
}
function getMakerAddresses(bytes32 makerId)
external
view
returns (address[] memory makerAddresses)
{
makerAddresses = _getMakerAddresses(makerId);
return makerAddresses;
}
///// SETTERS /////
function setZrxVault(address _zrxVault)

View File

@@ -19,6 +19,15 @@
pragma solidity ^0.5.5;
contract MakerRegistry {
contract MixinConstants {
uint256 constant TOKEN_MULTIPLIER = 10**18;
bytes32 constant INITIAL_MAKER_ID = 0x0000000000000000000000000000000080000000000000000000000000000000;
bytes32 constant public NIL_MAKER_ID = 0x0000000000000000000000000000000000000000000000000000000000000000;
address constant public NIL_ADDRESS = 0x0000000000000000000000000000000000000000;
}
}

View File

@@ -0,0 +1,171 @@
/*
Copyright 2018 ZeroEx Intl.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
pragma solidity ^0.5.5;
import "./MixinStorage.sol";
import "@0x/contracts-utils/contracts/src/SafeMath.sol";
import "../libs/LibConstants.sol";
contract MixinMakerRegistry is
SafeMath,
LibConstants,
MixinStorage
{
constructor()
internal
{
// the upper 128 bits are used for maker id
nextMakerId = 0x0000000000000000000000000000000080000000000000000000000000000000; // LibConstants.INITIAL_MAKER_ID
}
function _createMakerId(address makerAddress)
internal
returns (bytes32 makerId)
{
//
makerId = nextMakerId;
nextMakerId = bytes32(_safeAdd(uint256(nextMakerId), 1));
//
_recordMakerAddress(makerId, makerAddress);
return makerId;
}
function _addMakerAddress(
bytes32 makerId,
address makerAddress,
bytes memory makeSignature,
address requestingMakerAddress
)
internal
{
require(
_getMakerId(requestingMakerAddress) == makerId,
"MAKER_ID_NOT_ASSOCIATED_WITH_REQUESTING_ADDRESS"
);
_recordMakerAddress(makerId, makerAddress);
}
function _removeMakerAddress(
bytes32 makerId,
address makerAddress,
address requestingMakerAddress
)
internal
{
require(
_getMakerId(requestingMakerAddress) == makerId,
"MAKER_ID_NOT_ASSOCIATED_WITH_REQUESTING_ADDRESS"
);
_unrecordMakerAddress(makerId, makerAddress);
}
/*
function _isValidMakerSignature(address makerAddress, bytes memory makerSignature)
internal
returns (bool isValid)
{
}
*/
function _getMakerId(address makerAddress)
internal
view
returns (bytes32)
{
return makerIds[makerAddress];
}
function _isMakerAddressRegistered(address makerAddress)
internal
view
returns (bool)
{
return _getMakerId(makerAddress) != 0x0;
}
function _getMakerAddresses(bytes32 makerId)
internal
view
returns (address[] memory _makerAddresses)
{
//
address[] storage makerAddressesPtr = makerAddresses[makerId];
uint256 makerAddressesLength = makerAddressesPtr.length;
//
_makerAddresses = new address[](makerAddressesLength);
for(uint i = 0; i < makerAddressesLength; ++i) {
_makerAddresses[i] = makerAddressesPtr[i];
}
return _makerAddresses;
}
function _recordMakerAddress(
bytes32 makerId,
address makerAddress
)
private
{
require(
!_isMakerAddressRegistered(makerAddress),
"MAKER_ADDRESS_ALREADY_REGISTERED"
);
makerIds[makerAddress] = makerId;
makerAddresses[makerId].push(makerAddress);
}
function _unrecordMakerAddress(
bytes32 makerId,
address makerAddress
)
private
{
require(
_getMakerId(makerAddress) == makerId,
"MAKER_ADDRESS_ALREADY_REGISTERED"
);
//
address[] storage makerAddressesPtr = makerAddresses[makerId];
uint256 makerAddressesLength = makerAddressesPtr.length;
//
uint indexOfMakerAddress = 0;
for(; indexOfMakerAddress < makerAddressesLength; ++indexOfMakerAddress) {
if (makerAddressesPtr[indexOfMakerAddress] == makerAddress) {
break;
}
}
//
makerAddressesPtr[indexOfMakerAddress] = makerAddressesPtr[makerAddressesLength - 1];
makerAddressesPtr[indexOfMakerAddress] = NIL_ADDRESS;
makerAddressesPtr.length -= 1;
//
makerIds[makerAddress] = LibConstants.NIL_MAKER_ID;
}
}

View File

@@ -35,6 +35,16 @@ contract MixinStorage {
// mapping from Maker Id to Amount of Delegated Staked
mapping (bytes32 => uint256) totalDelegatedStake;
// tracking Maker Id
bytes32 nextMakerId;
// mapping from Maker address to Maker Id
// A Maker can only hold a single token
mapping (address => bytes32) makerIds;
// mapping from Maker Id to Addresses
mapping (bytes32 => address[]) makerAddresses;
// ZRX vault
IVault zrxVault;

View File

@@ -67,39 +67,6 @@ library LibMath {
// 4. Run Newton's Approximation to approximate the root
root := add(x, div(y, mul(n, exp(2, mul(div(m, n), sub(n, 1))))))
/**
* Note 1:
* On some clients (like ganache), execution freezes when running exponentiation
* with a dynamically generated base. Because of this, all exponentiation above
* is done base-2.
* Example:
* Call the solidity function below with `n >= 1` and execution will timeout.
*
* function repro(uint256 n) public pure returns (uint256) {
* uint256 m = 2**n;
* return m**n;
* }
*
* Call a similar function with the same input _does not_ timeout:
*
* function fix(uint256 n) public pure returns (uint256) {
* uint256 m = 2**(n*n);
* return m;
* }
*/
// ganache core workaround (issue #430)
function exp2(b,p) -> z {
z := b
for {p := sub(p, 1)}
gt(p, 0)
{p := sub(p, 1)}
{
z := mul(z, b)
}
}
// 5. Run Newton's nth Root Algorithm
let delta := 1 // run at least once
for {let i := 0}
@@ -123,6 +90,17 @@ library LibMath {
root := sub(root, delta)
}
}
// ganache core workaround (issue #430)
function exp2(b,p) -> z {
z := b
for {p := sub(p, 1)}
gt(p, 0)
{p := sub(p, 1)}
{
z := mul(z, b)
}
}
}
}

View File

@@ -36,7 +36,6 @@ contract ZrxVault is
public
{}
function depositFrom(address owner, uint256 amount)
external
onlyStakingContract