Skip to content

Commit

Permalink
test: add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kupermind committed Oct 23, 2024
1 parent 4dca0fd commit 80d45c2
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 13 deletions.
12 changes: 6 additions & 6 deletions contracts/contribute/ContributeManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ contract ContributeManager is ERC721TokenReceiver {
}

// Check for existing service corresponding to the msg.sender
(, uint256 serviceId, address multisig, ) = IContributors(contributorsProxy).mapSocialIdServiceInfo(msg.sender);
(, uint256 serviceId, address multisig, ) = IContributors(contributorsProxy).mapAccountServiceInfo(msg.sender);
if (serviceId > 0) {
revert ServiceAlreadyStaked(socialId, serviceId, multisig);
}
Expand Down Expand Up @@ -273,7 +273,7 @@ contract ContributeManager is ERC721TokenReceiver {
_locked = 2;

// Check for existing service corresponding to the msg.sender
(, uint256 serviceIdCheck, address multisig, ) = IContributors(contributorsProxy).mapSocialIdServiceInfo(msg.sender);
(, uint256 serviceIdCheck, address multisig, ) = IContributors(contributorsProxy).mapAccountServiceInfo(msg.sender);
if (serviceIdCheck > 0) {
revert ServiceAlreadyStaked(socialId, serviceIdCheck, multisig);
}
Expand All @@ -289,7 +289,7 @@ contract ContributeManager is ERC721TokenReceiver {
}

// Transfer the service NFT
IToken(serviceRegistry).transferFrom(msg.sender, address(this), serviceId);
INFToken(serviceRegistry).safeTransferFrom(msg.sender, address(this), serviceId);

// Stake the service
_stake(socialId, serviceId, multisig, stakingInstance);
Expand All @@ -309,7 +309,7 @@ contract ContributeManager is ERC721TokenReceiver {

// Check for existing service corresponding to the social Id
(uint256 socialId, uint256 serviceId, address multisig, address stakingInstance) =
IContributors(contributorsProxy).mapSocialIdServiceInfo(msg.sender);
IContributors(contributorsProxy).mapAccountServiceInfo(msg.sender);
if (serviceId == 0) {
revert ServiceNotDefined(socialId);
}
Expand All @@ -318,7 +318,7 @@ contract ContributeManager is ERC721TokenReceiver {
IStaking(stakingInstance).unstake(serviceId);

// Transfer the service back to the original owner
IToken(serviceRegistry).transfer(msg.sender, serviceId);
INFToken(serviceRegistry).transferFrom(address(this), msg.sender, serviceId);

// Zero the service info: the service is out of the contribute records, however multisig activity is still valid
// If the same service is staked back, the multisig activity continues being tracked
Expand All @@ -340,7 +340,7 @@ contract ContributeManager is ERC721TokenReceiver {

// Check for existing service corresponding to the social Id
(uint256 socialId, uint256 serviceId, address multisig, address stakingInstance) =
IContributors(contributorsProxy).mapSocialIdServiceInfo(msg.sender);
IContributors(contributorsProxy).mapAccountServiceInfo(msg.sender);
if (serviceId == 0) {
revert ServiceNotDefined(socialId);
}
Expand Down
6 changes: 3 additions & 3 deletions contracts/contribute/Contributors.sol
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ contract Contributors {
// Service manager contract address
address public manager;

// Mapping of address => service info
mapping(address => ServiceInfo) public mapSocialIdServiceInfo;
// Mapping of account address => service info
mapping(address => ServiceInfo) public mapAccountServiceInfo;
// Mapping of service multisig address => activity
mapping(address => uint256) public mapMutisigActivities;
// Mapping of whitelisted contributor agents
Expand Down Expand Up @@ -151,7 +151,7 @@ contract Contributors {
}

// Set (or remove) multisig for the corresponding social id
ServiceInfo storage serviceInfo = mapSocialIdServiceInfo[serviceOwner];
ServiceInfo storage serviceInfo = mapAccountServiceInfo[serviceOwner];
serviceInfo.socialId = socialId;
serviceInfo.serviceId = serviceId;
serviceInfo.multisig = multisig;
Expand Down
4 changes: 2 additions & 2 deletions contracts/contribute/interfaces/IContributors.sol
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ interface IContributors {
address stakingInstance
) external;

/// @dev Gets service info corresponding to a specified social Id.
/// @dev Gets service info corresponding to a specified service owner.
/// @param serviceOwner Service owner.
/// @return socialId Social Id.
/// @return serviceId Corresponding service Id.
/// @return multisig Corresponding service multisig.
/// @return stakingInstance Staking instance address.
function mapSocialIdServiceInfo(address serviceOwner) external view
function mapAccountServiceInfo(address serviceOwner) external view
returns (uint256 socialId, uint256 serviceId, address multisig, address stakingInstance);
}
12 changes: 12 additions & 0 deletions contracts/contribute/interfaces/IToken.sol
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,16 @@ interface INFToken {
/// @param spender Account address that will be able to transfer the token on behalf of the caller.
/// @param id Token id.
function approve(address spender, uint256 id) external;

/// @dev Transfers a specified token Id.
/// @param from Account address to transfer from.
/// @param to Account address to transfer to.
/// @param id Token id.
function transferFrom(address from, address to, uint256 id) external;

/// @dev Transfers a specified token Id with a callback.
/// @param from Account address to transfer from.
/// @param to Account address to transfer to.
/// @param id Token id.
function safeTransferFrom(address from, address to, uint256 id) external;
}
64 changes: 62 additions & 2 deletions test/StakingContribute.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,10 @@ describe("Staking", function () {
await serviceRegistry.changeMultisigPermission(gnosisSafeMultisig.address, true);

// Set the manager of contributorsProxy
contributors.changeManager(contributeManager.address);
await contributors.changeManager(contributeManager.address);

// Set deployer address to be the agent
await contributors.setContributeAgentStatuses([deployer.address], [true]);

// Fund the staking contract
await token.approve(stakingTokenAddress, ethers.utils.parseEther("1"));
Expand All @@ -166,10 +169,67 @@ describe("Staking", function () {
});

context("Contribute manager", function () {
it.only("Mint and stake", async function () {
it("Mint and stake", async function () {
// Approve OLAS for contributeManager
await token.approve(contributeManager.address, serviceParams.minStakingDeposit * 2);

// Mint and stake the service
await contributeManager.createAndStake(socialId, stakingToken.address, {value: 2});
});

it("Mint, stake, unstake and stake again", async function () {
// Take a snapshot of the current state of the blockchain
const snapshot = await helpers.takeSnapshot();

// Approve OLAS for contributeManager
await token.approve(contributeManager.address, serviceParams.minStakingDeposit * 2);

// Mint and stake the service
await contributeManager.createAndStake(socialId, stakingToken.address, {value: 2});

// Increase the time while the service does not reach the required amount of transactions per second (TPS)
await helpers.time.increase(maxInactivity);

// Unstake the service
await contributeManager.unstake();

// Approve the service for the contributeManager
await serviceRegistry.approve(contributeManager.address, serviceId);

// Stake the service again
await contributeManager.stake(socialId, serviceId, stakingToken.address);

// Restore a previous state of blockchain
snapshot.restore();
});

it("Mint, stake, perform activity, claim", async function () {
// Take a snapshot of the current state of the blockchain
const snapshot = await helpers.takeSnapshot();

// Approve OLAS for contributeManager
await token.approve(contributeManager.address, serviceParams.minStakingDeposit * 2);

// Mint and stake the service
await contributeManager.createAndStake(socialId, stakingToken.address, {value: 2});

// Get the user data
const serviceInfo = await contributors.mapAccountServiceInfo(deployer.address);

// Perform the service activity
await contributors.increaseActivity([serviceInfo.multisig], [10]);

// Increase the time until the next staking epoch
await helpers.time.increase(livenessPeriod);

// Call the checkpoint
await stakingToken.checkpoint();

// Claim rewards
await contributeManager.claim();

// Restore a previous state of blockchain
snapshot.restore();
});
});
});

0 comments on commit 80d45c2

Please sign in to comment.