@ottochain/sdk 1.0.3 → 1.1.0

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 (153) hide show
  1. package/dist/cjs/generated/index.js +11 -1
  2. package/dist/cjs/metakit/drop-nulls.js +50 -0
  3. package/dist/cjs/metakit/normalize.js +147 -0
  4. package/dist/cjs/ottochain/metagraph-client.js +147 -0
  5. package/dist/cjs/validation.js +1 -1
  6. package/dist/esm/generated/index.js +11 -1
  7. package/dist/esm/metakit/drop-nulls.js +46 -0
  8. package/dist/esm/metakit/normalize.js +140 -0
  9. package/dist/esm/ottochain/metagraph-client.js +147 -0
  10. package/dist/esm/validation.js +1 -1
  11. package/dist/types/generated/index.d.ts +11 -1
  12. package/dist/types/metakit/drop-nulls.d.ts +29 -0
  13. package/dist/types/metakit/normalize.d.ts +67 -0
  14. package/dist/types/ottochain/index.d.ts +1 -1
  15. package/dist/types/ottochain/metagraph-client.d.ts +90 -0
  16. package/package.json +12 -12
  17. package/dist/apps/contracts/index.d.ts +0 -21
  18. package/dist/apps/contracts/index.js +0 -39
  19. package/dist/apps/contracts/types.d.ts +0 -24
  20. package/dist/apps/contracts/types.js +0 -48
  21. package/dist/apps/identity/index.d.ts +0 -22
  22. package/dist/apps/identity/index.js +0 -40
  23. package/dist/apps/identity/types.d.ts +0 -30
  24. package/dist/apps/identity/types.js +0 -53
  25. package/dist/apps/index.d.ts +0 -29
  26. package/dist/apps/index.js +0 -60
  27. package/dist/apps/markets/index.d.ts +0 -26
  28. package/dist/apps/markets/index.js +0 -46
  29. package/dist/apps/markets/types.d.ts +0 -185
  30. package/dist/apps/markets/types.js +0 -252
  31. package/dist/apps/oracles/index.d.ts +0 -26
  32. package/dist/apps/oracles/index.js +0 -46
  33. package/dist/apps/oracles/types.d.ts +0 -211
  34. package/dist/apps/oracles/types.js +0 -306
  35. package/dist/cjs/apps/contracts/types.js +0 -48
  36. package/dist/cjs/apps/corporate/types.js +0 -44
  37. package/dist/cjs/apps/governance/types.js +0 -42
  38. package/dist/cjs/apps/identity/types.js +0 -53
  39. package/dist/cjs/apps/markets/types.js +0 -219
  40. package/dist/cjs/apps/oracles/types.js +0 -282
  41. package/dist/cjs/generated/ottochain/apps/contracts/v1/contract_pb.js +0 -100
  42. package/dist/cjs/generated/ottochain/apps/corporate/v1/corporate_pb.js +0 -392
  43. package/dist/cjs/generated/ottochain/apps/governance/v1/governance_pb.js +0 -235
  44. package/dist/cjs/generated/ottochain/apps/identity/v1/agent_pb.js +0 -116
  45. package/dist/cjs/generated/ottochain/apps/identity/v1/attestation_pb.js +0 -79
  46. package/dist/cjs/generated/ottochain/apps/markets/v1/market_pb.js +0 -151
  47. package/dist/cjs/generated/ottochain/apps/oracles/v1/oracle_pb.js +0 -109
  48. package/dist/cjs/generated/ottochain/v1/common_pb.js +0 -37
  49. package/dist/cjs/generated/ottochain/v1/fiber_pb.js +0 -86
  50. package/dist/cjs/generated/ottochain/v1/messages_pb.js +0 -44
  51. package/dist/cjs/generated/ottochain/v1/records_pb.js +0 -44
  52. package/dist/errors.d.ts +0 -221
  53. package/dist/errors.js +0 -293
  54. package/dist/esm/apps/contracts/types.js +0 -44
  55. package/dist/esm/apps/corporate/types.js +0 -38
  56. package/dist/esm/apps/governance/types.js +0 -35
  57. package/dist/esm/apps/identity/types.js +0 -50
  58. package/dist/esm/apps/markets/types.js +0 -206
  59. package/dist/esm/apps/oracles/types.js +0 -267
  60. package/dist/esm/generated/ottochain/apps/contracts/v1/contract_pb.js +0 -97
  61. package/dist/esm/generated/ottochain/apps/corporate/v1/corporate_pb.js +0 -389
  62. package/dist/esm/generated/ottochain/apps/governance/v1/governance_pb.js +0 -232
  63. package/dist/esm/generated/ottochain/apps/identity/v1/agent_pb.js +0 -113
  64. package/dist/esm/generated/ottochain/apps/identity/v1/attestation_pb.js +0 -76
  65. package/dist/esm/generated/ottochain/apps/markets/v1/market_pb.js +0 -148
  66. package/dist/esm/generated/ottochain/apps/oracles/v1/oracle_pb.js +0 -106
  67. package/dist/esm/generated/ottochain/v1/common_pb.js +0 -34
  68. package/dist/esm/generated/ottochain/v1/fiber_pb.js +0 -83
  69. package/dist/esm/generated/ottochain/v1/messages_pb.js +0 -41
  70. package/dist/esm/generated/ottochain/v1/records_pb.js +0 -41
  71. package/dist/generated/index.d.ts +0 -15
  72. package/dist/generated/index.js +0 -34
  73. package/dist/generated/ottochain/apps/contracts/v1/contract_pb.d.ts +0 -274
  74. package/dist/generated/ottochain/apps/contracts/v1/contract_pb.js +0 -100
  75. package/dist/generated/ottochain/apps/identity/v1/agent_pb.d.ts +0 -211
  76. package/dist/generated/ottochain/apps/identity/v1/agent_pb.js +0 -116
  77. package/dist/generated/ottochain/apps/identity/v1/attestation_pb.d.ts +0 -238
  78. package/dist/generated/ottochain/apps/identity/v1/attestation_pb.js +0 -79
  79. package/dist/generated/ottochain/apps/markets/v1/market_pb.d.ts +0 -436
  80. package/dist/generated/ottochain/apps/markets/v1/market_pb.js +0 -151
  81. package/dist/generated/ottochain/apps/oracles/v1/oracle_pb.d.ts +0 -393
  82. package/dist/generated/ottochain/apps/oracles/v1/oracle_pb.js +0 -109
  83. package/dist/generated/ottochain/v1/common_pb.d.ts +0 -86
  84. package/dist/generated/ottochain/v1/common_pb.js +0 -37
  85. package/dist/generated/ottochain/v1/fiber_pb.d.ts +0 -292
  86. package/dist/generated/ottochain/v1/fiber_pb.js +0 -86
  87. package/dist/generated/ottochain/v1/messages_pb.d.ts +0 -190
  88. package/dist/generated/ottochain/v1/messages_pb.js +0 -44
  89. package/dist/generated/ottochain/v1/records_pb.d.ts +0 -221
  90. package/dist/generated/ottochain/v1/records_pb.js +0 -44
  91. package/dist/index.d.ts +0 -21
  92. package/dist/index.js +0 -77
  93. package/dist/metakit/binary.d.ts +0 -38
  94. package/dist/metakit/binary.js +0 -58
  95. package/dist/metakit/canonicalize.d.ts +0 -26
  96. package/dist/metakit/canonicalize.js +0 -40
  97. package/dist/metakit/codec.d.ts +0 -16
  98. package/dist/metakit/codec.js +0 -45
  99. package/dist/metakit/currency-transaction.d.ts +0 -157
  100. package/dist/metakit/currency-transaction.js +0 -319
  101. package/dist/metakit/currency-types.d.ts +0 -55
  102. package/dist/metakit/currency-types.js +0 -13
  103. package/dist/metakit/hash.d.ts +0 -50
  104. package/dist/metakit/hash.js +0 -84
  105. package/dist/metakit/index.d.ts +0 -23
  106. package/dist/metakit/index.js +0 -74
  107. package/dist/metakit/network/client.d.ts +0 -23
  108. package/dist/metakit/network/client.js +0 -78
  109. package/dist/metakit/network/currency-l1-client.d.ts +0 -71
  110. package/dist/metakit/network/currency-l1-client.js +0 -101
  111. package/dist/metakit/network/data-l1-client.d.ts +0 -57
  112. package/dist/metakit/network/data-l1-client.js +0 -76
  113. package/dist/metakit/network/index.d.ts +0 -10
  114. package/dist/metakit/network/index.js +0 -16
  115. package/dist/metakit/network/types.d.ts +0 -74
  116. package/dist/metakit/network/types.js +0 -20
  117. package/dist/metakit/sign.d.ts +0 -65
  118. package/dist/metakit/sign.js +0 -120
  119. package/dist/metakit/signed-object.d.ts +0 -66
  120. package/dist/metakit/signed-object.js +0 -100
  121. package/dist/metakit/types.d.ts +0 -67
  122. package/dist/metakit/types.js +0 -14
  123. package/dist/metakit/verify.d.ts +0 -55
  124. package/dist/metakit/verify.js +0 -217
  125. package/dist/metakit/wallet.d.ts +0 -70
  126. package/dist/metakit/wallet.js +0 -127
  127. package/dist/ottochain/index.d.ts +0 -13
  128. package/dist/ottochain/index.js +0 -45
  129. package/dist/ottochain/metagraph-client.d.ts +0 -111
  130. package/dist/ottochain/metagraph-client.js +0 -157
  131. package/dist/ottochain/snapshot.d.ts +0 -86
  132. package/dist/ottochain/snapshot.js +0 -110
  133. package/dist/ottochain/types.d.ts +0 -278
  134. package/dist/ottochain/types.js +0 -11
  135. package/dist/types/apps/contracts/types.d.ts +0 -24
  136. package/dist/types/apps/corporate/types.d.ts +0 -9861
  137. package/dist/types/apps/governance/types.d.ts +0 -344
  138. package/dist/types/apps/identity/types.d.ts +0 -30
  139. package/dist/types/apps/markets/types.d.ts +0 -155
  140. package/dist/types/apps/oracles/types.d.ts +0 -193
  141. package/dist/types/generated/ottochain/apps/contracts/v1/contract_pb.d.ts +0 -274
  142. package/dist/types/generated/ottochain/apps/corporate/v1/corporate_pb.d.ts +0 -1172
  143. package/dist/types/generated/ottochain/apps/governance/v1/governance_pb.d.ts +0 -772
  144. package/dist/types/generated/ottochain/apps/identity/v1/agent_pb.d.ts +0 -211
  145. package/dist/types/generated/ottochain/apps/identity/v1/attestation_pb.d.ts +0 -238
  146. package/dist/types/generated/ottochain/apps/markets/v1/market_pb.d.ts +0 -436
  147. package/dist/types/generated/ottochain/apps/oracles/v1/oracle_pb.d.ts +0 -393
  148. package/dist/types/generated/ottochain/v1/common_pb.d.ts +0 -86
  149. package/dist/types/generated/ottochain/v1/fiber_pb.d.ts +0 -292
  150. package/dist/types/generated/ottochain/v1/messages_pb.d.ts +0 -190
  151. package/dist/types/generated/ottochain/v1/records_pb.d.ts +0 -221
  152. package/dist/validation.d.ts +0 -449
  153. package/dist/validation.js +0 -312
