Skip to content

Commit

Permalink
Merge pull request #51 from SundaeSwap-finance/feat/split-fees
Browse files Browse the repository at this point in the history
Feat: Split Fees
  • Loading branch information
cjkoepke authored Jul 2, 2024
2 parents 30f4688 + 4546120 commit ae3ac34
Show file tree
Hide file tree
Showing 10 changed files with 148 additions and 61 deletions.
26 changes: 13 additions & 13 deletions docs/typescript/core/classes/Lucid.DatumBuilderLucidV3.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ The current network id.

#### Defined in

[packages/core/src/DatumBuilders/DatumBuilder.Lucid.V3.class.ts:91](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/DatumBuilders/DatumBuilder.Lucid.V3.class.ts#L91)
[packages/core/src/DatumBuilders/DatumBuilder.Lucid.V3.class.ts:92](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/DatumBuilders/DatumBuilder.Lucid.V3.class.ts#L92)

___

Expand All @@ -32,7 +32,7 @@ The error to throw when the pool ident does not match V1 constraints.

#### Defined in

[packages/core/src/DatumBuilders/DatumBuilder.Lucid.V3.class.ts:93](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/DatumBuilders/DatumBuilder.Lucid.V3.class.ts#L93)
[packages/core/src/DatumBuilders/DatumBuilder.Lucid.V3.class.ts:94](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/DatumBuilders/DatumBuilder.Lucid.V3.class.ts#L94)

## Methods

Expand Down Expand Up @@ -63,7 +63,7 @@ An object comprising the hash of the inline datum, the inline datum itself,

#### Defined in

[packages/core/src/DatumBuilders/DatumBuilder.Lucid.V3.class.ts:158](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/DatumBuilders/DatumBuilder.Lucid.V3.class.ts#L158)
[packages/core/src/DatumBuilders/DatumBuilder.Lucid.V3.class.ts:159](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/DatumBuilders/DatumBuilder.Lucid.V3.class.ts#L159)

___

Expand All @@ -90,7 +90,7 @@ An object containing the hash of the inline datum, the inline datum itself,

#### Defined in

[packages/core/src/DatumBuilders/DatumBuilder.Lucid.V3.class.ts:249](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/DatumBuilders/DatumBuilder.Lucid.V3.class.ts#L249)
[packages/core/src/DatumBuilders/DatumBuilder.Lucid.V3.class.ts:250](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/DatumBuilders/DatumBuilder.Lucid.V3.class.ts#L250)

___

Expand Down Expand Up @@ -118,7 +118,7 @@ An object containing the hash of the inline datum, the inline datum itself,

#### Defined in

[packages/core/src/DatumBuilders/DatumBuilder.Lucid.V3.class.ts:297](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/DatumBuilders/DatumBuilder.Lucid.V3.class.ts#L297)
[packages/core/src/DatumBuilders/DatumBuilder.Lucid.V3.class.ts:298](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/DatumBuilders/DatumBuilder.Lucid.V3.class.ts#L298)

___

Expand Down Expand Up @@ -148,7 +148,7 @@ An object containing the hash of the inline datum, the inline datum itself,

#### Defined in

[packages/core/src/DatumBuilders/DatumBuilder.Lucid.V3.class.ts:114](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/DatumBuilders/DatumBuilder.Lucid.V3.class.ts#L114)
[packages/core/src/DatumBuilders/DatumBuilder.Lucid.V3.class.ts:115](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/DatumBuilders/DatumBuilder.Lucid.V3.class.ts#L115)

___

Expand Down Expand Up @@ -179,7 +179,7 @@ An object containing the hash of the inline datum, the inline datum itself,

#### Defined in

[packages/core/src/DatumBuilders/DatumBuilder.Lucid.V3.class.ts:203](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/DatumBuilders/DatumBuilder.Lucid.V3.class.ts#L203)
[packages/core/src/DatumBuilders/DatumBuilder.Lucid.V3.class.ts:204](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/DatumBuilders/DatumBuilder.Lucid.V3.class.ts#L204)

___

Expand All @@ -201,7 +201,7 @@ Computes the pool ID based on the provided UTxO being spent.

#### Defined in

[packages/core/src/DatumBuilders/DatumBuilder.Lucid.V3.class.ts:520](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/DatumBuilders/DatumBuilder.Lucid.V3.class.ts#L520)
[packages/core/src/DatumBuilders/DatumBuilder.Lucid.V3.class.ts:521](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/DatumBuilders/DatumBuilder.Lucid.V3.class.ts#L521)

___

Expand All @@ -223,7 +223,7 @@ Computes the pool liquidity name.

#### Defined in

[packages/core/src/DatumBuilders/DatumBuilder.Lucid.V3.class.ts:496](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/DatumBuilders/DatumBuilder.Lucid.V3.class.ts#L496)
[packages/core/src/DatumBuilders/DatumBuilder.Lucid.V3.class.ts:497](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/DatumBuilders/DatumBuilder.Lucid.V3.class.ts#L497)

___

Expand All @@ -245,7 +245,7 @@ Computes the pool NFT name.

#### Defined in

[packages/core/src/DatumBuilders/DatumBuilder.Lucid.V3.class.ts:484](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/DatumBuilders/DatumBuilder.Lucid.V3.class.ts#L484)
[packages/core/src/DatumBuilders/DatumBuilder.Lucid.V3.class.ts:485](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/DatumBuilders/DatumBuilder.Lucid.V3.class.ts#L485)

___

Expand All @@ -267,7 +267,7 @@ Computes the pool reference name.

#### Defined in

[packages/core/src/DatumBuilders/DatumBuilder.Lucid.V3.class.ts:508](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/DatumBuilders/DatumBuilder.Lucid.V3.class.ts#L508)
[packages/core/src/DatumBuilders/DatumBuilder.Lucid.V3.class.ts:509](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/DatumBuilders/DatumBuilder.Lucid.V3.class.ts#L509)

___

Expand Down Expand Up @@ -302,7 +302,7 @@ An object containing the staking and

#### Defined in

[packages/core/src/DatumBuilders/DatumBuilder.Lucid.V3.class.ts:551](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/DatumBuilders/DatumBuilder.Lucid.V3.class.ts#L551)
[packages/core/src/DatumBuilders/DatumBuilder.Lucid.V3.class.ts:552](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/DatumBuilders/DatumBuilder.Lucid.V3.class.ts#L552)

___

Expand Down Expand Up @@ -331,4 +331,4 @@ The signing key associated with the owner, extracted from the datum. This key is

#### Defined in

[packages/core/src/DatumBuilders/DatumBuilder.Lucid.V3.class.ts:590](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/DatumBuilders/DatumBuilder.Lucid.V3.class.ts#L590)
[packages/core/src/DatumBuilders/DatumBuilder.Lucid.V3.class.ts:591](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/DatumBuilders/DatumBuilder.Lucid.V3.class.ts#L591)
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ to the SundaeSwap Treasury wallet.

#### Defined in

[packages/core/src/@types/configs.ts:116](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/@types/configs.ts#L116)
[packages/core/src/@types/configs.ts:121](https://github.com/SundaeSwap-finance/sundae-sdk/blob/main/packages/core/src/@types/configs.ts#L121)
7 changes: 6 additions & 1 deletion packages/core/src/@types/configs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,14 +98,19 @@ export interface IWithdrawConfigArgs extends IOrderConfigArgs {
suppliedLPAsset: AssetAmount<IAssetAmountMetadata>;
}

export interface IFeesConfig {
ask: bigint;
bid: bigint;
}

/**
* Interface describing the method arguments for creating a pool
* in the V3 Pool Contract.
*/
export interface IMintV3PoolConfigArgs extends IBaseConfig {
assetA: AssetAmount<IAssetAmountMetadata>;
assetB: AssetAmount<IAssetAmountMetadata>;
fee: bigint;
fees: bigint | IFeesConfig;
ownerAddress: string;
// Defaults to immediately.
marketOpen?: bigint;
Expand Down
54 changes: 39 additions & 15 deletions packages/core/src/Configs/MintV3PoolConfig.class.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { AssetAmount, IAssetAmountMetadata } from "@sundaeswap/asset";
import { IMintV3PoolConfigArgs } from "../@types/index.js";
import { IFeesConfig, IMintV3PoolConfigArgs } from "../@types/index.js";

import { Config } from "../Abstracts/Config.abstract.class.js";

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

assetA?: AssetAmount<IAssetAmountMetadata>;
assetB?: AssetAmount<IAssetAmountMetadata>;
fee?: bigint;
fees?: IFeesConfig;
marketTimings?: bigint;
donateToTreasury?: bigint;
ownerAddress?: string;
Expand All @@ -21,7 +21,7 @@ export class MintV3PoolConfig extends Config<IMintV3PoolConfigArgs> {
setFromObject({
assetA,
assetB,
fee,
fees,
marketOpen,
ownerAddress,
referralFee,
Expand All @@ -30,18 +30,18 @@ export class MintV3PoolConfig extends Config<IMintV3PoolConfigArgs> {
referralFee && this.setReferralFee(referralFee);
this.setAssetA(assetA);
this.setAssetB(assetB);
this.setFee(fee);
this.setFees(fees);
this.setMarketOpen(marketOpen || 0n);
this.setOwnerAddress(ownerAddress);
this.setDonateToTreasury(donateToTreasury);
}

buildArgs(): IMintV3PoolConfigArgs {
buildArgs(): Omit<IMintV3PoolConfigArgs, "fees"> & { fees: IFeesConfig } {
this.validate();
return {
assetA: this.assetA as AssetAmount<IAssetAmountMetadata>,
assetB: this.assetB as AssetAmount<IAssetAmountMetadata>,
fee: this.fee as bigint,
fees: this.fees as IFeesConfig,
marketOpen: this.marketTimings as bigint,
ownerAddress: this.ownerAddress as string,
referralFee: this.referralFee,
Expand All @@ -64,8 +64,15 @@ export class MintV3PoolConfig extends Config<IMintV3PoolConfigArgs> {
return this;
}

setFee(fee: bigint) {
this.fee = fee;
setFees(fees: bigint | IFeesConfig) {
this.fees =
typeof fees === "bigint"
? {
ask: fees,
bid: fees,
}
: fees;

return this;
}

Expand All @@ -82,14 +89,31 @@ export class MintV3PoolConfig extends Config<IMintV3PoolConfigArgs> {
validate(): void {
super.validate();

if (!this.fee) {
throw new Error(`No fee was set, but is required.`);
if (!this.fees) {
throw new Error(`No fees were set, but are required.`);
}

if (this.fee > MintV3PoolConfig.MAX_FEE) {
throw new Error(
`Fees cannot supersede the max fee of ${MintV3PoolConfig.MAX_FEE}.`
);
if (this.fees.ask === this.fees.bid) {
if (
this.fees.ask > MintV3PoolConfig.MAX_FEE ||
this.fees.bid > MintV3PoolConfig.MAX_FEE
) {
throw new Error(
`Fees cannot supersede the max fee of ${MintV3PoolConfig.MAX_FEE}.`
);
}
} else {
if (this.fees.ask > MintV3PoolConfig.MAX_FEE) {
throw new Error(
`Ask fee cannot supersede the max fee of ${MintV3PoolConfig.MAX_FEE}.`
);
}

if (this.fees.bid > MintV3PoolConfig.MAX_FEE) {
throw new Error(
`Bid fee cannot supersede the max fee of ${MintV3PoolConfig.MAX_FEE}.`
);
}
}

if (
Expand Down
47 changes: 41 additions & 6 deletions packages/core/src/Configs/__tests__/MintV3PoolConfig.class.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { MintV3PoolConfig } from "../MintV3PoolConfig.class.js";
const defaultArgs: IMintV3PoolConfigArgs = {
assetA: PREVIEW_DATA.assets.tada,
assetB: PREVIEW_DATA.assets.tindy,
fee: 20n,
fees: 20n,
marketOpen: 0n,
ownerAddress: "addr_test",
};
Expand All @@ -32,7 +32,10 @@ describe("MintV3PoolConfig class", () => {
assetB: expect.objectContaining({
amount: PREVIEW_DATA.assets.tindy.amount,
}),
fee: 20n,
fees: {
bid: 20n,
ask: 20n,
},
marketOpen: 0n,
ownerAddress: "addr_test",
});
Expand All @@ -42,15 +45,47 @@ describe("MintV3PoolConfig class", () => {
expect(() =>
new MintV3PoolConfig({
...defaultArgs,
fee: 11_000n,
fees: 11_000n,
}).buildArgs()
).toThrowError("Fees cannot supersede the max fee of 10000.");
).toThrowError(
`Fees cannot supersede the max fee of ${MintV3PoolConfig.MAX_FEE}.`
);

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

it("should fail when an ask fee surpasses the max fee", () => {
expect(() =>
new MintV3PoolConfig({
...defaultArgs,
fees: {
bid: 10n,
ask: 11_000n,
},
}).buildArgs()
).toThrowError(
`Ask fee cannot supersede the max fee of ${MintV3PoolConfig.MAX_FEE}.`
);
});

it("should fail when a take fee surpasses the max fee", () => {
expect(() =>
new MintV3PoolConfig({
...defaultArgs,
fees: {
bid: 11_000n,
ask: 10n,
},
}).buildArgs()
).toThrowError(
`Take fee cannot supersede the max fee of ${MintV3PoolConfig.MAX_FEE}.`
);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { C, Constr, Credential, Data, Lucid, UTxO } from "lucid-cardano";

import {
EDatumType,
IFeesConfig,
TDatumResult,
TDestinationAddress,
TSupportedNetworks,
Expand Down Expand Up @@ -64,7 +65,7 @@ export interface IDatumBuilderMintPoolV3Args {
seedUtxo: UTxO;
assetA: AssetAmount<IAssetAmountMetadata>;
assetB: AssetAmount<IAssetAmountMetadata>;
fee: bigint;
fees: IFeesConfig;
depositFee: bigint;
marketOpen?: bigint;
}
Expand Down Expand Up @@ -249,7 +250,7 @@ export class DatumBuilderLucidV3 implements DatumBuilder {
public buildMintPoolDatum({
assetA,
assetB,
fee,
fees,
marketOpen,
depositFee,
seedUtxo,
Expand All @@ -265,8 +266,8 @@ export class DatumBuilderLucidV3 implements DatumBuilder {
const newPoolDatum: V3Types.TPoolDatum = {
assets: assetsPair,
circulatingLp: liquidity,
bidFeePer10Thousand: fee,
askFeePer10Thousand: fee,
bidFeePer10Thousand: fees.bid,
askFeePer10Thousand: fees.ask,
feeManager: null,
identifier: ident,
marketOpen: marketOpen || 0n,
Expand Down
Loading

0 comments on commit ae3ac34

Please sign in to comment.