Skip to content

Commit

Permalink
chore: add error checking to mint config
Browse files Browse the repository at this point in the history
  • Loading branch information
cjkoepke committed Feb 23, 2024
1 parent b71b352 commit 13f8631
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 0 deletions.
19 changes: 19 additions & 0 deletions packages/core/src/Configs/MintV3PoolConfig.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { TFee } from "../@types/queryprovider.js";
import { Config } from "../Abstracts/Config.abstract.class.js";

export class MintV3PoolConfig extends Config<IMintV3PoolConfigArgs> {
static MAX_FEE: bigint = 10_000n;

assetA?: AssetAmount<IAssetAmountMetadata>;
assetB?: AssetAmount<IAssetAmountMetadata>;
fees?: TFee;
Expand Down Expand Up @@ -83,5 +85,22 @@ export class MintV3PoolConfig extends Config<IMintV3PoolConfigArgs> {

validate(): void {
super.validate();

const [feeOpen = 0, feeClose = 0] = this.marketTimings || [];
if (feeClose < feeOpen) {
throw new Error(
"The second timestamp in the marketTimings tuple must be greater than the first."
);
}

const [feeStart = 0n, feeEnd = 0n] = this.fees || [];
if (
feeStart > MintV3PoolConfig.MAX_FEE ||
feeEnd > MintV3PoolConfig.MAX_FEE
) {
throw new Error(
`Fees cannot supersede the max fee of ${MintV3PoolConfig.MAX_FEE}.`
);
}
}
}
68 changes: 68 additions & 0 deletions packages/core/src/Configs/__tests__/MintV3PoolConfig.class.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { IMintV3PoolConfigArgs } from "../../@types/configs.js";
import { PREVIEW_DATA } from "../../exports/testing.js";
import { MintV3PoolConfig } from "../MintV3PoolConfig.class.js";

const defaultArgs: IMintV3PoolConfigArgs = {
assetA: PREVIEW_DATA.assets.tada,
assetB: PREVIEW_DATA.assets.tindy,
fees: [20n, 100n],
marketTimings: [0, 100],
ownerAddress: "addr_test",
};

let config: MintV3PoolConfig;
beforeEach(() => {
config = new MintV3PoolConfig();
});

describe("MintV3PoolConfig class", () => {
it("should construct with no parameters", () => {
expect(config).toBeInstanceOf(MintV3PoolConfig);
});

it("should construct with a config", () => {
const myConfig = new MintV3PoolConfig(defaultArgs);

expect(myConfig.buildArgs()).toMatchObject<
ReturnType<(typeof myConfig)["buildArgs"]>
>({
assetA: expect.objectContaining({
amount: PREVIEW_DATA.assets.tada.amount,
}),
assetB: expect.objectContaining({
amount: PREVIEW_DATA.assets.tindy.amount,
}),
fees: [20n, 100n],
marketTimings: [0n, 100n],
ownerAddress: "addr_test",
protocolFee: 2_000_000n,
});
});

it("should fail when marketTimings are not linearly increasing", () => {
const myConfig = new MintV3PoolConfig({
...defaultArgs,
marketTimings: [100, 0],
});

expect(() => myConfig.buildArgs()).toThrowError(
"The second timestamp in the marketTimings tuple must be greater than the first."
);
});

it("should fail when any of the fees surpass the max fee", () => {
expect(() =>
new MintV3PoolConfig({
...defaultArgs,
fees: [11_000n, 10n],
}).buildArgs()
).toThrowError("Fees cannot supersede the max fee of 10000.");

expect(() =>
new MintV3PoolConfig({
...defaultArgs,
fees: [10n, 10_200n],
}).buildArgs()
).toThrowError("Fees cannot supersede the max fee of 10000.");
});
});

0 comments on commit 13f8631

Please sign in to comment.