-
Notifications
You must be signed in to change notification settings - Fork 199
feat(swapper): add Fynd POC integration #12528
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Masha-lla
wants to merge
3
commits into
shapeshift:develop
Choose a base branch
from
Masha-lla:codex/poc-fynd-swapper
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,5 @@ | ||
| import type { Csp } from '../../../types' | ||
|
|
||
| export const csp: Csp = { | ||
| 'connect-src': ['http://localhost:3001', 'https://fynd-api.propellerheads.xyz'], | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,4 @@ | ||
| import type { Swapper } from '../../types' | ||
| import { executeEvmTransaction } from '../../utils' | ||
|
|
||
| export const fyndSwapper: Swapper = { executeEvmTransaction } |
This file contains hidden or 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,14 @@ | ||
| import type { SwapperApi } from '../../types' | ||
| import { checkEvmSwapStatus } from '../../utils' | ||
| import { getEvmTransactionFees, getUnsignedEvmTransaction } from '../../utils/evm' | ||
| import { getTradeQuote } from './getTradeQuote/getTradeQuote' | ||
| import { getTradeRate } from './getTradeRate/getTradeRate' | ||
| import type { FyndTradeQuoteInput, FyndTradeRateInput } from './types' | ||
|
|
||
| export const fyndApi: SwapperApi = { | ||
| getTradeQuote: (input, deps) => getTradeQuote(input as FyndTradeQuoteInput, deps), | ||
| getTradeRate: (input, deps) => getTradeRate(input as FyndTradeRateInput, deps), | ||
| getUnsignedEvmTransaction, | ||
| getEvmTransactionFees, | ||
| checkTradeStatus: checkEvmSwapStatus, | ||
| } |
89 changes: 89 additions & 0 deletions
89
packages/swapper/src/swappers/FyndSwapper/getTradeQuote/getTradeQuote.ts
This file contains hidden or 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,89 @@ | ||
| import type { Result } from '@sniptt/monads' | ||
| import { Err, Ok } from '@sniptt/monads' | ||
|
|
||
| import { getDefaultSlippageDecimalPercentageForSwapper } from '../../../constants' | ||
| import type { | ||
| SingleHopTradeQuoteSteps, | ||
| SwapErrorRight, | ||
| SwapperDeps, | ||
| TradeQuote, | ||
| } from '../../../types' | ||
| import { SwapperName, TradeQuoteError } from '../../../types' | ||
| import { assertQuoteAddresses, makeSwapErrorRight } from '../../../utils' | ||
| import type { FyndTradeQuoteInput } from '../types' | ||
| import { fetchFromFynd } from '../utils/fetchFromFynd' | ||
| import { getFyndStepData } from '../utils/getFyndStepData' | ||
| import { getFyndTradeContext } from '../utils/getFyndTradeContext' | ||
| import { assertValidTrade } from '../utils/helpers' | ||
|
|
||
| export const getTradeQuote = async ( | ||
| input: FyndTradeQuoteInput, | ||
| deps: SwapperDeps, | ||
| ): Promise<Result<TradeQuote[], SwapErrorRight>> => { | ||
| const maybeAddresses = assertQuoteAddresses(input) | ||
| if (maybeAddresses.isErr()) return Err(maybeAddresses.unwrapErr()) | ||
| const { sendAddress, receiveAddress } = maybeAddresses.unwrap() | ||
|
|
||
| const validation = assertValidTrade(input) | ||
| if (validation.isErr()) return Err(validation.unwrapErr()) | ||
|
|
||
| const slippageTolerancePercentageDecimal = | ||
| input.slippageTolerancePercentageDecimal ?? | ||
| getDefaultSlippageDecimalPercentageForSwapper(SwapperName.Fynd) | ||
| const maybeFynd = await fetchFromFynd({ | ||
| sellAsset: input.sellAsset, | ||
| buyAsset: input.buyAsset, | ||
| sellAmountCryptoBaseUnit: input.sellAmountIncludingProtocolFeesCryptoBaseUnit, | ||
| sender: sendAddress, | ||
| receiver: receiveAddress, | ||
| slippageTolerancePercentageDecimal, | ||
| baseUrl: deps.config.VITE_FYND_ETHEREUM_BASE_URL, | ||
| quoteOrRate: 'quote', | ||
| }) | ||
| if (maybeFynd.isErr()) return Err(maybeFynd.unwrapErr()) | ||
| const { quote, routerAddress } = maybeFynd.unwrap() | ||
| if (!quote.transaction) { | ||
| return Err( | ||
| makeSwapErrorRight({ | ||
| message: 'Fynd returned an unencoded quote', | ||
| code: TradeQuoteError.InvalidResponse, | ||
| }), | ||
| ) | ||
| } | ||
|
|
||
| const maybeContext = getFyndTradeContext({ | ||
| input, | ||
| quote, | ||
| routerAddress, | ||
| slippageTolerancePercentageDecimal, | ||
| }) | ||
| if (maybeContext.isErr()) return Err(maybeContext.unwrapErr()) | ||
| const { tradeCommon, stepCommon, protocolFees } = maybeContext.unwrap() | ||
|
|
||
| const maybeStepData = await getFyndStepData({ | ||
| type: 'quote', | ||
| input, | ||
| deps, | ||
| sellAsset: input.sellAsset, | ||
| transaction: quote.transaction, | ||
| from: sendAddress, | ||
| }) | ||
| if (maybeStepData.isErr()) return Err(maybeStepData.unwrapErr()) | ||
| const { transactionData, networkFeeCryptoBaseUnit } = maybeStepData.unwrap() | ||
|
|
||
| const tradeQuote: TradeQuote = { | ||
| ...tradeCommon, | ||
| quoteOrRate: 'quote', | ||
| receiveAddress, | ||
| steps: [ | ||
| { | ||
| ...stepCommon, | ||
| accountNumber: input.accountNumber, | ||
| transactionData, | ||
| feeData: { networkFeeCryptoBaseUnit, protocolFees }, | ||
| }, | ||
| ] as SingleHopTradeQuoteSteps, | ||
| } | ||
|
|
||
| return Ok([tradeQuote]) | ||
| } |
76 changes: 76 additions & 0 deletions
76
packages/swapper/src/swappers/FyndSwapper/getTradeRate/getTradeRate.ts
This file contains hidden or 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,76 @@ | ||
| import type { Result } from '@sniptt/monads' | ||
| import { Err, Ok } from '@sniptt/monads' | ||
|
|
||
| import { getDefaultSlippageDecimalPercentageForSwapper } from '../../../constants' | ||
| import type { | ||
| SingleHopTradeRateSteps, | ||
| SwapErrorRight, | ||
| SwapperDeps, | ||
| TradeRate, | ||
| } from '../../../types' | ||
| import { SwapperName } from '../../../types' | ||
| import type { FyndTradeRateInput } from '../types' | ||
| import { FYND_RATE_ADDRESS } from '../utils/constants' | ||
| import { fetchFromFynd } from '../utils/fetchFromFynd' | ||
| import { getFyndStepData } from '../utils/getFyndStepData' | ||
| import { getFyndTradeContext } from '../utils/getFyndTradeContext' | ||
| import { assertValidTrade } from '../utils/helpers' | ||
|
|
||
| export const getTradeRate = async ( | ||
| input: FyndTradeRateInput, | ||
| deps: SwapperDeps, | ||
| ): Promise<Result<TradeRate[], SwapErrorRight>> => { | ||
| const validation = assertValidTrade(input) | ||
| if (validation.isErr()) return Err(validation.unwrapErr()) | ||
|
|
||
| const address = input.receiveAddress ?? FYND_RATE_ADDRESS | ||
| const slippageTolerancePercentageDecimal = | ||
| input.slippageTolerancePercentageDecimal ?? | ||
| getDefaultSlippageDecimalPercentageForSwapper(SwapperName.Fynd) | ||
| const maybeFynd = await fetchFromFynd({ | ||
| sellAsset: input.sellAsset, | ||
| buyAsset: input.buyAsset, | ||
| sellAmountCryptoBaseUnit: input.sellAmountIncludingProtocolFeesCryptoBaseUnit, | ||
| sender: address, | ||
| receiver: address, | ||
| slippageTolerancePercentageDecimal, | ||
| baseUrl: deps.config.VITE_FYND_ETHEREUM_BASE_URL, | ||
| quoteOrRate: 'rate', | ||
| }) | ||
| if (maybeFynd.isErr()) return Err(maybeFynd.unwrapErr()) | ||
| const { quote, routerAddress } = maybeFynd.unwrap() | ||
|
|
||
| const maybeContext = getFyndTradeContext({ | ||
| input, | ||
| quote, | ||
| routerAddress, | ||
| slippageTolerancePercentageDecimal, | ||
| }) | ||
| if (maybeContext.isErr()) return Err(maybeContext.unwrapErr()) | ||
| const { tradeCommon, stepCommon, protocolFees } = maybeContext.unwrap() | ||
| const maybeStepData = await getFyndStepData({ | ||
| type: 'rate', | ||
| input, | ||
| deps, | ||
| sellAsset: input.sellAsset, | ||
| gasEstimate: quote.gas_estimate, | ||
| gasPrice: quote.gas_price, | ||
| }) | ||
| if (maybeStepData.isErr()) return Err(maybeStepData.unwrapErr()) | ||
| const { networkFeeCryptoBaseUnit } = maybeStepData.unwrap() | ||
|
|
||
| const tradeRate: TradeRate = { | ||
| ...tradeCommon, | ||
| quoteOrRate: 'rate', | ||
| receiveAddress: input.receiveAddress, | ||
| steps: [ | ||
| { | ||
| ...stepCommon, | ||
| accountNumber: input.accountNumber, | ||
| feeData: { networkFeeCryptoBaseUnit, protocolFees }, | ||
| }, | ||
| ] as SingleHopTradeRateSteps, | ||
| } | ||
|
|
||
| return Ok([tradeRate]) | ||
| } |
This file contains hidden or 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,3 @@ | ||
| export { fyndApi } from './endpoints' | ||
| export { fyndSwapper } from './FyndSwapper' | ||
| export * from './types' |
This file contains hidden or 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,60 @@ | ||
| import type { Address, Hex } from 'viem' | ||
|
|
||
| import type { GetEvmTradeQuoteInput, GetEvmTradeRateInput } from '../../types' | ||
|
|
||
| export type FyndTradeQuoteInput = GetEvmTradeQuoteInput | ||
| export type FyndTradeRateInput = GetEvmTradeRateInput | ||
|
|
||
| export type FyndQuoteStatus = | ||
| | 'success' | ||
| | 'no_route_found' | ||
| | 'insufficient_liquidity' | ||
| | 'timeout' | ||
| | 'not_ready' | ||
| | 'price_check_failed' | ||
|
|
||
| export type FyndTransaction = { | ||
| to: Address | ||
| value: string | ||
| data: Hex | ||
| client_fee_signature_offset: number | null | ||
| } | ||
|
|
||
| export type FyndFeeBreakdown = { | ||
| router_fee: string | ||
| client_fee: string | ||
| max_slippage: string | ||
| min_amount_received: string | ||
| swaps_hash: string | null | ||
| } | ||
|
|
||
| export type FyndOrderQuote = { | ||
| order_id: string | ||
| status: FyndQuoteStatus | ||
| amount_in: string | ||
| amount_out: string | ||
| amount_out_net_gas: string | ||
| gas_estimate: string | ||
| gas_price: string | null | ||
| price_impact_bps: number | null | ||
| route: { | ||
| swaps: { | ||
| protocol: string | ||
| }[] | ||
| } | null | ||
| transaction: FyndTransaction | null | ||
| fee_breakdown: FyndFeeBreakdown | null | ||
| } | ||
|
|
||
| export type FyndQuoteResponse = { | ||
| orders: FyndOrderQuote[] | ||
| total_gas_estimate: string | ||
| solve_time_ms: number | ||
| } | ||
|
|
||
| export type FyndInfoResponse = { | ||
| chain_id: number | ||
| router_address: Address | null | ||
| permit2_address: Address | null | ||
| version: string | ||
| } |
This file contains hidden or 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,9 @@ | ||
| import { KnownChainIds } from '@shapeshiftoss/types' | ||
| import type { Address } from 'viem' | ||
|
|
||
| export const FYND_SUPPORTED_CHAIN_IDS = [KnownChainIds.EthereumMainnet] as const | ||
| export type FyndSupportedChainId = (typeof FYND_SUPPORTED_CHAIN_IDS)[number] | ||
|
|
||
| export const FYND_NATIVE_ASSET_ADDRESS = '0x0000000000000000000000000000000000000000' as Address | ||
| export const FYND_RATE_ADDRESS = '0x0000000000000000000000000000000000000001' as Address | ||
| export const FYND_ROUTER_FEE_DIVISOR = '100000' |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🔒 Security & Privacy | 🟠 Major | ⚡ Quick win
🧩 Analysis chain
🏁 Script executed:
Repository: shapeshift/web
Length of output: 22478
🏁 Script executed:
Repository: shapeshift/web
Length of output: 18303
🏁 Script executed:
Repository: shapeshift/web
Length of output: 19607
🏁 Script executed:
Repository: shapeshift/web
Length of output: 39202
🏁 Script executed:
Repository: shapeshift/web
Length of output: 9015
Security And Privacy (CWE-16)
Reachability: Internal · Exploitability: Moderate
Reachability path
Gate the Fynd registry entry with
VITE_FEATURE_FYND_SWAP.packages/swapper/src/constants.tsexportsfyndSwapperandfyndApidirectly intoswappers[SwapperName.Fynd], making quote/rate/unsigned transaction methods reachable through public API paths. Add public-API env/config support for this flag and skip exporting Fynd unless it is enabled, instead of relying only on app selectors/UI gating.🤖 Prompt for AI Agents