Skip to content

Commit

Permalink
fix: various fixes and a bit of cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
pcarranzav committed May 14, 2024
1 parent 0b4e014 commit 4973ecd
Show file tree
Hide file tree
Showing 8 changed files with 232 additions and 57 deletions.
197 changes: 149 additions & 48 deletions packages/horizon/contracts/HorizonStaking.sol

Large diffs are not rendered by default.

43 changes: 42 additions & 1 deletion packages/horizon/contracts/HorizonStakingExtension.sol
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,47 @@ contract HorizonStakingExtension is StakingBackwardsCompatibility, IHorizonStaki
}
}

function getDelegationPool(
address _serviceProvider,
address _verifier
) external view override returns (DelegationPool memory) {
DelegationPool memory pool;
DelegationPoolInternal storage poolInternal;
if (_verifier == SUBGRAPH_DATA_SERVICE_ADDRESS) {
poolInternal = legacyDelegationPools[_serviceProvider];
} else {
poolInternal = delegationPools[_serviceProvider][_verifier];
}
pool.tokens = poolInternal.tokens;
pool.shares = poolInternal.shares;
pool.tokensThawing = poolInternal.tokensThawing;
pool.sharesThawing = poolInternal.sharesThawing;
return pool;
}

function getDelegation(
address _delegator,
address _serviceProvider,
address _verifier
) external view override returns (Delegation memory) {
if (_verifier == SUBGRAPH_DATA_SERVICE_ADDRESS) {
return legacyDelegationPools[_serviceProvider].delegators[_delegator];
} else {
return delegationPools[_serviceProvider][_verifier].delegators[_delegator];
}
}

function getThawRequest(bytes32 _thawRequestId) external view returns (ThawRequest memory) {
return thawRequests[_thawRequestId];
}

function getProvision(
address _serviceProvider,
address _verifier
) external view override returns (Provision memory) {
return provisions[_serviceProvider][_verifier];
}

/**
* @dev Receive an Indexer's stake from L1.
* The specified amount is added to the indexer's stake; the indexer's
Expand Down Expand Up @@ -161,7 +202,7 @@ contract HorizonStakingExtension is StakingBackwardsCompatibility, IHorizonStaki
IL2StakingTypes.ReceiveDelegationData memory _delegationData
) internal {
// Get the delegation pool of the indexer
DelegationPool storage pool = legacyDelegationPools[_delegationData.indexer];
DelegationPoolInternal storage pool = legacyDelegationPools[_delegationData.indexer];
Delegation storage delegation = pool.delegators[_delegationData.delegator];

// Calculate shares to issue (without applying any delegation tax)
Expand Down
6 changes: 3 additions & 3 deletions packages/horizon/contracts/HorizonStakingStorage.sol
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,9 @@ abstract contract HorizonStakingV1Storage is Managed, IHorizonStakingTypes {
/// Deprecated, no tax is applied now.
uint32 internal __DEPRECATED_delegationTaxPercentage;

/// @dev Delegation pools : serviceProvider => DelegationPool
/// @dev Delegation pools : serviceProvider => DelegationPoolInternal
/// These are for the subgraph data service.
mapping(address => DelegationPool) internal legacyDelegationPools;
mapping(address => DelegationPoolInternal) internal legacyDelegationPools;

// -- Operators --

Expand Down Expand Up @@ -143,5 +143,5 @@ abstract contract HorizonStakingV1Storage is Managed, IHorizonStakingTypes {
bool public delegationSlashingEnabled;

// delegation pools for each service provider and verifier
mapping(address => mapping(address => DelegationPool)) internal delegationPools;
mapping(address => mapping(address => DelegationPoolInternal)) internal delegationPools;
}
3 changes: 3 additions & 0 deletions packages/horizon/contracts/IHorizonStakingBase.sol
Original file line number Diff line number Diff line change
Expand Up @@ -167,4 +167,7 @@ interface IHorizonStakingBase is IHorizonStakingTypes {
* @param _verifier The verifier / data service on which they're claiming to act
*/
function isAuthorized(address _operator, address _serviceProvider, address _verifier) external view returns (bool);

