From 173157592ee41347f600478df7df92c4c934c9b3 Mon Sep 17 00:00:00 2001 From: Juliano Lazzarotto <30806844+stackchain@users.noreply.github.com> Date: Sun, 15 Oct 2023 23:24:33 +0100 Subject: [PATCH 1/8] wip: metadata --- .../ListOrders/CompletedOrders.tsx | 12 +++++----- .../processTransactions.ts | 22 +++++++++++++++++-- .../src/yoroi-wallets/types/other.ts | 6 ++--- 3 files changed, 30 insertions(+), 10 deletions(-) diff --git a/apps/wallet-mobile/src/features/Swap/useCases/StartSwapScreen/ListOrders/CompletedOrders.tsx b/apps/wallet-mobile/src/features/Swap/useCases/StartSwapScreen/ListOrders/CompletedOrders.tsx index 972cabf336..2212e38e2b 100644 --- a/apps/wallet-mobile/src/features/Swap/useCases/StartSwapScreen/ListOrders/CompletedOrders.tsx +++ b/apps/wallet-mobile/src/features/Swap/useCases/StartSwapScreen/ListOrders/CompletedOrders.tsx @@ -52,20 +52,22 @@ const findCompletedOrderTx = (transactions: TransactionInfo[], onError: (err: Er const filteredTx = sentTransactions .reduce((acc, sentTx) => { - const result: {id?: string; metadata?: TxMetadataInfo; date?: string} = {} + // TODO: metadata is Record + const result: {id?: string; metadata?: string; date?: string} = {} receivedTransactions.forEach((receivedTx) => { receivedTx.inputs.forEach((input) => { - if (Boolean(input.id) && input?.id?.slice(0, -1) === sentTx?.id && receivedTx.metadata !== null) { + if (Boolean(input.id) && input?.id?.slice(0, -1) === sentTx?.id && receivedTx.metadata?.['674'] != null) { result['id'] = sentTx?.id - result['metadata'] = sentTx?.metadata + result['metadata'] = sentTx?.metadata?.['674'] result['date'] = receivedTx?.lastUpdatedAt } }) }) - if (result['id'] !== undefined && result['metadata'] !== undefined) { + if (result['id'] !== undefined && result['metadata'] != null) { try { - const metadata = JSON.parse(result.metadata as string) + // TODO: need a parser for metadata for completed orders + const metadata = JSON.parse(result.metadata) as MappedRawOrder['metadata'] result['metadata'] = metadata return acc.concat(result as MappedRawOrder) } catch (error) { diff --git a/apps/wallet-mobile/src/yoroi-wallets/cardano/processTransactions/processTransactions.ts b/apps/wallet-mobile/src/yoroi-wallets/cardano/processTransactions/processTransactions.ts index ecb9287b21..0ff471fddf 100644 --- a/apps/wallet-mobile/src/yoroi-wallets/cardano/processTransactions/processTransactions.ts +++ b/apps/wallet-mobile/src/yoroi-wallets/cardano/processTransactions/processTransactions.ts @@ -16,6 +16,7 @@ import { } from '../../types' import {getDefaultNetworkTokenEntry, MultiToken, strToDefaultMultiAsset} from '../MultiToken' import {multiTokenFromRemote} from '../utils' +import {isArray, isString} from '@yoroi/common' export const ASSURANCE_LEVELS = { LOW: 3, @@ -90,7 +91,24 @@ export const processTxHistoryData = ( memo: string | null, defaultAsset: DefaultAsset, ): TransactionInfo => { - const metadata = tx.metadata?.[0]?.map_json.msg?.join('') ?? null + const metadata = tx.metadata?.reduce( + (metadatas: TransactionInfo['metadata'], metadata) => { + if (metadata?.label && metadatas != null) { + // 674, .msg string | string[] + if (isArray(metadata?.map_json?.msg)) { + metadatas[metadata.label] = metadata.map_json.msg.join('') + } + if (isString(metadata?.map_json?.msg)) { + metadatas[metadata.label] = metadata.map_json.msg + } + // ---- 674 + } + return metadatas + }, + {}, + ) + console.log(JSON.stringify(tx.metadata, null, 2)) + // const metadata = tx.metadata?.[0]?.map_json.msg?.join('') ?? null const _strToDefaultMultiAsset = (amount: string) => strToDefaultMultiAsset(amount, networkId, defaultAsset) // collateral const collateral = tx.collateralInputs || [] @@ -268,6 +286,6 @@ export const processTxHistoryData = ( tokens, blockNumber: tx.blockNum ?? 0, memo, - metadata: metadata ?? null, + metadata, } } diff --git a/apps/wallet-mobile/src/yoroi-wallets/types/other.ts b/apps/wallet-mobile/src/yoroi-wallets/types/other.ts index b6c1ccaf78..cf19fef102 100644 --- a/apps/wallet-mobile/src/yoroi-wallets/types/other.ts +++ b/apps/wallet-mobile/src/yoroi-wallets/types/other.ts @@ -398,8 +398,8 @@ export const TRANSACTION_STATUS = { } export type TransactionStatus = (typeof TRANSACTION_STATUS)[keyof typeof TRANSACTION_STATUS] -export type TxMetadata = Array<{label: string; map_json: {msg: Array}}> -export type TxMetadataInfo = unknown +export type TxMetadata = Array<{label: string; map_json?: any; text_scalar?: string | null}> +export type TxMetadataInfo = Record export type TransactionInfo = { id: string @@ -417,7 +417,7 @@ export type TransactionInfo = { tokens: Record blockNumber: number memo: null | string - metadata?: TxMetadataInfo + metadata: TxMetadataInfo } export type IOData = { From c6c975812ba10488f901b392415526c8bd7dbb66 Mon Sep 17 00:00:00 2001 From: SorinC6 Date: Mon, 16 Oct 2023 11:16:51 +0300 Subject: [PATCH 2/8] wip --- apps/wallet-mobile/ios/Podfile.lock | 442 +++++++++--------- .../src/features/Swap/common/helpers.ts | 48 ++ .../ListOrders/CompletedOrders.tsx | 14 +- .../processTransactions.ts | 7 +- 4 files changed, 279 insertions(+), 232 deletions(-) diff --git a/apps/wallet-mobile/ios/Podfile.lock b/apps/wallet-mobile/ios/Podfile.lock index 42e9bf455a..35e63de24e 100644 --- a/apps/wallet-mobile/ios/Podfile.lock +++ b/apps/wallet-mobile/ios/Podfile.lock @@ -34,14 +34,14 @@ PODS: - React-Core - React-RCTAppDelegate - ReactCommon/turbomodule/core - - FBLazyVector (0.71.14) - - FBReactNativeSpec (0.71.14): + - FBLazyVector (0.71.6) + - FBReactNativeSpec (0.71.6): - RCT-Folly (= 2021.07.22.00) - - RCTRequired (= 0.71.14) - - RCTTypeSafety (= 0.71.14) - - React-Core (= 0.71.14) - - React-jsi (= 0.71.14) - - ReactCommon/turbomodule/core (= 0.71.14) + - RCTRequired (= 0.71.6) + - RCTTypeSafety (= 0.71.6) + - React-Core (= 0.71.6) + - React-jsi (= 0.71.6) + - ReactCommon/turbomodule/core (= 0.71.6) - Flipper (0.125.0): - Flipper-Folly (~> 2.6) - Flipper-RSocket (~> 1.4) @@ -140,26 +140,26 @@ PODS: - fmt (~> 6.2.1) - glog - libevent - - RCTRequired (0.71.14) - - RCTTypeSafety (0.71.14): - - FBLazyVector (= 0.71.14) - - RCTRequired (= 0.71.14) - - React-Core (= 0.71.14) - - React (0.71.14): - - React-Core (= 0.71.14) - - React-Core/DevSupport (= 0.71.14) - - React-Core/RCTWebSocket (= 0.71.14) - - React-RCTActionSheet (= 0.71.14) - - React-RCTAnimation (= 0.71.14) - - React-RCTBlob (= 0.71.14) - - React-RCTImage (= 0.71.14) - - React-RCTLinking (= 0.71.14) - - React-RCTNetwork (= 0.71.14) - - React-RCTSettings (= 0.71.14) - - React-RCTText (= 0.71.14) - - React-RCTVibration (= 0.71.14) - - React-callinvoker (0.71.14) - - React-Codegen (0.71.14): + - RCTRequired (0.71.6) + - RCTTypeSafety (0.71.6): + - FBLazyVector (= 0.71.6) + - RCTRequired (= 0.71.6) + - React-Core (= 0.71.6) + - React (0.71.6): + - React-Core (= 0.71.6) + - React-Core/DevSupport (= 0.71.6) + - React-Core/RCTWebSocket (= 0.71.6) + - React-RCTActionSheet (= 0.71.6) + - React-RCTAnimation (= 0.71.6) + - React-RCTBlob (= 0.71.6) + - React-RCTImage (= 0.71.6) + - React-RCTLinking (= 0.71.6) + - React-RCTNetwork (= 0.71.6) + - React-RCTSettings (= 0.71.6) + - React-RCTText (= 0.71.6) + - React-RCTVibration (= 0.71.6) + - React-callinvoker (0.71.6) + - React-Codegen (0.71.6): - FBReactNativeSpec - hermes-engine - RCT-Folly @@ -170,209 +170,209 @@ PODS: - React-jsiexecutor - ReactCommon/turbomodule/bridging - ReactCommon/turbomodule/core - - React-Core (0.71.14): + - React-Core (0.71.6): - glog - hermes-engine - RCT-Folly (= 2021.07.22.00) - - React-Core/Default (= 0.71.14) - - React-cxxreact (= 0.71.14) + - React-Core/Default (= 0.71.6) + - React-cxxreact (= 0.71.6) - React-hermes - - React-jsi (= 0.71.14) - - React-jsiexecutor (= 0.71.14) - - React-perflogger (= 0.71.14) + - React-jsi (= 0.71.6) + - React-jsiexecutor (= 0.71.6) + - React-perflogger (= 0.71.6) - Yoga - - React-Core/CoreModulesHeaders (0.71.14): + - React-Core/CoreModulesHeaders (0.71.6): - glog - hermes-engine - RCT-Folly (= 2021.07.22.00) - React-Core/Default - - React-cxxreact (= 0.71.14) + - React-cxxreact (= 0.71.6) - React-hermes - - React-jsi (= 0.71.14) - - React-jsiexecutor (= 0.71.14) - - React-perflogger (= 0.71.14) + - React-jsi (= 0.71.6) + - React-jsiexecutor (= 0.71.6) + - React-perflogger (= 0.71.6) - Yoga - - React-Core/Default (0.71.14): + - React-Core/Default (0.71.6): - glog - hermes-engine - RCT-Folly (= 2021.07.22.00) - - React-cxxreact (= 0.71.14) + - React-cxxreact (= 0.71.6) - React-hermes - - React-jsi (= 0.71.14) - - React-jsiexecutor (= 0.71.14) - - React-perflogger (= 0.71.14) + - React-jsi (= 0.71.6) + - React-jsiexecutor (= 0.71.6) + - React-perflogger (= 0.71.6) - Yoga - - React-Core/DevSupport (0.71.14): + - React-Core/DevSupport (0.71.6): - glog - hermes-engine - RCT-Folly (= 2021.07.22.00) - - React-Core/Default (= 0.71.14) - - React-Core/RCTWebSocket (= 0.71.14) - - React-cxxreact (= 0.71.14) + - React-Core/Default (= 0.71.6) + - React-Core/RCTWebSocket (= 0.71.6) + - React-cxxreact (= 0.71.6) - React-hermes - - React-jsi (= 0.71.14) - - React-jsiexecutor (= 0.71.14) - - React-jsinspector (= 0.71.14) - - React-perflogger (= 0.71.14) + - React-jsi (= 0.71.6) + - React-jsiexecutor (= 0.71.6) + - React-jsinspector (= 0.71.6) + - React-perflogger (= 0.71.6) - Yoga - - React-Core/RCTActionSheetHeaders (0.71.14): + - React-Core/RCTActionSheetHeaders (0.71.6): - glog - hermes-engine - RCT-Folly (= 2021.07.22.00) - React-Core/Default - - React-cxxreact (= 0.71.14) + - React-cxxreact (= 0.71.6) - React-hermes - - React-jsi (= 0.71.14) - - React-jsiexecutor (= 0.71.14) - - React-perflogger (= 0.71.14) + - React-jsi (= 0.71.6) + - React-jsiexecutor (= 0.71.6) + - React-perflogger (= 0.71.6) - Yoga - - React-Core/RCTAnimationHeaders (0.71.14): + - React-Core/RCTAnimationHeaders (0.71.6): - glog - hermes-engine - RCT-Folly (= 2021.07.22.00) - React-Core/Default - - React-cxxreact (= 0.71.14) + - React-cxxreact (= 0.71.6) - React-hermes - - React-jsi (= 0.71.14) - - React-jsiexecutor (= 0.71.14) - - React-perflogger (= 0.71.14) + - React-jsi (= 0.71.6) + - React-jsiexecutor (= 0.71.6) + - React-perflogger (= 0.71.6) - Yoga - - React-Core/RCTBlobHeaders (0.71.14): + - React-Core/RCTBlobHeaders (0.71.6): - glog - hermes-engine - RCT-Folly (= 2021.07.22.00) - React-Core/Default - - React-cxxreact (= 0.71.14) + - React-cxxreact (= 0.71.6) - React-hermes - - React-jsi (= 0.71.14) - - React-jsiexecutor (= 0.71.14) - - React-perflogger (= 0.71.14) + - React-jsi (= 0.71.6) + - React-jsiexecutor (= 0.71.6) + - React-perflogger (= 0.71.6) - Yoga - - React-Core/RCTImageHeaders (0.71.14): + - React-Core/RCTImageHeaders (0.71.6): - glog - hermes-engine - RCT-Folly (= 2021.07.22.00) - React-Core/Default - - React-cxxreact (= 0.71.14) + - React-cxxreact (= 0.71.6) - React-hermes - - React-jsi (= 0.71.14) - - React-jsiexecutor (= 0.71.14) - - React-perflogger (= 0.71.14) + - React-jsi (= 0.71.6) + - React-jsiexecutor (= 0.71.6) + - React-perflogger (= 0.71.6) - Yoga - - React-Core/RCTLinkingHeaders (0.71.14): + - React-Core/RCTLinkingHeaders (0.71.6): - glog - hermes-engine - RCT-Folly (= 2021.07.22.00) - React-Core/Default - - React-cxxreact (= 0.71.14) + - React-cxxreact (= 0.71.6) - React-hermes - - React-jsi (= 0.71.14) - - React-jsiexecutor (= 0.71.14) - - React-perflogger (= 0.71.14) + - React-jsi (= 0.71.6) + - React-jsiexecutor (= 0.71.6) + - React-perflogger (= 0.71.6) - Yoga - - React-Core/RCTNetworkHeaders (0.71.14): + - React-Core/RCTNetworkHeaders (0.71.6): - glog - hermes-engine - RCT-Folly (= 2021.07.22.00) - React-Core/Default - - React-cxxreact (= 0.71.14) + - React-cxxreact (= 0.71.6) - React-hermes - - React-jsi (= 0.71.14) - - React-jsiexecutor (= 0.71.14) - - React-perflogger (= 0.71.14) + - React-jsi (= 0.71.6) + - React-jsiexecutor (= 0.71.6) + - React-perflogger (= 0.71.6) - Yoga - - React-Core/RCTSettingsHeaders (0.71.14): + - React-Core/RCTSettingsHeaders (0.71.6): - glog - hermes-engine - RCT-Folly (= 2021.07.22.00) - React-Core/Default - - React-cxxreact (= 0.71.14) + - React-cxxreact (= 0.71.6) - React-hermes - - React-jsi (= 0.71.14) - - React-jsiexecutor (= 0.71.14) - - React-perflogger (= 0.71.14) + - React-jsi (= 0.71.6) + - React-jsiexecutor (= 0.71.6) + - React-perflogger (= 0.71.6) - Yoga - - React-Core/RCTTextHeaders (0.71.14): + - React-Core/RCTTextHeaders (0.71.6): - glog - hermes-engine - RCT-Folly (= 2021.07.22.00) - React-Core/Default - - React-cxxreact (= 0.71.14) + - React-cxxreact (= 0.71.6) - React-hermes - - React-jsi (= 0.71.14) - - React-jsiexecutor (= 0.71.14) - - React-perflogger (= 0.71.14) + - React-jsi (= 0.71.6) + - React-jsiexecutor (= 0.71.6) + - React-perflogger (= 0.71.6) - Yoga - - React-Core/RCTVibrationHeaders (0.71.14): + - React-Core/RCTVibrationHeaders (0.71.6): - glog - hermes-engine - RCT-Folly (= 2021.07.22.00) - React-Core/Default - - React-cxxreact (= 0.71.14) + - React-cxxreact (= 0.71.6) - React-hermes - - React-jsi (= 0.71.14) - - React-jsiexecutor (= 0.71.14) - - React-perflogger (= 0.71.14) + - React-jsi (= 0.71.6) + - React-jsiexecutor (= 0.71.6) + - React-perflogger (= 0.71.6) - Yoga - - React-Core/RCTWebSocket (0.71.14): + - React-Core/RCTWebSocket (0.71.6): - glog - hermes-engine - RCT-Folly (= 2021.07.22.00) - - React-Core/Default (= 0.71.14) - - React-cxxreact (= 0.71.14) + - React-Core/Default (= 0.71.6) + - React-cxxreact (= 0.71.6) - React-hermes - - React-jsi (= 0.71.14) - - React-jsiexecutor (= 0.71.14) - - React-perflogger (= 0.71.14) + - React-jsi (= 0.71.6) + - React-jsiexecutor (= 0.71.6) + - React-perflogger (= 0.71.6) - Yoga - - React-CoreModules (0.71.14): + - React-CoreModules (0.71.6): - RCT-Folly (= 2021.07.22.00) - - RCTTypeSafety (= 0.71.14) - - React-Codegen (= 0.71.14) - - React-Core/CoreModulesHeaders (= 0.71.14) - - React-jsi (= 0.71.14) + - RCTTypeSafety (= 0.71.6) + - React-Codegen (= 0.71.6) + - React-Core/CoreModulesHeaders (= 0.71.6) + - React-jsi (= 0.71.6) - React-RCTBlob - - React-RCTImage (= 0.71.14) - - ReactCommon/turbomodule/core (= 0.71.14) - - React-cxxreact (0.71.14): + - React-RCTImage (= 0.71.6) + - ReactCommon/turbomodule/core (= 0.71.6) + - React-cxxreact (0.71.6): - boost (= 1.76.0) - DoubleConversion - glog - hermes-engine - RCT-Folly (= 2021.07.22.00) - - React-callinvoker (= 0.71.14) - - React-jsi (= 0.71.14) - - React-jsinspector (= 0.71.14) - - React-logger (= 0.71.14) - - React-perflogger (= 0.71.14) - - React-runtimeexecutor (= 0.71.14) - - React-hermes (0.71.14): + - React-callinvoker (= 0.71.6) + - React-jsi (= 0.71.6) + - React-jsinspector (= 0.71.6) + - React-logger (= 0.71.6) + - React-perflogger (= 0.71.6) + - React-runtimeexecutor (= 0.71.6) + - React-hermes (0.71.6): - DoubleConversion - glog - hermes-engine - RCT-Folly (= 2021.07.22.00) - RCT-Folly/Futures (= 2021.07.22.00) - - React-cxxreact (= 0.71.14) + - React-cxxreact (= 0.71.6) - React-jsi - - React-jsiexecutor (= 0.71.14) - - React-jsinspector (= 0.71.14) - - React-perflogger (= 0.71.14) - - React-jsi (0.71.14): + - React-jsiexecutor (= 0.71.6) + - React-jsinspector (= 0.71.6) + - React-perflogger (= 0.71.6) + - React-jsi (0.71.6): - boost (= 1.76.0) - DoubleConversion - glog - hermes-engine - RCT-Folly (= 2021.07.22.00) - - React-jsiexecutor (0.71.14): + - React-jsiexecutor (0.71.6): - DoubleConversion - glog - hermes-engine - RCT-Folly (= 2021.07.22.00) - - React-cxxreact (= 0.71.14) - - React-jsi (= 0.71.14) - - React-perflogger (= 0.71.14) - - React-jsinspector (0.71.14) - - React-logger (0.71.14): + - React-cxxreact (= 0.71.6) + - React-jsi (= 0.71.6) + - React-perflogger (= 0.71.6) + - React-jsinspector (0.71.6) + - React-logger (0.71.6): - glog - react-native-background-timer (2.4.1): - React-Core @@ -399,90 +399,90 @@ PODS: - React-Core - react-native-webview (11.26.1): - React-Core - - React-perflogger (0.71.14) - - React-RCTActionSheet (0.71.14): - - React-Core/RCTActionSheetHeaders (= 0.71.14) - - React-RCTAnimation (0.71.14): + - React-perflogger (0.71.6) + - React-RCTActionSheet (0.71.6): + - React-Core/RCTActionSheetHeaders (= 0.71.6) + - React-RCTAnimation (0.71.6): - RCT-Folly (= 2021.07.22.00) - - RCTTypeSafety (= 0.71.14) - - React-Codegen (= 0.71.14) - - React-Core/RCTAnimationHeaders (= 0.71.14) - - React-jsi (= 0.71.14) - - ReactCommon/turbomodule/core (= 0.71.14) - - React-RCTAppDelegate (0.71.14): + - RCTTypeSafety (= 0.71.6) + - React-Codegen (= 0.71.6) + - React-Core/RCTAnimationHeaders (= 0.71.6) + - React-jsi (= 0.71.6) + - ReactCommon/turbomodule/core (= 0.71.6) + - React-RCTAppDelegate (0.71.6): - RCT-Folly - RCTRequired - RCTTypeSafety - React-Core - ReactCommon/turbomodule/core - - React-RCTBlob (0.71.14): + - React-RCTBlob (0.71.6): - hermes-engine - RCT-Folly (= 2021.07.22.00) - - React-Codegen (= 0.71.14) - - React-Core/RCTBlobHeaders (= 0.71.14) - - React-Core/RCTWebSocket (= 0.71.14) - - React-jsi (= 0.71.14) - - React-RCTNetwork (= 0.71.14) - - ReactCommon/turbomodule/core (= 0.71.14) - - React-RCTImage (0.71.14): + - React-Codegen (= 0.71.6) + - React-Core/RCTBlobHeaders (= 0.71.6) + - React-Core/RCTWebSocket (= 0.71.6) + - React-jsi (= 0.71.6) + - React-RCTNetwork (= 0.71.6) + - ReactCommon/turbomodule/core (= 0.71.6) + - React-RCTImage (0.71.6): - RCT-Folly (= 2021.07.22.00) - - RCTTypeSafety (= 0.71.14) - - React-Codegen (= 0.71.14) - - React-Core/RCTImageHeaders (= 0.71.14) - - React-jsi (= 0.71.14) - - React-RCTNetwork (= 0.71.14) - - ReactCommon/turbomodule/core (= 0.71.14) - - React-RCTLinking (0.71.14): - - React-Codegen (= 0.71.14) - - React-Core/RCTLinkingHeaders (= 0.71.14) - - React-jsi (= 0.71.14) - - ReactCommon/turbomodule/core (= 0.71.14) - - React-RCTNetwork (0.71.14): + - RCTTypeSafety (= 0.71.6) + - React-Codegen (= 0.71.6) + - React-Core/RCTImageHeaders (= 0.71.6) + - React-jsi (= 0.71.6) + - React-RCTNetwork (= 0.71.6) + - ReactCommon/turbomodule/core (= 0.71.6) + - React-RCTLinking (0.71.6): + - React-Codegen (= 0.71.6) + - React-Core/RCTLinkingHeaders (= 0.71.6) + - React-jsi (= 0.71.6) + - ReactCommon/turbomodule/core (= 0.71.6) + - React-RCTNetwork (0.71.6): - RCT-Folly (= 2021.07.22.00) - - RCTTypeSafety (= 0.71.14) - - React-Codegen (= 0.71.14) - - React-Core/RCTNetworkHeaders (= 0.71.14) - - React-jsi (= 0.71.14) - - ReactCommon/turbomodule/core (= 0.71.14) - - React-RCTSettings (0.71.14): + - RCTTypeSafety (= 0.71.6) + - React-Codegen (= 0.71.6) + - React-Core/RCTNetworkHeaders (= 0.71.6) + - React-jsi (= 0.71.6) + - ReactCommon/turbomodule/core (= 0.71.6) + - React-RCTSettings (0.71.6): - RCT-Folly (= 2021.07.22.00) - - RCTTypeSafety (= 0.71.14) - - React-Codegen (= 0.71.14) - - React-Core/RCTSettingsHeaders (= 0.71.14) - - React-jsi (= 0.71.14) - - ReactCommon/turbomodule/core (= 0.71.14) - - React-RCTText (0.71.14): - - React-Core/RCTTextHeaders (= 0.71.14) - - React-RCTVibration (0.71.14): + - RCTTypeSafety (= 0.71.6) + - React-Codegen (= 0.71.6) + - React-Core/RCTSettingsHeaders (= 0.71.6) + - React-jsi (= 0.71.6) + - ReactCommon/turbomodule/core (= 0.71.6) + - React-RCTText (0.71.6): + - React-Core/RCTTextHeaders (= 0.71.6) + - React-RCTVibration (0.71.6): - RCT-Folly (= 2021.07.22.00) - - React-Codegen (= 0.71.14) - - React-Core/RCTVibrationHeaders (= 0.71.14) - - React-jsi (= 0.71.14) - - ReactCommon/turbomodule/core (= 0.71.14) - - React-runtimeexecutor (0.71.14): - - React-jsi (= 0.71.14) - - ReactCommon/turbomodule/bridging (0.71.14): + - React-Codegen (= 0.71.6) + - React-Core/RCTVibrationHeaders (= 0.71.6) + - React-jsi (= 0.71.6) + - ReactCommon/turbomodule/core (= 0.71.6) + - React-runtimeexecutor (0.71.6): + - React-jsi (= 0.71.6) + - ReactCommon/turbomodule/bridging (0.71.6): - DoubleConversion - glog - hermes-engine - RCT-Folly (= 2021.07.22.00) - - React-callinvoker (= 0.71.14) - - React-Core (= 0.71.14) - - React-cxxreact (= 0.71.14) - - React-jsi (= 0.71.14) - - React-logger (= 0.71.14) - - React-perflogger (= 0.71.14) - - ReactCommon/turbomodule/core (0.71.14): + - React-callinvoker (= 0.71.6) + - React-Core (= 0.71.6) + - React-cxxreact (= 0.71.6) + - React-jsi (= 0.71.6) + - React-logger (= 0.71.6) + - React-perflogger (= 0.71.6) + - ReactCommon/turbomodule/core (0.71.6): - DoubleConversion - glog - hermes-engine - RCT-Folly (= 2021.07.22.00) - - React-callinvoker (= 0.71.14) - - React-Core (= 0.71.14) - - React-cxxreact (= 0.71.14) - - React-jsi (= 0.71.14) - - React-logger (= 0.71.14) - - React-perflogger (= 0.71.14) + - React-callinvoker (= 0.71.6) + - React-Core (= 0.71.6) + - React-cxxreact (= 0.71.6) + - React-jsi (= 0.71.6) + - React-logger (= 0.71.6) + - React-perflogger (= 0.71.6) - RNBootSplash (4.7.5): - React-Core - RNCAsyncStorage (1.19.3): @@ -855,8 +855,8 @@ SPEC CHECKSUMS: ExpoDevice: 1c1b0c9cad96c292c1de73948649cfd654b2b3c0 ExpoKeepAwake: 69f5f627670d62318410392d03e0b5db0f85759a ExpoModulesCore: 653958063a301098b541ae4dfed1ac0b98db607b - FBLazyVector: 12ea01e587c9594e7b144e1bfc86ac4d9ac28fde - FBReactNativeSpec: faca7d16c37626ca5780a87adef703817722fe61 + FBLazyVector: a83ceaa8a8581003a623facdb3c44f6d4f342ac5 + FBReactNativeSpec: d850372b600f78cbb92585bfc7b2e48dbb73b3ad Flipper: 26fc4b7382499f1281eb8cb921e5c3ad6de91fe0 Flipper-Boost-iOSX: fd1e2b8cbef7e662a122412d7ac5f5bea715403c Flipper-DoubleConversion: 2dc99b02f658daf147069aad9dbd29d8feb06d30 @@ -879,19 +879,19 @@ SPEC CHECKSUMS: Permission-LocationAlways: 8fd5518716c3045f9c4edf77b901126098d79b60 Permission-LocationWhenInUse: 24d97eeb25d8ff9f2232e070f792eeb1360ccaf0 RCT-Folly: 424b8c9a7a0b9ab2886ffe9c3b041ef628fd4fb1 - RCTRequired: e9df143e880d0e879e7a498dc06923d728809c79 - RCTTypeSafety: c2d89c8308829c12c038ec1f431191eaa0d8c15c - React: 52b89a818f4b2579c98567f3aa8bde880d9e843b - React-callinvoker: 56e399c88c05e037fe99c31978f30e75fad5c286 - React-Codegen: 7ece62f4d4896ad1933f834a7dad697676636318 - React-Core: f06b7b00e0d49433a316760ae61a0f8f5dee6629 - React-CoreModules: bd520e5688b5aa4666965a1b3b8e6d4a2e19df20 - React-cxxreact: ba6a1663685837fa4c2ac97daa95dd2e47f1acdc - React-hermes: c862e573ca0228070936b5ec4f475c3e19e900e0 - React-jsi: 533030c161bcfcbc3a4ad0b357ced8f7b2be457e - React-jsiexecutor: 94cfc1788637ceaf8841ef1f69b10cc0d62baadc - React-jsinspector: 7bf923954b4e035f494b01ac16633963412660d7 - React-logger: 655ff5db8bd922acfbe76a4983ffab048916343e + RCTRequired: 5c6fd63b03abb06947d348dadac51c93e3485bd8 + RCTTypeSafety: 1c66daedd66f674e39ce9f40782f0d490c78b175 + React: e11ca7cdc7aa4ddd7e6a59278b808cfe17ebbd9f + React-callinvoker: 77a82869505c96945c074b80bbdc8df919646d51 + React-Codegen: 9ee33090c38ab3da3c4dc029924d50fb649f0dfc + React-Core: 44903e47b428a491f48fd0eae54caddb2ea05ebf + React-CoreModules: 83d989defdfc82be1f7386f84a56b6509f54ac74 + React-cxxreact: 058e7e6349649eae9cfcdec5854e702b26298932 + React-hermes: ba19a405804b833c9b832c1f2061ad5038bb97f2 + React-jsi: 3fe6f589c9cafbef85ed5a4be7c6dc8edfb4ab54 + React-jsiexecutor: 7894956638ff3e00819dd3f9f6f4a84da38f2409 + React-jsinspector: d5ce2ef3eb8fd30c28389d0bc577918c70821bd6 + React-logger: 9332c3e7b4ef007a0211c0a9868253aac3e1da82 react-native-background-timer: 17ea5e06803401a379ebf1f20505b793ac44d0fe react-native-ble-plx: f10240444452dfb2d2a13a0e4f58d7783e92d76e react-native-config: 86038147314e2e6d10ea9972022aa171e6b1d4d8 @@ -903,19 +903,19 @@ SPEC CHECKSUMS: react-native-slider: 33b8d190b59d4f67a541061bb91775d53d617d9d react-native-timezone: 92fa6c0a7c041efd632a372a515c957cd8ee9e72 react-native-webview: 9f111dfbcfc826084d6c507f569e5e03342ee1c1 - React-perflogger: 4987ad83731c23d11813c84263963b0d3028c966 - React-RCTActionSheet: 5ad952b2a9740d87a5bd77280c4bc23f6f89ea0c - React-RCTAnimation: d2de22af3f536cc80bb5b3918e1a455114d1b985 - React-RCTAppDelegate: 27f7d735cad3d522c13008ea80020d350017c422 - React-RCTBlob: b697e0e2e38ec85bd726176851a3b476a490ad33 - React-RCTImage: a07e8c7d4768f62ebc6277e4680f6b979c619967 - React-RCTLinking: d00ae55db37b2c12ebab91135f06f75391c0708d - React-RCTNetwork: b3a401276e5c08487d8a14fdec1720e78b5888db - React-RCTSettings: d606cbac31403604c5d5746e6dab53bb332f9301 - React-RCTText: b3bd40bc71bca0c3e2cc5ce2c40870a438f303b1 - React-RCTVibration: 64e412b9ac684c4edc938fa1187135ada9af7faf - React-runtimeexecutor: ffe826b7b1cfbc32a35ed5b64d5886c0ff75f501 - ReactCommon: 7f3dd5e98a9ec627c6b03d26c062bf37ea9fc888 + React-perflogger: 43392072a5b867a504e2b4857606f8fc5a403d7f + React-RCTActionSheet: c7b67c125bebeda9fb19fc7b200d85cb9d6899c4 + React-RCTAnimation: c2de79906f607986633a7114bee44854e4c7e2f5 + React-RCTAppDelegate: 96bc933c3228a549718a6475c4d3f9dd4bbae98d + React-RCTBlob: cf72446957310e7da6627a4bdaadf970d3a8f232 + React-RCTImage: c6093f1bf3d67c0428d779b00390617d5bd90699 + React-RCTLinking: 5de47e37937889d22599af4b99d0552bad1b1c3c + React-RCTNetwork: e7d7077e073b08e5dd486fba3fe87ccad90a9bc4 + React-RCTSettings: 72a04921b2e8fb832da7201a60ffffff2a7c62f7 + React-RCTText: 7123c70fef5367e2121fea37e65b9ad6d3747e54 + React-RCTVibration: 73d201599a64ea14b4e0b8f91b64970979fd92e6 + React-runtimeexecutor: 8692ac548bec648fa121980ccb4304afd136d584 + ReactCommon: 0c43eaeaaee231d7d8dc24fc5a6e4cf2b75bf196 RNBootSplash: 85f6b879c080e958afdb4c62ee04497b05fd7552 RNCAsyncStorage: c913ede1fa163a71cea118ed4670bbaaa4b511bb RNCClipboard: 41d8d918092ae8e676f18adada19104fa3e68495 @@ -933,7 +933,7 @@ SPEC CHECKSUMS: Sentry: 97161cac725da1ecbe77d1445bf8a61c1e5667f1 SentryPrivate: 9a76def09fb08f9501997b8df946e8097947b94f SocketRocket: fccef3f9c5cedea1353a9ef6ada904fde10d6608 - Yoga: e71803b4c1fff832ccf9b92541e00f9b873119b9 + Yoga: ba09b6b11e6139e3df8229238aa794205ca6a02a YogaKit: f782866e155069a2cca2517aafea43200b01fd5a ZXingObjC: fdbb269f25dd2032da343e06f10224d62f537bdb diff --git a/apps/wallet-mobile/src/features/Swap/common/helpers.ts b/apps/wallet-mobile/src/features/Swap/common/helpers.ts index a17edec839..bb711d8b33 100644 --- a/apps/wallet-mobile/src/features/Swap/common/helpers.ts +++ b/apps/wallet-mobile/src/features/Swap/common/helpers.ts @@ -11,6 +11,7 @@ import {YoroiWallet} from '../../../yoroi-wallets/cardano/types' import {generateCIP30UtxoCbor} from '../../../yoroi-wallets/cardano/utils' import {YoroiEntry} from '../../../yoroi-wallets/types' import {Quantities} from '../../../yoroi-wallets/utils' + export const createYoroiEntry = ( createOrder: Swap.CreateOrderData, address: string, @@ -66,3 +67,50 @@ export const useCancelOrderWithHw = ( cancelOrder: mutation.mutate, } } + +const VALID_PROVIDERS: Record = { + minswap: true, + wingriders: true, + sundaeswap: true, + muesliswap: true, + muesliswap_v2: true, + vyfi: true, +} + +interface ExpectedMetadata { + sellTokenId: string + buyTokenId: string + sellQuantity: string + buyQuantity: string + provider: Swap.SupportedProvider +} + +/** + * Parses and validates a JSON metadata string, transforming it into a structure compliant with MappedRawOrder['metadata']. + * + * @param metadataJson - The JSON string representation of metadata. + * @returns The parsed metadata object or null if parsing fails or validation fails. + */ +export const parseMetadata = (metadataJson: string): ExpectedMetadata | null => { + try { + const metadata = JSON.parse(metadataJson) + + if ( + !metadata || + typeof metadata !== 'object' || + typeof metadata.sellTokenId !== 'string' || + typeof metadata.buyTokenId !== 'string' || + typeof metadata.sellQuantity !== 'string' || + typeof metadata.buyQuantity !== 'string' || + (typeof metadata.provider === 'string' && metadata.provider in VALID_PROVIDERS) + ) { + console.error('Invalid metadata structure.') + return null + } + + return metadata as ExpectedMetadata + } catch (error) { + console.error('JSON parsing error:', error) + return null + } +} diff --git a/apps/wallet-mobile/src/features/Swap/useCases/StartSwapScreen/ListOrders/CompletedOrders.tsx b/apps/wallet-mobile/src/features/Swap/useCases/StartSwapScreen/ListOrders/CompletedOrders.tsx index 2212e38e2b..7190435071 100644 --- a/apps/wallet-mobile/src/features/Swap/useCases/StartSwapScreen/ListOrders/CompletedOrders.tsx +++ b/apps/wallet-mobile/src/features/Swap/useCases/StartSwapScreen/ListOrders/CompletedOrders.tsx @@ -25,6 +25,7 @@ import {useSync, useTokenInfo, useTransactionInfos} from '../../../../../yoroi-w import {TransactionInfo, TxMetadataInfo} from '../../../../../yoroi-wallets/types' import {asQuantity, openInExplorer, Quantities} from '../../../../../yoroi-wallets/utils' import {Counter} from '../../../common/Counter/Counter' +import {parseMetadata} from '../../../common/helpers' import {PoolIcon} from '../../../common/PoolIcon/PoolIcon' import {useStrings} from '../../../common/strings' @@ -52,10 +53,10 @@ const findCompletedOrderTx = (transactions: TransactionInfo[], onError: (err: Er const filteredTx = sentTransactions .reduce((acc, sentTx) => { - // TODO: metadata is Record - const result: {id?: string; metadata?: string; date?: string} = {} + const result: TxMetadataInfo | null = {} receivedTransactions.forEach((receivedTx) => { receivedTx.inputs.forEach((input) => { + console.log('receivedTx.metadata', receivedTx.metadata) if (Boolean(input.id) && input?.id?.slice(0, -1) === sentTx?.id && receivedTx.metadata?.['674'] != null) { result['id'] = sentTx?.id result['metadata'] = sentTx?.metadata?.['674'] @@ -66,10 +67,11 @@ const findCompletedOrderTx = (transactions: TransactionInfo[], onError: (err: Er if (result['id'] !== undefined && result['metadata'] != null) { try { - // TODO: need a parser for metadata for completed orders - const metadata = JSON.parse(result.metadata) as MappedRawOrder['metadata'] - result['metadata'] = metadata - return acc.concat(result as MappedRawOrder) + const metadata = parseMetadata(result.metadata) + if (metadata) { + result['metadata'] = metadata + return acc.concat(result as MappedRawOrder) + } } catch (error) { onError(error as Error) } diff --git a/apps/wallet-mobile/src/yoroi-wallets/cardano/processTransactions/processTransactions.ts b/apps/wallet-mobile/src/yoroi-wallets/cardano/processTransactions/processTransactions.ts index 0ff471fddf..a4ed7717db 100644 --- a/apps/wallet-mobile/src/yoroi-wallets/cardano/processTransactions/processTransactions.ts +++ b/apps/wallet-mobile/src/yoroi-wallets/cardano/processTransactions/processTransactions.ts @@ -1,4 +1,5 @@ /* eslint-disable @typescript-eslint/no-explicit-any */ +import {isArray, isString} from '@yoroi/common' import assert from 'assert' import {BigNumber} from 'bignumber.js' @@ -16,7 +17,6 @@ import { } from '../../types' import {getDefaultNetworkTokenEntry, MultiToken, strToDefaultMultiAsset} from '../MultiToken' import {multiTokenFromRemote} from '../utils' -import {isArray, isString} from '@yoroi/common' export const ASSURANCE_LEVELS = { LOW: 3, @@ -94,21 +94,18 @@ export const processTxHistoryData = ( const metadata = tx.metadata?.reduce( (metadatas: TransactionInfo['metadata'], metadata) => { if (metadata?.label && metadatas != null) { - // 674, .msg string | string[] if (isArray(metadata?.map_json?.msg)) { metadatas[metadata.label] = metadata.map_json.msg.join('') } if (isString(metadata?.map_json?.msg)) { metadatas[metadata.label] = metadata.map_json.msg } - // ---- 674 } return metadatas }, {}, ) console.log(JSON.stringify(tx.metadata, null, 2)) - // const metadata = tx.metadata?.[0]?.map_json.msg?.join('') ?? null const _strToDefaultMultiAsset = (amount: string) => strToDefaultMultiAsset(amount, networkId, defaultAsset) // collateral const collateral = tx.collateralInputs || [] @@ -286,6 +283,6 @@ export const processTxHistoryData = ( tokens, blockNumber: tx.blockNum ?? 0, memo, - metadata, + metadata: metadata || {}, } } From 5b686c3ecc772568dc952882b2062703d7d9db98 Mon Sep 17 00:00:00 2001 From: SorinC6 Date: Mon, 16 Oct 2023 12:50:42 +0300 Subject: [PATCH 3/8] add parseCompleteOrderMetadata helper --- .../src/features/Swap/common/helpers.ts | 18 +++------- .../src/features/Swap/common/useSwapTx.ts | 1 - .../ListOrders/CompletedOrders.tsx | 34 +++++++------------ .../processTransactions.ts | 2 +- 4 files changed, 17 insertions(+), 38 deletions(-) diff --git a/apps/wallet-mobile/src/features/Swap/common/helpers.ts b/apps/wallet-mobile/src/features/Swap/common/helpers.ts index bb711d8b33..b94160f8f7 100644 --- a/apps/wallet-mobile/src/features/Swap/common/helpers.ts +++ b/apps/wallet-mobile/src/features/Swap/common/helpers.ts @@ -68,16 +68,7 @@ export const useCancelOrderWithHw = ( } } -const VALID_PROVIDERS: Record = { - minswap: true, - wingriders: true, - sundaeswap: true, - muesliswap: true, - muesliswap_v2: true, - vyfi: true, -} - -interface ExpectedMetadata { +export type ExpectedOrderMetadata = { sellTokenId: string buyTokenId: string sellQuantity: string @@ -91,7 +82,7 @@ interface ExpectedMetadata { * @param metadataJson - The JSON string representation of metadata. * @returns The parsed metadata object or null if parsing fails or validation fails. */ -export const parseMetadata = (metadataJson: string): ExpectedMetadata | null => { +export const parseCompleteOrderMetadata = (metadataJson: string): ExpectedOrderMetadata | null => { try { const metadata = JSON.parse(metadataJson) @@ -101,14 +92,13 @@ export const parseMetadata = (metadataJson: string): ExpectedMetadata | null => typeof metadata.sellTokenId !== 'string' || typeof metadata.buyTokenId !== 'string' || typeof metadata.sellQuantity !== 'string' || - typeof metadata.buyQuantity !== 'string' || - (typeof metadata.provider === 'string' && metadata.provider in VALID_PROVIDERS) + typeof metadata.buyQuantity !== 'string' ) { console.error('Invalid metadata structure.') return null } - return metadata as ExpectedMetadata + return metadata as ExpectedOrderMetadata } catch (error) { console.error('JSON parsing error:', error) return null diff --git a/apps/wallet-mobile/src/features/Swap/common/useSwapTx.ts b/apps/wallet-mobile/src/features/Swap/common/useSwapTx.ts index 6f74c9a4f4..aec84c0853 100644 --- a/apps/wallet-mobile/src/features/Swap/common/useSwapTx.ts +++ b/apps/wallet-mobile/src/features/Swap/common/useSwapTx.ts @@ -21,7 +21,6 @@ export const useSwapTx = (options?: UseMutationOptions { return new Date(b.date).getTime() - new Date(a.date).getTime() } -const findCompletedOrderTx = (transactions: TransactionInfo[], onError: (err: Error) => void): MappedRawOrder[] => { +const findCompletedOrderTx = (transactions: TransactionInfo[]): MappedRawOrder[] => { const sentTransactions = transactions.filter((tx) => tx.direction === 'SENT') const receivedTransactions = transactions.filter((tx) => tx.direction === 'RECEIVED') const filteredTx = sentTransactions .reduce((acc, sentTx) => { - const result: TxMetadataInfo | null = {} + const result: TxMetadataInfo = {} receivedTransactions.forEach((receivedTx) => { receivedTx.inputs.forEach((input) => { - console.log('receivedTx.metadata', receivedTx.metadata) - if (Boolean(input.id) && input?.id?.slice(0, -1) === sentTx?.id && receivedTx.metadata?.['674'] != null) { + if (Boolean(input.id) && input?.id?.slice(0, -1) === sentTx?.id && receivedTx.metadata?.['674'] !== null) { result['id'] = sentTx?.id - result['metadata'] = sentTx?.metadata?.['674'] result['date'] = receivedTx?.lastUpdatedAt + const metadata: ExpectedOrderMetadata | null = parseCompleteOrderMetadata(sentTx?.metadata?.['674']) + if (metadata) { + result['metadata'] = metadata + return acc.push(result as MappedRawOrder) + } } }) }) - - if (result['id'] !== undefined && result['metadata'] != null) { - try { - const metadata = parseMetadata(result.metadata) - if (metadata) { - result['metadata'] = metadata - return acc.concat(result as MappedRawOrder) - } - } catch (error) { - onError(error as Error) - } - } return acc }, [] as Array) .sort(compareByDate) + return filteredTx.filter((tx) => tx.metadata !== null).sort(compareByDate) } @@ -89,9 +81,7 @@ export const CompletedOrders = () => { const transactionsInfos = useTransactionInfos(wallet) - const completeOrders = findCompletedOrderTx(Object.values(transactionsInfos), (error: Error) => { - Alert.alert(strings.generalErrorTitle, strings.generalErrorMessage(error)) - }) + const completeOrders = findCompletedOrderTx(Object.values(transactionsInfos)) const {track} = useMetrics() diff --git a/apps/wallet-mobile/src/yoroi-wallets/cardano/processTransactions/processTransactions.ts b/apps/wallet-mobile/src/yoroi-wallets/cardano/processTransactions/processTransactions.ts index a4ed7717db..91660df23d 100644 --- a/apps/wallet-mobile/src/yoroi-wallets/cardano/processTransactions/processTransactions.ts +++ b/apps/wallet-mobile/src/yoroi-wallets/cardano/processTransactions/processTransactions.ts @@ -105,7 +105,7 @@ export const processTxHistoryData = ( }, {}, ) - console.log(JSON.stringify(tx.metadata, null, 2)) + const _strToDefaultMultiAsset = (amount: string) => strToDefaultMultiAsset(amount, networkId, defaultAsset) // collateral const collateral = tx.collateralInputs || [] From 4e8185d62e940bc407273a76319b4e22b5d455b6 Mon Sep 17 00:00:00 2001 From: SorinC6 Date: Mon, 16 Oct 2023 14:02:28 +0300 Subject: [PATCH 4/8] add schema using zod --- .../src/features/Swap/common/helpers.ts | 25 +++++++++++-------- .../src/yoroi-wallets/mocks/transaction.ts | 4 +++ 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/apps/wallet-mobile/src/features/Swap/common/helpers.ts b/apps/wallet-mobile/src/features/Swap/common/helpers.ts index b94160f8f7..137ece29cb 100644 --- a/apps/wallet-mobile/src/features/Swap/common/helpers.ts +++ b/apps/wallet-mobile/src/features/Swap/common/helpers.ts @@ -1,6 +1,8 @@ +import {createTypeGuardFromSchema} from '@yoroi/common' import {Swap} from '@yoroi/types' import {SwapApi} from '@yoroi/types/src/swap/api' import {useMutation, UseMutationOptions} from 'react-query' +import {z} from 'zod' import {useSelectedWallet} from '../../../SelectedWallet' import { @@ -73,9 +75,19 @@ export type ExpectedOrderMetadata = { buyTokenId: string sellQuantity: string buyQuantity: string - provider: Swap.SupportedProvider + provider: string } +const CompleteOrderMetadataSchema: z.ZodSchema = z.object({ + sellTokenId: z.string(), + buyTokenId: z.string(), + sellQuantity: z.string(), + buyQuantity: z.string(), + provider: z.string(), +}) + +const isCompleteOrderMetadata = createTypeGuardFromSchema(CompleteOrderMetadataSchema) + /** * Parses and validates a JSON metadata string, transforming it into a structure compliant with MappedRawOrder['metadata']. * @@ -86,15 +98,8 @@ export const parseCompleteOrderMetadata = (metadataJson: string): ExpectedOrderM try { const metadata = JSON.parse(metadataJson) - if ( - !metadata || - typeof metadata !== 'object' || - typeof metadata.sellTokenId !== 'string' || - typeof metadata.buyTokenId !== 'string' || - typeof metadata.sellQuantity !== 'string' || - typeof metadata.buyQuantity !== 'string' - ) { - console.error('Invalid metadata structure.') + if (!isCompleteOrderMetadata(metadata)) { + console.error('Invalid metadata schema.') return null } diff --git a/apps/wallet-mobile/src/yoroi-wallets/mocks/transaction.ts b/apps/wallet-mobile/src/yoroi-wallets/mocks/transaction.ts index 188ce83db3..ba6836c85a 100644 --- a/apps/wallet-mobile/src/yoroi-wallets/mocks/transaction.ts +++ b/apps/wallet-mobile/src/yoroi-wallets/mocks/transaction.ts @@ -97,6 +97,7 @@ export const mockTransactionInfos: Record = { }, }, memo: null, + metadata: {}, }, '5e7eff1b687f538066ea08938e91ba562c88dc817782816a1fc6f1560d8905e8': { id: '5e7eff1b687f538066ea08938e91ba562c88dc817782816a1fc6f1560d8905e8', @@ -193,6 +194,7 @@ export const mockTransactionInfos: Record = { }, }, memo: null, + metadata: {}, }, '0953a5e90889ed0b2ea1e3230cccde871d90c17868aea22300716ecaeec93096': { id: '0953a5e90889ed0b2ea1e3230cccde871d90c17868aea22300716ecaeec93096', @@ -289,6 +291,7 @@ export const mockTransactionInfos: Record = { }, }, memo: null, + metadata: {}, }, } @@ -388,6 +391,7 @@ export const mockTransactionInfo = (transaction?: Partial): Tra }, }, memo: null, + metadata: {}, ...transaction, } } From 0b365cc1887931e38ff350f48b594cf7bb697757 Mon Sep 17 00:00:00 2001 From: SorinC6 Date: Mon, 16 Oct 2023 17:25:11 +0300 Subject: [PATCH 5/8] rename const --- .../src/features/Swap/common/helpers.ts | 18 +++++++----------- .../ListOrders/CompletedOrders.tsx | 4 ++-- .../processTransactions/processTransactions.ts | 2 +- .../src/yoroi-wallets/types/other.ts | 2 +- 4 files changed, 11 insertions(+), 15 deletions(-) diff --git a/apps/wallet-mobile/src/features/Swap/common/helpers.ts b/apps/wallet-mobile/src/features/Swap/common/helpers.ts index 137ece29cb..25dc941e2d 100644 --- a/apps/wallet-mobile/src/features/Swap/common/helpers.ts +++ b/apps/wallet-mobile/src/features/Swap/common/helpers.ts @@ -11,6 +11,7 @@ import { } from '../../../yoroi-wallets/cardano/common/signatureUtils' import {YoroiWallet} from '../../../yoroi-wallets/cardano/types' import {generateCIP30UtxoCbor} from '../../../yoroi-wallets/cardano/utils' +import {Logger} from '../../../yoroi-wallets/logging' import {YoroiEntry} from '../../../yoroi-wallets/types' import {Quantities} from '../../../yoroi-wallets/utils' @@ -70,7 +71,7 @@ export const useCancelOrderWithHw = ( } } -export type ExpectedOrderMetadata = { +export type OrderTxMetadata = { sellTokenId: string buyTokenId: string sellQuantity: string @@ -78,7 +79,7 @@ export type ExpectedOrderMetadata = { provider: string } -const CompleteOrderMetadataSchema: z.ZodSchema = z.object({ +const OrderTxMetadataSchema: z.ZodSchema = z.object({ sellTokenId: z.string(), buyTokenId: z.string(), sellQuantity: z.string(), @@ -86,7 +87,7 @@ const CompleteOrderMetadataSchema: z.ZodSchema = z.object provider: z.string(), }) -const isCompleteOrderMetadata = createTypeGuardFromSchema(CompleteOrderMetadataSchema) +const isOrderTxMetadata = createTypeGuardFromSchema(OrderTxMetadataSchema) /** * Parses and validates a JSON metadata string, transforming it into a structure compliant with MappedRawOrder['metadata']. @@ -94,18 +95,13 @@ const isCompleteOrderMetadata = createTypeGuardFromSchema(CompleteOrderMetadataS * @param metadataJson - The JSON string representation of metadata. * @returns The parsed metadata object or null if parsing fails or validation fails. */ -export const parseCompleteOrderMetadata = (metadataJson: string): ExpectedOrderMetadata | null => { +export const parseOrderTxMetadata = (metadataJson: string): OrderTxMetadata | null => { try { const metadata = JSON.parse(metadataJson) - if (!isCompleteOrderMetadata(metadata)) { - console.error('Invalid metadata schema.') - return null - } - - return metadata as ExpectedOrderMetadata + return isOrderTxMetadata(metadata) ? metadata : null } catch (error) { - console.error('JSON parsing error:', error) + Logger.warn('JSON parsing error:', error) return null } } diff --git a/apps/wallet-mobile/src/features/Swap/useCases/StartSwapScreen/ListOrders/CompletedOrders.tsx b/apps/wallet-mobile/src/features/Swap/useCases/StartSwapScreen/ListOrders/CompletedOrders.tsx index e08c65bd49..dafcd16f07 100644 --- a/apps/wallet-mobile/src/features/Swap/useCases/StartSwapScreen/ListOrders/CompletedOrders.tsx +++ b/apps/wallet-mobile/src/features/Swap/useCases/StartSwapScreen/ListOrders/CompletedOrders.tsx @@ -25,7 +25,7 @@ import {useSync, useTokenInfo, useTransactionInfos} from '../../../../../yoroi-w import {TransactionInfo, TxMetadataInfo} from '../../../../../yoroi-wallets/types' import {asQuantity, openInExplorer, Quantities} from '../../../../../yoroi-wallets/utils' import {Counter} from '../../../common/Counter/Counter' -import {ExpectedOrderMetadata, parseCompleteOrderMetadata} from '../../../common/helpers' +import {parseOrderTxMetadata} from '../../../common/helpers' import {PoolIcon} from '../../../common/PoolIcon/PoolIcon' import {useStrings} from '../../../common/strings' @@ -59,7 +59,7 @@ const findCompletedOrderTx = (transactions: TransactionInfo[]): MappedRawOrder[] if (Boolean(input.id) && input?.id?.slice(0, -1) === sentTx?.id && receivedTx.metadata?.['674'] !== null) { result['id'] = sentTx?.id result['date'] = receivedTx?.lastUpdatedAt - const metadata: ExpectedOrderMetadata | null = parseCompleteOrderMetadata(sentTx?.metadata?.['674']) + const metadata = parseOrderTxMetadata(sentTx?.metadata?.['674']) if (metadata) { result['metadata'] = metadata return acc.push(result as MappedRawOrder) diff --git a/apps/wallet-mobile/src/yoroi-wallets/cardano/processTransactions/processTransactions.ts b/apps/wallet-mobile/src/yoroi-wallets/cardano/processTransactions/processTransactions.ts index 91660df23d..5a57d3572c 100644 --- a/apps/wallet-mobile/src/yoroi-wallets/cardano/processTransactions/processTransactions.ts +++ b/apps/wallet-mobile/src/yoroi-wallets/cardano/processTransactions/processTransactions.ts @@ -283,6 +283,6 @@ export const processTxHistoryData = ( tokens, blockNumber: tx.blockNum ?? 0, memo, - metadata: metadata || {}, + metadata, } } diff --git a/apps/wallet-mobile/src/yoroi-wallets/types/other.ts b/apps/wallet-mobile/src/yoroi-wallets/types/other.ts index cf19fef102..8b04a2985c 100644 --- a/apps/wallet-mobile/src/yoroi-wallets/types/other.ts +++ b/apps/wallet-mobile/src/yoroi-wallets/types/other.ts @@ -417,7 +417,7 @@ export type TransactionInfo = { tokens: Record blockNumber: number memo: null | string - metadata: TxMetadataInfo + metadata: TxMetadataInfo | undefined } export type IOData = { From 01ee5ae7845492eafc13b5da8013886ad1c3935a Mon Sep 17 00:00:00 2001 From: SorinC6 Date: Mon, 16 Oct 2023 22:22:28 +0300 Subject: [PATCH 6/8] added parse safe helper --- .../src/features/Swap/common/helpers.ts | 13 +++---------- apps/wallet-mobile/src/yoroi-wallets/types/other.ts | 2 +- 2 files changed, 4 insertions(+), 11 deletions(-) diff --git a/apps/wallet-mobile/src/features/Swap/common/helpers.ts b/apps/wallet-mobile/src/features/Swap/common/helpers.ts index 25dc941e2d..c4e7221e0d 100644 --- a/apps/wallet-mobile/src/features/Swap/common/helpers.ts +++ b/apps/wallet-mobile/src/features/Swap/common/helpers.ts @@ -1,4 +1,4 @@ -import {createTypeGuardFromSchema} from '@yoroi/common' +import {createTypeGuardFromSchema, parseSafe} from '@yoroi/common' import {Swap} from '@yoroi/types' import {SwapApi} from '@yoroi/types/src/swap/api' import {useMutation, UseMutationOptions} from 'react-query' @@ -11,7 +11,6 @@ import { } from '../../../yoroi-wallets/cardano/common/signatureUtils' import {YoroiWallet} from '../../../yoroi-wallets/cardano/types' import {generateCIP30UtxoCbor} from '../../../yoroi-wallets/cardano/utils' -import {Logger} from '../../../yoroi-wallets/logging' import {YoroiEntry} from '../../../yoroi-wallets/types' import {Quantities} from '../../../yoroi-wallets/utils' @@ -96,12 +95,6 @@ const isOrderTxMetadata = createTypeGuardFromSchema(OrderTxMetadataSchema) * @returns The parsed metadata object or null if parsing fails or validation fails. */ export const parseOrderTxMetadata = (metadataJson: string): OrderTxMetadata | null => { - try { - const metadata = JSON.parse(metadataJson) - - return isOrderTxMetadata(metadata) ? metadata : null - } catch (error) { - Logger.warn('JSON parsing error:', error) - return null - } + const parsedMetadata = parseSafe(metadataJson) + return isOrderTxMetadata(parsedMetadata) ? parsedMetadata : null } diff --git a/apps/wallet-mobile/src/yoroi-wallets/types/other.ts b/apps/wallet-mobile/src/yoroi-wallets/types/other.ts index 8b04a2985c..cf19fef102 100644 --- a/apps/wallet-mobile/src/yoroi-wallets/types/other.ts +++ b/apps/wallet-mobile/src/yoroi-wallets/types/other.ts @@ -417,7 +417,7 @@ export type TransactionInfo = { tokens: Record blockNumber: number memo: null | string - metadata: TxMetadataInfo | undefined + metadata: TxMetadataInfo } export type IOData = { From 2fc4ed7f69f717ea581159b30b6fee58a3f8303f Mon Sep 17 00:00:00 2001 From: SorinC6 Date: Tue, 17 Oct 2023 09:49:57 +0300 Subject: [PATCH 7/8] fix ts --- apps/wallet-mobile/src/yoroi-wallets/types/other.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/wallet-mobile/src/yoroi-wallets/types/other.ts b/apps/wallet-mobile/src/yoroi-wallets/types/other.ts index cf19fef102..8b04a2985c 100644 --- a/apps/wallet-mobile/src/yoroi-wallets/types/other.ts +++ b/apps/wallet-mobile/src/yoroi-wallets/types/other.ts @@ -417,7 +417,7 @@ export type TransactionInfo = { tokens: Record blockNumber: number memo: null | string - metadata: TxMetadataInfo + metadata: TxMetadataInfo | undefined } export type IOData = { From 535de539cb9863cd0d1784212a54a7141cc5c994 Mon Sep 17 00:00:00 2001 From: Sorin Chis Date: Tue, 17 Oct 2023 10:07:44 +0300 Subject: [PATCH 8/8] Fix(swap) refactor makePossibleMarketOrder helper (#2785) --- .../LiquidityPool/LiquidityPool.stories.tsx | 2 +- .../src/features/Swap/common/helpers.ts | 2 +- .../CreateOrder/CreateOrder.tsx | 2 +- packages/openswap/src/api.ts | 2 +- .../factories/makePossibleMarketOrder.test.ts | 48 ++++----------- .../factories/makePossibleMarketOrder.ts | 59 +++++++------------ packages/swap/src/translators/constants.ts | 2 +- 7 files changed, 37 insertions(+), 80 deletions(-) diff --git a/apps/wallet-mobile/src/features/Swap/common/LiquidityPool/LiquidityPool.stories.tsx b/apps/wallet-mobile/src/features/Swap/common/LiquidityPool/LiquidityPool.stories.tsx index 35803180ba..2cea3aaa46 100644 --- a/apps/wallet-mobile/src/features/Swap/common/LiquidityPool/LiquidityPool.stories.tsx +++ b/apps/wallet-mobile/src/features/Swap/common/LiquidityPool/LiquidityPool.stories.tsx @@ -14,7 +14,7 @@ const supportedProviders: ReadonlyArray = [ 'sundaeswap', 'muesliswap', 'vyfi', - // 'muesliswap_v2', // TODO: enable after more clarification - right now user is not receiving tokens back when choosing this pool + 'muesliswap_v2', ] as const const LiquidityDexList = () => { diff --git a/apps/wallet-mobile/src/features/Swap/common/helpers.ts b/apps/wallet-mobile/src/features/Swap/common/helpers.ts index c4e7221e0d..2bb7ce638d 100644 --- a/apps/wallet-mobile/src/features/Swap/common/helpers.ts +++ b/apps/wallet-mobile/src/features/Swap/common/helpers.ts @@ -22,7 +22,7 @@ export const createYoroiEntry = ( const amountEntry = {} const sellTokenId = createOrder.amounts.sell.tokenId - // summing fees is missing the frontend fee + // TODO Frontend Fee is not added. Once will be defined needs to be added here if (sellTokenId === wallet.primaryTokenInfo.id) { amountEntry[sellTokenId] = Quantities.sum([ createOrder.selectedPool.deposit.quantity, diff --git a/apps/wallet-mobile/src/features/Swap/useCases/StartSwapScreen/CreateOrder/CreateOrder.tsx b/apps/wallet-mobile/src/features/Swap/useCases/StartSwapScreen/CreateOrder/CreateOrder.tsx index 8ff572eb3a..79c0b4a74f 100644 --- a/apps/wallet-mobile/src/features/Swap/useCases/StartSwapScreen/CreateOrder/CreateOrder.tsx +++ b/apps/wallet-mobile/src/features/Swap/useCases/StartSwapScreen/CreateOrder/CreateOrder.tsx @@ -168,7 +168,7 @@ export const CreateOrder = () => { const orderResult: Swap.CreateOrderData | undefined = makePossibleMarketOrder( orderDetails.sell, orderDetails.buy, - orderDetails.pools, + orderDetails.selectedPool, orderDetails.slippage, orderDetails.address, ) diff --git a/packages/openswap/src/api.ts b/packages/openswap/src/api.ts index 8dc1c8d5f3..ed83aedd86 100644 --- a/packages/openswap/src/api.ts +++ b/packages/openswap/src/api.ts @@ -119,5 +119,5 @@ export const supportedProviders: ReadonlyArray = [ 'sundaeswap', 'vyfi', 'wingriders', - // 'muesliswap_v2' // TODO: enable after more clarification - right now user is not receiving tokens back when choosing this pool + 'muesliswap_v2', ] as const diff --git a/packages/swap/src/helpers/orders/factories/makePossibleMarketOrder.test.ts b/packages/swap/src/helpers/orders/factories/makePossibleMarketOrder.test.ts index 849f789b43..260acf5622 100644 --- a/packages/swap/src/helpers/orders/factories/makePossibleMarketOrder.test.ts +++ b/packages/swap/src/helpers/orders/factories/makePossibleMarketOrder.test.ts @@ -1,4 +1,4 @@ -import {Balance, Swap} from '@yoroi/types' +import {Swap} from '@yoroi/types' import {makePossibleMarketOrder} from './makePossibleMarketOrder' @@ -12,7 +12,7 @@ describe('makePossibleMarketOrder', () => { quantity: '177' as const, // the expected buy quantity becsause makePossibleMarketOrder will ignore the buy quantity tokenId: 'tokenB', } - const pool1: Swap.Pool = { + const bestPool1: Swap.Pool = { tokenA: {quantity: '4500000', tokenId: 'tokenA'}, tokenB: {quantity: '9000000', tokenId: 'tokenB'}, ptPriceTokenA: '0', @@ -27,47 +27,21 @@ describe('makePossibleMarketOrder', () => { tokenId: '0', }, } - const pool2: Swap.Pool = { - tokenA: {quantity: '5500000', tokenId: 'tokenA'}, - tokenB: {quantity: '9000000', tokenId: 'tokenB'}, - ptPriceTokenA: '0', - ptPriceTokenB: '0', - fee: '0.3', - provider: 'sundaeswap', - batcherFee: {quantity: '10', tokenId: ''}, - deposit: {quantity: '1', tokenId: ''}, - poolId: '0', - lpToken: { - quantity: '0', - tokenId: '0', - }, - } - const pools = [pool1, pool2] + const slippage = 10 const address = '0xAddressHere' - const result = makePossibleMarketOrder(sell, buy, pools, slippage, address) + const result = makePossibleMarketOrder( + sell, + buy, + bestPool1, + slippage, + address, + ) - expect(result?.selectedPool).toEqual(pool1) + expect(result?.selectedPool).toEqual(bestPool1) expect(result?.slippage).toEqual(slippage) expect(result?.address).toEqual(address) expect(result?.amounts.buy.quantity).toEqual(buy.quantity) }) - - it('should return undefined if no pools are provided', () => { - const sell = { - quantity: '100' as Balance.Quantity, - tokenId: 'tokenA', - } - const buy = { - quantity: '200' as Balance.Quantity, - tokenId: 'tokenB', - } - const pools: Swap.Pool[] = [] - const slippage = 10 - const address = '0xAddressHere' - - const result = makePossibleMarketOrder(sell, buy, pools, slippage, address) - expect(result).toBeUndefined() - }) }) diff --git a/packages/swap/src/helpers/orders/factories/makePossibleMarketOrder.ts b/packages/swap/src/helpers/orders/factories/makePossibleMarketOrder.ts index e5dccb5e4a..b46c4e7ffd 100644 --- a/packages/swap/src/helpers/orders/factories/makePossibleMarketOrder.ts +++ b/packages/swap/src/helpers/orders/factories/makePossibleMarketOrder.ts @@ -8,55 +8,38 @@ import {getQuantityWithSlippage} from '../amounts/getQuantityWithSlippage' * * @param sell - The amount to sell. * @param buy - The desired buy amount. - * @param pools - Array of liquidity pools to choose from. + * @param bestPool - best liquidity pool. * @param slippage - Maximum acceptable slippage in percentage. * @param address - The address placing the order. * - * @returns The best market order data, or undefined if no pool is available. + * @returns The best market order data */ export const makePossibleMarketOrder = ( sell: Balance.Amount, buy: Balance.Amount, - pools: Readonly, + bestPool: Readonly, slippage: number, address: string, ): Swap.CreateOrderData | undefined => { - if (pools.length === 0) return undefined - - const findBestOrder = ( - bestOrder: Swap.CreateOrderData | undefined, - currentPool: Swap.Pool, - ): Swap.CreateOrderData => { - const rawBuy = getBuyAmount(currentPool, sell) - - const buyQuantityWithSlippage = getQuantityWithSlippage( - rawBuy.quantity, - slippage, - ) - - const newOrder: Swap.CreateOrderData = { - selectedPool: currentPool, - slippage, - amounts: { - sell, - buy: { - tokenId: buy.tokenId, - quantity: buyQuantityWithSlippage, - }, + const rawBuy = getBuyAmount(bestPool, sell) + + const buyQuantityWithSlippage = getQuantityWithSlippage( + rawBuy.quantity, + slippage, + ) + + const newOrder: Swap.CreateOrderData = { + selectedPool: bestPool, + slippage, + amounts: { + sell, + buy: { + tokenId: buy.tokenId, + quantity: buyQuantityWithSlippage, }, - address, - } - - if ( - bestOrder === undefined || - BigInt(bestOrder.amounts.buy.quantity) < - BigInt(newOrder.amounts.buy.quantity) - ) { - return newOrder - } - - return bestOrder + }, + address, } - return pools.reduce(findBestOrder, undefined) + return newOrder } diff --git a/packages/swap/src/translators/constants.ts b/packages/swap/src/translators/constants.ts index ed7ff9860b..73f4bb5b0e 100644 --- a/packages/swap/src/translators/constants.ts +++ b/packages/swap/src/translators/constants.ts @@ -8,7 +8,7 @@ export const supportedProviders: ReadonlyArray = [ 'wingriders', 'sundaeswap', 'muesliswap', - // 'muesliswap_v2', // TODO: enable after more clarification - right now user is not receiving tokens back when choosing this pool + 'muesliswap_v2', 'vyfi', ] as const