orbis1-sdk-rn 0.2.1 → 0.2.3

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 (51) hide show
  1. package/README.md +6 -9
  2. package/android/src/main/java/com/orbis1sdk/RgbModule.kt +47 -8
  3. package/ios/Orbis1Sdk.mm +3 -1
  4. package/ios/Rgb.mm +3 -1
  5. package/ios/Rgb.swift +86 -10
  6. package/lib/module/core/KeyManager.js +3 -3
  7. package/lib/module/core/KeyManager.js.map +1 -1
  8. package/lib/module/core/NativeRgb.js.map +1 -1
  9. package/lib/module/features/gas-free/GasFreeModule.js +6 -5
  10. package/lib/module/features/gas-free/GasFreeModule.js.map +1 -1
  11. package/lib/module/features/gas-free/index.js +3 -0
  12. package/lib/module/features/gas-free/index.js.map +1 -1
  13. package/lib/module/features/gas-free/types/FeeQuote.js.map +1 -1
  14. package/lib/module/features/gas-free/utils/FeeCalculator.js +218 -0
  15. package/lib/module/features/gas-free/utils/FeeCalculator.js.map +1 -0
  16. package/lib/module/features/gas-free/utils/index.js +3 -0
  17. package/lib/module/features/gas-free/utils/index.js.map +1 -1
  18. package/lib/module/features/gas-free/utils/validation.js +6 -5
  19. package/lib/module/features/gas-free/utils/validation.js.map +1 -1
  20. package/lib/module/index.js +1 -0
  21. package/lib/module/index.js.map +1 -1
  22. package/lib/typescript/src/core/KeyManager.d.ts +2 -2
  23. package/lib/typescript/src/core/KeyManager.d.ts.map +1 -1
  24. package/lib/typescript/src/core/NativeRgb.d.ts +1 -1
  25. package/lib/typescript/src/core/NativeRgb.d.ts.map +1 -1
  26. package/lib/typescript/src/features/gas-free/GasFreeModule.d.ts.map +1 -1
  27. package/lib/typescript/src/features/gas-free/index.d.ts +1 -0
  28. package/lib/typescript/src/features/gas-free/index.d.ts.map +1 -1
  29. package/lib/typescript/src/features/gas-free/types/FeeQuote.d.ts +4 -2
  30. package/lib/typescript/src/features/gas-free/types/FeeQuote.d.ts.map +1 -1
  31. package/lib/typescript/src/features/gas-free/types/GasFreeRequest.d.ts +2 -2
  32. package/lib/typescript/src/features/gas-free/types/GasFreeRequest.d.ts.map +1 -1
  33. package/lib/typescript/src/features/gas-free/utils/FeeCalculator.d.ts +137 -0
  34. package/lib/typescript/src/features/gas-free/utils/FeeCalculator.d.ts.map +1 -0
  35. package/lib/typescript/src/features/gas-free/utils/index.d.ts +1 -0
  36. package/lib/typescript/src/features/gas-free/utils/index.d.ts.map +1 -1
  37. package/lib/typescript/src/features/gas-free/utils/validation.d.ts +6 -3
  38. package/lib/typescript/src/features/gas-free/utils/validation.d.ts.map +1 -1
  39. package/lib/typescript/src/index.d.ts +1 -0
  40. package/lib/typescript/src/index.d.ts.map +1 -1
  41. package/package.json +1 -1
  42. package/src/core/KeyManager.ts +4 -3
  43. package/src/core/NativeRgb.ts +5 -1
  44. package/src/features/gas-free/GasFreeModule.ts +5 -4
  45. package/src/features/gas-free/index.ts +3 -0
  46. package/src/features/gas-free/types/FeeQuote.ts +4 -2
  47. package/src/features/gas-free/types/GasFreeRequest.ts +2 -2
  48. package/src/features/gas-free/utils/FeeCalculator.ts +229 -0
  49. package/src/features/gas-free/utils/index.ts +3 -0
  50. package/src/features/gas-free/utils/validation.ts +14 -4
  51. package/src/index.ts +1 -0
