Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ interface IRoleManagement {
// emitted when trying to remove all admin accounts
error NoAdminsLeft();

// emitted when assigning supplier role with 0 amount
error AmountIsZero();

/**
* @dev Grant the provided "roles" to all the "accounts", if CASHIN then "amounts" are the allowances
*
Expand Down
5 changes: 3 additions & 2 deletions contracts/contracts/extensions/RoleManagementFacet.sol
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ contract RoleManagementFacet is IRoleManagement, IStaticFunctionSelectors, Suppl
if (roles[i] == cashInRole) {
if (accounts.length != amounts.length) revert ArraysLengthNotEqual(accounts.length, amounts.length);
for (uint256 j = 0; j < accounts.length; j++) {
if (amounts[j] != 0) _grantSupplierRole(accounts[j], amounts[j]);
else _grantUnlimitedSupplierRole(accounts[j]);
if (amounts[j] == 0) revert AmountIsZero();
if (amounts[j] == type(uint256).max) _grantUnlimitedSupplierRole(accounts[j]);
else _grantSupplierRole(accounts[j], amounts[j]);
}
} else {
for (uint256 p = 0; p < accounts.length; p++) {
Expand Down
1 change: 1 addition & 0 deletions contracts/scripts/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export const DEFAULT_CONFIG_VERSION = 1
// * Ethereum
export const ADDRESS_ZERO = ZeroAddress
export const NUMBER_ZERO = 0n
export const UINT256_MAX = (1n << 256n) - 1n

// * Hedera
export const HBAR_DECIMALS = 8n
Expand Down
41 changes: 37 additions & 4 deletions contracts/test/thread1/roleManagement.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
DeployFullInfrastructureCommand,
MESSAGES,
ROLES,
UINT256_MAX,
ValidateTxResponseCommand,
} from '@scripts'
import { deployStableCoinInTests, GAS_LIMIT, randomAccountAddressList } from '@test/shared'
Expand Down Expand Up @@ -121,7 +122,7 @@ describe('➡️ Role Management Tests', function () {
const txResponse = await roleManagementFacet.grantRoles(
rolesToGrant,
randomAccountList,
randomAccountList.map((_, index) => toBigInt(index)),
randomAccountList.map((_, index) => toBigInt(index + 1)),
{
gasLimit: GAS_LIMIT.hederaTokenManager.grantRoles,
}
Expand All @@ -142,14 +143,14 @@ describe('➡️ Role Management Tests', function () {
gasLimit: GAS_LIMIT.hederaTokenManager.getSupplierAllowance,
})

expect(allowance.toString()).to.eq(accountIndex.toString())
expect(allowance.toString()).to.eq((accountIndex + 1).toString())

const isUnlimited = await supplierAdminFacet
.connect(nonOperator)
.isUnlimitedSupplierAllowance(randomAccountList[accountIndex], {
gasLimit: GAS_LIMIT.hederaTokenManager.isUnlimitedSupplierAllowance,
})
expect(isUnlimited).to.eq(accountIndex == 0)
expect(isUnlimited).to.eq(false)
}
})

Expand Down Expand Up @@ -310,11 +311,43 @@ describe('➡️ Role Management Tests', function () {
})

it('Granting role to account 0 fails', async function () {
const listOfAccounts = randomAccountList
const listOfAccounts: string[] = []

for (let i = 0; i < randomAccountList.length; i++) {
listOfAccounts.push(randomAccountList[i])
}

listOfAccounts.push(ADDRESS_ZERO)

await expect(
roleManagementFacet.grantRoles([ROLES.burn.hash], listOfAccounts, [])
).to.be.revertedWithCustomError(roleManagementFacet, 'AddressZero')
})

it('Granting CashInRole with 0 amount fails', async function () {
await expect(
roleManagementFacet.grantRoles(
[ROLES.cashin.hash],
randomAccountList,
randomAccountList.map(() => 0)
)
).to.be.revertedWithCustomError(roleManagementFacet, 'AmountIsZero')
})

it('Granting CashInRole with max uint256 amount grants unlimited rights', async function () {
await roleManagementFacet.grantRoles(
[ROLES.cashin.hash],
randomAccountList,
randomAccountList.map(() => UINT256_MAX)
)

for (let i = 0; i < randomAccountList.length; i++) {
const isUnlimited = await supplierAdminFacet
.connect(nonOperator)
.isUnlimitedSupplierAllowance(randomAccountList[i], {
gasLimit: GAS_LIMIT.hederaTokenManager.isUnlimitedSupplierAllowance,
})
expect(isUnlimited).to.eq(true)
}
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
*/

import { ICommandHandler } from '../../../../../../core/command/CommandHandler.js';
import { UINT256_MAX } from '../../../../../../core/Constants.js';
import { CommandHandler } from '../../../../../../core/decorator/CommandHandlerDecorator.js';
import { lazyInject } from '../../../../../../core/decorator/LazyInjectDecorator.js';
import BigDecimal from '../../../../../../domain/context/shared/BigDecimal.js';
Expand Down Expand Up @@ -72,9 +73,13 @@ export class GrantMultiRolesCommandHandler

const amountsFormatted: BigDecimal[] = [];
amounts.forEach((amount) => {
amountsFormatted.push(
BigDecimal.fromString(amount, capabilities.coin.decimals),
);
if (amount == '0') {
amountsFormatted.push(BigDecimal.fromString(UINT256_MAX));
} else {
amountsFormatted.push(
BigDecimal.fromString(amount, capabilities.coin.decimals),
);
}
});

const res = await handler.grantRoles(
Expand Down
3 changes: 3 additions & 0 deletions sdk/src/core/Constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,6 @@ export const CONFIG_RESERVE =
export const DEFAULT_VERSION = 1;

export const ONE_THOUSAND = 1000;

export const UINT256_MAX =
'0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff';
Loading