-
Notifications
You must be signed in to change notification settings - Fork 186
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: split trade rates and quotes final wire-up (#8079)
- Loading branch information
1 parent
abd0d45
commit bd8ac25
Showing
69 changed files
with
2,722 additions
and
2,304 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
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
106 changes: 106 additions & 0 deletions
106
packages/swapper/src/swappers/ArbitrumBridgeSwapper/getTradeRate/getTradeRate.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,106 @@ | ||
import { ethChainId } from '@shapeshiftoss/caip' | ||
import { type HDWallet } from '@shapeshiftoss/hdwallet-core' | ||
import type { Result } from '@sniptt/monads' | ||
import { Err, Ok } from '@sniptt/monads' | ||
import { v4 as uuid } from 'uuid' | ||
|
||
import { getDefaultSlippageDecimalPercentageForSwapper } from '../../../constants' | ||
import type { | ||
GetEvmTradeQuoteInputBase, | ||
GetEvmTradeRateInput, | ||
SingleHopTradeRateSteps, | ||
SwapErrorRight, | ||
SwapperDeps, | ||
} from '../../../types' | ||
import { SwapperName, TradeQuoteError } from '../../../types' | ||
import { makeSwapErrorRight } from '../../../utils' | ||
import type { ArbitrumBridgeTradeRate } from '../types' | ||
import { fetchArbitrumBridgePrice } from '../utils/fetchArbitrumBridgeSwap' | ||
import { assertValidTrade } from '../utils/helpers' | ||
|
||
export type GetEvmTradeQuoteInputWithWallet = Omit<GetEvmTradeQuoteInputBase, 'supportsEIP1559'> & { | ||
wallet: HDWallet | ||
} | ||
|
||
export async function getTradeRate( | ||
input: GetEvmTradeRateInput, | ||
{ assertGetEvmChainAdapter }: SwapperDeps, | ||
): Promise<Result<ArbitrumBridgeTradeRate, SwapErrorRight>> { | ||
const { | ||
chainId, | ||
sellAsset, | ||
buyAsset, | ||
supportsEIP1559, | ||
receiveAddress, | ||
sellAmountIncludingProtocolFeesCryptoBaseUnit, | ||
sendAddress, | ||
} = input | ||
|
||
const assertion = await assertValidTrade({ buyAsset, sellAsset }) | ||
if (assertion.isErr()) return Err(assertion.unwrapErr()) | ||
|
||
const isDeposit = sellAsset.chainId === ethChainId | ||
|
||
// 15 minutes for deposits, 7 days for withdrawals | ||
const estimatedExecutionTimeMs = isDeposit ? 15 * 60 * 1000 : 7 * 24 * 60 * 60 * 1000 | ||
|
||
// 1/1 when bridging on Arbitrum bridge | ||
const rate = '1' | ||
|
||
try { | ||
const args = { | ||
supportsEIP1559, | ||
chainId, | ||
buyAsset, | ||
sellAmountIncludingProtocolFeesCryptoBaseUnit, | ||
sellAsset, | ||
sendAddress, | ||
receiveAddress, | ||
assertGetEvmChainAdapter, | ||
quoteOrRate: 'rate', | ||
} | ||
const swap = await fetchArbitrumBridgePrice(args) | ||
|
||
const buyAmountBeforeFeesCryptoBaseUnit = sellAmountIncludingProtocolFeesCryptoBaseUnit | ||
const buyAmountAfterFeesCryptoBaseUnit = sellAmountIncludingProtocolFeesCryptoBaseUnit | ||
|
||
return Ok({ | ||
id: uuid(), | ||
accountNumber: undefined, | ||
receiveAddress: undefined, | ||
affiliateBps: '0', | ||
potentialAffiliateBps: '0', | ||
rate, | ||
slippageTolerancePercentageDecimal: getDefaultSlippageDecimalPercentageForSwapper( | ||
SwapperName.ArbitrumBridge, | ||
), | ||
steps: [ | ||
{ | ||
estimatedExecutionTimeMs, | ||
allowanceContract: swap.allowanceContract, | ||
rate, | ||
buyAsset, | ||
sellAsset, | ||
accountNumber: undefined, | ||
buyAmountBeforeFeesCryptoBaseUnit, | ||
buyAmountAfterFeesCryptoBaseUnit, | ||
sellAmountIncludingProtocolFeesCryptoBaseUnit, | ||
feeData: { | ||
protocolFees: {}, | ||
networkFeeCryptoBaseUnit: swap.networkFeeCryptoBaseUnit, | ||
}, | ||
source: SwapperName.ArbitrumBridge, | ||
}, | ||
] as SingleHopTradeRateSteps, | ||
direction: isDeposit ? ('deposit' as const) : ('withdrawal' as const), | ||
}) | ||
} catch (err) { | ||
return Err( | ||
makeSwapErrorRight({ | ||
message: '[ArbitrumBridge: tradeQuote] - failed to get fee data', | ||
cause: err, | ||
code: TradeQuoteError.NetworkFeeEstimationFailed, | ||
}), | ||
) | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
packages/swapper/src/swappers/ArbitrumBridgeSwapper/types.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 |
---|---|---|
@@ -1,6 +1,21 @@ | ||
import type { HDWallet } from '@shapeshiftoss/hdwallet-core' | ||
|
||
import type { GetEvmTradeQuoteInputBase, TradeQuote, TradeRate } from '../../types' | ||
|
||
export enum BRIDGE_TYPE { | ||
ETH_DEPOSIT = 'ETH Deposit', | ||
ERC20_DEPOSIT = 'ERC20 Deposit', | ||
ETH_WITHDRAWAL = 'ETH Withdrawal', | ||
ERC20_WITHDRAWAL = 'ERC20 Withdrawal', | ||
} | ||
|
||
export type GetEvmTradeQuoteInputWithWallet = Omit<GetEvmTradeQuoteInputBase, 'supportsEIP1559'> & { | ||
wallet: HDWallet | ||
} | ||
|
||
type ArbitrumBridgeSpecificMetadata = { | ||
direction: 'deposit' | 'withdrawal' | ||
} | ||
|
||
export type ArbitrumBridgeTradeQuote = TradeQuote & ArbitrumBridgeSpecificMetadata | ||
export type ArbitrumBridgeTradeRate = TradeRate & ArbitrumBridgeSpecificMetadata |
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
20 changes: 20 additions & 0 deletions
20
packages/swapper/src/swappers/ChainflipSwapper/swapperApi/getTradeRate.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,20 @@ | ||
import type { Result } from '@sniptt/monads' | ||
|
||
import type { | ||
CommonTradeQuoteInput, | ||
GetTradeRateInput, | ||
SwapErrorRight, | ||
SwapperDeps, | ||
TradeRate, | ||
} from '../../../types' | ||
import { _getTradeQuote } from './getTradeQuote' | ||
|
||
// This isn't a mistake. A trade rate *is* a trade quote. Chainflip doesn't really have a notion of a trade quote, | ||
// they do have a notion of a "swap" (which we effectively only use to get the deposit address), which is irrelevant to the notion of quote vs. rate | ||
export const getTradeRate = async ( | ||
input: GetTradeRateInput, | ||
deps: SwapperDeps, | ||
): Promise<Result<TradeRate[], SwapErrorRight>> => { | ||
const rates = await _getTradeQuote(input as unknown as CommonTradeQuoteInput, deps) | ||
return rates as Result<TradeRate[], SwapErrorRight> | ||
} |
Oops, something went wrong.