Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: contribute contracts stack #33

Merged
merged 27 commits into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
c534fc0
feat: contribute contracts stack
kupermind Oct 22, 2024
f0620e7
chore: name fix
kupermind Oct 22, 2024
badceb8
chore: removing unnecessary event
kupermind Oct 22, 2024
aa04650
feat: getting close to creating a service
kupermind Oct 22, 2024
6199f79
refactor: first implementation
kupermind Oct 22, 2024
8b52f29
chore: cleanup
kupermind Oct 22, 2024
b5b734c
refactor: polishing
kupermind Oct 22, 2024
0b24408
refactor: polishing
kupermind Oct 22, 2024
a43e44f
refactor: staking verifier
kupermind Oct 23, 2024
edf4380
refactor: service owner dependency first
kupermind Oct 23, 2024
585003f
chore: spacing
kupermind Oct 23, 2024
528f7c1
chore: adding deployment scripts
kupermind Oct 23, 2024
aaa2039
doc: v.1.4.0-internal-audit
Oct 23, 2024
63eedea
doc: v.1.4.0-internal-audit
Oct 23, 2024
bc9a4e9
refactor: NFT fix
kupermind Oct 23, 2024
3918217
chore: set initial contribute agent statuses
kupermind Oct 23, 2024
fda1740
Merge remote-tracking branch 'origin/v.1.4.0-internal-audit' into scr…
kupermind Oct 23, 2024
e5eb2de
addressing audit remarks and adding graphics
kupermind Oct 23, 2024
4dca0fd
chore: adding optimus deployment script for mode
kupermind Oct 23, 2024
80d45c2
test: add more tests
kupermind Oct 23, 2024
4783cd9
chore: missing ABIs
kupermind Oct 23, 2024
b9b5919
chore: adding ABIs
kupermind Oct 23, 2024
4c0c33d
chore: testnet re-deployment
kupermind Oct 23, 2024
b57872f
doc: update contribute flowchart
kupermind Oct 24, 2024
7bd49bb
doc: update contribute flowchart
kupermind Oct 24, 2024
011463c
test: next to full coverage
kupermind Oct 24, 2024
33044d6
Merge pull request #34 from valory-xyz/scripts_tests
DavidMinarsch Oct 24, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions contracts/contribute/ContributeActivityChecker.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.28;

interface IContributors {
function mapMutisigActivities(address multisig) external view returns (uint256);
}

/// @dev Zero address.
error ZeroAddress();

/// @dev Zero value.
error ZeroValue();

/// @title ContributeActivityChecker - Smart contract for performing contributors service staking activity check
/// @author Aleksandr Kuperman - <[email protected]>
/// @author Andrey Lebedev - <[email protected]>
/// @author Tatiana Priemova - <[email protected]>
/// @author David Vilela - <[email protected]>
contract ContributeActivityChecker {
// Liveness ratio in the format of 1e18
uint256 public immutable livenessRatio;
// Contributors proxy contract address
address public immutable contributorsProxy;

/// @dev StakingNativeToken initialization.
/// @param _contributorsProxy Contributors proxy contract address.
/// @param _livenessRatio Liveness ratio in the format of 1e18.
constructor(address _contributorsProxy, uint256 _livenessRatio) {
// Check the zero address
if (_contributorsProxy == address(0)) {
revert ZeroAddress();
}

// Check for zero value
if (_livenessRatio == 0) {
revert ZeroValue();
}

contributorsProxy = _contributorsProxy;
livenessRatio = _livenessRatio;
}

/// @dev Gets service multisig nonces.
/// @param multisig Service multisig address.
/// @return nonces Set of a single service multisig nonce.
function getMultisigNonces(address multisig) external view virtual returns (uint256[] memory nonces) {
nonces = new uint256[](1);
// The nonces are equal to the social off-chain activity corresponding multisig activity
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the main idea of contributors activity checking. This can be then modified / extended.

nonces[0] = IContributors(contributorsProxy).mapMutisigActivities(multisig);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo

}

/// @dev Checks if the service multisig liveness ratio passes the defined liveness threshold.
/// @notice The formula for calculating the ratio is the following:
/// currentNonce - service multisig nonce at time now (block.timestamp);
/// lastNonce - service multisig nonce at the previous checkpoint or staking time (tsStart);
/// ratio = (currentNonce - lastNonce) / (block.timestamp - tsStart).
/// @param curNonces Current service multisig set of a single nonce.
/// @param lastNonces Last service multisig set of a single nonce.
/// @param ts Time difference between current and last timestamps.
/// @return ratioPass True, if the liveness ratio passes the check.
function isRatioPass(
uint256[] memory curNonces,
uint256[] memory lastNonces,
uint256 ts
) external view virtual returns (bool ratioPass) {
// If the checkpoint was called in the exact same block, the ratio is zero
// If the current nonce is not greater than the last nonce, the ratio is zero
if (ts > 0 && curNonces[0] > lastNonces[0]) {
uint256 ratio = ((curNonces[0] - lastNonces[0]) * 1e18) / ts;
ratioPass = (ratio >= livenessRatio);
}
}
}
51 changes: 51 additions & 0 deletions contracts/contribute/ContributeServiceManager.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.28;

