Skip to content

Commit

Permalink
fix(sdk,tests): actually test managedbudget
Browse files Browse the repository at this point in the history
  • Loading branch information
sammccord committed Sep 25, 2024
1 parent 74e818c commit 5e7b307
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 19 deletions.
71 changes: 67 additions & 4 deletions packages/sdk/src/Budgets/ManagedBudget.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -37,21 +38,83 @@ 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,
);
expect(await budget.isAuthorized(zeroAddress)).toBe(false);
});

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);
});

Expand Down
32 changes: 17 additions & 15 deletions packages/sdk/src/Budgets/ManagedBudget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export interface ManagedBudgetPayload {
/**
* List of roles to assign to the corresponding account by index.
*
* @type {bigint[]}
* @type {ManagedBudgetRoles[]}
*/
roles: ManagedBudgetRoles[];
}
Expand Down Expand Up @@ -447,13 +447,13 @@ export class ManagedBudget extends DeployableTarget<
* @public
* @async
* @param {Address[]} addresses
* @param {bigint[]} roles
* @param {ManagedBudgetRoles[]} roles
* @param {?WriteParams<typeof managedBudgetAbi, 'grantRoles'>} [params]
* @returns {unknown}
*/
public async grantRoles(
addresses: Address[],
roles: bigint[],
roles: ManagedBudgetRoles[],
params?: WriteParams<typeof managedBudgetAbi, 'grantRoles'>,
) {
return await this.awaitResult(this.grantRolesRaw(addresses, roles, params));
Expand All @@ -469,13 +469,13 @@ export class ManagedBudget extends DeployableTarget<
* @public
* @async
* @param {Address[]} addresses
* @param {bigint[]} roles
* @param {ManagedBudgetRoles[]} roles
* @param {?WriteParams<typeof managedBudgetAbi, 'grantRoles'>} [params]
* @returns {unknown}
*/
public async grantRolesRaw(
addresses: Address[],
roles: bigint[],
roles: ManagedBudgetRoles[],
params?: WriteParams<typeof managedBudgetAbi, 'grantRoles'>,
) {
const { request, result } = await simulateManagedBudgetGrantRoles(
Expand Down Expand Up @@ -506,13 +506,13 @@ export class ManagedBudget extends DeployableTarget<
* @public
* @async
* @param {Address[]} addresses
* @param {bigint[]} roles
* @param {ManagedBudgetRoles[]} roles
* @param {?WriteParams<typeof managedBudgetAbi, 'revokeRoles'>} [params]
* @returns {unknown}
*/
public async revokeRoles(
addresses: Address[],
roles: bigint[],
roles: ManagedBudgetRoles[],
params?: WriteParams<typeof managedBudgetAbi, 'revokeRoles'>,
) {
return await this.awaitResult(
Expand All @@ -529,13 +529,13 @@ export class ManagedBudget extends DeployableTarget<
* @public
* @async
* @param {Address[]} addresses
* @param {bigint[]} roles
* @param {ManagedBudgetRoles[]} roles
* @param {?WriteParams<typeof managedBudgetAbi, 'revokeRoles'>} [params]
* @returns {unknown}
*/
public async revokeRolesRaw(
addresses: Address[],
roles: bigint[],
roles: ManagedBudgetRoles[],
params?: WriteParams<typeof managedBudgetAbi, 'revokeRoles'>,
) {
const { request, result } = await simulateManagedBudgetRevokeRoles(
Expand Down Expand Up @@ -564,7 +564,7 @@ export class ManagedBudget extends DeployableTarget<
* @public
* @param {Address} account
* @param {?ReadParams<typeof managedBudgetAbi, 'rolesOf'>} [params]
* @returns {Promise<Array<bigint>>}
* @returns {Promise<Array<ManagedBudgetRoles>>}
*/
public async rolesOf(
account: Address,
Expand All @@ -582,7 +582,9 @@ export class ManagedBudget extends DeployableTarget<
ManagedBudgetRoles.MANAGER,
ManagedBudgetRoles.ADMIN,
] as unknown as Array<bigint>
).filter((role) => (roles & role) === role);
).filter(
(role) => (roles & role) === role,
) as unknown as ManagedBudgetRoles[];
}

/**
Expand All @@ -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<typeof managedBudgetAbi, 'hasAnyRole'>} [params]
* @returns {Promise<boolean>}
*/
public hasAnyRole(
account: Address,
roles: bigint,
roles: ManagedBudgetRoles,
params?: ReadParams<typeof managedBudgetAbi, 'hasAnyRole'>,
) {
return readManagedBudgetHasAnyRole(this._config, {
Expand All @@ -620,13 +622,13 @@ export class ManagedBudget extends DeployableTarget<
*
* @public
* @param {Address} account
* @param {bigint} roles
* @param {ManagedBudgetRoles} roles
* @param {?ReadParams<typeof managedBudgetAbi, 'hasAllRoles'>} [params]
* @returns {*}
*/
public hasAllRoles(
account: Address,
roles: bigint,
roles: ManagedBudgetRoles,
params?: ReadParams<typeof managedBudgetAbi, 'hasAllRoles'>,
) {
return readManagedBudgetHasAllRoles(this._config, {
Expand Down

0 comments on commit 5e7b307

Please sign in to comment.