From 5e7b307c8340ace107b1e4937d5c1505fbe20288 Mon Sep 17 00:00:00 2001 From: Sam McCord Date: Wed, 25 Sep 2024 16:43:32 -0600 Subject: [PATCH] fix(sdk,tests): actually test managedbudget --- .../sdk/src/Budgets/ManagedBudget.test.ts | 71 +++++++++++++++++-- packages/sdk/src/Budgets/ManagedBudget.ts | 32 +++++---- 2 files changed, 84 insertions(+), 19 deletions(-) diff --git a/packages/sdk/src/Budgets/ManagedBudget.test.ts b/packages/sdk/src/Budgets/ManagedBudget.test.ts index f0a8ba6d..8d372126 100644 --- a/packages/sdk/src/Budgets/ManagedBudget.test.ts +++ b/packages/sdk/src/Budgets/ManagedBudget.test.ts @@ -4,6 +4,7 @@ import { isAddress, parseEther, zeroAddress } from 'viem'; import { beforeAll, beforeEach, describe, expect, test } from 'vitest'; import type { MockERC20 } from '../../test/MockERC20'; import type { MockERC1155 } from '../../test/MockERC1155'; +import { accounts } from '../../test/accounts'; import { type Fixtures, defaultOptions, @@ -15,7 +16,7 @@ import { fundManagedBudget, } from '../../test/helpers'; import { testAccount } from '../../test/viem'; -import { ManagedBudget } from './ManagedBudget'; +import { ManagedBudget, ManagedBudgetRoles } from './ManagedBudget'; let fixtures: Fixtures, budget: ManagedBudget, @@ -37,13 +38,73 @@ describe('ManagedBudget', () => { expect(isAddress(action.assertValidAddress())).toBe(true); }); + test('can grant manager role to many users', async () => { + const budget = await loadFixture( + freshManagedBudget(defaultOptions, fixtures), + ); + const one = accounts.at(1)!.account; + const two = accounts.at(2)!.account; + await budget.setAuthorized([one, two], [true, true]); + expect(await budget.hasAllRoles(one, ManagedBudgetRoles.ADMIN)).toBe(false); + expect(await budget.hasAllRoles(one, ManagedBudgetRoles.MANAGER)).toBe( + true, + ); + expect(await budget.hasAllRoles(two, ManagedBudgetRoles.MANAGER)).toBe( + true, + ); + }); + + test('can grant roles', async () => { + const budget = await loadFixture( + freshManagedBudget(defaultOptions, fixtures), + ); + const admin = accounts.at(1)!.account; + const manager = accounts.at(2)!.account; + await budget.grantRoles( + [admin, manager], + [ManagedBudgetRoles.ADMIN, ManagedBudgetRoles.MANAGER], + ); + expect(await budget.hasAllRoles(admin, ManagedBudgetRoles.ADMIN)).toBe( + true, + ); + expect(await budget.hasAllRoles(manager, ManagedBudgetRoles.MANAGER)).toBe( + true, + ); + }); + + test('can revoke roles', async () => { + const budget = await loadFixture( + freshManagedBudget(defaultOptions, fixtures), + ); + const admin = accounts.at(1)!.account; + const manager = accounts.at(2)!.account; + await budget.grantRoles( + [admin, manager], + [ManagedBudgetRoles.ADMIN, ManagedBudgetRoles.MANAGER], + ); + await budget.revokeRoles( + [admin, manager], + [ManagedBudgetRoles.ADMIN, ManagedBudgetRoles.MANAGER], + ); + expect(await budget.hasAllRoles(admin, ManagedBudgetRoles.ADMIN)).toBe( + false, + ); + expect(await budget.hasAllRoles(manager, ManagedBudgetRoles.MANAGER)).toBe( + false, + ); + }); + test('can be owned', async () => { - const budget = await loadFixture(freshBudget(defaultOptions, fixtures)); + const budget = await loadFixture( + freshManagedBudget(defaultOptions, fixtures), + ); expect(await budget.owner()).toBe(defaultOptions.account.address); }); test('can have authorized users', async () => { - const budget = await loadFixture(freshBudget(defaultOptions, fixtures)); + const budget = await loadFixture( + freshManagedBudget(defaultOptions, fixtures), + ); expect(await budget.isAuthorized(defaultOptions.account.address)).toBe( true, ); @@ -51,7 +112,9 @@ describe('ManagedBudget', () => { }); test('can have no initial balance', async () => { - const budget = await loadFixture(freshBudget(defaultOptions, fixtures)); + const budget = await loadFixture( + freshManagedBudget(defaultOptions, fixtures), + ); expect(await budget.available(zeroAddress)).toBe(0n); }); diff --git a/packages/sdk/src/Budgets/ManagedBudget.ts b/packages/sdk/src/Budgets/ManagedBudget.ts index 300822fd..2f639e7e 100644 --- a/packages/sdk/src/Budgets/ManagedBudget.ts +++ b/packages/sdk/src/Budgets/ManagedBudget.ts @@ -81,7 +81,7 @@ export interface ManagedBudgetPayload { /** * List of roles to assign to the corresponding account by index. * - * @type {bigint[]} + * @type {ManagedBudgetRoles[]} */ roles: ManagedBudgetRoles[]; } @@ -447,13 +447,13 @@ export class ManagedBudget extends DeployableTarget< * @public * @async * @param {Address[]} addresses - * @param {bigint[]} roles + * @param {ManagedBudgetRoles[]} roles * @param {?WriteParams} [params] * @returns {unknown} */ public async grantRoles( addresses: Address[], - roles: bigint[], + roles: ManagedBudgetRoles[], params?: WriteParams, ) { return await this.awaitResult(this.grantRolesRaw(addresses, roles, params)); @@ -469,13 +469,13 @@ export class ManagedBudget extends DeployableTarget< * @public * @async * @param {Address[]} addresses - * @param {bigint[]} roles + * @param {ManagedBudgetRoles[]} roles * @param {?WriteParams} [params] * @returns {unknown} */ public async grantRolesRaw( addresses: Address[], - roles: bigint[], + roles: ManagedBudgetRoles[], params?: WriteParams, ) { const { request, result } = await simulateManagedBudgetGrantRoles( @@ -506,13 +506,13 @@ export class ManagedBudget extends DeployableTarget< * @public * @async * @param {Address[]} addresses - * @param {bigint[]} roles + * @param {ManagedBudgetRoles[]} roles * @param {?WriteParams} [params] * @returns {unknown} */ public async revokeRoles( addresses: Address[], - roles: bigint[], + roles: ManagedBudgetRoles[], params?: WriteParams, ) { return await this.awaitResult( @@ -529,13 +529,13 @@ export class ManagedBudget extends DeployableTarget< * @public * @async * @param {Address[]} addresses - * @param {bigint[]} roles + * @param {ManagedBudgetRoles[]} roles * @param {?WriteParams} [params] * @returns {unknown} */ public async revokeRolesRaw( addresses: Address[], - roles: bigint[], + roles: ManagedBudgetRoles[], params?: WriteParams, ) { const { request, result } = await simulateManagedBudgetRevokeRoles( @@ -564,7 +564,7 @@ export class ManagedBudget extends DeployableTarget< * @public * @param {Address} account * @param {?ReadParams} [params] - * @returns {Promise>} + * @returns {Promise>} */ public async rolesOf( account: Address, @@ -582,7 +582,9 @@ export class ManagedBudget extends DeployableTarget< ManagedBudgetRoles.MANAGER, ManagedBudgetRoles.ADMIN, ] as unknown as Array - ).filter((role) => (roles & role) === role); + ).filter( + (role) => (roles & role) === role, + ) as unknown as ManagedBudgetRoles[]; } /** @@ -593,13 +595,13 @@ export class ManagedBudget extends DeployableTarget< * await budget.hasAnyRole(0xfoo, ManagedBudgetRoles.ADMIN | ManagedBudgetRoles.MANAGER) * @public * @param {Address} account - * @param {bigint} roles + * @param {ManagedBudgetRoles} roles * @param {?ReadParams} [params] * @returns {Promise} */ public hasAnyRole( account: Address, - roles: bigint, + roles: ManagedBudgetRoles, params?: ReadParams, ) { return readManagedBudgetHasAnyRole(this._config, { @@ -620,13 +622,13 @@ export class ManagedBudget extends DeployableTarget< * * @public * @param {Address} account - * @param {bigint} roles + * @param {ManagedBudgetRoles} roles * @param {?ReadParams} [params] * @returns {*} */ public hasAllRoles( account: Address, - roles: bigint, + roles: ManagedBudgetRoles, params?: ReadParams, ) { return readManagedBudgetHasAllRoles(this._config, {