/// @dev Zero address.
error ZeroAddress();

/// @dev Zero value.
error ZeroValue();

/// @dev Only contribute is allowed to have access.
error OnlyContribute(address sender, address contribute);

/// @title ContributeServiceManager - Smart contract for managing services for contributors
/// @author Aleksandr Kuperman - <[email protected]>
/// @author Andrey Lebedev - <[email protected]>
/// @author Tatiana Priemova - <[email protected]>
/// @author David Vilela - <[email protected]>
contract ContributeServiceManager {
// Contributors proxy contract address
address public immutable contributorsProxy;

/// @dev StakingNativeToken initialization.
/// @param _contributorsProxy Contributors proxy contract address.
constructor(address _contributorsProxy) {
// Check the zero address
if (_contributorsProxy == address(0)) {
revert ZeroAddress();
}

contributorsProxy = _contributorsProxy;
}

function register(uint256 id, address stakingInstance) external {
kupermind marked this conversation as resolved.
Show resolved Hide resolved
// if (mapSocialHashMultisigs[handleHash] != address(0))
// revert();
//
// createSerivce();
// deploy();
//
// stake(stakingInstance);
// mapUserImplemnetations[multisig] = stakingInstance;
}

function stake(uint256 serviceId, address stakingInstance) public {

}

function unstake() external {

}
}
191 changes: 191 additions & 0 deletions contracts/contribute/Contributors.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.28;

/// @dev Only `owner` has a privilege, but the `sender` was provided.
/// @param sender Sender address.
/// @param owner Required sender address as an owner.
error OwnerOnly(address sender, address owner);

/// @dev The contract is already initialized.
error AlreadyInitialized();

/// @dev Zero address.
error ZeroAddress();

/// @dev Only manager is allowed to have access.
error OnlyManager(address sender, address manager);

/// @dev Wrong length of two arrays.
/// @param numValues1 Number of values in a first array.
/// @param numValues2 Number of values in a second array.
error WrongArrayLength(uint256 numValues1, uint256 numValues2);

/// @dev Account is unauthorized.
/// @param account Account address.
error UnauthorizedAccount(address account);