function getProviderTokensAvailable(address _serviceProvider, address _verifier) external view returns (uint256);
function setMaxThawingPeriod(uint64 _maxThawingPeriod) external;
}
16 changes: 15 additions & 1 deletion packages/horizon/contracts/IHorizonStakingExtension.sol
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ interface IHorizonStakingExtension {
function getStake(address serviceProvider) external view returns (uint256);

function getDelegatedTokensAvailable(address _serviceProvider, address _verifier) external view returns (uint256);

function getTokensAvailable(address _serviceProvider, address _verifier) external view returns (uint256);

function getServiceProvider(
Expand All @@ -30,4 +29,19 @@ interface IHorizonStakingExtension {
function setOperator(address _operator, address _verifier, bool _allowed) external;

function getMaxThawingPeriod() external view returns (uint64);

function getDelegationPool(
address _serviceProvider,
address _verifier
) external view returns (IHorizonStakingTypes.DelegationPool memory);
function getDelegation(
address _delegator,
address _serviceProvider,
address _verifier
) external view returns (IHorizonStakingTypes.Delegation memory);
function getThawRequest(bytes32 _thawRequestId) external view returns (IHorizonStakingTypes.ThawRequest memory);
function getProvision(
address _serviceProvider,
address _verifier
) external view returns (IHorizonStakingTypes.Provision memory);
}
10 changes: 9 additions & 1 deletion packages/horizon/contracts/IHorizonStakingTypes.sol
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ interface IHorizonStakingTypes {
uint32 maxVerifierCut;
// time, in seconds, tokens must thaw before being withdrawn
uint64 thawingPeriod;
uint64 createdAt;
bytes32 firstThawRequestId;
bytes32 lastThawRequestId;
uint256 nThawRequests;
Expand All @@ -29,7 +30,7 @@ interface IHorizonStakingTypes {
uint256 nextThawRequestNonce;
}

struct DelegationPool {
struct DelegationPoolInternal {
uint32 __DEPRECATED_cooldownBlocks; // solhint-disable-line var-name-mixedcase
uint32 __DEPRECATED_indexingRewardCut; // in PPM
uint32 __DEPRECATED_queryFeeCut; // in PPM
Expand All @@ -41,6 +42,13 @@ interface IHorizonStakingTypes {
uint256 sharesThawing; // Shares representing the thawing tokens
}

struct DelegationPool {
uint256 tokens; // Total tokens as pool reserves
uint256 shares; // Total shares minted in the pool
uint256 tokensThawing; // Tokens thawing in the pool
uint256 sharesThawing; // Shares representing the thawing tokens
}

struct Delegation {
uint256 shares; // Shares owned by a delegator in the pool
uint256 __DEPRECATED_tokensLocked; // Tokens locked for undelegation
Expand Down
4 changes: 2 additions & 2 deletions packages/horizon/contracts/StakingBackwardsCompatibility.sol
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ abstract contract StakingBackwardsCompatibility is
*/
function _collectDelegationQueryRewards(address _indexer, uint256 _tokens) private returns (uint256) {
uint256 delegationRewards = 0;
DelegationPool storage pool = legacyDelegationPools[_indexer];
DelegationPoolInternal storage pool = legacyDelegationPools[_indexer];
if (pool.tokens > 0 && pool.__DEPRECATED_queryFeeCut < MAX_PPM) {
uint256 indexerCut = (uint256(pool.__DEPRECATED_queryFeeCut) * _tokens) / MAX_PPM;
delegationRewards = _tokens - indexerCut;
Expand All @@ -338,7 +338,7 @@ abstract contract StakingBackwardsCompatibility is
*/
function _collectDelegationIndexingRewards(address _indexer, uint256 _tokens) private returns (uint256) {
uint256 delegationRewards = 0;
DelegationPool storage pool = legacyDelegationPools[_indexer];
DelegationPoolInternal storage pool = legacyDelegationPools[_indexer];
if (pool.tokens > 0 && pool.__DEPRECATED_indexingRewardCut < MAX_PPM) {
uint256 indexerCut = (uint256(pool.__DEPRECATED_indexingRewardCut) * _tokens) / MAX_PPM;
delegationRewards = _tokens - indexerCut;
Expand Down
10 changes: 9 additions & 1 deletion packages/horizon/hardhat.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,15 @@ import '@nomicfoundation/hardhat-toolbox'
import { HardhatUserConfig } from 'hardhat/config'

const config: HardhatUserConfig = {
solidity: '0.8.24',
solidity: {
version: '0.8.24',
settings: {
optimizer: {
enabled: true,
runs: 200,
},
},
},
paths: {
artifacts: './build/contracts',
sources: './contracts',
Expand Down

0 comments on commit 4973ecd

Please sign in to comment.