@@ -0,0 +1,229 @@
1
+ /**
2
+ * Fee Calculator
3
+ *
4
+ * Provides local fee calculation for gas-free transfers.
5
+ * Calculates service fees based on transfer amounts using a simple percentage-based model.
6
+ *
7
+ * Fee Structure:
8
+ * - Base rate: 0.1% (0.001)
9
+ * - Minimum fee: $0.50 (floor dominates until $500)
10
+ * - Maximum fee: $10.00 (cap applies above $10,000)
11
+ *
12
+ * Assumptions:
13
+ * - 1 asset unit (no precision) = 1 USD (will be fetched dynamically in the future)
14
+ * - Default precision: 6 decimals
15
+ *
16
+ * Examples:
17
+ * - Transfer $100: $0.50 fee (minimum applies)
18
+ * - Transfer $500: $0.50 fee (0.1% × $500 = $0.50 exactly)
19
+ * - Transfer $1,000: $1.00 fee (0.1% applies)
20
+ * - Transfer $10,000: $10.00 fee (0.1% × $10,000 = $10.00, cap reached)
21
+ * - Transfer $50,000: $10.00 fee (maximum cap applies)
22
+ *
23
+ * @module features/gas-free/utils/FeeCalculator
24
+ */
25
+
26
+ /**
27
+ * Fee calculator for gas-free transfers
28
+ *
29
+ * Uses a simple percentage-based pricing model with floor and cap.
30
+ */
31
+ export class FeeCalculator {
32
+ private readonly assetToUsdRate: number = 1; // 1 asset unit (no precision) = 1 USD
33
+ private readonly assetPrecision: number = 6; // Default precision
34
+
35
+ // Fee calculation parameters
36
+ private readonly FEE_RATE = 0.001; // 0.1%
37
+ private readonly MIN_FEE = 0.5; // $0.50 minimum (floor)
38
+ private readonly MAX_FEE = 10.0; // $10.00 maximum (cap)
39
+
40
+ /**
41
+ * Create a fee estimator with default parameters
42
+ */
43
+ constructor() {
44
+ // No configuration needed - fixed parameters
45
+ }
46
+
47
+ /**
48
+ * Calculate service fee in USD based on transfer amount
49
+ *
50
+ * Uses a simple percentage-based model with floor and cap:
51
+ * - 0.1% of transfer amount
52
+ * - Minimum: $0.50 (applies for amounts < $500)
53
+ * - Maximum: $10.00 (applies for amounts > $10,000)
54
+ *
55
+ * @param amountUsd - Transfer amount in USD
56
+ * @returns Fee in USD
57
+ * @throws Error if amount is invalid
58
+ *
59
+ * @example
60
+ * ```typescript
61
+ * const calculator = new FeeCalculator();
62
+ * console.log(calculator.calculateFee(100)); // $0.50 (minimum)
63
+ * console.log(calculator.calculateFee(500)); // $0.50 (0.1% × $500)
64
+ * console.log(calculator.calculateFee(1000)); // $1.00 (0.1% × $1000)
65
+ * console.log(calculator.calculateFee(10000)); // $10.00 (0.1% × $10000, cap reached)
66
+ * console.log(calculator.calculateFee(50000)); // $10.00 (maximum cap)
67
+ * ```
68
+ */
69
+ public calculateFee(amountUsd: number): number {
70
+ if (amountUsd < 0) {
71
+ throw new Error('Amount cannot be negative');
72
+ }
73
+
74
+ if (!Number.isFinite(amountUsd)) {
75
+ throw new Error('Amount must be a valid number');
76
+ }
77
+
78
+ // Calculate percentage-based fee
79
+ const percentageFee = amountUsd * this.FEE_RATE;
80
+
81
+ // Apply floor (minimum) and cap (maximum)
82
+ const feeUsd = Math.max(
83
+ this.MIN_FEE,
84
+ Math.min(this.MAX_FEE, percentageFee)
85
+ );
86
+
87
+ return feeUsd;
88
+ }
89
+
90
+ /**
91
+ * Estimate service fee in RGB asset base units
92
+ *
93
+ * This method uses hardcoded parameters (1:1 USD rate, precision 6) to estimate fees.
94
+ *
95
+ * Steps:
96
+ * 1. Converts transfer amount from base units to asset units
97
+ * 2. Converts asset units to USD (1:1 rate)
98
+ * 3. Calculates fee in USD (0.1% with $0.50 min, $10 max)
99
+ * 4. Converts fee back to asset base units
100
+ *
101
+ * @param transferAmount - Amount being transferred in base units (e.g., 100250000 for 100.25 TUSDT with precision 6)
102
+ * @param assetToUsdRate - Conversion rate from asset to USD (optional, defaults to 1)
103
+ * @param assetPrecision - Asset precision exponent (optional, defaults to 6)
104
+ * @returns Object with fee in asset base units, USD, and effective percentage
105
+ * @throws Error if inputs are invalid
106
+ *
107
+ * @example
108
+ * ```typescript
109
+ * const calculator = new FeeCalculator();
110
+ *
111
+ * // Transfer 100 TUSDT (100000000 base units with precision 6)
112
+ * const result1 = calculator.estimateFeeInAsset(100000000, 1, 6);
113
+ * console.log(`Fee: ${result1.feeInAsset} base units`); // 500000 ($0.50 minimum)
114
+ * console.log(`Fee: $${result1.feeUsd}`); // $0.50
115
+ *
116
+ * // Transfer 1,000 TUSDT (1000000000 base units)
117
+ * const result2 = calculator.estimateFeeInAsset(1000000000, 1, 6);
118
+ * console.log(`Fee: ${result2.feeInAsset} base units`); // 1000000 ($1.00)
119
+ * console.log(`Fee: $${result2.feeUsd}`); // $1.00
120
+ * console.log(`Rate: ${result2.effectivePercentage}%`); // 0.1%
121
+ * ```
122
+ */
123
+ public estimateFeeInAsset(
124
+ transferAmount: number,
125
+ assetToUsdRate?: number,
126
+ assetPrecision?: number
127
+ ): { feeInAsset: number; feeUsd: number; effectivePercentage: number } {
128
+ // Use provided values or defaults
129
+ const rate = assetToUsdRate ?? this.assetToUsdRate;
130
+ const precision = assetPrecision ?? this.assetPrecision;
131
+
132
+ // Validate inputs
133
+ if (transferAmount < 0) {
134
+ throw new Error('Transfer amount cannot be negative');
135
+ }
136
+
137
+ if (!Number.isFinite(transferAmount)) {
138
+ throw new Error('Transfer amount must be a valid number');
139
+ }
140
+
141
+ if (rate <= 0) {
142
+ throw new Error('Asset to USD rate must be positive');
143
+ }
144
+
145
+ if (!Number.isFinite(rate)) {
146
+ throw new Error('Asset to USD rate must be a valid number');
147
+ }
148
+
149
+ if (precision <= 0 || !Number.isInteger(precision)) {
150
+ throw new Error('Asset precision must be a positive integer');
151
+ }
152
+
153
+ // Step 1: Convert transfer amount from base units to asset units
154
+ const transferAmountInAssetUnits = transferAmount / Math.pow(10, precision);
155
+
156
+ // Step 2: Convert to USD
157
+ const transferAmountUsd = transferAmountInAssetUnits * rate;
158
+
159
+ // Step 3: Calculate fee in USD using amount-based formula
160
+ const feeUsd = this.calculateFee(transferAmountUsd);
161
+
162
+ // Step 4: Convert USD fee to asset base units
163
+ // USD → asset units → base units (with precision)
164
+ const feeInAssetUnits = feeUsd / rate;
165
+ const feeInAsset = Math.round(feeInAssetUnits * Math.pow(10, precision));
166
+
167
+ // Calculate effective percentage for tracking
168
+ const effectivePercentage = (feeUsd / transferAmountUsd) * 100;
169
+
170
+ return {
171
+ feeInAsset,
172
+ feeUsd,
173
+ effectivePercentage,
174
+ };
175
+ }
176
+
177
+ /**
178
+ * Get the current asset to USD exchange rate
179
+ */
180
+ public getAssetToUsdRate(): number {
181
+ return this.assetToUsdRate;
182
+ }
183
+
184
+ /**
185
+ * Get the current asset precision
186
+ */
187
+ public getAssetPrecision(): number {
188
+ return this.assetPrecision;
189
+ }
190
+
191
+ /**
192
+ * Get the fee rate (0.1%)
193
+ */
194
+ public getFeeRate(): number {
195
+ return this.FEE_RATE;
196
+ }
197
+
198
+ /**
199
+ * Get the minimum fee ($0.50)
200
+ */
201
+ public getMinFee(): number {
202
+ return this.MIN_FEE;
203
+ }
204
+
205
+ /**
206
+ * Get the maximum fee ($10.00)
207
+ */
208
+ public getMaxFee(): number {
209
+ return this.MAX_FEE;
210
+ }
211
+ }
212
+
213
+ /**
214
+ * Create a default fee calculator instance
215
+ *
216
+ * @returns FeeCalculator instance
217
+ *
218
+ * @example
219
+ * ```typescript
220
+ * import { createFeeCalculator } from './FeeCalculator';
221
+ *
222
+ * const calculator = createFeeCalculator();
223
+ * const fee = calculator.estimateFeeInAsset(100000000); // 100 TUSDT
224
+ * console.log(`Fee: $${fee.feeUsd}`); // $0.50 (minimum applies)
225
+ * ```
226
+ */
227
+ export function createFeeCalculator(): FeeCalculator {
228
+ return new FeeCalculator();
229
+ }
@@ -35,3 +35,6 @@ export {
35
35
  retryWithBackoff,
36
36
  withRetry,
37
37
  } from './retry';