/// @title Contributors - Smart contract for managing contributors
/// @author Aleksandr Kuperman - <[email protected]>
/// @author Andrey Lebedev - <[email protected]>
/// @author Tatiana Priemova - <[email protected]>
/// @author David Vilela - <[email protected]>
contract ContributeActivityChecker {
event ImplementationUpdated(address indexed implementation);
event OwnerUpdated(address indexed owner);
event ManagerUpdated(address indexed manager);
event SetMultisigForId(uint256 indexed id, address indexed multisig);
event SetContributeAgentStatuses(address[] mechMarketplaces, bool[] statuses);
event MultisigActivityChanged(address indexed senderAgent, address[] multisigs, uint256[] activityChanges);

// Version number
string public constant VERSION = "1.0.0";
// Code position in storage is keccak256("CONTRIBUTORS_PROXY") = "0x8f33b4c48c4f3159dc130f2111086160da6c94439c147bd337ecee0aa81518c7"
bytes32 public constant CONTRIBUTORS_PROXY = 0x8f33b4c48c4f3159dc130f2111086160da6c94439c147bd337ecee0aa81518c7;

// Contract owner
address public owner;
// Service manager contract address
address public manager;

// Mapping of social id => service multisig address
mapping(uint256 => address) public mapSocialHashMultisigs;
// Mapping of service multisig address => activity
mapping(address => uint256) public mapMutisigActivities;
// Mapping of whitelisted contributor agents
mapping(address => bool) public mapContributeAgents;

/// @dev Contributors initializer.
/// @param _manager Manager address.
function initialize(address _manager) external{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What was the idea behind doing another periphery<>core design here? Isn't it overengineered and a single contract would suffice?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The activity management logic might change in the future or become multi-dimensional. This way we update implementation without changing current contributors activity storage.

// Check for already initialized
if (owner != address(0)) {
revert AlreadyInitialized();
}

// Check for zero address
if (_manager == address(0)) {
revert ZeroAddress();
}

owner = msg.sender;
manager = _manager;
}

/// @dev Changes the contributors implementation contract address.
/// @param newImplementation New implementation contract address.
function changeImplementation(address newImplementation) external {
// Check for the ownership
if (msg.sender != owner) {
revert OwnerOnly(msg.sender, owner);
}

// Check for zero address
if (newImplementation == address(0)) {
revert ZeroAddress();
}

// Store the contributors implementation address
assembly {
sstore(CONTRIBUTORS_PROXY, newImplementation)
}

emit ImplementationUpdated(newImplementation);
}

/// @dev Changes contract owner address.
/// @param newOwner Address of a new owner.
function changeOwner(address newOwner) external {
// Check for the ownership
if (msg.sender != owner) {
revert OwnerOnly(msg.sender, owner);
}

// Check for the zero address
if (newOwner == address(0)) {
revert ZeroAddress();
}

owner = newOwner;
emit OwnerUpdated(newOwner);
}

/// @dev Changes contract manager address.
/// @param newManager Address of a new manager.
function changeManager(address newManager) external {
// Check for the ownership
if (msg.sender != owner) {
revert OwnerOnly(msg.sender, owner);
}

// Check for the zero address
if (newManager == address(0)) {
revert ZeroAddress();
}

manager = newManager;
emit ManagerUpdated(newManager);
}

/// @dev Sets service multisig for the social id.
/// @param id Social id.
/// @param multisig Service multisig address.
function setMultisigForId(uint256 id, address multisig) external {
// Check for manager
if (msg.sender != manager) {
revert OnlyManager(msg.sender, manager);
}

// Set (or remove) multisig for the corresponding social id
mapSocialHashMultisigs[id] = multisig;

emit SetMultisigForId(id, multisig);
}

/// @dev Sets contribute agent statues.
/// @param contributeAgents Contribute agent addresses.
/// @param statuses Corresponding whitelisting statues.
function setMechMarketplaceStatuses(address[] memory contributeAgents, bool[] memory statuses) external {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure why mech marketplace is connected to this.. what's the puprose?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The entity which will report the activity is the contribute service. That should be different from the owner.

And what's the manager?

I need a graphic showing the intended design

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo related to copy-pasting :)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will work on the graphics.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

// Check for the ownership
if (msg.sender != owner) {
revert OwnerOnly(msg.sender, owner);
}

// Check for array lengths
if (contributeAgents.length != statuses.length) {
revert WrongArrayLength(contributeAgents.length, statuses.length);
}

// Traverse all the mech marketplaces and statuses
for (uint256 i = 0; i < contributeAgents.length; ++i) {
if (contributeAgents[i] == address(0)) {
revert ZeroAddress();
}

mapContributeAgents[contributeAgents[i]] = statuses[i];
}

emit SetContributeAgentStatuses(contributeAgents, statuses);
}

