@gala-chain/launchpad 1.0.1 → 1.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (88) hide show
  1. package/lib/package.json +1 -1
  2. package/lib/src/api/types/LaunchpadBatchSubmitAuthorities.d.ts +12 -0
  3. package/lib/src/api/types/LaunchpadBatchSubmitAuthorities.d.ts.map +1 -0
  4. package/lib/src/api/types/LaunchpadBatchSubmitAuthorities.js +56 -0
  5. package/lib/src/api/types/LaunchpadBatchSubmitAuthorities.js.map +1 -0
  6. package/lib/src/api/types/LaunchpadDtos.d.ts +14 -0
  7. package/lib/src/api/types/LaunchpadDtos.d.ts.map +1 -1
  8. package/lib/src/api/types/LaunchpadDtos.js +45 -1
  9. package/lib/src/api/types/LaunchpadDtos.js.map +1 -1
  10. package/lib/src/api/types/LaunchpadFeeConfig.d.ts +3 -2
  11. package/lib/src/api/types/LaunchpadFeeConfig.d.ts.map +1 -1
  12. package/lib/src/api/types/LaunchpadFeeConfig.js +14 -4
  13. package/lib/src/api/types/LaunchpadFeeConfig.js.map +1 -1
  14. package/lib/src/api/types/index.d.ts +1 -0
  15. package/lib/src/api/types/index.d.ts.map +1 -1
  16. package/lib/src/api/types/index.js +1 -0
  17. package/lib/src/api/types/index.js.map +1 -1
  18. package/lib/src/chaincode/LaunchpadContract.d.ts +6 -1
  19. package/lib/src/chaincode/LaunchpadContract.d.ts.map +1 -1
  20. package/lib/src/chaincode/LaunchpadContract.js +85 -0
  21. package/lib/src/chaincode/LaunchpadContract.js.map +1 -1
  22. package/lib/src/chaincode/launchpad/buyExactToken.d.ts.map +1 -1
  23. package/lib/src/chaincode/launchpad/buyExactToken.js +15 -0
  24. package/lib/src/chaincode/launchpad/buyExactToken.js.map +1 -1
  25. package/lib/src/chaincode/launchpad/buyWithNative.d.ts.map +1 -1
  26. package/lib/src/chaincode/launchpad/buyWithNative.js +24 -0
  27. package/lib/src/chaincode/launchpad/buyWithNative.js.map +1 -1
  28. package/lib/src/chaincode/launchpad/callMemeTokenIn.d.ts +1 -0
  29. package/lib/src/chaincode/launchpad/callMemeTokenIn.d.ts.map +1 -1
  30. package/lib/src/chaincode/launchpad/callMemeTokenIn.js +3 -1
  31. package/lib/src/chaincode/launchpad/callMemeTokenIn.js.map +1 -1
  32. package/lib/src/chaincode/launchpad/callMemeTokenOut.d.ts +1 -0
  33. package/lib/src/chaincode/launchpad/callMemeTokenOut.d.ts.map +1 -1
  34. package/lib/src/chaincode/launchpad/callMemeTokenOut.js +4 -1
  35. package/lib/src/chaincode/launchpad/callMemeTokenOut.js.map +1 -1
  36. package/lib/src/chaincode/launchpad/callNativeTokenIn.d.ts +1 -0
  37. package/lib/src/chaincode/launchpad/callNativeTokenIn.d.ts.map +1 -1
  38. package/lib/src/chaincode/launchpad/callNativeTokenIn.js +6 -2
  39. package/lib/src/chaincode/launchpad/callNativeTokenIn.js.map +1 -1
  40. package/lib/src/chaincode/launchpad/callNativeTokenOut.d.ts +1 -0
  41. package/lib/src/chaincode/launchpad/callNativeTokenOut.d.ts.map +1 -1
  42. package/lib/src/chaincode/launchpad/callNativeTokenOut.js +3 -1
  43. package/lib/src/chaincode/launchpad/callNativeTokenOut.js.map +1 -1
  44. package/lib/src/chaincode/launchpad/configureLaunchpadFeeConfig.d.ts +12 -0
  45. package/lib/src/chaincode/launchpad/configureLaunchpadFeeConfig.d.ts.map +1 -1
  46. package/lib/src/chaincode/launchpad/configureLaunchpadFeeConfig.js +26 -6
  47. package/lib/src/chaincode/launchpad/configureLaunchpadFeeConfig.js.map +1 -1
  48. package/lib/src/chaincode/launchpad/fees.d.ts +1 -0
  49. package/lib/src/chaincode/launchpad/fees.d.ts.map +1 -1
  50. package/lib/src/chaincode/launchpad/fees.js +6 -2
  51. package/lib/src/chaincode/launchpad/fees.js.map +1 -1
  52. package/lib/src/chaincode/launchpad/finaliseSale.js +3 -3
  53. package/lib/src/chaincode/launchpad/finaliseSale.js.map +1 -1
  54. package/lib/src/chaincode/launchpad/index.d.ts +1 -0
  55. package/lib/src/chaincode/launchpad/index.d.ts.map +1 -1
  56. package/lib/src/chaincode/launchpad/index.js +1 -0
  57. package/lib/src/chaincode/launchpad/index.js.map +1 -1
  58. package/lib/src/chaincode/launchpad/launchpadBatchSubmitAuthorizations.d.ts +24 -0
  59. package/lib/src/chaincode/launchpad/launchpadBatchSubmitAuthorizations.d.ts.map +1 -0
  60. package/lib/src/chaincode/launchpad/launchpadBatchSubmitAuthorizations.js +84 -0
  61. package/lib/src/chaincode/launchpad/launchpadBatchSubmitAuthorizations.js.map +1 -0
  62. package/lib/src/chaincode/launchpad/sellExactToken.d.ts.map +1 -1
  63. package/lib/src/chaincode/launchpad/sellExactToken.js +27 -8
  64. package/lib/src/chaincode/launchpad/sellExactToken.js.map +1 -1
  65. package/lib/src/chaincode/launchpad/sellWithNative.d.ts.map +1 -1
  66. package/lib/src/chaincode/launchpad/sellWithNative.js +26 -2
  67. package/lib/src/chaincode/launchpad/sellWithNative.js.map +1 -1
  68. package/lib/tsconfig.tsbuildinfo +1 -1
  69. package/package.json +2 -2
  70. package/src/api/types/LaunchpadBatchSubmitAuthorities.ts +53 -0
  71. package/src/api/types/LaunchpadDtos.ts +38 -0
  72. package/src/api/types/LaunchpadFeeConfig.ts +13 -4
  73. package/src/api/types/index.ts +1 -0
  74. package/src/chaincode/LaunchpadContract.ts +96 -1
  75. package/src/chaincode/launchpad/buyExactToken.ts +17 -1
  76. package/src/chaincode/launchpad/buyWithNative.ts +28 -1
  77. package/src/chaincode/launchpad/callMemeTokenIn.ts +8 -3
  78. package/src/chaincode/launchpad/callMemeTokenOut.ts +8 -2
  79. package/src/chaincode/launchpad/callNativeTokenIn.ts +10 -3
  80. package/src/chaincode/launchpad/callNativeTokenOut.ts +8 -3
  81. package/src/chaincode/launchpad/configureLaunchpadFeeConfig.ts +27 -4
  82. package/src/chaincode/launchpad/fees.ts +5 -1
  83. package/src/chaincode/launchpad/finaliseSale.ts +3 -3
  84. package/src/chaincode/launchpad/index.ts +1 -0
  85. package/src/chaincode/launchpad/launchpadBatchSubmitAuthorizations.spec.ts +185 -0
  86. package/src/chaincode/launchpad/launchpadBatchSubmitAuthorizations.ts +105 -0
  87. package/src/chaincode/launchpad/sellExactToken.ts +30 -11
  88. package/src/chaincode/launchpad/sellWithNative.ts +30 -2
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gala-chain/launchpad",
3
- "version": "1.0.1",
3
+ "version": "1.0.2",
4
4
  "description": "GalaChain Launchpad Chaincode",
