react-native-candle 0.1.20 → 0.1.22

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 (35) hide show
  1. package/ReactNativeCandle.podspec +1 -1
  2. package/ios/Sources/CandleActionViewModel.swift +13 -0
  3. package/ios/Sources/CandleTradeExecutionSheetWrapper.swift +31 -0
  4. package/ios/Sources/HostingViewController.swift +7 -0
  5. package/ios/Sources/RNCandle.swift +166 -39
  6. package/lib/commonjs/index.js +77 -13
  7. package/lib/commonjs/index.js.map +1 -1
  8. package/lib/module/index.js +77 -13
  9. package/lib/module/index.js.map +1 -1
  10. package/lib/typescript/commonjs/src/index.d.ts +21 -7
  11. package/lib/typescript/commonjs/src/index.d.ts.map +1 -1
  12. package/lib/typescript/commonjs/src/specs/RNCandle.nitro.d.ts +6 -1
  13. package/lib/typescript/commonjs/src/specs/RNCandle.nitro.d.ts.map +1 -1
  14. package/lib/typescript/module/src/index.d.ts +21 -7
  15. package/lib/typescript/module/src/index.d.ts.map +1 -1
  16. package/lib/typescript/module/src/specs/RNCandle.nitro.d.ts +6 -1
  17. package/lib/typescript/module/src/specs/RNCandle.nitro.d.ts.map +1 -1
  18. package/nitrogen/generated/ios/ReactNativeCandle-Swift-Cxx-Bridge.cpp +8 -0
  19. package/nitrogen/generated/ios/ReactNativeCandle-Swift-Cxx-Bridge.hpp +115 -81
  20. package/nitrogen/generated/ios/ReactNativeCandle-Swift-Cxx-Umbrella.hpp +3 -3
  21. package/nitrogen/generated/ios/c++/HybridRNCandleSpecSwift.hpp +57 -59
  22. package/nitrogen/generated/ios/swift/Func_void_TradeExecutionResult.swift +46 -0
  23. package/nitrogen/generated/ios/swift/HybridRNCandleSpec.swift +3 -1
  24. package/nitrogen/generated/ios/swift/HybridRNCandleSpec_cxx.swift +23 -21
  25. package/nitrogen/generated/ios/swift/TradeExecutionResult.swift +82 -0
  26. package/nitrogen/generated/ios/swift/TradeQuote.swift +13 -2
  27. package/nitrogen/generated/shared/c++/HybridRNCandleSpec.cpp +1 -1
  28. package/nitrogen/generated/shared/c++/HybridRNCandleSpec.hpp +7 -7
  29. package/nitrogen/generated/shared/c++/TradeExecutionResult.hpp +76 -0
  30. package/nitrogen/generated/shared/c++/TradeQuote.hpp +7 -2
  31. package/package.json +1 -1
  32. package/src/index.ts +88 -20
  33. package/src/specs/RNCandle.nitro.ts +11 -1
  34. package/nitrogen/generated/ios/swift/ExecuteTradeRequest.swift +0 -44
  35. package/nitrogen/generated/shared/c++/ExecuteTradeRequest.hpp +0 -73
