Skip to content

Commit

Permalink
Update in response to review comments I
Browse files Browse the repository at this point in the history
  • Loading branch information
kronosapiens committed Sep 29, 2022
1 parent b55617f commit ab9a146
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
23 changes: 14 additions & 9 deletions contracts/extensions/ReputationBootstrapper.sol
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ contract ReputationBootstrapper is ColonyExtensionMeta {

// Constants

uint256 constant INT256_MAX = 2**255 - 1;
uint256 constant INT128_MAX = 2**127 - 1;

// Events

event GrantSet(bytes32 hashedPin, uint256 reputationAmount);
event GrantSet(bytes32 hashedSecret, uint256 reputationAmount);
event GrantClaimed(address recipient, uint256 reputationAmount, uint256 tokenAmount);

// Data structures
Expand All @@ -48,6 +48,7 @@ contract ReputationBootstrapper is ColonyExtensionMeta {

address public token;

uint256 decayPeriod;
uint256 decayNumerator;
uint256 decayDenominator;

Expand Down Expand Up @@ -82,6 +83,7 @@ contract ReputationBootstrapper is ColonyExtensionMeta {

address colonyNetwork = colony.getColonyNetwork();
address repCycle = IColonyNetwork(colonyNetwork).getReputationMiningCycle(false);
decayPeriod = IReputationMiningCycle(repCycle).getMiningWindowDuration();
(decayNumerator, decayDenominator) = IReputationMiningCycle(repCycle).getDecayConstant();
}

Expand All @@ -95,35 +97,38 @@ contract ReputationBootstrapper is ColonyExtensionMeta {

/// @notice Called when uninstalling the extension
function uninstall() public override auth {
uint256 balance = ERC20(token).balanceOf(address(this));
require(ERC20(token).transfer(address(colony), balance), "reputation-bootstrapper-transfer-failed");

selfdestruct(address(uint160(address(colony))));
}

// Public

function setGrants(bytes32[] memory _hashedSecrets, uint256[] memory _amounts) public onlyRoot {
function setGrants(bytes32[] memory _hashedSecrets, uint256[] memory _amounts) public onlyRoot notDeprecated {
require(_hashedSecrets.length == _amounts.length, "reputation-bootsrapper-invalid-arguments");

for (uint256 i; i < _hashedSecrets.length; i++) {
require(_amounts[i] <= INT256_MAX, "repuatation-bootsrapper-invalid-amount");
require(_amounts[i] <= INT128_MAX, "reputation-bootstrapper-invalid-amount");
grants[_hashedSecrets[i]] = Grant(_amounts[i], block.timestamp);

emit GrantSet(_hashedSecrets[i], _amounts[i]);
}
}

function claimGrant(uint256 _secret) public {
bytes32 hashedPin = keccak256(abi.encodePacked(_secret));
uint256 grantAmount = grants[hashedPin].amount;
uint256 grantTimestamp = grants[hashedPin].timestamp;
bytes32 hashedSecret = keccak256(abi.encodePacked(_secret));
uint256 grantAmount = grants[hashedSecret].amount;
uint256 grantTimestamp = grants[hashedSecret].timestamp;

require(grantAmount > 0, "reputation-bootstrapper-nothing-to-claim");

delete grants[hashedPin];
delete grants[hashedSecret];

uint256 tokenAmount = min(ERC20(token).balanceOf(address(this)), grantAmount);
require(tokenAmount >= uint256(grantAmount) || tokenAmount <= 0, "reputation-bootstrapper-insufficient-tokens");

for (; grantTimestamp <= block.timestamp - 1 hours; grantTimestamp += 1 hours) {
for (; grantTimestamp <= block.timestamp - decayPeriod; grantTimestamp += decayPeriod) {
grantAmount = grantAmount * decayNumerator / decayDenominator;
}

Expand Down
8 changes: 6 additions & 2 deletions test/extensions/reputation-bootstrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const bnChai = require("bn-chai");
const { ethers } = require("ethers");
const { soliditySha3 } = require("web3-utils");

const { WAD, ADDRESS_ZERO, SECONDS_PER_DAY } = require("../../helpers/constants");
const { WAD, INT128_MAX, ADDRESS_ZERO, SECONDS_PER_DAY } = require("../../helpers/constants");
const { checkErrorRevert, web3GetCode, forwardTime } = require("../../helpers/test-helper");
const { setupRandomColony, getMetaTransactionParameters } = require("../../helpers/test-data-generator");

Expand Down Expand Up @@ -105,14 +105,18 @@ contract("Reputation Bootstrapper", (accounts) => {
expect(grant.amount).to.eq.BN(WAD);
});

it("cannot setup repuation amounts if not root", async () => {
it("cannot setup reputation amounts if not root", async () => {
await checkErrorRevert(reputationBootstrapper.setGrants([], []), "reputation-bootsrapper-caller-not-root");
});

it("cannot setup repuation amounts with mismatched arguments", async () => {
await checkErrorRevert(reputationBootstrapper.setGrants([], [WAD]), "reputation-bootsrapper-invalid-arguments");
});

it("cannot setup repuation amounts with invalid values", async () => {
await checkErrorRevert(reputationBootstrapper.setGrants([soliditySha3(PIN1)], [INT128_MAX.addn(1)]), "reputation-bootstrapper-invalid-amount");
});

it("can claim repuation amounts", async () => {
await reputationBootstrapper.setGrants([soliditySha3(PIN1), soliditySha3(PIN2)], [WAD, WAD.muln(2)]);

Expand Down

0 comments on commit ab9a146

Please sign in to comment.