@pear-protocol/types 0.0.17 → 0.0.19

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.
@@ -1,73 +1,54 @@
1
1
  import { z } from 'zod';
2
2
  import { RefineMinimumOneSymbol, RefineUniqueSymbols } from '../../common/refinement';
3
3
  import { TradeLegPostOnly } from '../../common/trade-legs';
4
- import { BracketType, Trailing } from '../entities';
5
- import { CreateTriggerConditionOpen, CreateTriggerConditionClose, CreateTriggerConditionAttach } from './conditions';
4
+ import { TrailingMode, BracketType } from '../common';
5
+ import { RefineStaticLogicalRules } from '../refinement/static.refinement';
6
+ import { RefineTrailingConfig, RefineTrailingLogicalRules } from '../refinement/trailing.refinement';
7
+ import { CreateTriggerConditionOpen, CreateTriggerConditionCloseStatic, CreateTriggerConditionCloseTrailing, CreateTriggerConditionAttachStatic, CreateTriggerConditionAttachTrailing } from './conditions';
6
8
 
7
- const RefineTrailingRequiresBracket = (value, context) => {
8
- if (value.trailing !== void 0 && value.bracketType === "NONE") {
9
- context.addIssue({
10
- code: "custom",
11
- message: "bracketType must be STOP_LOSS or TAKE_PROFIT when trailing is provided",
12
- path: ["bracketType"]
13
- });
14
- }
15
- };
9
+ const TrailingConfig = z.object({
10
+ mode: TrailingMode,
11
+ deltaValue: z.number(),
12
+ activationValue: z.number().optional()
13
+ }).superRefine(RefineTrailingConfig);
16
14
  const CreateTriggerMarketOpen = z.object({
17
15
  intent: z.literal("OPEN"),
18
16
  type: z.literal("MARKET"),
19
- /**
20
- * Condition to fire the trigger
21
- */
22
17
  condition: CreateTriggerConditionOpen,
23
- /**
24
- * MARKET Execution details to execute when the trigger is fired
25
- */
26
18
  payload: z.object({
27
19
  type: z.literal("MARKET"),
28
- /**
29
- * Requested legs
30
- */
31
20
  legs: z.tuple([TradeLegPostOnly]).rest(TradeLegPostOnly).superRefine(RefineMinimumOneSymbol).superRefine(RefineUniqueSymbols).describe("Array of legs to make up the exposure")
32
21
  })
33
22
  });
34
- const CreateTriggerMarketClose = z.object({
23
+ const CreateTriggerMarketCloseStatic = z.object({
35
24
  intent: z.literal("CLOSE"),
36
25
  type: z.literal("MARKET"),
37
- /**
38
- * Condition to fire the trigger
39
- */
40
- condition: CreateTriggerConditionClose,
41
- /**
42
- * Position UUID
43
- */
44
26
  positionId: z.uuid(),
45
- /**
46
- * Bracket type (stop loss / take profit / none)
47
- */
48
27
  bracketType: BracketType,
49
- /**
50
- * Optional: trailing configuration
51
- */
52
- trailing: Trailing.optional()
53
- }).superRefine(RefineTrailingRequiresBracket);
54
- const AttachTriggerMarketClose = z.object({
28
+ condition: CreateTriggerConditionCloseStatic
29
+ }).superRefine(RefineStaticLogicalRules);
30
+ const CreateTriggerMarketCloseTrailing = z.object({
31
+ intent: z.literal("CLOSE"),
32
+ type: z.literal("MARKET"),
33
+ positionId: z.uuid(),
34
+ condition: CreateTriggerConditionCloseTrailing,
35
+ trailing: TrailingConfig
36
+ }).superRefine(RefineTrailingLogicalRules);
37
+ const CreateTriggerMarketClose = z.union([CreateTriggerMarketCloseStatic, CreateTriggerMarketCloseTrailing]);
38
+ const AttachTriggerMarketCloseStatic = z.object({
55
39
  intent: z.literal("CLOSE"),
56
40
  type: z.literal("MARKET"),
57
- /**
58
- * Condition to fire the trigger
59
- */
60
- condition: CreateTriggerConditionAttach,
61
- /**
62
- * Bracket type (stop loss / take profit / none)
63
- */
64
41
  bracketType: BracketType,
65
- /**
66
- * Optional: trailing configuration
67
- */
68
- trailing: Trailing.optional()
69
- }).superRefine(RefineTrailingRequiresBracket);
42
+ condition: CreateTriggerConditionAttachStatic
43
+ }).superRefine(RefineStaticLogicalRules);
44
+ const AttachTriggerMarketCloseTrailing = z.object({
45
+ intent: z.literal("CLOSE"),
46
+ type: z.literal("MARKET"),
47
+ condition: CreateTriggerConditionAttachTrailing,
48
+ trailing: TrailingConfig
49
+ }).superRefine(RefineTrailingLogicalRules);
50
+ const AttachTriggerMarketClose = z.union([AttachTriggerMarketCloseStatic, AttachTriggerMarketCloseTrailing]);
70
51
  const CreateTriggerOpen = z.discriminatedUnion("type", [CreateTriggerMarketOpen]);