package/src/index.ts CHANGED
@@ -18,6 +18,7 @@ import type {
18
18
  TradeAsset as InternalTradeAsset,
19
19
  TradeAssetQuoteRequest,
20
20
  TradeQuery as InternalTradeQuery,
21
+ TradeQuote as InternalTradeQuote,
21
22
  TransportAsset,
22
23
  TransportAssetQuoteRequest,
23
24
  LegalAccountKind,
@@ -29,7 +30,6 @@ import type {
29
30
  ServiceCounterparty,
30
31
  Counterparty as InternalCounterparty,
31
32
  ActiveLinkedAccountDetails,
32
- ExecuteTradeRequest,
33
33
  AssetAccountRef,
34
34
  LinkedAccountRef,
35
35
  NothingAssetRef,
@@ -50,6 +50,41 @@ export class CandleClient {
50
50
  this.candle.initialize(appUser);
51
51
  }
52
52
 
53
+ public presentTradeExecutionSheet(input: {
54
+ tradeQuote: TradeQuote;
55
+ presentationBackground?: PresentationBackground;
56
+ completion?: (
57
+ result: ({ kind: "success" } & Trade) | { kind: "failure"; error: string }
58
+ ) => void;
59
+ }): void {
60
+ const quote = this.convertTradeQuote(input.tradeQuote);
61
+ this.candle.candleTradeExecutionSheet(
62
+ quote,
63
+ input.presentationBackground ?? "default",
64
+ (result) => {
65
+ if (input.completion === undefined) {
66
+ return;
67
+ }
68
+ if (result.trade !== undefined) {
69
+ input.completion({
70
+ kind: "success",
71
+ ...result.trade,
72
+ counterparty: this.convertToCounterparty(result.trade.counterparty),
73
+ lost: this.convertTradeAsset(result.trade.lost),
74
+ gained: this.convertTradeAsset(result.trade.gained),
75
+ });
76
+ } else {
77
+ if (result.error === undefined) {
78
+ throw new Error(
79
+ "Internal Candle Error: corrupted trade execution result."
80
+ );
81
+ }
82
+ input.completion({ kind: "failure", error: result.error });
83
+ }
84
+ }
85
+ );
86
+ }
87
+
53
88
  public presentCandleLinkSheet({
54
89
  services = undefined,
55
90
  cornerRadius = 24,
@@ -130,17 +165,6 @@ export class CandleClient {
130
165
  return this.candle.getAvailableTools();
131
166
  }
132
167
 
133
- public async executeTrade(request: ExecuteTradeRequest): Promise<Trade> {
134
- const result = await this.candle.executeTrade(request);
135
- return {
136
- dateTime: result.dateTime,
137
- state: result.state,
138
- counterparty: this.convertToCounterparty(result.counterparty),
139
- lost: this.convertTradeAsset(result.lost),
140
- gained: this.convertTradeAsset(result.gained),
141
- };
142
- }
143
-
144
168
  public async executeTool(tool: {
145
169
  name: string;
146
170
  arguments: string;
@@ -197,12 +221,7 @@ export class CandleClient {
197
221
  | ({ assetKind: "transport" } & TransportAssetQuoteRequest)
198
222
  | ({ assetKind: "fiat" } & FiatAssetQuoteRequest)
199
223
  | ({ assetKind: "stock" | "crypto" } & MarketAssetQuoteRequest);
200
- }): Promise<
201
- {
202
- gained: TradeAsset;
203
- lost: TradeAsset;
204
- }[]
205
- > {
224
+ }): Promise<TradeQuote[]> {
206
225
  let gainedRequest: TradeAssetQuoteRequest;
207
226
 
208
227
  switch (request.gained.assetKind) {
@@ -230,6 +249,7 @@ export class CandleClient {
230
249
  return {
231
250
  gained: this.convertTradeAsset(quote.gained),
232
251
  lost: this.convertTradeAsset(quote.lost),
252
+ context: quote.context,
233
253
  };
234
254
  });
235
255
  }
@@ -278,6 +298,36 @@ export class CandleClient {
278
298
  }
279
299
  }
280
300
 
301
+ private toInternalTradeAsset(asset: TradeAsset): InternalTradeAsset {
302
+ switch (asset.assetKind) {
303
+ case "fiat":
304
+ return { fiatAsset: asset };
305
+ case "stock":
306
+ case "crypto":
307
+ return {
308
+ marketTradeAsset: {
309
+ ...asset,
310
+ assetKind: asset.assetKind,
311
+ },
312
+ };
313
+ case "transport":
314
+ return { transportAsset: asset };
315
+ case "other":
316
+ return { otherAsset: asset };
317
+ case "nothing":
318
+ return { nothingAsset: asset };
319
+ }
320
+ }
321
+
322
+ private convertTradeQuote(tradeQuote: TradeQuote): InternalTradeQuote {
323
+ const { context, gained, lost } = tradeQuote;
324
+ return {
325
+ context,
326
+ gained: this.toInternalTradeAsset(gained),
327
+ lost: this.toInternalTradeAsset(lost),
328
+ };
329
+ }
330
+
281
331
  private convertTradeAsset(tradeAsset: InternalTradeAsset): TradeAsset {
282
332
  if (tradeAsset.fiatAsset !== undefined) {
283
333
  return {
@@ -285,9 +335,12 @@ export class CandleClient {
285
335
  assetKind: "fiat",
286
336
  };
287
337
  } else if (tradeAsset.marketTradeAsset !== undefined) {
338
+ this.assertMarketAssetKind(tradeAsset.marketTradeAsset.assetKind);
288
339
  return {
289
340
  ...tradeAsset.marketTradeAsset,
290
- assetKind: tradeAsset.marketTradeAsset.assetKind as "stock" | "crypto",
341
+ assetKind: this.assertMarketAssetKind(
342
+ tradeAsset.marketTradeAsset.assetKind
343
+ ),
291
344
  };
292
345
  } else if (tradeAsset.transportAsset !== undefined) {
293
346
  return {
@@ -326,6 +379,14 @@ export class CandleClient {
326
379
  }
327
380
  }
328
381
 
382
+ private assertMarketAssetKind(kind: string): "stock" | "crypto" {
383
+ if (kind !== "stock" && kind !== "crypto") {
384
+ throw new Error("Internal Candle Error: corrupted market account.");
385
+ } else {
386
+ return kind;
387
+ }
388
+ }
389
+
329
390
  private convertToAssetAccount(account: InternalAssetAccount): AssetAccount {
330
391
  const { legalAccountKind, nickname } = account;
331
392
  const { fiatAccountDetails, marketAccountDetails } = account.details;
@@ -345,7 +406,7 @@ export class CandleClient {
345
406
  nickname,
346
407
  details: {
347
408
  ...marketAccountDetails,
348
- assetKind: marketAccountDetails.assetKind as "stock" | "crypto",
409
+ assetKind: this.assertMarketAssetKind(marketAccountDetails.assetKind),
349
410
  },
350
411
  };
351
412
  } else {
@@ -380,6 +441,12 @@ type TradeQuery = {
380
441
  counterpartyKind?: "merchant" | "user" | "service";
381
442
  } & InternalTradeQuery;
382
443
 
444
+ type TradeQuote = {
445
+ gained: TradeAsset;
446
+ lost: TradeAsset;
447
+ context: string;
448
+ };
449
+
383
450
  type TradeQueryAssetKind =
384
451
  | "fiat"
385
452
  | "stock"
@@ -438,4 +505,5 @@ export type {
438
505
  TradeQuery,
439
506
  Counterparty,
440
507
  AssetAccount,
508
+ TradeQuote,
441
509
  };
@@ -226,6 +226,7 @@ export type ExecuteTradeRequest = {
226
226
  export type TradeQuote = {
227
227
  lost: TradeAsset;
228
228
  gained: TradeAsset;
229
+ context: string;
229
230
  };
230
231
 
231
232
  export type Service =
@@ -393,6 +394,11 @@ export type TradeRef = {
393
394
  gained: TradeAssetRef;
394
395
  };
395
396
 
397
+ export type TradeExecutionResult = {
398
+ trade?: Trade;
399
+ error?: string;
400
+ };
401
+
396
402
  export interface RNCandle extends HybridObject<{ ios: "swift" }> {
397
403
  candleLinkSheet(
398
404
  isPresented: boolean,
@@ -404,6 +410,11 @@ export interface RNCandle extends HybridObject<{ ios: "swift" }> {
404
410
  presentationStyle: PresentationStyle,
405
411
  onSuccess: (account: LinkedAccount) => void
406
412
  ): void;
413
+ candleTradeExecutionSheet(
414
+ tradeQuote: TradeQuote,
415
+ presentationBackground: PresentationBackground,
416
+ completion: (result: TradeExecutionResult) => void
417
+ ): void;
407
418
  initialize(appUser: AppUser): void;
408
419
  getLinkedAccounts(): Promise<LinkedAccount[]>;
409
420
  getLinkedAccount(ref: LinkedAccountRef): Promise<LinkedAccount>;
@@ -413,7 +424,6 @@ export interface RNCandle extends HybridObject<{ ios: "swift" }> {
413
424
  getTrades(query: TradeQuery): Promise<Trade[]>;
414
425
  getTrade(ref: TradeRef): Promise<Trade>;
415
426
  getTradeQuotes(request: TradeQuoteRequest): Promise<TradeQuote[]>;
416
- executeTrade(request: ExecuteTradeRequest): Promise<Trade>;
417
427
  deleteUser(): Promise<void>;
418
428
  // FIXME: The return type should be a more specific type based on the actual tool calls available.
419
429
  getAvailableTools(): Promise<Array<AnyMap>>;
@@ -1,44 +0,0 @@
1
- ///
2
- /// ExecuteTradeRequest.swift
3
- /// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
4
- /// https://github.com/mrousavy/nitro
5
- /// Copyright © 2025 Marc Rousavy @ Margelo
6
- ///
7
-
8
- import NitroModules
9
-
10
- /// Represents an instance of `ExecuteTradeRequest`, backed by a C++ struct.
11
- public typealias ExecuteTradeRequest = margelo.nitro.rncandle.ExecuteTradeRequest
12
-
13
- extension ExecuteTradeRequest {
14
- private typealias bridge = margelo.nitro.rncandle.bridge.swift
15
-
16
- /**
17
- * Create a new instance of `ExecuteTradeRequest`.
18
- */
19
- public init(linkedAccountID: String, context: String) {
20
- self.init(std.string(linkedAccountID), std.string(context))
21
- }
22
-
23
- public var linkedAccountID: String {
24
- @inline(__always)
25
- get {
26
- return String(self.__linkedAccountID)
27
- }
28
- @inline(__always)
29
- set {
30
- self.__linkedAccountID = std.string(newValue)
31
- }
32
- }
33
-
34
- public var context: String {
35
- @inline(__always)
36
- get {
37
- return String(self.__context)
38
- }
39
- @inline(__always)
40
- set {
41
- self.__context = std.string(newValue)
42
- }
43
- }
44
- }
@@ -1,73 +0,0 @@
1
- ///
2
- /// ExecuteTradeRequest.hpp
3
- /// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
4
- /// https://github.com/mrousavy/nitro
5
- /// Copyright © 2025 Marc Rousavy @ Margelo
6
- ///
7
-
8
- #pragma once
9
-
10
- #if __has_include(<NitroModules/JSIConverter.hpp>)
11
- #include <NitroModules/JSIConverter.hpp>
12
- #else
13
- #error NitroModules cannot be found! Are you sure you installed NitroModules properly?
14
- #endif
15
- #if __has_include(<NitroModules/NitroDefines.hpp>)
16
- #include <NitroModules/NitroDefines.hpp>
17
- #else
18
- #error NitroModules cannot be found! Are you sure you installed NitroModules properly?
19
- #endif
20
-
21
-
22
-
23
- #include <string>
24
-
25
- namespace margelo::nitro::rncandle {
26
-
27
- /**
28
- * A struct which can be represented as a JavaScript object (ExecuteTradeRequest).
29
- */
30
- struct ExecuteTradeRequest {
31
- public:
32
- std::string linkedAccountID SWIFT_PRIVATE;
33
- std::string context SWIFT_PRIVATE;
34
-
35
- public:
36
- ExecuteTradeRequest() = default;
37
- explicit ExecuteTradeRequest(std::string linkedAccountID, std::string context): linkedAccountID(linkedAccountID), context(context) {}
38
- };
39
-
40
- } // namespace margelo::nitro::rncandle
41
-
42
- namespace margelo::nitro {
43
-
44
- using namespace margelo::nitro::rncandle;
45
-
46
- // C++ ExecuteTradeRequest <> JS ExecuteTradeRequest (object)
47
- template <>
48
- struct JSIConverter<ExecuteTradeRequest> final {
49
- static inline ExecuteTradeRequest fromJSI(jsi::Runtime& runtime, const jsi::Value& arg) {
50
- jsi::Object obj = arg.asObject(runtime);
51
- return ExecuteTradeRequest(
52
- JSIConverter<std::string>::fromJSI(runtime, obj.getProperty(runtime, "linkedAccountID")),
53
- JSIConverter<std::string>::fromJSI(runtime, obj.getProperty(runtime, "context"))
54
- );
55
- }
56
- static inline jsi::Value toJSI(jsi::Runtime& runtime, const ExecuteTradeRequest& arg) {
57
- jsi::Object obj(runtime);
58
- obj.setProperty(runtime, "linkedAccountID", JSIConverter<std::string>::toJSI(runtime, arg.linkedAccountID));
59
- obj.setProperty(runtime, "context", JSIConverter<std::string>::toJSI(runtime, arg.context));
60
- return obj;
61
- }
62
- static inline bool canConvert(jsi::Runtime& runtime, const jsi::Value& value) {
63
- if (!value.isObject()) {
64
- return false;
65
- }
66
- jsi::Object obj = value.getObject(runtime);
67
- if (!JSIConverter<std::string>::canConvert(runtime, obj.getProperty(runtime, "linkedAccountID"))) return false;
68
- if (!JSIConverter<std::string>::canConvert(runtime, obj.getProperty(runtime, "context"))) return false;
69
- return true;
70
- }
71
- };
72
-
73
- } // namespace margelo::nitro