Skip to content

Commit

Permalink
feat(earn): Add pending transaction for EarnSwapDeposit (#6054)
Browse files Browse the repository at this point in the history
### Description

title

### Test plan


https://github.com/user-attachments/assets/3b39f849-80b8-4f5c-ad9b-d94d1cedc067

Unit tests updated

### Related issues

- Fixes ACT-1361

### Backwards compatibility

Yes

### Network scalability

If a new NetworkId and/or Network are added in the future, the changes
in this PR will:

- [X] Continue to work without code changes, OR trigger a compilation
error (guaranteeing we find it when a new network is added)
  • Loading branch information
finnian0826 authored Sep 19, 2024
1 parent 4727655 commit 2aeec76
Show file tree
Hide file tree
Showing 5 changed files with 243 additions and 7 deletions.
40 changes: 37 additions & 3 deletions src/earn/saga.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,40 @@ describe('depositSubmitSaga', () => {
providerId: mockEarnPositions[0].appId,
}

const expectedSwapDepositStandbyTx = {
__typename: 'EarnSwapDeposit',
context: {
id: 'id-earn/saga-Earn/SwapDeposit',
tag: 'earn/saga',
description: 'Earn/SwapDeposit',
},
networkId: NetworkId['arbitrum-sepolia'],
swap: {
inAmount: {
value: '100',
tokenId: mockArbUsdcTokenId,
},
outAmount: {
value: '50',
tokenId: mockArbArbTokenId,
},
},
deposit: {
inAmount: {
value: '100',
tokenId: networkConfig.aaveArbUsdcTokenId,
},
outAmount: {
value: '100',
tokenId: mockArbUsdcTokenId,
},
providerId: mockEarnPositions[0].appId,
},
transactionHash: '0x2',
type: TokenTransactionTypeV2.EarnSwapDeposit,
feeCurrencyId: undefined,
}

const expectedApproveGasAnalyticsProperties = {
approveTxCumulativeGasUsed: 3129217,
approveTxEffectiveGasPrice: 5000000000,
Expand Down Expand Up @@ -372,7 +406,7 @@ describe('depositSubmitSaga', () => {
approvedAmount: '50',
tokenId: mockArbArbTokenId,
})
expect(mockStandbyHandler).toHaveBeenNthCalledWith(2, expectedDepositStandbyTx)
expect(mockStandbyHandler).toHaveBeenNthCalledWith(2, expectedSwapDepositStandbyTx)
expect(AppAnalytics.track).toHaveBeenCalledWith(EarnEvents.earn_deposit_submit_start, {
...expectedAnalyticsProps,
fromTokenAmount: '50',
Expand Down Expand Up @@ -419,7 +453,7 @@ describe('depositSubmitSaga', () => {
expect(navigateHome).toHaveBeenCalled()
expect(decodeFunctionData).not.toHaveBeenCalled()
expect(mockStandbyHandler).toHaveBeenCalledTimes(1)
expect(mockStandbyHandler).toHaveBeenCalledWith(expectedDepositStandbyTx)
expect(mockStandbyHandler).toHaveBeenCalledWith(expectedSwapDepositStandbyTx)
expect(AppAnalytics.track).toHaveBeenCalledWith(EarnEvents.earn_deposit_submit_start, {
...expectedAnalyticsProps,
fromTokenAmount: '50',
Expand Down Expand Up @@ -519,7 +553,7 @@ describe('depositSubmitSaga', () => {
})
expect(mockStandbyHandler).toHaveBeenCalledTimes(2)
expect(mockStandbyHandler).toHaveBeenNthCalledWith(1, null)
expect(mockStandbyHandler).toHaveBeenNthCalledWith(2, expectedDepositStandbyTx)
expect(mockStandbyHandler).toHaveBeenNthCalledWith(2, expectedSwapDepositStandbyTx)
})

it('dispatches cancel action if pin input is cancelled and does not navigate home', async () => {
Expand Down
26 changes: 25 additions & 1 deletion src/earn/saga.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,31 @@ export function* depositSubmitSaga(action: PayloadAction<DepositInfo>) {
feeCurrencyId,
}
}
createDepositStandbyTxHandlers.push(createDepositStandbyTx)
const createSwapDepositStandbyTx = (
transactionHash: string,
feeCurrencyId?: string
): BaseStandbyTransaction => {
return {
context: newTransactionContext(TAG, 'Earn/SwapDeposit'),
__typename: 'EarnSwapDeposit',
networkId,
type: TokenTransactionTypeV2.EarnSwapDeposit,
swap: {
inAmount: { value: amount, tokenId: depositTokenId },
outAmount: { value: fromTokenAmount, tokenId: fromTokenId },
},
deposit: {
inAmount: { value: amount, tokenId: pool.dataProps.withdrawTokenId },
outAmount: { value: amount, tokenId: depositTokenId },
providerId: pool.appId,
},
transactionHash,
feeCurrencyId,
}
}
createDepositStandbyTxHandlers.push(
mode === 'deposit' ? createDepositStandbyTx : createSwapDepositStandbyTx
)
} else {
Logger.info(TAG, 'More than 2 deposit transactions, using empty standby handlers')
createDepositStandbyTxHandlers.push(...preparedTransactions.map(() => () => null))
Expand Down
2 changes: 2 additions & 0 deletions src/transactions/actions.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
EarnClaimReward,
EarnDeposit,
EarnSwapDeposit,
EarnWithdraw,
Fee,
NetworkId,
Expand Down Expand Up @@ -28,6 +29,7 @@ export type BaseStandbyTransaction =
| BaseStandbyTransactionType<TokenApproval>
| BaseStandbyTransactionType<NftTransfer>
| BaseStandbyTransactionType<EarnDeposit>
| BaseStandbyTransactionType<EarnSwapDeposit>
| BaseStandbyTransactionType<EarnWithdraw>
| BaseStandbyTransactionType<EarnClaimReward>

Expand Down
2 changes: 2 additions & 0 deletions src/transactions/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export type ConfirmedStandbyTransaction = (
| Omit<TokenApproval, 'status'>
| Omit<NftTransfer, 'status'>
| Omit<EarnDeposit, 'status'>
| Omit<EarnSwapDeposit, 'status'>
| Omit<EarnWithdraw, 'status'>
| Omit<EarnClaimReward, 'status'>
) & {
Expand All @@ -55,6 +56,7 @@ export type StandbyTransaction =
| PendingStandbyTransaction<TokenApproval>
| PendingStandbyTransaction<NftTransfer>
| PendingStandbyTransaction<EarnDeposit>
| PendingStandbyTransaction<EarnSwapDeposit>
| PendingStandbyTransaction<EarnWithdraw>
| PendingStandbyTransaction<EarnClaimReward>
| ConfirmedStandbyTransaction
Expand Down
180 changes: 177 additions & 3 deletions test/RootStateSchema.json
Original file line number Diff line number Diff line change
Expand Up @@ -2633,6 +2633,84 @@
],
"type": "object"
},
"PendingStandbyTransaction<EarnSwapDeposit>": {
"additionalProperties": false,
"properties": {
"__typename": {
"const": "EarnSwapDeposit",
"type": "string"
},
"context": {
"$ref": "#/definitions/TransactionContext"
},
"deposit": {
"additionalProperties": false,
"properties": {
"inAmount": {
"$ref": "#/definitions/TokenAmount"
},
"outAmount": {
"$ref": "#/definitions/TokenAmount"
},
"providerId": {
"type": "string"
}
},
"required": [
"inAmount",
"outAmount",
"providerId"
],
"type": "object"
},
"feeCurrencyId": {
"type": "string"
},
"networkId": {
"$ref": "#/definitions/NetworkId"
},
"status": {
"const": "Pending",
"type": "string"
},
"swap": {
"additionalProperties": false,
"properties": {
"inAmount": {
"$ref": "#/definitions/TokenAmount"
},
"outAmount": {
"$ref": "#/definitions/TokenAmount"
}
},
"required": [
"inAmount",
"outAmount"
],
"type": "object"
},
"timestamp": {
"type": "number"
},
"transactionHash": {
"type": "string"
},
"type": {
"$ref": "#/definitions/TokenTransactionTypeV2"
}
},
"required": [
"__typename",
"context",
"deposit",
"networkId",
"status",
"swap",
"timestamp",
"type"
],
"type": "object"
},
"PendingStandbyTransaction<EarnWithdraw>": {
"additionalProperties": false,
"properties": {
Expand Down Expand Up @@ -3685,6 +3763,9 @@
{
"$ref": "#/definitions/PendingStandbyTransaction<EarnDeposit>"
},
{
"$ref": "#/definitions/PendingStandbyTransaction<EarnSwapDeposit>"
},
{
"$ref": "#/definitions/PendingStandbyTransaction<EarnWithdraw>"
},
Expand Down Expand Up @@ -4023,6 +4104,99 @@
],
"type": "object"
},
{
"additionalProperties": false,
"properties": {
"__typename": {
"const": "EarnSwapDeposit",
"type": "string"
},
"block": {
"type": "string"
},
"context": {
"$ref": "#/definitions/TransactionContext"
},
"deposit": {
"additionalProperties": false,
"properties": {
"inAmount": {
"$ref": "#/definitions/TokenAmount"
},
"outAmount": {
"$ref": "#/definitions/TokenAmount"
},
"providerId": {
"type": "string"
}
},
"required": [
"inAmount",
"outAmount",
"providerId"
],
"type": "object"
},
"feeCurrencyId": {
"type": "string"
},
"fees": {
"items": {
"$ref": "#/definitions/Fee"
},
"type": "array"
},
"networkId": {
"$ref": "#/definitions/NetworkId"
},
"status": {
"enum": [
"Complete",
"Failed"
],
"type": "string"
},
"swap": {
"additionalProperties": false,
"properties": {
"inAmount": {
"$ref": "#/definitions/TokenAmount"
},
"outAmount": {
"$ref": "#/definitions/TokenAmount"
}
},
"required": [
"inAmount",
"outAmount"
],
"type": "object"
},
"timestamp": {
"type": "number"
},
"transactionHash": {
"type": "string"
},
"type": {
"$ref": "#/definitions/TokenTransactionTypeV2"
}
},
"required": [
"__typename",
"block",
"context",
"deposit",
"fees",
"networkId",
"status",
"swap",
"timestamp",
"transactionHash",
"type"
],
"type": "object"
},
{
"additionalProperties": false,
"properties": {
Expand Down Expand Up @@ -5885,13 +6059,13 @@
"$ref": "#/definitions/EarnDeposit"
},
{
"$ref": "#/definitions/EarnWithdraw"
"$ref": "#/definitions/EarnSwapDeposit"
},
{
"$ref": "#/definitions/EarnClaimReward"
"$ref": "#/definitions/EarnWithdraw"
},
{
"$ref": "#/definitions/EarnSwapDeposit"
"$ref": "#/definitions/EarnClaimReward"
}
]
},
Expand Down

0 comments on commit 2aeec76

Please sign in to comment.