71
- const CreateTriggerClose = z.discriminatedUnion("type", [CreateTriggerMarketClose]);
52
+ const CreateTriggerClose = CreateTriggerMarketClose;
72
53
 
73
- export { AttachTriggerMarketClose, CreateTriggerClose, CreateTriggerMarketClose, CreateTriggerMarketOpen, CreateTriggerOpen };
54
+ export { AttachTriggerMarketClose, AttachTriggerMarketCloseStatic, AttachTriggerMarketCloseTrailing, CreateTriggerClose, CreateTriggerMarketClose, CreateTriggerMarketCloseStatic, CreateTriggerMarketCloseTrailing, CreateTriggerMarketOpen, CreateTriggerOpen, TrailingConfig };
@@ -0,0 +1,2 @@
1
+ export * from './static.refinement';
2
+ export * from './trailing.refinement';
@@ -0,0 +1,2 @@
1
+ export * from './static.refinement';
2
+ export * from './trailing.refinement';
@@ -0,0 +1,18 @@
1
+ import type { z } from 'zod';
2
+ import type { BracketType } from '../common';
3
+ import type { CreateTriggerConditionCloseStatic } from '../payloads/conditions';
4
+ type ValidationFieldIssue = {
5
+ field: 'threshold';
6
+ message: string;
7
+ };
8
+ type ConditionType = CreateTriggerConditionCloseStatic['type'];
9
+ export declare const RefineStaticLogicalRules: (value: {
10
+ bracketType: BracketType;
11
+ condition: CreateTriggerConditionCloseStatic;
12
+ }, context: z.RefinementCtx) => void;
13
+ export declare function getStaticLogicalIssues(input: {
14
+ bracketType: BracketType;
15
+ conditionType: ConditionType;
16
+ threshold: number;
17
+ }): ValidationFieldIssue[];
18
+ export {};
@@ -0,0 +1,40 @@
1
+ const RefineStaticLogicalRules = (value, context) => {
2
+ for (const issue of getStaticLogicalIssues({
3
+ bracketType: value.bracketType,
4
+ conditionType: value.condition.type,
5
+ threshold: value.condition.data.threshold
6
+ })) {
7
+ context.addIssue({
8
+ code: "custom",
9
+ message: issue.message
10
+ });
11
+ }
12
+ };
13
+ function getStaticLogicalIssues(input) {
14
+ const { conditionType, threshold } = input;
15
+ const issues = [];
16
+ switch (conditionType) {
17
+ case "notional": {
18
+ if (threshold <= 0) {
19
+ issues.push({
20
+ field: "threshold",
21
+ message: "threshold must be greater than 0 for notional static triggers"
22
+ });
23
+ }
24
+ break;
25
+ }
26
+ case "upnl":
27
+ case "upnl_bps": {
28
+ if (threshold === 0) {
29
+ issues.push({
30
+ field: "threshold",
31
+ message: "threshold must not be 0 for uPnL BPS static triggers"
32
+ });
33
+ }
34
+ break;
35
+ }
36
+ }
37
+ return issues;
38
+ }
39
+
40
+ export { RefineStaticLogicalRules, getStaticLogicalIssues };
@@ -0,0 +1,27 @@
1
+ import { z } from 'zod';
2
+ import type { TrailingMode } from '../common';
3
+ import type { CreateTriggerConditionCloseTrailing } from '../payloads/conditions';
4
+ type TrailingConfigValue = {
5
+ mode: TrailingMode;
6
+ deltaValue: number;
7
+ activationValue?: number | null;
8
+ };
9
+ type ValidationFieldIssue = {
10
+ field: 'deltaValue' | 'activationValue';
11
+ message: string;
12
+ };
13
+ type ConditionType = CreateTriggerConditionCloseTrailing['type'];
14
+ export declare const RefineTrailingConfig: (value: {
15
+ mode: TrailingMode;
16
+ deltaValue: number;
17
+ }, ctx: z.RefinementCtx) => void;
18
+ export declare const RefineTrailingLogicalRules: (value: {
19
+ condition: CreateTriggerConditionCloseTrailing;
20
+ trailing: TrailingConfigValue;
21
+ }, ctx: z.RefinementCtx) => void;
22
+ export declare function getTrailingLogicalIssues(input: {
23
+ conditionType: ConditionType;
24
+ trailing: TrailingConfigValue;
25
+ }): ValidationFieldIssue[];
26
+ export declare function getTrailingDeltaIssues(trailing: TrailingConfigValue): ValidationFieldIssue[];
27
+ export {};
@@ -0,0 +1,81 @@
1
+ import 'zod';
2
+
3
+ const RefineTrailingConfig = (value, ctx) => {
4
+ for (const issue of getTrailingDeltaIssues(value)) {
5
+ ctx.addIssue({
6
+ code: "custom",
7
+ message: issue.message
8
+ });
9
+ }
10
+ };
11
+ const RefineTrailingLogicalRules = (value, ctx) => {
12
+ for (const issue of getTrailingLogicalIssues({
13
+ conditionType: value.condition.type,
14
+ trailing: value.trailing
15
+ })) {
16
+ ctx.addIssue({
17
+ code: "custom",
18
+ message: issue.message
19
+ });
20
+ }
21
+ };
22
+ function getTrailingLogicalIssues(input) {
23
+ const { trailing } = input;
24
+ const issues = [];
25
+ switch (input.conditionType) {
26
+ case "notional_trailing": {
27
+ if (trailing.activationValue != null && trailing.activationValue <= 0) {
28
+ issues.push({
29
+ field: "activationValue",
30
+ message: "activationValue must be greater than 0 for notional trailing"
31
+ });
32
+ }
33
+ break;
34
+ }
35
+ case "upnl_trailing":
36
+ case "upnl_bps_trailing": {
37
+ if (trailing.activationValue === 0) {
38
+ issues.push({
39
+ field: "activationValue",
40
+ message: "activationValue must not be 0 for uPnL trailing"
41
+ });
42
+ }
43
+ break;
44
+ }
45
+ }
46
+ if (input.conditionType === "notional_trailing" && trailing.activationValue != null && trailing.mode === "ABSOLUTE_POINTS" && trailing.deltaValue >= trailing.activationValue) {
47
+ issues.push({
48
+ field: "deltaValue",
49
+ message: "deltaValue must be less than activationValue for notional trailing in ABSOLUTE_POINTS mode"
50
+ });
51
+ }
52
+ return issues;
53
+ }
54
+ function getTrailingDeltaIssues(trailing) {
55
+ const issues = [];
56
+ if (trailing.deltaValue <= 0) {
57
+ issues.push({
58
+ field: "deltaValue",
59
+ message: "deltaValue must be greater than 0"
60
+ });
61
+ return issues;
62
+ }
63
+ if (trailing.mode !== "RELATIVE_BPS") {
64
+ return issues;
65
+ }
66
+ if (!Number.isInteger(trailing.deltaValue)) {
67
+ issues.push({
68
+ field: "deltaValue",
69
+ message: "deltaValue must be an integer in RELATIVE_BPS mode"
70
+ });
71
+ }
72
+ if (trailing.deltaValue < 1 || trailing.deltaValue > 5e3) {
73
+ issues.push({
74
+ field: "deltaValue",
75
+ message: "deltaValue must be between 1 and 5000 in RELATIVE_BPS mode"
76
+ });
77
+ }
78
+ return issues;
79
+ }
80
+
81
+ export { RefineTrailingConfig, RefineTrailingLogicalRules, getTrailingDeltaIssues, getTrailingLogicalIssues };