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

[ADHOC] chore(sdk): test action interface instantiation #88

Merged
merged 2 commits into from
Sep 16, 2024
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
2 changes: 1 addition & 1 deletion .nvmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
>=22
>=20
77 changes: 77 additions & 0 deletions packages/sdk/src/Actions/Action.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import { selectors } from '@boostxyz/signatures/events';
import { loadFixture } from '@nomicfoundation/hardhat-toolbox-viem/network-helpers';
import type { Hex } from 'viem';
import { beforeAll, beforeEach, describe, expect, test } from 'vitest';
import type { MockERC721 } from '../../test/MockERC721';
import { accounts } from '../../test/accounts';
import {
type Fixtures,
defaultOptions,
deployFixtures,
fundErc721,
} from '../../test/helpers';
import {
type EventActionPayloadSimple,
FilterType,
PrimitiveType,
SignatureType,
} from '../utils';
import { EventAction, actionFromAddress } from './Action';

let fixtures: Fixtures, erc721: MockERC721;

export function basicErc721TransferAction(
erc721: MockERC721,
): EventActionPayloadSimple {
return {
actionClaimant: {
signatureType: SignatureType.EVENT,
signature: selectors['Transfer(address,address,uint256)'] as Hex,
fieldIndex: 2,
targetContract: erc721.assertValidAddress(),
},
actionSteps: [
{
signature: selectors['Transfer(address,address,uint256)'] as Hex,
signatureType: SignatureType.EVENT,
actionType: 0,
targetContract: erc721.assertValidAddress(),
actionParameter: {
filterType: FilterType.EQUAL,
fieldType: PrimitiveType.ADDRESS,
fieldIndex: 2,
filterData: accounts.at(1)!.account,
},
},
],
};
}

export function cloneEventAction(fixtures: Fixtures, erc721: MockERC721) {
return function cloneEventAction() {
return fixtures.registry.clone(
crypto.randomUUID(),
new fixtures.bases.EventAction(
defaultOptions,
basicErc721TransferAction(erc721),
),
);
};
}

beforeAll(async () => {
fixtures = await loadFixture(deployFixtures);
});

describe('Action', () => {
beforeEach(async () => {
erc721 = await loadFixture(fundErc721(defaultOptions));
});

test('can automatically instantiate EventAction given an address', async () => {
const _action = await loadFixture(cloneEventAction(fixtures, erc721));
expect(
await actionFromAddress(defaultOptions, _action.assertValidAddress()),
).toBeInstanceOf(EventAction);
});
});
64 changes: 64 additions & 0 deletions packages/sdk/src/AllowLists/AllowList.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { loadFixture } from '@nomicfoundation/hardhat-network-helpers';
import { beforeAll, describe, expect, test } from 'vitest';
import {
type Fixtures,
defaultOptions,
deployFixtures,
} from '../../test/helpers';
import {
SimpleAllowList,
SimpleDenyList,
allowListFromAddress,
} from './AllowList';

let fixtures: Fixtures;

beforeAll(async () => {
fixtures = await loadFixture(deployFixtures);
});

function freshAllowList(fixtures: Fixtures) {
return function freshAllowList() {
return fixtures.registry.clone(
crypto.randomUUID(),
new fixtures.bases.SimpleAllowList(defaultOptions, {
owner: defaultOptions.account.address,
allowed: [defaultOptions.account.address],
}),
);
};
}

function freshDenyList(fixtures: Fixtures) {
return function freshDenyList() {
return fixtures.registry.clone(
crypto.randomUUID(),
new fixtures.bases.SimpleDenyList(defaultOptions, {
owner: defaultOptions.account.address,
denied: [defaultOptions.account.address],
}),
);
};
}

describe('AllowList', () => {
test('can automatically instantiate SimpleAllowList given an address', async () => {
const _allowList = await loadFixture(freshAllowList(fixtures));
expect(
await allowListFromAddress(
defaultOptions,
_allowList.assertValidAddress(),
),
).toBeInstanceOf(SimpleAllowList);
});

test('can automatically instantiate SimpleAllowList given an address', async () => {
const _allowList = await loadFixture(freshDenyList(fixtures));
expect(
await allowListFromAddress(
defaultOptions,
_allowList.assertValidAddress(),
),
).toBeInstanceOf(SimpleDenyList);
});
});
27 changes: 27 additions & 0 deletions packages/sdk/src/Budgets/Budget.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { loadFixture } from '@nomicfoundation/hardhat-network-helpers';
import { beforeAll, describe, expect, test } from 'vitest';
import {
type Fixtures,
defaultOptions,
deployFixtures,
freshManagedBudget,
} from '../../test/helpers';
import { budgetFromAddress } from './Budget';
import { ManagedBudget } from './ManagedBudget';

let fixtures: Fixtures;

beforeAll(async () => {
fixtures = await loadFixture(deployFixtures);
});

