Skip to content
This repository has been archived by the owner on May 10, 2024. It is now read-only.

Commit

Permalink
Fix #8603: Jupiter v4 to v6 migration & BraveCore bump to v1.63.107 (#…
Browse files Browse the repository at this point in the history
…8633)

* Update `SwapTokenStore` & `SwapTokenStoreTests` for Jupiter v6 changes.

* Update BraveCore to v1.63.107
  • Loading branch information
StephenHeaps authored Jan 10, 2024
1 parent b4a9c54 commit da2e760
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 43 deletions.
26 changes: 10 additions & 16 deletions Sources/BraveWallet/Crypto/Stores/SwapTokenStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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),
Expand All @@ -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),
Expand Down
46 changes: 26 additions & 20 deletions Tests/BraveWalletTests/SwapTokenStoreTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -738,7 +744,7 @@ class SwapStoreTests: XCTestCase {
selectedFromToken: .mockSolToken,
selectedToToken: .mockSpdToken,
sellAmount: "0.01",
jupiterQuote: .init(routes: [route])
jupiterQuote: jupiterQuote
)
store.state = .swap

Expand Down
12 changes: 6 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit da2e760

Please sign in to comment.