38
+
39
+ // Fee Calculation
40
+ export { FeeCalculator, createFeeCalculator } from './FeeCalculator';
@@ -85,17 +85,27 @@ export const FeeQuoteSchema = z.object({
85
85
  * Schema for Fee Quote Request
86
86
  */
87
87
  export const FeeQuoteRequestSchema = z.object({
88
- userId: z.string().min(1, 'User ID must not be empty'),
89
- assetId: z.string().optional(),
90
- numInputs: z.number().int().positive('Number of inputs must be positive'),
88
+ userId: z.string().min(1, 'User ID cannot be empty'),
89
+ assetId: z
90
+ .string()
91
+ .min(1, 'Asset ID cannot be empty')
92
+ .regex(/^rgb:/, 'Asset ID must start with rgb: prefix'),
93
+ numInputs: z
94
+ .number()
95
+ .int()
96
+ .positive('Number of inputs must be a positive integer'),
91
97
  numOutputs: z
92
98
  .number()
93
99
  .int()
94
- .min(2, 'Number of outputs must be at least 2 (includes OP_RETURN)'),
100
+ .positive('Number of outputs must be a positive integer'),
95
101
  recipientInvoice: z
96
102
  .string()
97
103
  .min(1, 'Recipient invoice cannot be empty')
98
104
  .regex(/^rgb1?:/, 'Recipient invoice must start with rgb: or rgb1:'),
105
+ transferAmount: z
106
+ .number()
107
+ .positive('Transfer amount must be positive')
108
+ .finite('Transfer amount must be finite'),
99
109
  }) satisfies z.ZodType<FeeQuoteRequest>;
100
110
 
101
111
  /**
package/src/index.ts CHANGED
@@ -42,6 +42,7 @@ export type {
42
42
 
43
43
  // Gas-Free feature
44
44
  export { GasFreeModule, createGasFreeModule } from './features/gas-free';
45
+ export { FeeCalculator, createFeeCalculator } from './features/gas-free';
45
46
  export type {
46
47
  GasFreeConfig,
47
48
  GasFreeTransferRequest,