describe('Budget', () => {
test('can automatically instantiate ManagedBudget given an address', async () => {
const budget = await loadFixture(
freshManagedBudget(defaultOptions, fixtures),
);
expect(
await budgetFromAddress(defaultOptions, budget.assertValidAddress()),
).toBeInstanceOf(ManagedBudget);
});
});
92 changes: 92 additions & 0 deletions packages/sdk/src/Incentives/Incentive.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import { zeroAddress } from 'viem';
import { describe, expect, test } from 'vitest';
import { defaultOptions } from '../../test/helpers';
import { StrategyType } from '../utils';
import {
AllowListIncentive,
CGDAIncentive,
ERC20Incentive,
ERC20VariableIncentive,
incentiveFromAddress,
} from './Incentive';
import { PointsIncentive } from './PointsIncentive';

describe('Incentive', () => {
test('can automatically instantiate PointsIncentive given an address', async () => {
const incentive = new PointsIncentive(defaultOptions, {
venue: zeroAddress,
selector: '0xdeadb33f',
reward: 1n,
limit: 1n,
});
await incentive.deploy();
expect(
await incentiveFromAddress(
defaultOptions,
incentive.assertValidAddress(),
),
).toBeInstanceOf(PointsIncentive);
});

test('can automatically instantiate AllowListIncentive given an address', async () => {
const incentive = new AllowListIncentive(defaultOptions, {
allowList: zeroAddress,
limit: 3n,
});
await incentive.deploy();
expect(
await incentiveFromAddress(
defaultOptions,
incentive.assertValidAddress(),
),
).toBeInstanceOf(AllowListIncentive);
});

test('can automatically instantiate CGDAIncentive given an address', async () => {
const incentive = new CGDAIncentive(defaultOptions, {
asset: zeroAddress,
initialReward: 1n,
totalBudget: 10n,
rewardBoost: 1n,
rewardDecay: 1n,
});
await incentive.deploy();
expect(
await incentiveFromAddress(
defaultOptions,
incentive.assertValidAddress(),
),
).toBeInstanceOf(CGDAIncentive);
});

test('can automatically instantiate ERC20Incentive given an address', async () => {
const incentive = new ERC20Incentive(defaultOptions, {
asset: zeroAddress,
strategy: StrategyType.POOL,
reward: 1n,
limit: 10n,
});
await incentive.deploy();
expect(
await incentiveFromAddress(
defaultOptions,
incentive.assertValidAddress(),
),
).toBeInstanceOf(ERC20Incentive);
});

test('can automatically instantiate ERC20VariableIncentive given an address', async () => {
const incentive = new ERC20VariableIncentive(defaultOptions, {
asset: zeroAddress,
reward: 1n,
limit: 10n,
});
await incentive.deploy();
expect(
await incentiveFromAddress(
defaultOptions,
incentive.assertValidAddress(),
),
).toBeInstanceOf(ERC20VariableIncentive);
});
});
3 changes: 2 additions & 1 deletion packages/sdk/src/Incentives/Incentive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ export type Incentive =
export const IncentiveByComponentInterface = {
[APointsIncentive as Hex]: PointsIncentive,
[AERC20Incentive as Hex]: ERC20Incentive,
[AAllowListIncentive as Hex]: AllowListIncentive,
// TODO: figure out why evm generates bad bytes4 here
['0x0a466e6f']: AllowListIncentive,
// [APointsIncentive as Hex]: ERC1155Incentive,
[ACGDAIncentive as Hex]: CGDAIncentive,
[AERC20VariableIncentive as Hex]: ERC20VariableIncentive,
Expand Down
21 changes: 21 additions & 0 deletions packages/sdk/src/Validators/Validator.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { describe, expect, test } from 'vitest';
import { defaultOptions } from '../../test/helpers';
import { testAccount } from '../../test/viem';
import { SignerValidator } from './SignerValidator';
import { validatorFromAddress } from './Validator';

describe('Validator', () => {
test('can automatically instantiate SignerValidator given an address', async () => {
const validator = new SignerValidator(defaultOptions, {
signers: [testAccount.address],
validatorCaller: testAccount.address,
});
await validator.deploy();
expect(
await validatorFromAddress(
defaultOptions,
validator.assertValidAddress(),
),
).toBeInstanceOf(SignerValidator);
});
});
2 changes: 1 addition & 1 deletion packages/sdk/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ export type GenericLog<
* @param {string} input
* @returns {Hex}
*/
export function bytes4(input: Hex) {
export function bytes4(input: string) {
return slice(isHex(input) ? keccak256(input) : keccak256(toHex(input)), 0, 4);
}

Expand Down
9 changes: 8 additions & 1 deletion turbo.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,14 @@
"tasks": {
"build": {
"dependsOn": ["^build"],
"outputs": ["dist/**", "artifacts/**", "out/**", "cache/**", ".next/**"]
"outputs": [
"dist/**",
"artifacts/**",
"out/**",
"cache/**",
".next/**",
"deploys/**"
]
},
"signatures#build": {
"inputs": ["manifests"],
Expand Down
Loading