5
5
  "main": "lib/src/index.js",
6
6
  "types": "lib/src/index.d.ts",
@@ -51,4 +51,4 @@
51
51
  "ts-node": "^10.9.1",
52
52
  "typescript": "^5.3.3"
53
53
  }
54
- }
54
+ }
@@ -0,0 +1,53 @@
1
+ /*
2
+ * Copyright (c) Gala Games Inc. All rights reserved.
3
+ * Licensed under the Apache License, Version 2.0 (the "License");
4
+ * you may not use this file except in compliance with the License.
5
+ * You may obtain a copy of the License at
6
+ *
7
+ * http://www.apache.org/licenses/LICENSE-2.0
8
+ *
9
+ * Unless required by applicable law or agreed to in writing, software
10
+ * distributed under the License is distributed on an "AS IS" BASIS,
11
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ * See the License for the specific language governing permissions and
13
+ * limitations under the License.
14
+ */
15
+ import { ChainObject } from "@gala-chain/api";
16
+ import { Exclude } from "class-transformer";
17
+ import { ArrayNotEmpty, IsString } from "class-validator";
18
+
19
+ export class LaunchpadBatchSubmitAuthorities extends ChainObject {
20
+ @Exclude()
21
+ public static INDEX_KEY = "GCLBSA"; // GalaChain Launchpad Batch Submit Authorities
22
+
23
+ @ArrayNotEmpty()
24
+ @IsString({ each: true })
25
+ authorities: string[];
26
+
27
+ constructor(authorities: string[]) {
28
+ super();
29
+ this.authorities = authorities;
30
+ }
31
+
32
+ public addOrUpdateAuthorities(newAuthorities: string[]) {
33
+ this.authorities = newAuthorities;
34
+ }
35
+
36
+ public addAuthority(authority: string) {
37
+ if (!this.authorities.includes(authority)) {
38
+ this.authorities.push(authority);
39
+ }
40
+ }
41
+
42
+ public removeAuthority(authority: string) {
43
+ this.authorities = this.authorities.filter((auth) => auth !== authority);
44
+ }
45
+
46
+ public isAuthorized(authority: string): boolean {
47
+ return this.authorities.includes(authority);
48
+ }
49
+
50
+ public getAuthorities(): string[] {
51
+ return [...this.authorities];
52
+ }
53
+ }
@@ -24,6 +24,8 @@ import {
24
24
  import BigNumber from "bignumber.js";
25
25
  import { Type } from "class-transformer";
26
26
  import {
27
+ ArrayNotEmpty,
28
+ IsArray,
27
29
  IsBoolean,
28
30
  IsNotEmpty,
29
31
  IsNumber,
@@ -275,6 +277,9 @@ export class TradeResDto {
275
277
  @IsNotEmpty()
276
278
  public inputQuantity: string;
277
279
 
280
+ @IsNotEmpty()
281
+ public totalFees: string;
282
+
278
283
  @IsNotEmpty()
279
284
  public outputQuantity: string;
280
285
 
@@ -328,6 +333,12 @@ export class ConfigureLaunchpadFeeAddressDto extends SubmitCallDTO {
328
333
  @IsUserAlias()
329
334
  public newPlatformFeeAddress?: UserAlias;
330
335
 
336
+ @IsOptional()
337
+ @IsNumber()
338
+ @Min(0)
339
+ @Max(1)
340
+ public newFeeAmount?: number;
341
+
331
342
  @IsOptional()
332
343
  @IsUserAlias({ each: true })
333
344
  public newAuthorities?: UserAlias[];
@@ -355,6 +366,10 @@ export class TradeCalculationResFeesDto {
355
366
  @IsNotEmpty()
356
367
  @IsString()
357
368
  reverseBondingCurve: string;
369
+
370
+ @IsNotEmpty()
371
+ @IsString()
372
+ transactionFees: string;
358
373
  }
359
374
 
360
375
  export class TradeCalculationResDto {
@@ -366,3 +381,26 @@ export class TradeCalculationResDto {
366
381
  @Type(() => TradeCalculationResFeesDto)
367
382
  public extraFees: TradeCalculationResFeesDto;
368
383
  }
384
+
385
+ export class AuthorizeBatchSubmitterDto extends SubmitCallDTO {
386
+ @IsArray()
387
+ @ArrayNotEmpty()
388
+ @IsString({ each: true })
389
+ authorities: string[];
390
+ }
391
+
392
+ export class DeauthorizeBatchSubmitterDto extends SubmitCallDTO {
393
+ @IsString()
394
+ authority: string;
395
+ }
396
+
397
+ export class FetchBatchSubmitAuthoritiesDto extends ChainCallDTO {
398
+ // No additional fields needed for fetching all authorities
399
+ }
400
+
401
+ export class BatchSubmitAuthoritiesResDto extends ChainCallDTO {
402
+ @IsArray()
403
+ @ArrayNotEmpty()
404
+ @IsString({ each: true })
405
+ authorities: string[];
406
+ }
@@ -14,7 +14,7 @@
14
14
  */
15
15
  import { ChainObject, IsUserAlias, UserAlias } from "@gala-chain/api";
16
16
  import { Exclude } from "class-transformer";
17
- import { ArrayNotEmpty, IsNotEmpty } from "class-validator";
17
+ import { ArrayNotEmpty, IsNotEmpty, IsNumber, Max, Min } from "class-validator";
18
18
  import { JSONSchema } from "class-validator-jsonschema";
19
19
 
20
20
  @JSONSchema({
@@ -22,24 +22,33 @@ import { JSONSchema } from "class-validator-jsonschema";
22
22
  })
23
23
  export class LaunchpadFeeConfig extends ChainObject {
24
24
  @Exclude()
25
- public static INDEX_KEY = "GCLFC"; // GalaChain Launchpad Fee Configuration
25
+ public static INDEX_KEY = "GCLPFC"; // GalaChain Launchpad Fee Configuration
26
26
 
27
27
  @IsNotEmpty()
28
28
  @IsUserAlias()
29
29
  feeAddress: UserAlias;
30
30
 
31
+ @IsNotEmpty()
32
+ @IsNumber()
33
+ @Min(0)
34
+ @Max(1)
35
+ public feeAmount: number;
36
+
31
37
  @ArrayNotEmpty()
32
38
  @IsUserAlias({ each: true })
33
39
  authorities: UserAlias[];
34
40
 
35
- constructor(feeAddress: UserAlias, authorities: UserAlias[]) {
41
+ constructor(feeAddress: UserAlias, feeAmount: number, authorities: UserAlias[]) {
36
42
  super();
37
43
  this.feeAddress = feeAddress;
44
+ this.feeAmount = feeAmount;
38
45
  this.authorities = authorities;
39
46
  }
40
47
 
41
- public setNewFeeAddress(newfeeAddress: UserAlias, newAuthorities: UserAlias[]) {
48
+ public updateFeeConfig(newfeeAddress: UserAlias, newFeeAmount: number, newAuthorities: UserAlias[]) {
42
49
  this.feeAddress = newfeeAddress;
50
+ this.feeAmount = newFeeAmount;
43
51
  this.authorities = newAuthorities;
52
+ this.validate();
44
53
  }
45
54
  }
@@ -17,3 +17,4 @@ export * from "./LaunchpadDtos";
17
17
  export * from "./LaunchpadFinalizeAllocation";
18
18
  export * from "./LaunchpadFeeConfig";
19
19
  export * from "./LaunchpadSale";
20
+ export * from "./LaunchpadBatchSubmitAuthorities";
@@ -12,21 +12,28 @@
12
12
  * See the License for the specific language governing permissions and
13
13
  * limitations under the License.
14
14
  */
15
+ import { BatchDto, GalaChainResponse, UnauthorizedError } from "@gala-chain/api";
15
16
  import {
17
+ BatchWriteLimitExceededError,
16
18
  EVALUATE,
17
19
  Evaluate,
18
20
  GalaChainContext,
19
21
  GalaContract,
20
22
  GalaTransaction,
21
- Submit
23
+ Submit,
24
+ getApiMethod
22
25
  } from "@gala-chain/chaincode";
23
26
 
24
27
  import { version } from "../../package.json";
25
28
  import {
29
+ AuthorizeBatchSubmitterDto,
30
+ BatchSubmitAuthoritiesResDto,
26
31
  ConfigureLaunchpadFeeAddressDto,
27
32
  CreateSaleResDto,
28
33
  CreateTokenSaleDTO,
34
+ DeauthorizeBatchSubmitterDto,
29
35
  ExactTokenQuantityDto,
36
+ FetchBatchSubmitAuthoritiesDto,
30
37
  FetchSaleDto,
31
38
  FinalizeTokenAllocationDto,
32
39
  LaunchpadFeeConfig,
@@ -45,6 +52,7 @@ import {
45
52
  sellWithNativeFeeGate
46
53
  } from "./dexLaunchpadFeeGate";
47
54
  import {
55
+ authorizeLaunchpadBatchSubmitter,
48
56
  buyExactToken,
49
57
  buyWithNative,
50
58
  calculatePreMintTokens,
@@ -54,9 +62,12 @@ import {
54
62
  callNativeTokenOut,
55
63
  configureLaunchpadFeeAddress,
56
64
  createSale,
65
+ deauthorizeLaunchpadBatchSubmitter,
66
+ fetchLaunchpadBatchSubmitAuthorities,
57
67
  fetchLaunchpadFeeConfig,
58
68
  fetchSaleDetails,
59
69
  finalizeTokenAllocation,
70
+ getLaunchpadBatchSubmitAuthorities,
60
71
  sellExactToken,
61
72
  sellWithNative
62
73
  } from "./launchpad";
@@ -208,4 +219,88 @@ export class LaunchpadContract extends GalaContract {
208
219
  public async FetchLaunchpadFeeConfig(ctx: GalaChainContext): Promise<LaunchpadFeeConfig> {
209
220
  return fetchLaunchpadFeeConfig(ctx);
210
221
  }
222
+
223
+ public async BatchSubmit(ctx: GalaChainContext, batchDto: BatchDto): Promise<GalaChainResponse<unknown>[]> {
224
+ // Check if the calling user is authorized to submit batches
225
+ const batchAuthorities = await fetchLaunchpadBatchSubmitAuthorities(ctx);
226
+ if (!batchAuthorities.isAuthorized(ctx.callingUser)) {
227
+ throw new UnauthorizedError(
228
+ `CallingUser ${ctx.callingUser} is not authorized to submit batches. ` +
229
+ `Authorized users: ${batchAuthorities.getAuthorities().join(", ")}`
230
+ );
231
+ }
232
+
233
+ const responses: GalaChainResponse<unknown>[] = [];
234
+
235
+ const softWritesLimit = batchDto.writesLimit ?? BatchDto.WRITES_DEFAULT_LIMIT;
236
+ const writesLimit = Math.min(softWritesLimit, BatchDto.WRITES_HARD_LIMIT);
237
+ let writesCount = ctx.stub.getWritesCount();
238
+
239
+ for (const [index, op] of batchDto.operations.entries()) {
240
+ // Use sandboxed context to avoid flushes of writes and deletes, and populate
241
+ // the stub with current writes and deletes.
242
+ const sandboxCtx = ctx.createReadOnlyContext(index);
243
+ sandboxCtx.stub.setWrites(ctx.stub.getWrites());
244
+ sandboxCtx.stub.setDeletes(ctx.stub.getDeletes());
245
+
246
+ // Execute the operation. Collect both successful and failed responses.
247
+ let response: GalaChainResponse<unknown>;
248
+ try {
249
+ if (writesCount >= writesLimit) {
250
+ throw new BatchWriteLimitExceededError(writesLimit);
251
+ }
252
+
253
+ const method = getApiMethod(this, op.method, (m) => m.isWrite && m.methodName !== "BatchSubmit");
254
+ response = await this[method.methodName](sandboxCtx, op.dto);
255
+ } catch (error) {
256
+ response = GalaChainResponse.Error(error);
257
+ }
258
+ responses.push(response);
259
+
260
+ // Update the current context with the writes and deletes if the operation
261
+ // is successful.
262
+ if (GalaChainResponse.isSuccess(response)) {
263
+ ctx.stub.setWrites(sandboxCtx.stub.getWrites());
264
+ ctx.stub.setDeletes(sandboxCtx.stub.getDeletes());
265
+ writesCount = ctx.stub.getWritesCount();
266
+ }
267
+ }
268
+ return responses;
269
+ }
270
+
271
+ @Submit({
272
+ in: AuthorizeBatchSubmitterDto,
273
+ out: BatchSubmitAuthoritiesResDto,
274
+ allowedOrgs: [process.env.CURATOR_ORG_MSP ?? "CuratorOrg"]
275
+ })
276
+ public async AuthorizeBatchSubmitter(
277
+ ctx: GalaChainContext,
278
+ dto: AuthorizeBatchSubmitterDto
279
+ ): Promise<BatchSubmitAuthoritiesResDto> {
280
+ return await authorizeLaunchpadBatchSubmitter(ctx, dto);
281
+ }
282
+
283
+ @Submit({
284
+ in: DeauthorizeBatchSubmitterDto,
285
+ out: BatchSubmitAuthoritiesResDto,
286
+ allowedOrgs: [process.env.CURATOR_ORG_MSP ?? "CuratorOrg"]
287
+ })
288
+ public async DeauthorizeBatchSubmitter(
289
+ ctx: GalaChainContext,
290
+ dto: DeauthorizeBatchSubmitterDto
291
+ ): Promise<BatchSubmitAuthoritiesResDto> {
292
+ return await deauthorizeLaunchpadBatchSubmitter(ctx, dto);
293
+ }
294
+
295
+ @GalaTransaction({
296
+ type: EVALUATE,
297
+ in: FetchBatchSubmitAuthoritiesDto,
298
+ out: BatchSubmitAuthoritiesResDto
299
+ })
300
+ public async GetBatchSubmitAuthorities(
301
+ ctx: GalaChainContext,
302
+ dto: FetchBatchSubmitAuthoritiesDto
303
+ ): Promise<BatchSubmitAuthoritiesResDto> {
304
+ return await getLaunchpadBatchSubmitAuthorities(ctx, dto);
305
+ }
211
306
  }
@@ -17,7 +17,7 @@ import { BigNumber } from "bignumber.js";
17
17
 
18
18
  import { ExactTokenQuantityDto, LaunchpadSale, TradeResDto } from "../../api/types";
19
19
  import { SlippageToleranceExceededError } from "../../api/utils/error";
20
- import { fetchAndValidateSale } from "../utils";
20
+ import { fetchAndValidateSale, fetchLaunchpadFeeAddress } from "../utils";
21
21
  import { callNativeTokenIn } from "./callNativeTokenIn";
22
22
  import { finalizeSale } from "./finaliseSale";
23
23
 
@@ -53,6 +53,7 @@ export async function buyExactToken(
53
53
 
54
54
  // Calculate the required amount of native tokens to buy the specified token amount
55
55
  const callNativeTokenInResult1 = await callNativeTokenIn(ctx, buyTokenDTO);
56
+ let transactionFees = callNativeTokenInResult1.extraFees.transactionFees;
56
57
  let nativeTokensToBuy = new BigNumber(callNativeTokenInResult1.calculatedQuantity);
57
58
  const nativeToken = sale.fetchNativeTokenInstanceKey();
58
59
  const memeToken = sale.fetchSellingTokenInstanceKey();
@@ -62,6 +63,7 @@ export async function buyExactToken(
62
63
  buyTokenDTO.tokenQuantity = tokenLeftInVault;
63
64
  const callNativeTokenInResult2 = await callNativeTokenIn(ctx, buyTokenDTO);
64
65
  nativeTokensToBuy = new BigNumber(callNativeTokenInResult2.calculatedQuantity);
66
+ transactionFees = callNativeTokenInResult2.extraFees.transactionFees;
65
67
  isSaleFinalized = true;
66
68
  }
67
69
 
@@ -81,6 +83,19 @@ export async function buyExactToken(
81
83
  );
82
84
  }
83
85
 
86
+ // Transfer transaction fees
87
+ const launchpadFeeAddressConfiguration = await fetchLaunchpadFeeAddress(ctx);
88
+ if (launchpadFeeAddressConfiguration && transactionFees) {
89
+ await transferToken(ctx, {
90
+ from: ctx.callingUser,
91
+ to: launchpadFeeAddressConfiguration.feeAddress,
92
+ tokenInstanceKey: nativeToken,
93
+ quantity: new BigNumber(transactionFees),
94
+ allowancesToUse: [],
95
+ authorizedOnBehalf: undefined
96
+ });
97
+ }
98
+
84
99
  // Transfer native tokens from the buyer to the vault
85
100
  await transferToken(ctx, {
86
101
  from: ctx.callingUser,
@@ -117,6 +132,7 @@ export async function buyExactToken(
117
132
  const token = await fetchTokenClass(ctx, sale.sellingToken);
118
133
  return {
119
134
  inputQuantity: nativeTokensToBuy.toFixed(),
135
+ totalFees: transactionFees,
120
136
  outputQuantity: buyTokenDTO.tokenQuantity.toFixed(),
121
137
  tokenName: token.name,
122
138
  tradeType: "Buy",
@@ -17,7 +17,7 @@ import { BigNumber } from "bignumber.js";
17
17
 
18
18
  import { ExactTokenQuantityDto, LaunchpadSale, NativeTokenQuantityDto, TradeResDto } from "../../api/types";
19
19
  import { SlippageToleranceExceededError } from "../../api/utils/error";
20
- import { fetchAndValidateSale } from "../utils";
20
+ import { fetchAndValidateSale, fetchLaunchpadFeeAddress } from "../utils";
21
21
  import { callMemeTokenOut } from "./callMemeTokenOut";
22
22
  import { callNativeTokenIn } from "./callNativeTokenIn";
23
23
  import { finalizeSale } from "./finaliseSale";
@@ -49,22 +49,30 @@ export async function buyWithNative(
49
49
  buyTokenDTO: NativeTokenQuantityDto
50
50
  ): Promise<TradeResDto> {
51
51
  let isSaleFinalized = false;
52
+
53
+ // Fetch and validate sale state
52
54
  const sale = await fetchAndValidateSale(ctx, buyTokenDTO.vaultAddress);
53
55
  const tokensLeftInVault = new BigNumber(sale.sellingTokenQuantity);
56
+
57
+ // Calculate how many tokens the user can buy and fee info
54
58
  const callMemeTokenOutResult = await callMemeTokenOut(ctx, buyTokenDTO);
59
+ let transactionFees = callMemeTokenOutResult.extraFees.transactionFees;
55
60
  let tokensToBuy = new BigNumber(callMemeTokenOutResult.calculatedQuantity);
56
61
 
57
62
  const nativeToken = sale.fetchNativeTokenInstanceKey();
58
63
  const memeToken = sale.fetchSellingTokenInstanceKey();
59
64
 
65
+ // If vault has fewer tokens than what user wants to buy, cap the purchase
60
66
  if (tokensLeftInVault.comparedTo(tokensToBuy) <= 0) {
61
67
  tokensToBuy = tokensLeftInVault;
62
68
  const nativeTokensrequiredToBuyDto = new ExactTokenQuantityDto(buyTokenDTO.vaultAddress, tokensToBuy);
63
69
  const callNativeTokenInResult = await callNativeTokenIn(ctx, nativeTokensrequiredToBuyDto);
70
+ transactionFees = callMemeTokenOutResult.extraFees.transactionFees;
64
71
  buyTokenDTO.nativeTokenQuantity = new BigNumber(callNativeTokenInResult.calculatedQuantity);
65
72
  isSaleFinalized = true;
66
73
  }
67
74
 
75
+ // Finalize sale if market cap is reached
68
76
  if (
69
77
  buyTokenDTO.nativeTokenQuantity
70
78
  .plus(new BigNumber(sale.nativeTokenQuantity))
@@ -72,12 +80,27 @@ export async function buyWithNative(
72
80
  )
73
81
  isSaleFinalized = true;
74
82
 
83
+ // Check for slippage condition
75
84
  if (buyTokenDTO.expectedToken && buyTokenDTO.expectedToken.comparedTo(tokensToBuy) > 0) {
76
85
  throw new SlippageToleranceExceededError(
77
86
  "Tokens expected from this operation are more than the the actual amount that will be provided."
78
87
  );
79
88
  }
80
89
 
90
+ // Transfer transaction fees to launchpad fee address
91
+ const launchpadFeeAddressConfiguration = await fetchLaunchpadFeeAddress(ctx);
92
+ if (launchpadFeeAddressConfiguration && transactionFees) {
93
+ await transferToken(ctx, {
94
+ from: ctx.callingUser,
95
+ to: launchpadFeeAddressConfiguration.feeAddress,
96
+ tokenInstanceKey: nativeToken,
97
+ quantity: new BigNumber(transactionFees),
98
+ allowancesToUse: [],
99
+ authorizedOnBehalf: undefined
100
+ });
101
+ }
102
+
103
+ // Transfer native tokens from buyer to vault
81
104
  await transferToken(ctx, {
82
105
  from: ctx.callingUser,
83
106
  to: buyTokenDTO.vaultAddress,
@@ -87,6 +110,7 @@ export async function buyWithNative(
87
110
  authorizedOnBehalf: undefined
88
111
  });
89
112
 
113
+ // Transfer meme tokens from vault to buyer
90
114
  await transferToken(ctx, {
91
115
  from: buyTokenDTO.vaultAddress,
92
116
  to: ctx.callingUser,
@@ -99,9 +123,11 @@ export async function buyWithNative(
99
123
  }
100
124
  });
101
125
 
126
+ // Update sale object with purchase data
102
127
  sale.buyToken(tokensToBuy, buyTokenDTO.nativeTokenQuantity);
103
128
  await putChainObject(ctx, sale);
104
129
 
130
+ // Finalize sale if it's complete
105
131
  if (isSaleFinalized) {
106
132
  await finalizeSale(ctx, sale);
107
133
  }
@@ -109,6 +135,7 @@ export async function buyWithNative(
109
135
  const token = await fetchTokenClass(ctx, sale.sellingToken);
110
136
  return {
111
137
  inputQuantity: buyTokenDTO.nativeTokenQuantity.toFixed(),
138
+ totalFees: transactionFees,
112
139
  outputQuantity: tokensToBuy.toFixed(),
113
140
  tokenName: token.name,
114
141
  tradeType: "Buy",
@@ -18,8 +18,8 @@ import { BigNumber } from "bignumber.js";
18
18
  import Decimal from "decimal.js";
19
19
 
20
20
  import { LaunchpadSale, NativeTokenQuantityDto } from "../../api/types";
21
- import { fetchAndValidateSale, getBondingConstants } from "../utils";
22
- import { calculateReverseBondingCurveFee } from "./fees";
21
+ import { fetchAndValidateSale, fetchLaunchpadFeeAddress, getBondingConstants } from "../utils";
22
+ import { calculateReverseBondingCurveFee, calculateTransactionFee } from "./fees";
23
23
 
24
24
  BigNumber.config({
25
25
  ROUNDING_MODE: BigNumber.ROUND_UP
@@ -72,11 +72,16 @@ function calculateMemeTokensRequired(sale: LaunchpadSale, requestedNativeTokenQu
72
72
  */
73
73
  export async function callMemeTokenIn(ctx: GalaChainContext, sellTokenDTO: NativeTokenQuantityDto) {
74
74
  const sale = await fetchAndValidateSale(ctx, sellTokenDTO.vaultAddress);
75
+ const launchpadFeeAddressConfiguration = await fetchLaunchpadFeeAddress(ctx);
75
76
 
76
77
  return {
77
78
  calculatedQuantity: calculateMemeTokensRequired(sale, sellTokenDTO.nativeTokenQuantity),
78
79
  extraFees: {
79
- reverseBondingCurve: calculateReverseBondingCurveFee(sale, sellTokenDTO.nativeTokenQuantity).toString()
80
+ reverseBondingCurve: calculateReverseBondingCurveFee(sale, sellTokenDTO.nativeTokenQuantity).toString(),
81
+ transactionFees: calculateTransactionFee(
82
+ sellTokenDTO.nativeTokenQuantity,
83
+ launchpadFeeAddressConfiguration?.feeAmount
84
+ )
80
85
  }
81
86
  };
82
87
  }
@@ -17,7 +17,8 @@ import { BigNumber } from "bignumber.js";
17
17
  import Decimal from "decimal.js";
18
18
 
19
19
  import { LaunchpadSale, NativeTokenQuantityDto } from "../../api/types";
20
- import { fetchAndValidateSale, getBondingConstants } from "../utils";
20
+ import { fetchAndValidateSale, fetchLaunchpadFeeAddress, getBondingConstants } from "../utils";
21
+ import { calculateTransactionFee } from "./fees";
21
22
 
22
23
  BigNumber.config({
23
24
  ROUNDING_MODE: BigNumber.ROUND_UP
@@ -71,10 +72,15 @@ export async function callMemeTokenOut(ctx: GalaChainContext, buyTokenDTO: Nativ
71
72
  roundedResult = new Decimal("1e+7").minus(new Decimal(totalTokensSold));
72
73
  }
73
74
 
75
+ const launchpadFeeAddressConfiguration = await fetchLaunchpadFeeAddress(ctx);
74
76
  return {
75
77
  calculatedQuantity: roundedResult.toFixed(),
76
78
  extraFees: {
77
- reverseBondingCurve: "0"
79
+ reverseBondingCurve: "0",
80
+ transactionFees: calculateTransactionFee(
81
+ BigNumber(nativeTokens.toFixed()),
82
+ launchpadFeeAddressConfiguration?.feeAmount
83
+ )
78
84
  }
79
85
  };
80
86
  }
@@ -17,7 +17,8 @@ import { BigNumber } from "bignumber.js";
17
17
  import Decimal from "decimal.js";
18
18
 
19
19
  import { ExactTokenQuantityDto } from "../../api/types";
20
- import { fetchAndValidateSale, getBondingConstants } from "../utils";
20
+ import { fetchAndValidateSale, fetchLaunchpadFeeAddress, getBondingConstants } from "../utils";
21
+ import { calculateTransactionFee } from "./fees";
21
22
 
22
23
  BigNumber.config({
23
24
  ROUNDING_MODE: BigNumber.ROUND_UP
@@ -62,10 +63,16 @@ export async function callNativeTokenIn(ctx: GalaChainContext, buyTokenDTO: Exac
62
63
 
63
64
  const price = constantFactor.mul(differenceOfExponentials);
64
65
 
66
+ const launchpadFeeAddressConfiguration = await fetchLaunchpadFeeAddress(ctx);
67
+ const roundedPrice = price.toDecimalPlaces(8, Decimal.ROUND_UP).toFixed();
65
68
  return {
66
- calculatedQuantity: price.toDecimalPlaces(8, Decimal.ROUND_UP).toFixed(),
69
+ calculatedQuantity: roundedPrice,
67
70
  extraFees: {
68
- reverseBondingCurve: "0"
71
+ reverseBondingCurve: "0",
72
+ transactionFees: calculateTransactionFee(
73
+ BigNumber(roundedPrice),
74
+ launchpadFeeAddressConfiguration?.feeAmount
75
+ )
69
76
  }
70
77
  };
71
78
  }
@@ -17,8 +17,8 @@ import { BigNumber } from "bignumber.js";
17
17
  import Decimal from "decimal.js";
18
18
 
19
19
  import { ExactTokenQuantityDto, LaunchpadSale } from "../../api/types";
20
- import { fetchAndValidateSale, getBondingConstants } from "../utils";
21
- import { calculateReverseBondingCurveFee } from "./fees";
20
+ import { fetchAndValidateSale, fetchLaunchpadFeeAddress, getBondingConstants } from "../utils";
21
+ import { calculateReverseBondingCurveFee, calculateTransactionFee } from "./fees";
22
22
 
23
23
  BigNumber.config({
24
24
  ROUNDING_MODE: BigNumber.ROUND_UP
@@ -72,10 +72,15 @@ function calculateNativeTokensReceived(sale: LaunchpadSale, tokensToSellBn: BigN
72
72
  export async function callNativeTokenOut(ctx: GalaChainContext, sellTokenDTO: ExactTokenQuantityDto) {
73
73
  const sale = await fetchAndValidateSale(ctx, sellTokenDTO.vaultAddress);
74
74
  const nativeTokensReceived = calculateNativeTokensReceived(sale, sellTokenDTO.tokenQuantity);
75
+ const launchpadFeeAddressConfiguration = await fetchLaunchpadFeeAddress(ctx);
75
76
  return {
76
77
  calculatedQuantity: nativeTokensReceived,
77
78
  extraFees: {
78
- reverseBondingCurve: calculateReverseBondingCurveFee(sale, BigNumber(nativeTokensReceived)).toString()
79
+ reverseBondingCurve: calculateReverseBondingCurveFee(sale, BigNumber(nativeTokensReceived)).toString(),
80
+ transactionFees: calculateTransactionFee(
81
+ BigNumber(nativeTokensReceived),
82
+ launchpadFeeAddressConfiguration?.feeAmount
83
+ )
79
84
  }
80
85
  };
81
86
  }
@@ -18,19 +18,35 @@ import { getObjectByKey, putChainObject } from "@gala-chain/chaincode";
18
18
 
19
19
  import { ConfigureLaunchpadFeeAddressDto, LaunchpadFeeConfig } from "../../api/types";
20
20
 
21
+ /**
22
+ * Configures or updates the Launchpad platform fee settings.
23
+ *
24
+ * This function initializes or modifies the fee address, fee amount, and list of authorities
25
+ * responsible for managing Launchpad platform fees. Only users from the designated curator
26
+ * organization or authorized addresses are permitted to make these changes.
27
+ *
28
+ * @param ctx - The context object providing access to the GalaChain environment.
29
+ * @param dto - The data transfer object containing the new fee address, fee amount,
30
+ * and optional list of new authorities.
31
+ * @returns A promise that resolves to the updated or newly created `LaunchpadFeeConfig` object.
32
+ */
21
33
  export async function configureLaunchpadFeeAddress(
22
34
  ctx: GalaChainContext,
23
35
  dto: ConfigureLaunchpadFeeAddressDto
24
36
  ): Promise<LaunchpadFeeConfig> {
25
- if (!dto.newPlatformFeeAddress && !dto.newAuthorities?.length) {
37
+ // Validate input: at least one field must be present
38
+ if (!dto.newPlatformFeeAddress && !dto.newAuthorities?.length && !dto.newFeeAmount) {
26
39
  throw new ValidationFailedError("None of the input fields are present.");
27
40
  }
28
41
 
29
42
  const curatorOrgMsp = process.env.CURATOR_ORG_MSP ?? "CuratorOrg";
30
43
 
31
44
  const key = ctx.stub.createCompositeKey(LaunchpadFeeConfig.INDEX_KEY, []);
45
+
46
+ // Attempt to fetch existing config from the chain
32
47
  let platformFeeAddress = await getObjectByKey(ctx, LaunchpadFeeConfig, key).catch((e) => {
33
48
  const chainError = ChainError.from(e);
49
+ // Handle case where config doesn't exist yet
34
50
  if (chainError.matches(ErrorCode.NOT_FOUND)) {
35
51
  return undefined;
36
52
  } else {
@@ -38,25 +54,32 @@ export async function configureLaunchpadFeeAddress(
38
54
  }
39
55
  });
40
56
 
57
+ // Authorization check
41
58
  if (ctx.clientIdentity.getMSPID() !== curatorOrgMsp) {
42
59
  if (!platformFeeAddress || !platformFeeAddress.authorities.includes(ctx.callingUser)) {
43
60
  throw new UnauthorizedError(`CallingUser ${ctx.callingUser} is not authorized to create or update`);
44
61
  }
45
62
  }
46
63
 
64
+ // If no existing config, this is the initial setup
47
65
  if (!platformFeeAddress) {
48
- if (!dto.newPlatformFeeAddress) {
66
+ // Require both address and fee amount on initial setup
67
+ if (!dto.newPlatformFeeAddress || !dto.newFeeAmount) {
49
68
  throw new ValidationFailedError(
50
- "Must provide a platform fee address in the initial setup of the configuration."
69
+ "Must provide a launchpad platform fee address and fee amount in the initial setup of the configuration."
51
70
  );
52
71
  }
72
+ // Create new configuration
53
73
  platformFeeAddress = new LaunchpadFeeConfig(
54
74
  dto.newPlatformFeeAddress,
75
+ dto.newFeeAmount,
55
76
  dto.newAuthorities ?? [ctx.callingUser]
56
77
  );
57
78
  } else if (platformFeeAddress && platformFeeAddress.authorities.includes(ctx.callingUser)) {
58
- platformFeeAddress.setNewFeeAddress(
79
+ // Caller is authorized and updating the existing config
80
+ platformFeeAddress.updateFeeConfig(
59
81
  dto.newPlatformFeeAddress ?? platformFeeAddress.feeAddress,
82
+ dto.newFeeAmount ?? platformFeeAddress.feeAmount,
60
83
  dto.newAuthorities ?? platformFeeAddress.authorities
61
84
  );
62
85
  } else {
@@ -56,7 +56,7 @@ export async function payReverseBondingCurveFee(
56
56
  nativeTokensToReceive: BigNumber,
57
57
  maxAcceptableFee?: BigNumber
58
58
  ) {
59
- const feeAmount = await calculateReverseBondingCurveFee(sale, nativeTokensToReceive);
59
+ const feeAmount = calculateReverseBondingCurveFee(sale, nativeTokensToReceive);
60
60
 
61
61
  if (feeAmount.isZero()) {
62
62
  return; // No fee
@@ -103,3 +103,7 @@ export async function payReverseBondingCurveFee(
103
103
  authorizedOnBehalf: undefined
104
104
  });
105
105
  }
106
+
107
+ export function calculateTransactionFee(tokensBeingTraded: BigNumber, feeAmount?: number) {
108
+ return tokensBeingTraded.multipliedBy(feeAmount ?? 0).toFixed(8, BigNumber.ROUND_UP);
109
+ }