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

F pending balance #42

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
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
Prev Previous commit
Next Next commit
Added support to setting multiple user pending balance in Registry.sol
  • Loading branch information
aminlatifi committed May 12, 2021
commit 4e9f9fc49c678fcd1c6b5d0fc03a21f99c122ca4
45 changes: 33 additions & 12 deletions contracts/registry/Registry.sol
Original file line number Diff line number Diff line change
@@ -172,27 +172,32 @@ contract Registry is Context, AdminRole {

emit MinterContractSet(_minterContract);
}

// @notice Set pending balance of an address
// @param _adr (address) Address to set
// @param _pendingBalance (uint256) Pending balance of the address
function setPendingBalance(address _adr, uint256 _pendingBalance)
external
onlyAdmin
{
require(
_adr != address(0),
"Cannot set pending balance for zero balance"
);
require(maxTrusts[_adr] != 0, "Address is not a contributor");
require(
cstkToken.balanceOf(_adr) == 0,
"User has activated his membership"
);
_setPendingBalance(_adr, _pendingBalance);
}

pendingBalances[_adr] = _pendingBalance;
/// @notice Set a list of contributors pending balance
/// @dev Can only be called by Admin role.
/// @param _cnt (uint256) Number of contributors to set pending balance
/// @param _adrs (address[]) Addresses to set pending balance
/// @param _pendingBalances (uint256[]) Pending balance values to set to each contributor (in order)
function setPendingBalances(
uint256 _cnt,
address[] calldata _adrs,
uint256[] calldata _pendingBalances
) external onlyAdmin {
require(_adrs.length == _cnt, "Invalid number of addresses");
require(_pendingBalances.length == _cnt, "Invalid number of trust values");

emit PendingBalanceChanged(_adr, _pendingBalance);
for (uint256 i = 0; i < _cnt; i++) {
_setPendingBalance(_adrs[i], _pendingBalances[i]);
}
}

function clearPendingBalance(address _adr)
@@ -237,4 +242,20 @@ contract Registry is Context, AdminRole {

emit ContributorRemoved(_adr);
}

function _setPendingBalance(address _adr, uint256 _pendingBalance) internal {
require(
_adr != address(0),
aminlatifi marked this conversation as resolved.
Show resolved Hide resolved
"Cannot set pending balance for zero balance"
);
require(maxTrusts[_adr] != 0, "Address is not a contributor");
require(
cstkToken.balanceOf(_adr) == 0,
"User has activated his membership"
);

pendingBalances[_adr] = _pendingBalance;

emit PendingBalanceChanged(_adr, _pendingBalance);
}
}
22 changes: 22 additions & 0 deletions test/unit/Registry.spec.ts
Original file line number Diff line number Diff line change
@@ -167,10 +167,15 @@ describe("Test Registry", function () {

it("Should revert if is not called by an Admin role", async function () {
await registry.registerContributor(other, eth("1"));
await registry.registerContributor(otherSecond, eth("1"));

const otherSecondSigner = ethers.provider.getSigner(otherSecond);
await expect(
registry.connect(otherSecondSigner).setPendingBalance(other, eth("1")),
).to.be.revertedWith("AdminRole: caller does not have the Admin role");
await expect(
registry.connect(otherSecondSigner).setPendingBalances(1, [otherSecond], [eth("2")]),
).to.be.revertedWith("AdminRole: caller does not have the Admin role");
});

it("Should change a valid pending balance", async function () {
@@ -181,6 +186,23 @@ describe("Test Registry", function () {
expect(await registry.getPendingBalance(other)).to.eq(eth("3"));
});

it("Should change a multiple valid pending balance", async function () {
const maxTrusts = [eth("1"), eth("2"), eth("3"), eth("4")];
await registry.registerContributors(4, contributors, maxTrusts);

const pendingBalances = [eth("1"), eth("2"), eth("3"), eth("4")];
await expect(
registry.setPendingBalances(4, contributors, pendingBalances),
"Pending balance is not set",
).to.be.ok;

for (let i = 0; i < contributors.length; i++) {
const contributor = contributors[i];
const pendingBalance = pendingBalances[i];
expect(await registry.getPendingBalance(contributor)).to.eq(pendingBalance);
}
});

it("Should revert if address is zero", async function () {
await expect(registry.setPendingBalance(AddressZero, eth("1"))).to.be.revertedWith(
"Cannot set pending balance for zero balance",