Skip to content

Commit

Permalink
chore(test): add hardhat for fixture helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
sammccord committed Jul 11, 2024
1 parent d821b08 commit 10b19a6
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 13 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"@changesets/cli": "^2.27.7",
"@commitlint/cli": "^19.3.0",
"@commitlint/config-conventional": "^19.2.2",
"@nomicfoundation/hardhat-network-helpers": "^1.0.11",
"@nomicfoundation/hardhat-toolbox-viem": "^3.0.0",
"@vitest/coverage-v8": "^1.6.0",
"@wagmi/connectors": "^5.0.19",
Expand Down
73 changes: 71 additions & 2 deletions packages/sdk/src/BoostCore.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
readErc20IncentiveOwner,
readSimpleBudgetIsAuthorized,
} from '@boostxyz/evm';
import { loadFixture } from '@nomicfoundation/hardhat-toolbox-viem/network-helpers';
import { parseEther } from 'viem';
import { beforeAll, describe, expect, test } from 'vitest';
import {
Expand Down Expand Up @@ -132,7 +133,75 @@ describe('BoostCore', () => {
expect(boost.incentives.at(0) instanceof ERC20Incentive).toBe(true);
});

// test('can reuse an existing action', async () => {
test('can reuse an existing action', async () => {
const { core, bases } = fixtures;
const client = new BoostCore({
...defaultOptions,
address: core.assertValidAddress(),
});

// to whom it may concern, this syntax is only used because we need to use test classes
// that are preconfigured with the dynamic base addresses generated at test time.
// normally you would use the follow api for brevity
// budget: client.SimpleBudget({} | '0xaddress')
const { budget, erc20 } = await fundBudget(defaultOptions, fixtures);

// });
await erc20.mint(defaultOptions.account.address, parseEther('1000'));
await erc20.approve(defaultOptions.account.address, parseEther('1000'));
await budget.allocate({
amount: parseEther('1000'),
asset: erc20.assertValidAddress(),
target: defaultOptions.account.address,
});

const _boost = await client.createBoost({
budget: budget,
action: new bases.ContractAction(defaultOptions, {
chainId: BigInt(31_337),
target: core.assertValidAddress(),
selector: '0xdeadbeef',
value: 0n,
}),
validator: new bases.SignerValidator(defaultOptions, {
signers: [defaultOptions.account.address],
}),
allowList: new bases.SimpleAllowList(defaultOptions, {
owner: defaultOptions.account.address,
allowed: [defaultOptions.account.address],
}),
incentives: [
new bases.ERC20Incentive(defaultOptions, {
asset: erc20.address!,
reward: parseEther('1'),
limit: 100n,
strategy: StrategyType.POOL,
}),
],
});
const boost = await client.createBoost({
budget: budget,
action: new bases.ContractAction(
defaultOptions,
_boost.action.assertValidAddress(),
false,
),
validator: new bases.SignerValidator(defaultOptions, {
signers: [defaultOptions.account.address],
}),
allowList: new bases.SimpleAllowList(defaultOptions, {
owner: defaultOptions.account.address,
allowed: [defaultOptions.account.address],
}),
incentives: [
new bases.ERC20Incentive(defaultOptions, {
asset: erc20.address!,
reward: parseEther('1'),
limit: 100n,
strategy: StrategyType.POOL,
}),
],
});
const onChainBoost = await client.readBoost(boost.id);
expect(onChainBoost.action).toBe(_boost.action.assertValidAddress());
});
});
26 changes: 26 additions & 0 deletions packages/sdk/test/MockERC20.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import {
mockErc20Abi,
simulateMockErc20Approve,
simulateMockErc20Mint,
simulateMockErc20MintPayable,
writeMockErc20Approve,
writeMockErc20Mint,
writeMockErc20MintPayable,
} from '@boostxyz/evm';
Expand All @@ -15,6 +17,30 @@ import {
import type { WriteParams } from './../src/utils';

export class MockERC20 extends Deployable {
public async approve(
address: Address,
value: bigint,
params?: WriteParams<typeof mockErc20Abi, 'approve'>,
) {
return this.awaitResult(this.approveRaw(address, value, params));
}

public async approveRaw(
address: Address,
value: bigint,
params?: WriteParams<typeof mockErc20Abi, 'approve'>,
) {
const { request, result } = await simulateMockErc20Approve(this._config, {
address: this.assertValidAddress(),
args: [address, value],
...this.optionallyAttachAccount(),
// biome-ignore lint/suspicious/noExplicitAny: Accept any shape of valid wagmi/viem parameters, wagmi does the same thing internally
...(params as any),
});
const hash = await writeMockErc20Approve(this._config, request);
return { hash, result };
}

public async mint(
address: Address,
value: bigint,
Expand Down
25 changes: 14 additions & 11 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 10b19a6

Please sign in to comment.