Skip to content
Open
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
72 changes: 72 additions & 0 deletions src/api/types/DexDtos.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,32 @@ describe("DexDtos", () => {
expect(dto.positionId).toBe("position-123");
});

it("should create valid BurnDto with recipient", async () => {
// Given
const dto = new BurnDto(
mockToken0,
mockToken1,
DexFeePercentageTypes.FEE_0_3_PERCENT,
new BigNumber("1000"),
-500,
500,
new BigNumber("10"),
new BigNumber("20"),
"position-123",
asValidUserAlias("client|user789")
);

// When
const validationErrors = await dto.validate();

// Then
if (validationErrors.length > 0) {
console.log("Validation errors:", validationErrors);
}
expect(validationErrors.length).toBe(0);
expect(dto.recipient).toEqual(asValidUserAlias("client|user789"));
});

it("should fail validation with negative amounts", async () => {
// Given
const dto = new BurnDto(
Expand Down Expand Up @@ -807,6 +833,28 @@ describe("DexDtos", () => {
expect(dto.amount0Requested).toEqual(new BigNumber("100"));
expect(dto.amount1Requested).toEqual(new BigNumber("200"));
});

it("should create valid CollectDto with recipient", async () => {
// Given
const dto = new CollectDto(
mockToken0,
mockToken1,
DexFeePercentageTypes.FEE_1_PERCENT,
new BigNumber("100"),
new BigNumber("200"),
-400,
400,
"position-789",
asValidUserAlias("client|user456")
);

// When
const validationErrors = await dto.validate();

// Then
expect(validationErrors.length).toBe(0);
expect(dto.recipient).toEqual(asValidUserAlias("client|user456"));
});
});

describe("AddLiquidityDTO", () => {
Expand All @@ -833,6 +881,30 @@ describe("DexDtos", () => {
expect(dto.amount0Desired).toEqual(new BigNumber("1000"));
expect(dto.amount1Desired).toEqual(new BigNumber("2000"));
});

it("should create valid AddLiquidityDTO with liquidityProvider", async () => {
// Given
const dto = new AddLiquidityDTO(
mockToken0,
mockToken1,
DexFeePercentageTypes.FEE_0_05_PERCENT,
-600,
600,
new BigNumber("1000"),
new BigNumber("2000"),
new BigNumber("900"),
new BigNumber("1800"),
"position-abc",
asValidUserAlias("client|user123")
);

// When
const validationErrors = await dto.validate();

// Then
expect(validationErrors.length).toBe(0);
expect(dto.liquidityProvider).toEqual(asValidUserAlias("client|user123"));
});
});

describe("CollectProtocolFeesDto", () => {
Expand Down
24 changes: 21 additions & 3 deletions src/api/types/DexDtos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,10 @@ export class BurnDto extends SubmitCallDTO {
@IsString()
public positionId?: string;

@IsOptional()
@IsUserAlias()
public recipient?: UserAlias;

constructor(
token0: TokenClassKey,
token1: TokenClassKey,
Expand All @@ -314,7 +318,8 @@ export class BurnDto extends SubmitCallDTO {
tickUpper: number,
amount0Min: BigNumber,
amount1Min: BigNumber,
positionId: string | undefined
positionId: string | undefined,
recipient?: UserAlias
) {
super();
this.tickLower = tickLower;
Expand All @@ -326,6 +331,7 @@ export class BurnDto extends SubmitCallDTO {
this.amount0Min = amount0Min;
this.amount1Min = amount1Min;
this.positionId = positionId;
this.recipient = recipient;
}
}

Expand Down Expand Up @@ -570,6 +576,10 @@ export class CollectDto extends SubmitCallDTO {
@IsString()
public positionId?: string;

@IsOptional()
@IsUserAlias()
public recipient?: UserAlias;

constructor(
token0: TokenClassKey,
token1: TokenClassKey,
Expand All @@ -578,7 +588,8 @@ export class CollectDto extends SubmitCallDTO {
amount1Requested: BigNumber,
tickLower: number,
tickUpper: number,
positionId: string | undefined
positionId: string | undefined,
recipient?: UserAlias
) {
super();
this.token0 = token0;
Expand All @@ -589,6 +600,7 @@ export class CollectDto extends SubmitCallDTO {
this.tickLower = tickLower;
this.tickUpper = tickUpper;
this.positionId = positionId;
this.recipient = recipient;
}
}

Expand Down Expand Up @@ -637,6 +649,10 @@ export class AddLiquidityDTO extends SubmitCallDTO {
@IsString()
public positionId?: string;

@IsOptional()
@IsUserAlias()
public liquidityProvider?: UserAlias;

constructor(
token0: TokenClassKey,
token1: TokenClassKey,
Expand All @@ -647,7 +663,8 @@ export class AddLiquidityDTO extends SubmitCallDTO {
amount1Desired: BigNumber,
amount0Min: BigNumber,
amount1Min: BigNumber,
positionId: string | undefined
positionId: string | undefined,
liquidityProvider?: UserAlias
) {
super();
this.token0 = token0;
Expand All @@ -660,6 +677,7 @@ export class AddLiquidityDTO extends SubmitCallDTO {
this.amount0Min = amount0Min;
this.amount1Min = amount1Min;
this.positionId = positionId;
this.liquidityProvider = liquidityProvider;
}
}

Expand Down
18 changes: 8 additions & 10 deletions src/chaincode/dex/addLiquidity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { TokenInstanceKey, UserAlias } from "@gala-chain/api";
import { TokenInstanceKey, UserAlias, asValidUserAlias } from "@gala-chain/api";
import {
GalaChainContext,
fetchOrCreateBalance,
Expand Down Expand Up @@ -63,7 +63,11 @@ export async function addLiquidity(
const token0InstanceKey = TokenInstanceKey.fungibleKey(pool.token0ClassKey);
const token1InstanceKey = TokenInstanceKey.fungibleKey(pool.token1ClassKey);

const liquidityProvider = launchpadAddress ?? ctx.callingUser;
// Determine the actual liquidity provider - this may be different from the caller if adding liquidity on behalf of another user
const liquidityProvider =
dto.liquidityProvider && dto.liquidityProvider !== ctx.callingUser
? asValidUserAlias(dto.liquidityProvider)
: (launchpadAddress ?? ctx.callingUser);
const tickLower = parseInt(dto.tickLower.toString()),
tickUpper = parseInt(dto.tickUpper.toString());

Expand Down Expand Up @@ -131,10 +135,7 @@ export async function addLiquidity(
tokenInstanceKey: token0InstanceKey,
quantity: roundedToken0Amount,
allowancesToUse: [],
authorizedOnBehalf: {
callingOnBehalf: liquidityProvider,
callingUser: liquidityProvider
}
authorizedOnBehalf: undefined
});

// transfer token1
Expand All @@ -144,10 +145,7 @@ export async function addLiquidity(
tokenInstanceKey: token1InstanceKey,
quantity: roundedToken1Amount,
allowancesToUse: [],
authorizedOnBehalf: {
callingOnBehalf: liquidityProvider,
callingUser: liquidityProvider
}
authorizedOnBehalf: undefined
});

await putChainObject(ctx, pool);
Expand Down
Loading
Loading