@@ -1,42 +0,0 @@
1
- "use strict";
2
- /**
3
- * Governance & DAO Types
4
- *
5
- * TypeScript types matching the JSON Logic state machine definitions
6
- * in ottochain/docs/trust-graph/state-machines/dao/
7
- *
8
- * @packageDocumentation
9
- */
10
- Object.defineProperty(exports, "__esModule", { value: true });
11
- exports.isThresholdDAO = exports.isTokenDAO = exports.isMultisigDAO = exports.isSingleOwnerDAO = void 0;
12
- // =============================================================================
13
- // Helpers
14
- // =============================================================================
15
- /**
16
- * Type guard for SingleOwnerDAO
17
- */
18
- function isSingleOwnerDAO(state) {
19
- return state.schema === 'SingleOwnerDAO';
20
- }
21
- exports.isSingleOwnerDAO = isSingleOwnerDAO;
22
- /**
23
- * Type guard for MultisigDAO
24
- */
25
- function isMultisigDAO(state) {
26
- return state.schema === 'MultisigDAO';
27
- }
28
- exports.isMultisigDAO = isMultisigDAO;
29
- /**
30
- * Type guard for TokenDAO
31
- */
32
- function isTokenDAO(state) {
33
- return state.schema === 'TokenDAO';
34
- }
35
- exports.isTokenDAO = isTokenDAO;
36
- /**
37
- * Type guard for ThresholdDAO
38
- */
39
- function isThresholdDAO(state) {
40
- return state.schema === 'ThresholdDAO';
41
- }
42
- exports.isThresholdDAO = isThresholdDAO;
@@ -1,53 +0,0 @@
1
- "use strict";
2
- /**
3
- * Agent Identity Utilities
4
- *
5
- * Constants and utilities for the Agent Identity application.
6
- * Core types are generated from protobuf - see the generated exports.
7
- *
8
- * @packageDocumentation
9
- */
10
- Object.defineProperty(exports, "__esModule", { value: true });
11
- exports.ATTESTATION_DELTAS = exports.AGENT_TRANSITIONS = exports.DEFAULT_REPUTATION_CONFIG = void 0;
12
- const agent_pb_js_1 = require("../../generated/ottochain/apps/identity/v1/agent_pb.js");
13
- const attestation_pb_js_1 = require("../../generated/ottochain/apps/identity/v1/attestation_pb.js");
14
- // ---------------------------------------------------------------------------
15
- // Configuration Defaults
16
- // ---------------------------------------------------------------------------
17
- /**
18
- * Default reputation configuration for agent identity
19
- */
20
- exports.DEFAULT_REPUTATION_CONFIG = {
21
- baseReputation: 10,
22
- completionDelta: 5,
23
- vouchDelta: 2,
24
- violationDelta: -10,
25
- behavioralDelta: 3,
26
- minReputation: 0,
27
- challengeThreshold: 5,
28
- };
29
- // ---------------------------------------------------------------------------
30
- // State Machine Transitions
31
- // ---------------------------------------------------------------------------
32
- /**
33
- * Valid transitions for each agent state
34
- */
35
- exports.AGENT_TRANSITIONS = {
36
- [agent_pb_js_1.AgentState.UNSPECIFIED]: [],
37
- [agent_pb_js_1.AgentState.REGISTERED]: ['activate', 'withdraw'],
38
- [agent_pb_js_1.AgentState.ACTIVE]: ['challenge', 'withdraw'],
39
- [agent_pb_js_1.AgentState.CHALLENGED]: ['uphold_challenge', 'dismiss_challenge'],
40
- [agent_pb_js_1.AgentState.SUSPENDED]: ['begin_probation'],
41
- [agent_pb_js_1.AgentState.PROBATION]: ['complete_probation'],
42
- [agent_pb_js_1.AgentState.WITHDRAWN]: [], // Terminal state
43
- };
44
- /**
45
- * Reputation delta by attestation type
46
- */
47
- exports.ATTESTATION_DELTAS = {
48
- [attestation_pb_js_1.AttestationType.UNSPECIFIED]: 0,
49
- [attestation_pb_js_1.AttestationType.COMPLETION]: 5,
50
- [attestation_pb_js_1.AttestationType.VOUCH]: 2,
51
- [attestation_pb_js_1.AttestationType.VIOLATION]: -10,
52
- [attestation_pb_js_1.AttestationType.BEHAVIORAL]: 3,
53
- };
@@ -1,219 +0,0 @@
1
- "use strict";
2
- /**
3
- * Markets Application Types
4
- *
5
- * Constants, types, and utilities for the Markets application on OttoChain.
6
- *
7
- * Core types (MarketType, MarketState, Market, Commitment, Resolution) are
8
- * exported from proto-generated types in index.ts.
9
- *
10
- * @packageDocumentation
11
- */
12
- Object.defineProperty(exports, "__esModule", { value: true });
13
- exports.isMarketType = exports.isMarketState = exports.calculateGroupBuyDiscount = exports.calculateCrowdfundProgress = exports.validateCommitment = exports.calculateRefund = exports.calculatePayout = exports.calculateFees = exports.calculateNetCommitment = exports.isTerminalMarketState = exports.MARKET_TRANSITIONS = exports.MARKET_TYPE_CONFIGS = exports.DEFAULT_MARKET_CONFIG = exports.CommitmentSide = void 0;
14
- const market_pb_js_1 = require("../../generated/ottochain/apps/markets/v1/market_pb.js");
15
- /**
16
- * Commitment direction (for prediction markets)
17
- */
18
- var CommitmentSide;
19
- (function (CommitmentSide) {
20
- CommitmentSide[CommitmentSide["UNSPECIFIED"] = 0] = "UNSPECIFIED";
21
- CommitmentSide[CommitmentSide["YES"] = 1] = "YES";
22
- CommitmentSide[CommitmentSide["NO"] = 2] = "NO";
23
- })(CommitmentSide || (exports.CommitmentSide = CommitmentSide = {}));
24
- /**
25
- * Default market configuration
26
- */
27
- exports.DEFAULT_MARKET_CONFIG = {
28
- platformFeePercent: 0.01,
29
- creatorFeePercent: 0.02,
30
- oracleFeePercent: 0.01,
31
- minQuorum: 100n,
32
- resolutionWindowEpochs: 24,
33
- disputeWindowEpochs: 12,
34
- minCommitment: 1n,
35
- maxSlippagePercent: 0.05,
36
- };
37
- /**
38
- * Type-specific market configurations
39
- */
40
- exports.MARKET_TYPE_CONFIGS = {
41
- [market_pb_js_1.MarketType.UNSPECIFIED]: {},
42
- [market_pb_js_1.MarketType.PREDICTION]: {
43
- platformFeePercent: 0.02,
44
- oracleFeePercent: 0.02,
45
- },
46
- [market_pb_js_1.MarketType.AUCTION]: {
47
- platformFeePercent: 0.025,
48
- creatorFeePercent: 0,
49
- oracleFeePercent: 0,
50
- minQuorum: 1n,
51
- },
52
- [market_pb_js_1.MarketType.CROWDFUND]: {
53
- platformFeePercent: 0.03,
54
- creatorFeePercent: 0,
55
- oracleFeePercent: 0,
56
- disputeWindowEpochs: 0,
57
- },
58
- [market_pb_js_1.MarketType.GROUP_BUY]: {
59
- platformFeePercent: 0.015,
60
- creatorFeePercent: 0.01,
61
- oracleFeePercent: 0,
62
- },
63
- };
64
- // ---------------------------------------------------------------------------
65
- // State Machine Transitions
66
- // ---------------------------------------------------------------------------
67
- /**
68
- * Valid transitions for each market state (aligned with proto MarketState enum)
69
- */
70
- exports.MARKET_TRANSITIONS = {
71
- [market_pb_js_1.MarketState.UNSPECIFIED]: [],
72
- [market_pb_js_1.MarketState.PROPOSED]: ['open', 'cancel'],
73
- [market_pb_js_1.MarketState.OPEN]: ['close', 'cancel', 'commit'],
74
- [market_pb_js_1.MarketState.CLOSED]: ['submit_resolution', 'refund'],
75
- [market_pb_js_1.MarketState.RESOLVING]: ['submit_resolution', 'finalize', 'refund'],
76
- [market_pb_js_1.MarketState.SETTLED]: ['claim'], // Terminal (only claims allowed)
77
- [market_pb_js_1.MarketState.REFUNDED]: [], // Terminal state
78
- [market_pb_js_1.MarketState.CANCELLED]: [], // Terminal state
79
- };
80
- /**
81
- * Check if a market state is terminal
82
- */
83
- function isTerminalMarketState(state) {
84
- return [
85
- market_pb_js_1.MarketState.SETTLED,
86
- market_pb_js_1.MarketState.REFUNDED,
87
- market_pb_js_1.MarketState.CANCELLED,
88
- ].includes(state);
89
- }
90
- exports.isTerminalMarketState = isTerminalMarketState;
91
- // ---------------------------------------------------------------------------
92
- // Commitment Calculations
93
- // ---------------------------------------------------------------------------
94
- /**
95
- * Calculate effective commitment after fees
96
- *
97
- * @param amount - Raw commitment amount
98
- * @param config - Market configuration (uses defaults if not provided)
99
- * @returns Net commitment amount after platform fees
100
- */
101
- function calculateNetCommitment(amount, config = {}) {
102
- const feePercent = config.platformFeePercent ?? exports.DEFAULT_MARKET_CONFIG.platformFeePercent;
103
- const feeAmount = (amount * BigInt(Math.floor(feePercent * 10000))) / 10000n;
104
- return amount - feeAmount;
105
- }
106
- exports.calculateNetCommitment = calculateNetCommitment;
107
- /**
108
- * Calculate total fees for a commitment
109
- *
110
- * @param amount - Commitment amount
111
- * @param marketType - Type of market for type-specific fees
112
- * @returns Fee breakdown object
113
- */
114
- function calculateFees(amount, marketType = market_pb_js_1.MarketType.PREDICTION) {
115
- const typeConfig = { ...exports.DEFAULT_MARKET_CONFIG, ...exports.MARKET_TYPE_CONFIGS[marketType] };
116
- const platform = (amount * BigInt(Math.floor(typeConfig.platformFeePercent * 10000))) / 10000n;
117
- const creator = (amount * BigInt(Math.floor(typeConfig.creatorFeePercent * 10000))) / 10000n;
118
- const oracle = (amount * BigInt(Math.floor(typeConfig.oracleFeePercent * 10000))) / 10000n;
119
- return {
120
- platform,
121
- creator,
122
- oracle,
123
- total: platform + creator + oracle,
124
- };
125
- }
126
- exports.calculateFees = calculateFees;
127
- /**
128
- * Calculate payout for a winning commitment in a prediction market
129
- *
130
- * Winner receives: their original + proportional share of losing pool (minus fees)
131
- *
132
- * @param shares - Pool and commitment details
133
- * @param marketType - Type of market for fee calculation
134
- * @returns Payout amount
135
- */
136
- function calculatePayout(shares, marketType = market_pb_js_1.MarketType.PREDICTION) {
137
- if (shares.winningPool === 0n)
138
- return 0n;
139
- const fees = calculateFees(shares.losingPool, marketType);
140
- const distributablePool = shares.losingPool - fees.total;
141
- // Proportional share of losing pool
142
- const winnings = (distributablePool * shares.userCommitment) / shares.winningPool;
143
- // Return original commitment + winnings
144
- return shares.userCommitment + winnings;
145
- }
146
- exports.calculatePayout = calculatePayout;
147
- /**
148
- * Calculate refund for cancelled market
149
- *
150
- * @param commitment - Original commitment amount
151
- * @param refundFeePercent - Optional fee retained on refund (default 0)
152
- * @returns Refund amount
153
- */
154
- function calculateRefund(commitment, refundFeePercent = 0) {
155
- const fee = (commitment * BigInt(Math.floor(refundFeePercent * 10000))) / 10000n;
156
- return commitment - fee;
157
- }
158
- exports.calculateRefund = calculateRefund;
159
- /**
160
- * Validate a commitment against market rules
161
- */
162
- function validateCommitment(amount, marketState, config = {}) {
163
- const minCommitment = config.minCommitment ?? exports.DEFAULT_MARKET_CONFIG.minCommitment;
164
- if (marketState !== market_pb_js_1.MarketState.OPEN) {
165
- return { valid: false, reason: 'Market is not open for commitments' };
166
- }
167
- if (amount < minCommitment) {
168
- return { valid: false, reason: `Commitment below minimum: ${minCommitment}` };
169
- }
170
- return { valid: true };
171
- }
172
- exports.validateCommitment = validateCommitment;
173
- /**
174
- * Calculate crowdfund campaign progress
175
- */
176
- function calculateCrowdfundProgress(current, goal) {
177
- if (goal === 0n) {
178
- return { current, goal, percentComplete: 0, goalReached: false };
179
- }
180
- const percentComplete = Number((current * 10000n) / goal) / 100;
181
- return {
182
- current,
183
- goal,
184
- percentComplete: Math.min(percentComplete, 100),
185
- goalReached: current >= goal,
186
- };
187
- }
188
- exports.calculateCrowdfundProgress = calculateCrowdfundProgress;
189
- /**
190
- * Calculate applicable discount based on participant count
191
- */
192
- function calculateGroupBuyDiscount(participantCount, tiers) {
193
- // Sort tiers descending by minParticipants
194
- const sortedTiers = [...tiers].sort((a, b) => b.minParticipants - a.minParticipants);
195
- for (const tier of sortedTiers) {
196
- if (participantCount >= tier.minParticipants) {
197
- return tier.discountPercent;
198
- }
199
- }
200
- return 0;
201
- }
202
- exports.calculateGroupBuyDiscount = calculateGroupBuyDiscount;
203
- // ---------------------------------------------------------------------------
204
- // Type Guards
205
- // ---------------------------------------------------------------------------
206
- /**
207
- * Check if a value is a valid MarketState
208
- */
209
- function isMarketState(value) {
210
- return typeof value === 'number' && value in market_pb_js_1.MarketState;
211
- }
212
- exports.isMarketState = isMarketState;
213
- /**
214
- * Check if a value is a valid MarketType
215
- */
216
- function isMarketType(value) {
217
- return typeof value === 'number' && value in market_pb_js_1.MarketType;
218
- }
219
- exports.isMarketType = isMarketType;
@@ -1,282 +0,0 @@
1
- "use strict";
2
- /**
3
- * Oracles Application Types
4
- *
5
- * Constants, types, and utilities for the Oracle system on OttoChain.
6
- * Oracles provide truth resolution for markets and disputes.
7
- *
8
- * Core types (OracleState, Oracle, etc.) are exported from proto-generated
9
- * types in index.ts.
10
- *
11
- * @packageDocumentation
12
- */
13
- Object.defineProperty(exports, "__esModule", { value: true });
14
- exports.isResolutionType = exports.isSlashingReason = exports.isOracleState = exports.calculateOracleReward = exports.calculateSelectionScore = exports.calculateStakeAfterSlash = exports.calculateSlashAmount = exports.SLASHING_CONDITIONS = exports.qualifiesForHighValue = exports.calculateWeightedReputation = exports.calculateReputation = exports.REPUTATION_DELTAS = exports.canAcceptAssignment = exports.isTerminalOracleState = exports.ORACLE_TRANSITIONS = exports.SLASHING_PERCENTAGES = exports.DEFAULT_ORACLE_CONFIG = exports.SlashingReason = exports.ResolutionType = void 0;
15
- const oracle_pb_js_1 = require("../../generated/ottochain/apps/oracles/v1/oracle_pb.js");
16
- /**
17
- * Types of oracle resolutions
18
- */
19
- var ResolutionType;
20
- (function (ResolutionType) {
21
- ResolutionType[ResolutionType["UNSPECIFIED"] = 0] = "UNSPECIFIED";
22
- /** Binary yes/no outcome */
23
- ResolutionType[ResolutionType["BINARY"] = 1] = "BINARY";
24
- /** One of multiple predefined outcomes */
25
- ResolutionType[ResolutionType["MULTI_CHOICE"] = 2] = "MULTI_CHOICE";
26
- /** Numeric value (e.g., price) */
27
- ResolutionType[ResolutionType["NUMERIC"] = 3] = "NUMERIC";
28
- /** Free-form attestation */
29
- ResolutionType[ResolutionType["ATTESTATION"] = 4] = "ATTESTATION";
30
- })(ResolutionType || (exports.ResolutionType = ResolutionType = {}));
31
- /**
32
- * Reasons for slashing oracle stake
33
- */
34
- var SlashingReason;
35
- (function (SlashingReason) {
36
- SlashingReason[SlashingReason["UNSPECIFIED"] = 0] = "UNSPECIFIED";
37
- /** Failed to submit resolution in time */
38
- SlashingReason[SlashingReason["TIMEOUT"] = 1] = "TIMEOUT";
39
- /** Resolution overturned by dispute */
40
- SlashingReason[SlashingReason["INCORRECT_RESOLUTION"] = 2] = "INCORRECT_RESOLUTION";
41
- /** Evidence of collusion or manipulation */
42
- SlashingReason[SlashingReason["COLLUSION"] = 3] = "COLLUSION";
43
- /** Violation of oracle protocol */
44
- SlashingReason[SlashingReason["PROTOCOL_VIOLATION"] = 4] = "PROTOCOL_VIOLATION";
45
- })(SlashingReason || (exports.SlashingReason = SlashingReason = {}));
46
- /**
47
- * Default oracle configuration
48
- */
49
- exports.DEFAULT_ORACLE_CONFIG = {
50
- minStake: 10000n,
51
- timeoutSlashPercent: 0.05,
52
- incorrectSlashPercent: 0.25,
53
- collusionSlashPercent: 1.0,
54
- violationSlashPercent: 0.10,
55
- cooldownEpochs: 48,
56
- baseReputation: 50,
57
- highValueThreshold: 100,
58
- resolutionWindowEpochs: 12,
59
- challengeWindowEpochs: 6,
60
- oracleRewardPercent: 0.01,
61
- };
62
- /**
63
- * Slashing percentages by reason
64
- */
65
- exports.SLASHING_PERCENTAGES = {
66
- [SlashingReason.UNSPECIFIED]: 0,
67
- [SlashingReason.TIMEOUT]: exports.DEFAULT_ORACLE_CONFIG.timeoutSlashPercent,
68
- [SlashingReason.INCORRECT_RESOLUTION]: exports.DEFAULT_ORACLE_CONFIG.incorrectSlashPercent,
69
- [SlashingReason.COLLUSION]: exports.DEFAULT_ORACLE_CONFIG.collusionSlashPercent,
70
- [SlashingReason.PROTOCOL_VIOLATION]: exports.DEFAULT_ORACLE_CONFIG.violationSlashPercent,
71
- };
72
- // ---------------------------------------------------------------------------
73
- // State Machine Transitions
74
- // ---------------------------------------------------------------------------
75
- /**
76
- * Valid transitions for each oracle state (aligned with proto OracleState enum)
77
- */
78
- exports.ORACLE_TRANSITIONS = {
79
- [oracle_pb_js_1.OracleState.UNSPECIFIED]: [],
80
- [oracle_pb_js_1.OracleState.UNREGISTERED]: ['register'],
81
- [oracle_pb_js_1.OracleState.REGISTERED]: ['activate', 'withdraw'],
82
- [oracle_pb_js_1.OracleState.ACTIVE]: ['add_stake', 'record_resolution', 'slash', 'withdraw'],
83
- [oracle_pb_js_1.OracleState.SLASHED]: ['reactivate', 'withdraw'],
84
- [oracle_pb_js_1.OracleState.WITHDRAWN]: [], // Terminal state
85
- };
86
- /**
87
- * Check if an oracle state is terminal
88
- */
89
- function isTerminalOracleState(state) {
90
- return state === oracle_pb_js_1.OracleState.WITHDRAWN;
91
- }
92
- exports.isTerminalOracleState = isTerminalOracleState;
93
- /**
94
- * Check if an oracle can accept new assignments
95
- */
96
- function canAcceptAssignment(state) {
97
- return state === oracle_pb_js_1.OracleState.ACTIVE;
98
- }
99
- exports.canAcceptAssignment = canAcceptAssignment;
100
- // ---------------------------------------------------------------------------
101
- // Reputation Calculations
102
- // ---------------------------------------------------------------------------
103
- /**
104
- * Reputation update factors
105
- */
106
- exports.REPUTATION_DELTAS = {
107
- /** Successfully resolved market without challenge */
108
- successfulResolution: 5,
109
- /** Resolution upheld after challenge */
110
- upheldChallenge: 10,
111
- /** Resolution overturned (negative) */
112
- overturnedResolution: -25,
113
- /** Timeout on assigned market (negative) */
114
- timeout: -15,
115
- /** Collusion detected (negative) */
116
- collusion: -100,
117
- /** Protocol violation (negative) */
118
- violation: -20,
119
- /** Bonus for high-value market resolution */
120
- highValueBonus: 3,
121
- };
122
- /**
123
- * Calculate new reputation after an event
124
- *
125
- * @param currentReputation - Current reputation score
126
- * @param delta - Reputation change (positive or negative)
127
- * @param minReputation - Minimum reputation floor (default 0)
128
- * @returns New reputation score
129
- */
130
- function calculateReputation(currentReputation, delta, minReputation = 0) {
131
- return Math.max(minReputation, currentReputation + delta);
132
- }
133
- exports.calculateReputation = calculateReputation;
134
- /**
135
- * Calculate weighted reputation considering history
136
- *
137
- * @param baseReputation - Current base reputation
138
- * @param successCount - Number of successful resolutions
139
- * @param failureCount - Number of failed/overturned resolutions
140
- * @returns Weighted reputation score
141
- */
142
- function calculateWeightedReputation(baseReputation, successCount, failureCount) {
143
- const totalAttempts = successCount + failureCount;
144
- if (totalAttempts === 0)
145
- return baseReputation;
146
- const successRate = successCount / totalAttempts;
147
- const experienceMultiplier = Math.min(1 + Math.log10(totalAttempts + 1) * 0.2, 1.5);
148
- return Math.round(baseReputation * successRate * experienceMultiplier);
149
- }
150
- exports.calculateWeightedReputation = calculateWeightedReputation;
151
- /**
152
- * Check if oracle qualifies for high-value markets
153
- */
154
- function qualifiesForHighValue(reputation, stake, threshold = exports.DEFAULT_ORACLE_CONFIG.highValueThreshold, minStakeMultiplier = 5) {
155
- return (reputation >= threshold &&
156
- stake >= exports.DEFAULT_ORACLE_CONFIG.minStake * BigInt(minStakeMultiplier));
157
- }
158
- exports.qualifiesForHighValue = qualifiesForHighValue;
159
- /**
160
- * Predefined slashing conditions
161
- */
162
- exports.SLASHING_CONDITIONS = {
163
- [SlashingReason.UNSPECIFIED]: {
164
- reason: SlashingReason.UNSPECIFIED,
165
- slashPercent: 0,
166
- description: 'No slashing',
167
- appealable: false,
168
- suspensionEpochs: 0,
169
- },
170
- [SlashingReason.TIMEOUT]: {
171
- reason: SlashingReason.TIMEOUT,
172
- slashPercent: exports.DEFAULT_ORACLE_CONFIG.timeoutSlashPercent,
173
- description: 'Failed to submit resolution within deadline',
174
- appealable: true,
175
- suspensionEpochs: 12,
176
- },
177
- [SlashingReason.INCORRECT_RESOLUTION]: {
178
- reason: SlashingReason.INCORRECT_RESOLUTION,
179
- slashPercent: exports.DEFAULT_ORACLE_CONFIG.incorrectSlashPercent,
180
- description: 'Resolution overturned by dispute process',
181
- appealable: true,
182
- suspensionEpochs: 48,
183
- },
184
- [SlashingReason.COLLUSION]: {
185
- reason: SlashingReason.COLLUSION,
186
- slashPercent: exports.DEFAULT_ORACLE_CONFIG.collusionSlashPercent,
187
- description: 'Evidence of collusion or market manipulation',
188
- appealable: false,
189
- suspensionEpochs: -1, // Permanent
190
- },
191
- [SlashingReason.PROTOCOL_VIOLATION]: {
192
- reason: SlashingReason.PROTOCOL_VIOLATION,
193
- slashPercent: exports.DEFAULT_ORACLE_CONFIG.violationSlashPercent,
194
- description: 'Violation of oracle operating protocol',
195
- appealable: true,
196
- suspensionEpochs: 24,
197
- },
198
- };
199
- /**
200
- * Calculate slash amount for a given stake and reason
201
- *
202
- * @param stake - Oracle's current stake
203
- * @param reason - Reason for slashing
204
- * @returns Amount to be slashed
205
- */
206
- function calculateSlashAmount(stake, reason) {
207
- const condition = exports.SLASHING_CONDITIONS[reason];
208
- return (stake * BigInt(Math.floor(condition.slashPercent * 10000))) / 10000n;
209
- }
210
- exports.calculateSlashAmount = calculateSlashAmount;
211
- /**
212
- * Calculate remaining stake after slashing
213
- */
214
- function calculateStakeAfterSlash(stake, reason) {
215
- return stake - calculateSlashAmount(stake, reason);
216
- }
217
- exports.calculateStakeAfterSlash = calculateStakeAfterSlash;
218
- /**
219
- * Calculate oracle selection score
220
- * Higher score = more likely to be selected
221
- *
222
- * @param candidate - Oracle candidate details
223
- * @param marketValue - Value of market to resolve (affects weight for high-value markets)
224
- * @returns Selection score
225
- */
226
- function calculateSelectionScore(candidate, marketValue = 0n) {
227
- const reputationWeight = 0.4;
228
- const stakeWeight = 0.3;
229
- const successWeight = 0.2;
230
- const loadWeight = 0.1;
231
- // Normalize stake to 0-100 range (assuming 1M max)
232
- const normalizedStake = Math.min(Number(candidate.stake / 10000n), 100);
233
- // Load penalty (fewer active = better)
234
- const loadScore = Math.max(0, 100 - candidate.activeAssignments * 20);
235
- // High-value market bonus for qualified oracles
236
- const highValueBonus = marketValue > 100000n &&
237
- qualifiesForHighValue(candidate.reputation, candidate.stake) ? 10 : 0;
238
- return (candidate.reputation * reputationWeight +
239
- normalizedStake * stakeWeight +
240
- candidate.successRate * 100 * successWeight +
241
- loadScore * loadWeight +
242
- highValueBonus);
243
- }
244
- exports.calculateSelectionScore = calculateSelectionScore;
245
- // ---------------------------------------------------------------------------
246
- // Reward Calculations
247
- // ---------------------------------------------------------------------------
248
- /**
249
- * Calculate oracle reward for successful resolution
250
- *
251
- * @param marketFees - Total fees collected from market
252
- * @param rewardPercent - Oracle's reward percentage (default from config)
253
- * @returns Oracle reward amount
254
- */
255
- function calculateOracleReward(marketFees, rewardPercent = exports.DEFAULT_ORACLE_CONFIG.oracleRewardPercent) {
256
- return (marketFees * BigInt(Math.floor(rewardPercent * 10000))) / 10000n;
257
- }
258
- exports.calculateOracleReward = calculateOracleReward;
259
- // ---------------------------------------------------------------------------
260
- // Type Guards
261
- // ---------------------------------------------------------------------------
262
- /**
263
- * Check if a value is a valid OracleState
264
- */
265
- function isOracleState(value) {
266
- return typeof value === 'number' && value in oracle_pb_js_1.OracleState;
267
- }
268
- exports.isOracleState = isOracleState;
269
- /**
270
- * Check if a value is a valid SlashingReason
271
- */
272
- function isSlashingReason(value) {
273
- return typeof value === 'number' && value in SlashingReason;
274
- }
275
- exports.isSlashingReason = isSlashingReason;
276
- /**
277
- * Check if a value is a valid ResolutionType
278
- */
279
- function isResolutionType(value) {
280
- return typeof value === 'number' && value in ResolutionType;
281
- }
282
- exports.isResolutionType = isResolutionType;