From 5e7c4bfa9e6c641c49977dd117834a148f98d4d1 Mon Sep 17 00:00:00 2001 From: Simon McLoughlin Date: Thu, 11 Jan 2024 16:55:51 +0000 Subject: [PATCH] - wire up the generic screen to WC2 - implement new extractors to use screens correctly - fix issue with generic using contractCall pending - add sending XTZ amount to generic screen for now - use generic as batch screen for now --- .../xcshareddata/swiftpm/Package.resolved | 6 +- .../WalletConnectSignViewController.swift | 2 +- .../Modules/Send/Base.lproj/Send.storyboard | 201 +++++++++++++++++- .../SendContractConfirmViewController.swift | 2 +- .../SendGenericConfirmViewController.swift | 62 ++++-- .../Services/WalletConnectService.swift | 51 ++--- 6 files changed, 264 insertions(+), 60 deletions(-) diff --git a/Kukai Mobile.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved b/Kukai Mobile.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved index 6d02394c..a16f736b 100644 --- a/Kukai Mobile.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved +++ b/Kukai Mobile.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved @@ -78,7 +78,7 @@ "location" : "https://github.com/kukai-wallet/kukai-core-swift", "state" : { "branch" : "develop", - "revision" : "f4a08fb37302b01cd3cf48cd998fb28cbe39e1a7" + "revision" : "4ad4585f999a0b2dea57213a3154b91d762a9aba" } }, { @@ -86,8 +86,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/kukai-wallet/kukai-crypto-swift", "state" : { - "revision" : "4087e7251d43c73f97f3b572b1006be6fe0e1f7e", - "version" : "1.0.17" + "revision" : "b83b7109b577337600cc064cb95aa1df9f8a9979", + "version" : "1.0.18" } }, { diff --git a/Kukai Mobile/Modules/ConnectedApps/WalletConnectSignViewController.swift b/Kukai Mobile/Modules/ConnectedApps/WalletConnectSignViewController.swift index c3e2dfb9..4b73b6e6 100644 --- a/Kukai Mobile/Modules/ConnectedApps/WalletConnectSignViewController.swift +++ b/Kukai Mobile/Modules/ConnectedApps/WalletConnectSignViewController.swift @@ -156,7 +156,7 @@ class WalletConnectSignViewController: UIViewController, BottomSheetCustomFixedP let updatedSignature = Base58Check.encode(message: signature, ellipticalCurve: wallet.privateKeyCurve()) self?.handleApproval(signature: updatedSignature) - case .failure(let sendError): + case .failure(_): self?.windowError(withTitle: "error".localized(), description: String.localized(String.localized("error-cant-sign"), withArguments: result.getFailure().description)) self?.slideButton?.resetSlider() } diff --git a/Kukai Mobile/Modules/Send/Base.lproj/Send.storyboard b/Kukai Mobile/Modules/Send/Base.lproj/Send.storyboard index d69f2db6..b63e569a 100644 --- a/Kukai Mobile/Modules/Send/Base.lproj/Send.storyboard +++ b/Kukai Mobile/Modules/Send/Base.lproj/Send.storyboard @@ -1250,7 +1250,7 @@ - + @@ -3234,7 +3234,7 @@ - + @@ -3555,7 +3555,7 @@ - + @@ -3739,8 +3739,184 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - + @@ -3758,14 +3934,14 @@ - - + @@ -4024,12 +4200,21 @@ + + + + + + + + + diff --git a/Kukai Mobile/Modules/Send/SendContractConfirmViewController.swift b/Kukai Mobile/Modules/Send/SendContractConfirmViewController.swift index 4f005030..62eab2b7 100644 --- a/Kukai Mobile/Modules/Send/SendContractConfirmViewController.swift +++ b/Kukai Mobile/Modules/Send/SendContractConfirmViewController.swift @@ -262,8 +262,8 @@ class SendContractConfirmViewController: SendAbstractConfirmViewController, Slid let currentOps = selectedOperationsAndFees() let counter = Decimal(string: currentOps.last?.counter ?? "0") ?? 0 - let contractOp = OperationFactory.Extractor.firstContractCallOperation(operations: currentOps) + let contractOp = OperationFactory.Extractor.isSingleContractCall(operations: currentOps)?.operation let entrypoint = (contractOp?.parameters?["entrypoint"] as? String) ?? "" let parameterValueDict = contractOp?.parameters?["value"] as? [String: String] ?? [:] let parameterValueString = String(data: (try? JSONEncoder().encode(parameterValueDict)) ?? Data(), encoding: .utf8) diff --git a/Kukai Mobile/Modules/Send/SendGenericConfirmViewController.swift b/Kukai Mobile/Modules/Send/SendGenericConfirmViewController.swift index 364682d7..24b45b2b 100644 --- a/Kukai Mobile/Modules/Send/SendGenericConfirmViewController.swift +++ b/Kukai Mobile/Modules/Send/SendGenericConfirmViewController.swift @@ -31,6 +31,18 @@ class SendGenericConfirmViewController: SendAbstractConfirmViewController, Slide @IBOutlet weak var fromStackViewRegular: UIStackView! @IBOutlet weak var fromRegularAddress: UILabel! + // Send + @IBOutlet weak var largeDisplayStackView: UIStackView! + @IBOutlet weak var largeDisplayIcon: UIImageView! + @IBOutlet weak var largeDisplayAmount: UILabel! + @IBOutlet weak var largeDisplaySymbol: UILabel! + @IBOutlet weak var largeDisplayFiat: UILabel! + + @IBOutlet weak var smallDisplayStackView: UIStackView! + @IBOutlet weak var smallDisplayIcon: UIImageView! + @IBOutlet weak var smallDisplayAmount: UILabel! + @IBOutlet weak var smallDisplayFiat: UILabel! + // Operation @IBOutlet weak var moreButton: CustomisableButton! @IBOutlet weak var operationTextView: UITextView! @@ -69,7 +81,7 @@ class SendGenericConfirmViewController: SendAbstractConfirmViewController, Slide } self.isWalletConnectOp = true - self.currentContractData = TransactionService.shared.walletConnectOperationData.contractCallData + self.currentSendData = TransactionService.shared.walletConnectOperationData.sendData self.selectedMetadata = walletMetadataForRequestedAccount self.connectedAppNameLabel.text = session.peer.name @@ -91,7 +103,7 @@ class SendGenericConfirmViewController: SendAbstractConfirmViewController, Slide } else { self.isWalletConnectOp = false - self.currentContractData = TransactionService.shared.contractCallData + self.currentSendData = TransactionService.shared.sendData self.selectedMetadata = DependencyManager.shared.selectedWalletMetadata connectedAppMetadataStackView.isHidden = true @@ -101,6 +113,7 @@ class SendGenericConfirmViewController: SendAbstractConfirmViewController, Slide // Display JSON + updateAmountDisplay(withValue: currentSendData.chosenAmount ?? .zero()) updateOperationDisplay() @@ -188,6 +201,33 @@ class SendGenericConfirmViewController: SendAbstractConfirmViewController, Slide } } + func updateAmountDisplay(withValue value: TokenAmount) { + guard let token = currentSendData.chosenToken else { + largeDisplayStackView.isHidden = true + smallDisplayIcon.image = UIImage.unknownToken() + smallDisplayAmount.text = "0" + smallDisplayFiat.text = DependencyManager.shared.balanceService.fiatAmountDisplayString(forToken: Token.xtz(), ofAmount: TokenAmount.zero()) + return + } + + let amountText = value.normalisedRepresentation + if amountText.count > Int(UIScreen.main.bounds.width / 4) { + // small display + largeDisplayStackView.isHidden = true + smallDisplayIcon.addTokenIcon(token: token) + smallDisplayAmount.text = amountText + token.symbol + smallDisplayFiat.text = DependencyManager.shared.balanceService.fiatAmountDisplayString(forToken: token, ofAmount: value) + + } else { + // large display + smallDisplayStackView.isHidden = true + largeDisplayIcon.addTokenIcon(token: token) + largeDisplayAmount.text = amountText + largeDisplaySymbol.text = token.symbol + largeDisplayFiat.text = DependencyManager.shared.balanceService.fiatAmountDisplayString(forToken: token, ofAmount: value) + } + } + func updateOperationDisplay() { let ops = selectedOperationsAndFees() @@ -219,26 +259,18 @@ class SendGenericConfirmViewController: SendAbstractConfirmViewController, Slide func addPendingTransaction(opHash: String) { guard let selectedWalletMetadata = selectedMetadata else { return } - let destinationAddress = currentContractData.contractAddress ?? "" - let amount = currentContractData.chosenAmount ?? .zero() - let currentOps = selectedOperationsAndFees() let counter = Decimal(string: currentOps.last?.counter ?? "0") ?? 0 - let contractOp = OperationFactory.Extractor.firstContractCallOperation(operations: currentOps) - - let entrypoint = (contractOp?.parameters?["entrypoint"] as? String) ?? "" - let parameterValueDict = contractOp?.parameters?["value"] as? [String: String] ?? [:] - let parameterValueString = String(data: (try? JSONEncoder().encode(parameterValueDict)) ?? Data(), encoding: .utf8) - let parameters: [String: String] = ["entrypoint": entrypoint, "value": parameterValueString ?? ""] + let totalAmount = OperationFactory.Extractor.totalTezAmountSent(operations: currentOps) let addPendingResult = DependencyManager.shared.activityService.addPending(opHash: opHash, - type: .transaction, + type: .unknown, counter: counter, fromWallet: selectedWalletMetadata, - destinationAddress: destinationAddress, + destinationAddress: "", destinationAlias: nil, - xtzAmount: amount, - parameters: parameters, + xtzAmount: totalAmount, + parameters: nil, primaryToken: nil) DependencyManager.shared.activityService.addUniqueAddressToPendingOperation(address: selectedWalletMetadata.address) diff --git a/Kukai Mobile/Services/WalletConnectService.swift b/Kukai Mobile/Services/WalletConnectService.swift index e0085cde..ecca9d9c 100644 --- a/Kukai Mobile/Services/WalletConnectService.swift +++ b/Kukai Mobile/Services/WalletConnectService.swift @@ -683,29 +683,20 @@ public class WalletConnectService { TransactionService.shared.currentRemoteForgedString = estimationResult.forgedString - if let contractDetails = OperationFactory.Extractor.isContractCall(operations: operations) { - let totalXTZ = OperationFactory.Extractor.totalXTZAmountForContractCall(operations: operations) - TransactionService.shared.walletConnectOperationData.currentTransactionType = .contractCall - TransactionService.shared.walletConnectOperationData.contractCallData = TransactionService.ContractCallData(chosenToken: Token.xtz(), chosenAmount: totalXTZ, contractAddress: contractDetails.address, operationCount: operations.count, mainEntrypoint: contractDetails.entrypoint) - mainThreadProcessedOperations(ofType: .contractCall) - - } else if let transactionOperation = OperationFactory.Extractor.isTezTransfer(operations: operations) as? OperationTransaction { - + if let op = OperationFactory.Extractor.isTezTransfer(operations: operations) { DependencyManager.shared.tezosNodeClient.getBalance(forAddress: forWallet.address) { [weak self] res in - let xtzAmount = XTZAmount(fromRpcAmount: transactionOperation.amount) ?? .zero() + let xtzAmount = XTZAmount(fromRpcAmount: op.amount) ?? .zero() let accountBalance = (try? res.get()) ?? xtzAmount let selectedToken = Token.xtz(withAmount: accountBalance) TransactionService.shared.walletConnectOperationData.currentTransactionType = .send TransactionService.shared.walletConnectOperationData.sendData.chosenToken = selectedToken TransactionService.shared.walletConnectOperationData.sendData.chosenAmount = xtzAmount - TransactionService.shared.walletConnectOperationData.sendData.destination = transactionOperation.destination + TransactionService.shared.walletConnectOperationData.sendData.destination = op.destination self?.mainThreadProcessedOperations(ofType: .sendToken) } - } else if let result = OperationFactory.Extractor.faTokenDetailsFrom(operations: operationsObj.selectedOperationsAndFees()), - let token = DependencyManager.shared.balanceService.token(forAddress: result.tokenContract, andTokenId: result.tokenId) { - + } else if let result = OperationFactory.Extractor.isFaTokenTransfer(operations: operations), let token = DependencyManager.shared.balanceService.token(forAddress: result.tokenContract, andTokenId: result.tokenId) { TransactionService.shared.walletConnectOperationData.currentTransactionType = .send TransactionService.shared.walletConnectOperationData.sendData.destination = result.destination TransactionService.shared.walletConnectOperationData.sendData.chosenAmount = TokenAmount(fromRpcAmount: result.rpcAmount, decimalPlaces: token.token.decimalPlaces) @@ -719,31 +710,27 @@ public class WalletConnectService { mainThreadProcessedOperations(ofType: .sendToken) } - } else { - /* - var firstTransaction: OperationTransaction? = nil - let totalAmount = operations.compactMap({ ($0 as? OperationTransaction)?.amount }).compactMap({ XTZAmount(fromRpcAmount: $0) }).reduce(XTZAmount.zero(), +) - - if let transactionOperation = OperationFactory.Extractor.isTezTransfer(operations: operations) as? OperationTransaction { - firstTransaction = transactionOperation - } else { - firstTransaction = operations.first(where: { ($0 as? OperationTransaction)?.destination != nil }) as? OperationTransaction - } - + } else if let contractDetails = OperationFactory.Extractor.isSingleContractCall(operations: operations) { + let totalXTZ = OperationFactory.Extractor.totalTezAmountSent(operations: operations) + TransactionService.shared.walletConnectOperationData.currentTransactionType = .contractCall + TransactionService.shared.walletConnectOperationData.contractCallData = TransactionService.ContractCallData(chosenToken: Token.xtz(), chosenAmount: totalXTZ, contractAddress: contractDetails.address, operationCount: operations.count, mainEntrypoint: contractDetails.entrypoint) + mainThreadProcessedOperations(ofType: .contractCall) + }/* else if OperationFactory.Extractor.containsAllOperationTransactions(operations: operations) { + let totalXTZ = OperationFactory.Extractor.totalTezAmountSent(operations: operations) + TransactionService.shared.walletConnectOperationData.currentTransactionType = .contractCall + mainThreadProcessedOperations(ofType: .contractCall) + }*/ else { DependencyManager.shared.tezosNodeClient.getBalance(forAddress: forWallet.address) { [weak self] res in - let accountBalance = (try? res.get()) ?? totalAmount + let xtzAmount = OperationFactory.Extractor.totalTezAmountSent(operations: operations) + let accountBalance = (try? res.get()) ?? xtzAmount let selectedToken = Token.xtz(withAmount: accountBalance) - TransactionService.shared.walletConnectOperationData.currentTransactionType = .send TransactionService.shared.walletConnectOperationData.sendData.chosenToken = selectedToken - TransactionService.shared.walletConnectOperationData.sendData.chosenAmount = totalAmount - TransactionService.shared.walletConnectOperationData.sendData.destination = firstTransaction?.destination ?? "" - self?.mainThreadProcessedOperations(ofType: .sendToken) - }*/ - - self.mainThreadProcessedOperations(ofType: .generic) + TransactionService.shared.walletConnectOperationData.sendData.chosenAmount = xtzAmount + self?.mainThreadProcessedOperations(ofType: .generic) + } } }