forked from brave/brave-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: use api endpoints for swap logic (brave#21454)
* chore: use api endpoints for swap logic * chore: move sendSolanaSerializedTransaction to a mutation
- Loading branch information
1 parent
67c3a5f
commit 6e1a0a2
Showing
7 changed files
with
207 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
components/brave_wallet_ui/common/slices/endpoints/swap.endpoints.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
// Copyright (c) 2023 The Brave Authors. All rights reserved. | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this file, | ||
// You can obtain one at https://mozilla.org/MPL/2.0/. | ||
|
||
// types | ||
import { BraveWallet } from '../../../constants/types' | ||
import { WalletApiEndpointBuilderParams } from '../api-base.slice' | ||
|
||
// utils | ||
import { handleEndpointError } from '../../../utils/api-utils' | ||
|
||
export const swapEndpoints = ({ | ||
mutation, | ||
query | ||
}: WalletApiEndpointBuilderParams) => { | ||
return { | ||
generateBraveSwapFee: mutation< | ||
{ | ||
response: BraveWallet.BraveSwapFeeResponse | null | ||
errorString: string | ||
}, | ||
BraveWallet.BraveSwapFeeParams | ||
>({ | ||
queryFn: async (params, { endpoint }, extraOptions, baseQuery) => { | ||
const { swapService } = baseQuery(undefined).data | ||
try { | ||
const result = await swapService.getBraveFee(params) | ||
return { | ||
data: result | ||
} | ||
} catch (error) { | ||
return handleEndpointError( | ||
endpoint, | ||
'Unable to fetch Brave Swap fee', | ||
error | ||
) | ||
} | ||
} | ||
}), | ||
|
||
generateSwapQuote: mutation< | ||
{ | ||
response: BraveWallet.SwapQuoteUnion | null | ||
error: BraveWallet.SwapErrorUnion | null | ||
errorString: string | ||
}, | ||
BraveWallet.SwapQuoteParams | ||
>({ | ||
queryFn: async (params, { endpoint }, extraOptions, baseQuery) => { | ||
const { swapService } = baseQuery(undefined).data | ||
try { | ||
const result = await swapService.getQuote(params) | ||
return { | ||
data: result | ||
} | ||
} catch (error) { | ||
return handleEndpointError( | ||
endpoint, | ||
'Unable to generate Brave Swap quote', | ||
error | ||
) | ||
} | ||
} | ||
}), | ||
|
||
generateSwapTransaction: mutation< | ||
{ | ||
response: BraveWallet.SwapTransactionUnion | null | ||
error: BraveWallet.SwapErrorUnion | null | ||
errorString: string | ||
}, | ||
BraveWallet.SwapTransactionParamsUnion | ||
>({ | ||
queryFn: async (params, { endpoint }, extraOptions, baseQuery) => { | ||
const { swapService } = baseQuery(undefined).data | ||
try { | ||
const result = await swapService.getTransaction(params) | ||
return { | ||
data: result | ||
} | ||
} catch (error) { | ||
return handleEndpointError( | ||
endpoint, | ||
'Unable to generate Brave Swap transaction', | ||
error | ||
) | ||
} | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.