Skip to content

Commit

Permalink
included more tests for grantRoleFrom function
Browse files Browse the repository at this point in the history
  • Loading branch information
ernanirst committed Nov 11, 2023
1 parent dd37dcf commit 3502b89
Show file tree
Hide file tree
Showing 4 changed files with 360 additions and 99 deletions.
62 changes: 41 additions & 21 deletions contracts/RolesRegistry/SftRolesRegistry.sol
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,9 @@ contract SftRolesRegistry is IERCXXXX, ERC1155Holder {
modifier onlyOwnerOrApprovedWithBalance(address _account, address _tokenAddress, uint256 _tokenId, uint256 _tokenAmount) {
require(_tokenAmount > 0, "SftRolesRegistry: tokenAmount must be greater than zero");
require(
_account == msg.sender || IERC1155(_tokenAddress).isApprovedForAll(_account, msg.sender),
_account == msg.sender || isRoleApprovedForAll(_tokenAddress, _account, msg.sender),
"SftRolesRegistry: account not approved"
);
require(
IERC1155(_tokenAddress).balanceOf(_account, _tokenId) >= _tokenAmount,
"SftRolesRegistry: account has insufficient balance"
);
_;
}

Expand All @@ -61,8 +57,7 @@ contract SftRolesRegistry is IERCXXXX, ERC1155Holder {
);

bytes32 rootKey = _getHeadKey(_roleAssignment.grantee, _roleAssignment.role, _roleAssignment.tokenAddress, _roleAssignment.tokenId);
uint256 headNonce = lists.heads[rootKey];
LinkedLists.ListItem storage item = lists.items[headNonce];
LinkedLists.ListItem storage item = lists.items[_roleAssignment.nonce];
if (item.data.expirationDate == 0) {
// nonce is not being used

Expand All @@ -78,29 +73,54 @@ contract SftRolesRegistry is IERCXXXX, ERC1155Holder {
// nonce is being used
require(item.data.hash == hash, "SftRolesRegistry: nonce exist, but data mismatch"); // validates nonce, role, tokenAddress, tokenId, grantor
require(item.data.expirationDate < block.timestamp || item.data.revocable, "SftRolesRegistry: nonce is not expired or is not revocable");
require(item.data.tokenAmount >= _roleAssignment.tokenAmount, "SftRolesRegistry: insufficient tokenAmount in nonce");

// return tokens if any
uint256 tokensToReturn = item.data.tokenAmount - _roleAssignment.tokenAmount;
if (tokensToReturn > 0) {
_transferFrom(
address(this),
_roleAssignment.grantor,
_roleAssignment.tokenAddress,
_roleAssignment.tokenId,
tokensToReturn
);
}

// deposit or withdraw tokens
_depositOrWithdrawTokens(
_roleAssignment.tokenAddress,
_roleAssignment.tokenId,
_roleAssignment.grantor,
item.data.tokenAmount,
_roleAssignment.tokenAmount
);

// remove from the list
lists.remove(rootKey, _roleAssignment.nonce);

}

// insert on the list
_insert(hash, rootKey, _roleAssignment);
}

function _depositOrWithdrawTokens(
address _tokenAddress,
uint256 _tokenId,
address _account,
uint256 _depositedAmount,
uint256 _amountRequired
) internal {
if (_depositedAmount > _amountRequired) {
// return leftover tokens
uint256 tokensToReturn = _depositedAmount - _amountRequired;
_transferFrom(
address(this),
_account,
_tokenAddress,
_tokenId,
tokensToReturn
);
} else if (_amountRequired > _depositedAmount) {
// deposit missing tokens
uint256 tokensToDeposit = _amountRequired - _depositedAmount;
_transferFrom(
_account,
address(this),
_tokenAddress,
_tokenId,
tokensToDeposit
);
}
}

function _insert(bytes32 _hash, bytes32 _rootKey, RoleAssignment calldata _roleAssignment) internal {
RoleData memory data = RoleData(
_hash,
Expand Down
8 changes: 2 additions & 6 deletions test/LinkedLists.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import { beforeEach } from 'mocha'
import { generateRandomInt, assertListItem, assertList } from './helpers'

const { HashZero } = ethers.constants
const HashOne = ethers.utils.formatBytes32String("1")
const HashTwo = ethers.utils.formatBytes32String("2")
const HashOne = ethers.utils.formatBytes32String('1')
const HashTwo = ethers.utils.formatBytes32String('2')

describe('LinkedLists', async () => {
let LinkedLists: Contract
Expand All @@ -23,7 +23,6 @@ describe('LinkedLists', async () => {
})

describe('Insert Item', async () => {

describe('List with one item', async () => {
let FirstItem: { nonce: number; expirationDate: number }

Expand Down Expand Up @@ -118,7 +117,6 @@ describe('LinkedLists', async () => {
})

describe('Remove Item', async () => {

it('when list is empty, should revert', async () => {
await expect(LinkedLists.remove(HashZero, 1)).to.revertedWith('LinkedLists: empty list or invalid nonce')
})
Expand Down Expand Up @@ -220,7 +218,6 @@ describe('LinkedLists', async () => {
await assertList(LinkedLists, HashZero, 0)
await assertList(LinkedLists, HashOne, 0)
await assertList(LinkedLists, HashTwo, 0)

})

it('should insert and remove 1,000 items @skip-on-coverage', async () => {
Expand All @@ -235,5 +232,4 @@ describe('LinkedLists', async () => {
}
await assertList(LinkedLists, HashZero, 0)
})

})
Loading

0 comments on commit 3502b89

Please sign in to comment.