/// @dev Increases multisig activity by the contribute agent.
/// @param multisigs Multisig addresses.
/// @param activityChanges Corresponding activity changes
function increaseActivity(address[] memory multisigs, uint256[] memory activityChanges) external {
// Check for whitelisted contribute agent
if (!mapContributeAgents[msg.sender]) {
revert UnauthorizedAccount(msg.sender);
}

// Check for array lengths
if (multisigs.length != activityChanges.length) {
revert WrongArrayLength(multisigs.length, activityChanges.length);
}

// Increase / decrease multisig activity
for (uint256 i = 0; i < multisigs.length; ++i) {
mapMutisigActivities[multisigs[i]] += activityChanges[i];
}

emit MultisigActivityChanged(msg.sender, multisigs, activityChanges);
}
}
70 changes: 70 additions & 0 deletions contracts/contribute/ContributorsProxy.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.28;

/// @dev Zero implementation address.
error ZeroImplementationAddress();

/// @dev Zero contributors data.
error ZeroContributorsData();

/// @dev Proxy initialization failed.
error InitializationFailed();

/*
* This is a Contributors proxy contract.
* Proxy implementation is created based on the Universal Upgradeable Proxy Standard (UUPS) EIP-1822.
* The implementation address must be located in a unique storage slot of the proxy contract.
* The upgrade logic must be located in the implementation contract.
* Special contributors implementation address slot is produced by hashing the "CONTRIBUTORS_PROXY"
* string in order to make the slot unique.
* The fallback() implementation for all the delegatecall-s is inspired by the Gnosis Safe set of contracts.
*/

/// @title ContributorsProxy - Smart contract for contributors proxy
/// @author Aleksandr Kuperman - <[email protected]>
/// @author Andrey Lebedev - <[email protected]>
/// @author Tatiana Priemova - <[email protected]>
/// @author David Vilela - <[email protected]>
contract ContributorsProxy {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Full analogy with recent Karma contracts.

// Code position in storage is keccak256("CONTRIBUTORS_PROXY") = "0x8f33b4c48c4f3159dc130f2111086160da6c94439c147bd337ecee0aa81518c7"
bytes32 public constant CONTRIBUTORS_PROXY = 0x8f33b4c48c4f3159dc130f2111086160da6c94439c147bd337ecee0aa81518c7;

/// @dev ContributorsProxy constructor.
/// @param implementation Contributors implementation address.
/// @param contributorsData Contributors initialization data.
constructor(address implementation, bytes memory contributorsData) {
// Check for the zero address, since the delegatecall works even with the zero one
if (implementation == address(0)) {
revert ZeroImplementationAddress();
}

// Check for the zero data
if (contributorsData.length == 0) {
revert ZeroContributorsData();
}

// Store the contributors implementation address
assembly {
sstore(CONTRIBUTORS_PROXY, implementation)
}
// Initialize proxy tokenomics storage
(bool success, ) = implementation.delegatecall(contributorsData);
if (!success) {
revert InitializationFailed();
}
}

/// @dev Delegatecall to all the incoming data.
fallback() external {
assembly {
let implementation := sload(CONTRIBUTORS_PROXY)
calldatacopy(0, 0, calldatasize())
let success := delegatecall(gas(), implementation, 0, calldatasize(), 0, 0)
returndatacopy(0, 0, returndatasize())
if eq(success, 0) {
revert(0, returndatasize())
}
return(0, returndatasize())
}
}
}
Loading
Loading