From da2e76060bbc6736ec4b18ba50fdba8018745584 Mon Sep 17 00:00:00 2001 From: StephenHeaps <5314553+StephenHeaps@users.noreply.github.com> Date: Wed, 10 Jan 2024 15:02:34 -0500 Subject: [PATCH] Fix #8603: Jupiter v4 to v6 migration & BraveCore bump to v1.63.107 (#8633) * Update `SwapTokenStore` & `SwapTokenStoreTests` for Jupiter v6 changes. * Update BraveCore to v1.63.107 --- .../Crypto/Stores/SwapTokenStore.swift | 26 ++++------- .../SwapTokenStoreTests.swift | 46 +++++++++++-------- package-lock.json | 12 ++--- package.json | 2 +- 4 files changed, 43 insertions(+), 43 deletions(-) diff --git a/Sources/BraveWallet/Crypto/Stores/SwapTokenStore.swift b/Sources/BraveWallet/Crypto/Stores/SwapTokenStore.swift index c9d869850b5..2b865effb23 100644 --- a/Sources/BraveWallet/Crypto/Stores/SwapTokenStore.swift +++ b/Sources/BraveWallet/Crypto/Stores/SwapTokenStore.swift @@ -687,20 +687,19 @@ public class SwapTokenStore: ObservableObject, WalletObserverStore { _ response: BraveWallet.JupiterQuote, swapQuoteParams: BraveWallet.SwapQuoteParams ) async { - guard let route = response.routes.first else { return } self.jupiterQuote = response let formatter = WeiFormatter(decimalFormatStyle: .balance) if let selectedToToken { - buyAmount = formatter.decimalString(for: "\(route.otherAmountThreshold)", radix: .decimal, decimals: Int(selectedToToken.decimals)) ?? "" + buyAmount = formatter.decimalString(for: response.outAmount, radix: .decimal, decimals: Int(selectedToToken.decimals)) ?? "" } // No exchange rate is returned by Jupiter API, so we estimate it from the quote. if let selectedFromToken, - let newFromAmount = formatter.decimalString(for: "\(route.inAmount)", radix: .decimal, decimals: Int(selectedFromToken.decimals)), + let newFromAmount = formatter.decimalString(for: response.inAmount, radix: .decimal, decimals: Int(selectedFromToken.decimals)), let newFromAmountWrapped = BDouble(newFromAmount), let selectedToToken, - let newToAmount = formatter.decimalString(for: "\(route.otherAmountThreshold)", radix: .decimal, decimals: Int(selectedToToken.decimals)), + let newToAmount = formatter.decimalString(for: response.outAmount, radix: .decimal, decimals: Int(selectedToToken.decimals)), let newToAmountWrapped = BDouble(newToAmount), newFromAmountWrapped != 0 { let rate = newToAmountWrapped / newFromAmountWrapped @@ -742,25 +741,20 @@ public class SwapTokenStore: ObservableObject, WalletObserverStore { @MainActor private func createSolSwapTransaction() async -> Bool { guard let jupiterQuote, - let route = jupiterQuote.routes.first, - let accountInfo = self.accountInfo, - let selectedToToken, - let selectedFromToken else { + let accountInfo = self.accountInfo else { return false } self.isMakingTx = true defer { self.isMakingTx = false } let network = await rpcService.network(.sol, origin: nil) - let jupiterSwapParams: BraveWallet.JupiterTransactionParams = .init( + let jupiterTransactionParams: BraveWallet.JupiterTransactionParams = .init( + quote: jupiterQuote, chainId: network.chainId, - route: route, - userPublicKey: accountInfo.address, - inputMint: selectedFromToken.contractAddress(in: network), - outputMint: selectedToToken.contractAddress(in: network) + userPublicKey: accountInfo.address ) - let (swapTransactionsUnion, errorResponseUnion, _) = await swapService.transaction(.init(jupiterTransactionParams: jupiterSwapParams)) - guard let swapTransactions = swapTransactionsUnion?.jupiterTransaction else { + let (swapTransactionUnion, errorResponseUnion, _) = await swapService.transaction(.init(jupiterTransactionParams: jupiterTransactionParams)) + guard let jupiterTransaction = swapTransactionUnion?.jupiterTransaction else { // check balance first because error can cause by insufficient balance if let sellTokenBalance = self.selectedFromTokenBalance, let sellAmountValue = BDouble(self.sellAmount.normalizedDecimals), @@ -777,7 +771,7 @@ public class SwapTokenStore: ObservableObject, WalletObserverStore { return false } let (solTxData, status, _) = await solTxManagerProxy.makeTxData( - fromBase64EncodedTransaction: swapTransactions, + fromBase64EncodedTransaction: jupiterTransaction, txType: .solanaSwap, send: .init( maxRetries: .init(maxRetries: 2), diff --git a/Tests/BraveWalletTests/SwapTokenStoreTests.swift b/Tests/BraveWalletTests/SwapTokenStoreTests.swift index 15e36569990..8c7fb8c1d83 100644 --- a/Tests/BraveWalletTests/SwapTokenStoreTests.swift +++ b/Tests/BraveWalletTests/SwapTokenStoreTests.swift @@ -440,19 +440,22 @@ class SwapStoreTests: XCTestCase { network: .mockSolana, coin: .sol ) - swapService._quote = { jupiterQuoteParams, completion in + swapService._quote = { swapQuoteParams, completion in // verify 0.005 is converted to 0.5 - XCTAssertEqual(jupiterQuoteParams.slippagePercentage, "0.5") - let route: BraveWallet.JupiterRoute = .init( - inAmount: 10000000, // 0.01 SOL (9 decimals) - outAmount: 2500000, // 2.5 SPD (6 decimals) - amount: 2500000, // 2.5 SPD (6 decimals) - otherAmountThreshold: 2500000, // 2.5 SPD (6 decimals) + XCTAssertEqual(swapQuoteParams.slippagePercentage, "0.5") + let jupiterQuote: BraveWallet.JupiterQuote = .init( + inputMint: BraveWallet.BlockchainToken.mockSolToken.contractAddress, + inAmount: "10000000", // 0.01 SOL (9 decimals) + outputMint: BraveWallet.BlockchainToken.mockSpdToken.contractAddress, + outAmount: "2500000", // 2.5 SPD (6 decimals) + otherAmountThreshold: "2500000", // 2.5 SPD (6 decimals) swapMode: "", - priceImpactPct: 0, - slippageBps: 50, // 0.5% - marketInfos: []) - completion(.init(jupiterQuote: .init(routes: [route])), nil, "") + slippageBps: "50", // 0.5% + platformFee: nil, + priceImpactPct: "0", + routePlan: [] + ) + completion(.init(jupiterQuote: jupiterQuote), nil, "") } swapService._braveFee = { params, completion in let feeResponse = BraveWallet.BraveSwapFeeResponse( @@ -697,15 +700,18 @@ class SwapStoreTests: XCTestCase { /// Test creating a sol swap transaction @MainActor func testSwapSolSwapTransaction() async { - let route: BraveWallet.JupiterRoute = .init( - inAmount: 3000000000, - outAmount: 2500000, // 2.5 SPD (6 decimals) - amount: 2500000, // 2.5 SPD (6 decimals) - otherAmountThreshold: 2500000, // 2.5 SPD (6 decimals) + let jupiterQuote: BraveWallet.JupiterQuote = .init( + inputMint: BraveWallet.BlockchainToken.mockSolToken.contractAddress, + inAmount: "3000000000", + outputMint: BraveWallet.BlockchainToken.mockSpdToken.contractAddress, + outAmount: "2500000", // 2.5 SPD (6 decimals) + otherAmountThreshold: "2500000", // 2.5 SPD (6 decimals) swapMode: "", - priceImpactPct: 0, - slippageBps: 50, // 0.5% - marketInfos: []) + slippageBps: "50", // 0.5% + platformFee: nil, + priceImpactPct: "0", + routePlan: [] + ) let (keyringService, blockchainRegistry, rpcService, swapService, txService, walletService, ethTxManagerProxy, solTxManagerProxy, mockAssetManager) = setupServices( network: .mockSolana, coin: .sol @@ -738,7 +744,7 @@ class SwapStoreTests: XCTestCase { selectedFromToken: .mockSolToken, selectedToToken: .mockSpdToken, sellAmount: "0.01", - jupiterQuote: .init(routes: [route]) + jupiterQuote: jupiterQuote ) store.state = .swap diff --git a/package-lock.json b/package-lock.json index e4c94b4e0e8..261ec9c72ad 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10,7 +10,7 @@ "license": "MPL-2.0", "dependencies": { "@mozilla/readability": "^0.4.2", - "brave-core-ios": "https://github.com/brave/brave-browser/releases/download/v1.63.90/brave-core-ios-1.63.90.tgz", + "brave-core-ios": "https://github.com/brave/brave-browser/releases/download/v1.63.107/brave-core-ios-1.63.107.tgz", "leo": "github:brave/leo#792ab5c9f82784578e8f8fc14b9eaa24fa1956d2", "leo-sf-symbols": "github:brave/leo-sf-symbols#775bb8fca9df76679b9b272545e162418127c5de", "page-metadata-parser": "^1.1.3", @@ -514,9 +514,9 @@ } }, "node_modules/brave-core-ios": { - "version": "1.63.90", - "resolved": "https://github.com/brave/brave-browser/releases/download/v1.63.90/brave-core-ios-1.63.90.tgz", - "integrity": "sha512-ubHolAfELcJLXdBmFPnV1EAWUaQXlCGZln24MXFHIdVAKyq5FhsRR9zNVuvNaWqwefbxcra+QPVXT6TZcCDIpA==", + "version": "1.63.107", + "resolved": "https://github.com/brave/brave-browser/releases/download/v1.63.107/brave-core-ios-1.63.107.tgz", + "integrity": "sha512-PmAVQXWUoQh3flzt15eUWvdic2v70Gvh+hwT4bRSS2NnpoQr2H5qMRY1YhTHRv+fDwUuwUH8sJyMwxXrpdBjow==", "license": "ISC" }, "node_modules/browserslist": { @@ -3214,8 +3214,8 @@ } }, "brave-core-ios": { - "version": "https://github.com/brave/brave-browser/releases/download/v1.63.90/brave-core-ios-1.63.90.tgz", - "integrity": "sha512-ubHolAfELcJLXdBmFPnV1EAWUaQXlCGZln24MXFHIdVAKyq5FhsRR9zNVuvNaWqwefbxcra+QPVXT6TZcCDIpA==" + "version": "https://github.com/brave/brave-browser/releases/download/v1.63.107/brave-core-ios-1.63.107.tgz", + "integrity": "sha512-PmAVQXWUoQh3flzt15eUWvdic2v70Gvh+hwT4bRSS2NnpoQr2H5qMRY1YhTHRv+fDwUuwUH8sJyMwxXrpdBjow==" }, "browserslist": { "version": "4.17.1", diff --git a/package.json b/package.json index bc00dd222ac..d172a02e0b1 100644 --- a/package.json +++ b/package.json @@ -12,7 +12,7 @@ "license": "MPL-2.0", "dependencies": { "@mozilla/readability": "^0.4.2", - "brave-core-ios": "https://github.com/brave/brave-browser/releases/download/v1.63.90/brave-core-ios-1.63.90.tgz", + "brave-core-ios": "https://github.com/brave/brave-browser/releases/download/v1.63.107/brave-core-ios-1.63.107.tgz", "leo": "github:brave/leo#792ab5c9f82784578e8f8fc14b9eaa24fa1956d2", "leo-sf-symbols": "github:brave/leo-sf-symbols#775bb8fca9df76679b9b272545e162418127c5de", "page-metadata-parser": "^1.1.3",