Skip to content
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

feat(earn): support cross chain swap and deposit #6382

Merged
merged 3 commits into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/analytics/Properties.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1551,7 +1551,7 @@ interface PointsEventsProperties {
export interface EarnCommonProperties {
providerId: string
poolId: string
networkId: NetworkId
networkId: NetworkId // this is always the pool's networkId
depositTokenId: string
}

Expand All @@ -1562,6 +1562,8 @@ interface EarnDepositProperties extends EarnCommonProperties {
// same as the depositTokenAmount and depositTokenId
fromTokenAmount: string
fromTokenId: string
fromNetworkId: NetworkId
swapType?: SwapType
}

interface EarnWithdrawProperties extends EarnCommonProperties {
Expand Down Expand Up @@ -1609,7 +1611,9 @@ interface EarnEventsProperties {
// For withdrawals this will be in units of the depositToken
fromTokenAmount: string
fromTokenId: string
fromNetworkId: NetworkId
depositTokenAmount?: string
swapType?: SwapType // only for swap-deposit
} & EarnCommonProperties
[EarnEvents.earn_deposit_add_gas_press]: EarnCommonProperties & { gasTokenId: string }
[EarnEvents.earn_feed_item_select]: {
Expand Down
55 changes: 50 additions & 5 deletions src/earn/EarnDepositBottomSheet.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
mockAccount,
mockArbEthTokenId,
mockArbUsdcTokenId,
mockCeloTokenId,
mockEarnPositions,
mockTokenBalances,
mockUSDCAddress,
Expand Down Expand Up @@ -88,13 +89,36 @@ const mockSwapDepositProps = {
},
}

const mockCrossChainProps = {
...mockSwapDepositProps,
preparedTransaction: {
...mockPreparedTransaction,
feeCurrency: {
...mockTokenBalances[mockCeloTokenId],
isNative: true,
balance: new BigNumber(10),
priceUsd: new BigNumber(1),
lastKnownPriceUsd: new BigNumber(1),
},
},
inputTokenId: mockCeloTokenId,
swapTransaction: {
...mockSwapDepositProps.swapTransaction,
swapType: 'cross-chain' as const,
estimatedDuration: 300,
maxCrossChainFee: '0.1',
estimatedCrossChainFee: '0.05',
},
}

describe('EarnDepositBottomSheet', () => {
const commonAnalyticsProperties = {
depositTokenId: mockArbUsdcTokenId,
depositTokenAmount: '100',
networkId: NetworkId['arbitrum-sepolia'],
providerId: mockEarnPositions[0].appId,
poolId: mockEarnPositions[0].positionId,
fromNetworkId: NetworkId['arbitrum-sepolia'],
}

beforeEach(() => {
Expand Down Expand Up @@ -143,14 +167,17 @@ describe('EarnDepositBottomSheet', () => {
expect(getByTestId('EarnDeposit/SecondaryCta')).toBeTruthy()
})

it('renders all elements for swap-deposit', () => {
it.each([
{ swapType: 'same-chain', props: mockSwapDepositProps, fromSymbol: 'ETH', fiatFee: '0.012' },
{ swapType: 'cross-chain', props: mockCrossChainProps, fromSymbol: 'CELO', fiatFee: '0.00011' },
])('renders all elements for $swapType swap-deposit', ({ props, fromSymbol, fiatFee }) => {
const { getByTestId, queryByTestId, getByText } = render(
<Provider
store={createMockStore({
tokens: { tokenBalances: mockTokenBalances },
})}
>
<EarnDepositBottomSheet {...mockSwapDepositProps} />
<EarnDepositBottomSheet {...props} />
</Provider>
)
expect(getByText('earnFlow.depositBottomSheet.title')).toBeTruthy()
Expand All @@ -166,11 +193,11 @@ describe('EarnDepositBottomSheet', () => {
expect(getByText('earnFlow.depositBottomSheet.amount')).toBeTruthy()
expect(getByTestId('EarnDeposit/Amount')).toHaveTextContent('100.00 USDC(₱133.00)')

expect(getByTestId('EarnDeposit/Swap/From')).toHaveTextContent('0.041 ETH')
expect(getByTestId('EarnDeposit/Swap/From')).toHaveTextContent(`0.041 ${fromSymbol}`)
expect(getByTestId('EarnDeposit/Swap/To')).toHaveTextContent('100.00 USDC')

expect(getByText('earnFlow.depositBottomSheet.fee')).toBeTruthy()
expect(getByTestId('EarnDeposit/Fee')).toHaveTextContent('₱0.012(0.000006 ETH)')
expect(getByTestId('EarnDeposit/Fee')).toHaveTextContent(`₱${fiatFee}(0.000006 ${fromSymbol})`)

expect(getByText('earnFlow.depositBottomSheet.provider')).toBeTruthy()
expect(getByText('Aave')).toBeTruthy()
Expand All @@ -189,26 +216,43 @@ describe('EarnDepositBottomSheet', () => {

describe.each([
{
testName: 'deposit',
mode: 'deposit',
props: mockDepositProps,
fromTokenAmount: '100',
fromTokenId: mockArbUsdcTokenId,
depositTokenAmount: '100',
swapType: undefined,
},
{
testName: 'same chain swap & deposit',
mode: 'swap-deposit',
props: mockSwapDepositProps,
fromTokenAmount: '0.041',
fromTokenId: mockArbEthTokenId,
depositTokenAmount: '99.999',
swapType: 'same-chain',
},
{
testName: 'cross chain swap & deposit',
mode: 'swap-deposit',
props: mockCrossChainProps,
fromTokenAmount: '0.041',
fromTokenId: mockCeloTokenId,
depositTokenAmount: '99.999',
swapType: 'cross-chain',
},
])('$mode', ({ mode, props, fromTokenAmount, fromTokenId, depositTokenAmount }) => {
])('$testName', ({ mode, props, fromTokenAmount, fromTokenId, depositTokenAmount, swapType }) => {
const fromNetworkId =
swapType === 'cross-chain' ? NetworkId['celo-alfajores'] : NetworkId['arbitrum-sepolia']
const expectedAnalyticsProperties = {
...commonAnalyticsProperties,
mode,
fromTokenAmount,
fromTokenId,
depositTokenAmount,
swapType,
fromNetworkId,
}

it('pressing complete submits action and fires analytics event', () => {
Expand Down Expand Up @@ -362,6 +406,7 @@ describe('EarnDepositBottomSheet', () => {
)

expect(getByTestId('EarnDeposit/GasSubsidized')).toBeTruthy()
expect(earnUtils.isGasSubsidizedForNetwork).toHaveBeenCalledWith(fromNetworkId)
})
})
})
8 changes: 4 additions & 4 deletions src/earn/EarnDepositBottomSheet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,11 @@ export default function EarnDepositBottomSheet({
depositTokenAmount: depositAmount.toString(),
fromTokenId: inputTokenId,
fromTokenAmount: inputAmount.toString(),
fromNetworkId: preparedTransaction.feeCurrency.networkId,
networkId: pool.networkId,
poolId: pool.positionId,
mode,
swapType: swapTransaction?.swapType,
}

const { estimatedFeeAmount, feeCurrency } = getFeeCurrencyAndAmounts(preparedTransaction)
Expand All @@ -84,7 +86,7 @@ export default function EarnDepositBottomSheet({
return null
}

const isGasSubsidized = isGasSubsidizedForNetwork(pool.networkId)
const isGasSubsidized = isGasSubsidizedForNetwork(preparedTransaction.feeCurrency.networkId)
const { termsUrl } = pool.dataProps

const onPressProviderIcon = () => {
Expand Down Expand Up @@ -236,9 +238,7 @@ export default function EarnDepositBottomSheet({
</View>
</LabelledItem>
<LabelledItem label={t('earnFlow.depositBottomSheet.network')}>
<Text style={styles.value}>
{NETWORK_NAMES[preparedTransaction.feeCurrency.networkId]}
</Text>
<Text style={styles.value}>{NETWORK_NAMES[pool.networkId]}</Text>
</LabelledItem>
{termsUrl ? (
<Text style={styles.footer}>
Expand Down
Loading
Loading