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: approve token transactions on swap #21076

Merged
merged 1 commit into from
Sep 4, 2024

Conversation

briansztamfater
Copy link
Member

@briansztamfater briansztamfater commented Aug 16, 2024

fixes #20343

Summary

This PR implements Approve transctions for ERC20 tokens when getting a Swap Proposal that needs the pay token to be approved before proceeding.

In the video below, an example of Approving 8 DAI on Mainnet

approvetxswap.mp4

Platforms

  • Android
  • iOS
Functional
  • 1-1 chats
  • public chats
  • group chats
  • wallet / transactions
  • dapps / app browsing
  • account recovery
  • new account
  • user profile updates
  • networks
  • mailservers
  • fleet
  • bootnodes
Non-functional
  • battery performance
  • CPU performance / speed of the app
  • network consumption

Steps to test

  • Open Status
  • Login
  • Go to wallet
  • Select an account
  • Select Swap option
  • Select a ERC20 token on Select asset to pay screen
  • Enter a valid amount which is higher than the current approved amount for Paraswap contract
  • Wait for swap proposal to appear
  • At this stage, a valid amount appears on the receive token side, but "Review swap" button should be disabled because we need to first Approve / Set spending cap for the paying amount for the pay token.
  • Tap on Approve
  • Slide to execute the transction
  • Enter password
  • Wait for the approve transction to be confirmed and the status is shown correctly below the token input
  • When the Approve transaction is confirmed, a toast should appear

status: ready

@status-im-auto
Copy link
Member

status-im-auto commented Aug 16, 2024

Jenkins Builds

Click to see older builds (39)
Commit #️⃣ Finished (UTC) Duration Platform Result
8593e74 #1 2024-08-16 21:02:05 ~2 min tests 📄log
✔️ 8593e74 #1 2024-08-16 21:05:48 ~6 min android-e2e 🤖apk 📲
✔️ 8593e74 #1 2024-08-16 21:07:10 ~7 min android 🤖apk 📲
✔️ 8593e74 #1 2024-08-16 21:08:27 ~9 min ios 📱ipa 📲
✔️ 94723fb #2 2024-08-21 19:45:08 ~4 min tests 📄log
✔️ 94723fb #2 2024-08-21 19:49:02 ~8 min android-e2e 🤖apk 📲
✔️ 94723fb #2 2024-08-21 19:49:15 ~9 min android 🤖apk 📲
✔️ 94723fb #2 2024-08-21 19:49:51 ~9 min ios 📱ipa 📲
✔️ 4da2d50 #3 2024-08-21 23:39:53 ~4 min tests 📄log
✔️ 4da2d50 #3 2024-08-21 23:43:50 ~8 min android-e2e 🤖apk 📲
✔️ 4da2d50 #3 2024-08-21 23:44:15 ~9 min android 🤖apk 📲
✔️ 4da2d50 #3 2024-08-21 23:44:16 ~9 min ios 📱ipa 📲
228a938 #4 2024-08-22 04:21:13 ~3 min tests 📄log
✔️ 228a938 #4 2024-08-22 04:24:41 ~6 min android 🤖apk 📲
✔️ 228a938 #4 2024-08-22 04:27:18 ~9 min ios 📱ipa 📲
✔️ 228a938 #4 2024-08-22 04:29:33 ~11 min android-e2e 🤖apk 📲
✔️ b549591 #5 2024-08-22 17:17:35 ~4 min tests 📄log
✔️ b549591 #5 2024-08-22 17:21:23 ~8 min android-e2e 🤖apk 📲
✔️ b549591 #5 2024-08-22 17:21:43 ~9 min android 🤖apk 📲
✔️ b549591 #5 2024-08-22 17:21:52 ~9 min ios 📱ipa 📲
49888c6 #6 2024-08-22 22:18:59 ~3 min tests 📄log
✔️ 49888c6 #6 2024-08-22 22:24:23 ~8 min android-e2e 🤖apk 📲
✔️ 49888c6 #6 2024-08-22 22:24:48 ~8 min android 🤖apk 📲
✔️ 49888c6 #6 2024-08-22 22:25:22 ~9 min ios 📱ipa 📲
✔️ 49888c6 #7 2024-08-22 22:38:01 ~4 min tests 📄log
37c70c8 #8 2024-08-29 13:03:36 ~3 min tests 📄log
✔️ 37c70c8 #7 2024-08-29 13:08:40 ~8 min android-e2e 🤖apk 📲
✔️ 37c70c8 #7 2024-08-29 13:09:02 ~8 min android 🤖apk 📲
✔️ 37c70c8 #7 2024-08-29 13:11:38 ~11 min ios 📱ipa 📲
11156eb #9 2024-09-02 14:08:37 ~3 min tests 📄log
✔️ 11156eb #8 2024-09-02 14:11:30 ~6 min android-e2e 🤖apk 📲
4c7487f #10 2024-09-02 14:22:24 ~9 min tests 📄log
✔️ 4c7487f #9 2024-09-02 14:22:34 ~9 min android-e2e 🤖apk 📲
✔️ 4c7487f #9 2024-09-02 14:22:57 ~9 min android 🤖apk 📲
✔️ 4c7487f #9 2024-09-02 14:32:44 ~19 min ios 📱ipa 📲
4321942 #11 2024-09-03 19:02:56 ~2 min tests 📄log
✔️ 4321942 #10 2024-09-03 19:08:31 ~8 min android-e2e 🤖apk 📲
✔️ 4321942 #10 2024-09-03 19:08:59 ~8 min android 🤖apk 📲
✔️ 4321942 #10 2024-09-03 19:13:51 ~13 min ios 📱ipa 📲
Commit #️⃣ Finished (UTC) Duration Platform Result
✔️ 51487f6 #12 2024-09-03 20:08:22 ~4 min tests 📄log
✔️ 51487f6 #11 2024-09-03 20:12:07 ~8 min android-e2e 🤖apk 📲
✔️ 51487f6 #11 2024-09-03 20:12:31 ~9 min android 🤖apk 📲
✔️ 51487f6 #11 2024-09-03 20:16:19 ~12 min ios 📱ipa 📲
✔️ fbe2bfc #14 2024-09-04 16:45:23 ~4 min tests 📄log
✔️ fbe2bfc #13 2024-09-04 16:48:29 ~7 min android-e2e 🤖apk 📲
✔️ fbe2bfc #13 2024-09-04 16:49:18 ~7 min android 🤖apk 📲
✔️ fbe2bfc #13 2024-09-04 16:53:19 ~11 min ios 📱ipa 📲

@briansztamfater briansztamfater force-pushed the feat/fetch-swap-proposal branch from 5b2f7df to ad1fa0b Compare August 21, 2024 19:36
@briansztamfater briansztamfater force-pushed the feat/swap-approve-token branch 2 times, most recently from 94723fb to 4da2d50 Compare August 21, 2024 23:34
@@ -8,9 +8,17 @@
(rf/reg-event-fx
:wallet/pending-transaction-status-changed-received
(fn [{:keys [db]} [{:keys [message]}]]
(let [details (transforms/json->clj message)
tx-hash (:hash details)]
{:db (update-in db [:wallet :transactions tx-hash] assoc :status :confirmed :blocks 1)})))
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:blocks param was not used, and also it didn't make much sense to hardcode it to 1. Ideally, if we need it in the future, we should listen to transaction events and update the value accordingly, but for now it is not needed.

Comment on lines +329 to +470
(money/->wei
:gwei
max-fee-per-gas-medium))
:MaxPriorityFeePerGas
(money/to-hex
(money/->wei
:gwei
max-priority-fee-per-gas)))
(not eip-1559-enabled?) (assoc :TxType "0x00"
:GasPrice
(money/to-hex
(money/->wei
:gwei
gas-price))))))

(defn approval-path
[{:keys [route from-address to-address token-address]}]
(let [{:keys [from]} route
from-chain-id (:chain-id from)
approval-amount-required (:approval-amount-required route)
approval-amount-required-sanitized (-> approval-amount-required
(utils.hex/normalize-hex)
(native-module/hex-to-number))
approval-contract-address (:approval-contract-address route)
data (native-module/encode-function-call
constants/contract-function-signature-erc20-approve
[approval-contract-address
approval-amount-required-sanitized])
tx-data (transaction-data {:from-address from-address
:to-address to-address
:token-address token-address
:route route
:data data
:eth-transfer? false})]
{:BridgeName constants/bridge-name-transfer
:ChainID from-chain-id
:TransferTx tx-data}))

(defn transaction-path
[{:keys [from-address to-address token-id token-address route data eth-transfer?]}]
(let [{:keys [bridge-name amount-in bonder-fees from
to]} route
tx-data (transaction-data {:from-address from-address
:to-address to-address
:token-address token-address
:route route
:data data
:eth-transfer? eth-transfer?})
to-chain-id (:chain-id to)
from-chain-id (:chain-id from)]
(cond-> {:BridgeName bridge-name
:ChainID from-chain-id}

(= bridge-name constants/bridge-name-erc-721-transfer)
(assoc :ERC721TransferTx
(assoc tx-data
:Recipient to-address
:TokenID token-id
:ChainID to-chain-id))

(= bridge-name constants/bridge-name-erc-1155-transfer)
(assoc :ERC1155TransferTx
(assoc tx-data
:Recipient to-address
:TokenID token-id
:ChainID to-chain-id
:Amount amount-in))

(= bridge-name constants/bridge-name-transfer)
(assoc :TransferTx tx-data)

(= bridge-name constants/bridge-name-hop)
(assoc :HopTx
(assoc tx-data
:ChainID from-chain-id
:ChainIDTo to-chain-id
:Symbol token-id
:Recipient to-address
:Amount amount-in
:BonderFee bonder-fees))

(not (or (= bridge-name constants/bridge-name-erc-721-transfer)
(= bridge-name constants/bridge-name-transfer)
(= bridge-name constants/bridge-name-hop)))
(assoc :CbridgeTx
(assoc tx-data
:ChainID to-chain-id
:Symbol token-id
:Recipient to-address
:Amount amount-in)))))

(defn multi-transaction-command
[{:keys [from-address to-address from-asset to-asset amount-out multi-transaction-type]
:or {multi-transaction-type constants/multi-transaction-type-unknown}}]
{:fromAddress from-address
:toAddress to-address
:fromAsset from-asset
:toAsset to-asset
:fromAmount amount-out
:type multi-transaction-type})
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved functions to utils file

@briansztamfater briansztamfater changed the title [WIP] feat: approve token transactions on swap feat: approve token transactions on swap Aug 22, 2024
@briansztamfater briansztamfater marked this pull request as ready for review August 22, 2024 22:34
(money/to-hex
(money/->wei
:gwei
gas-price))))))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe it makes sense to replace cond with a simple if here for better readability?

Copy link
Contributor

@mohsen-ghafouri mohsen-ghafouri left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work 🚀, it would be great also to see events-test and utils-test files as there are many important funcs here.

@briansztamfater
Copy link
Member Author

Already tested by @VolodLytvynenko on #21134

@briansztamfater briansztamfater force-pushed the feat/fetch-swap-proposal branch 2 times, most recently from 703b6aa to a2b1a3c Compare September 3, 2024 18:58
@briansztamfater briansztamfater force-pushed the feat/swap-approve-token branch 2 times, most recently from 4321942 to 51487f6 Compare September 3, 2024 20:03
@status-im-auto
Copy link
Member

100% of end-end tests have passed

Total executed tests: 7
Failed tests: 0
Expected to fail tests: 0
Passed tests: 7

Passed tests (7)

Click to expand

Class TestWalletMultipleDevice:

1. test_wallet_send_asset_from_drawer, id: 727230
2. test_wallet_send_eth, id: 727229

Class TestWalletOneDevice:

1. test_wallet_add_remove_regular_account, id: 727231
Device sessions

Class TestCommunityMultipleDeviceMerged:

1. test_community_message_edit, id: 702843
Device sessions

Class TestOneToOneChatMultipleSharedDevicesNewUi:

1. test_1_1_chat_non_latin_messages_stack_update_profile_photo, id: 702745
Device sessions

Class TestCommunityOneDeviceMerged:

1. test_community_copy_and_paste_message_in_chat_input, id: 702742
Device sessions

2. test_restore_multiaccount_with_waku_backup_remove_switch, id: 703133
Device sessions

@pavloburykh
Copy link
Contributor

Thank you @briansztamfater! E2E have passed. PR is ready for merge.

@briansztamfater briansztamfater force-pushed the feat/fetch-swap-proposal branch from a2b1a3c to 59a257d Compare September 4, 2024 16:15
Base automatically changed from feat/fetch-swap-proposal to develop September 4, 2024 16:40
@briansztamfater briansztamfater merged commit d5238ac into develop Sep 4, 2024
6 checks passed
@briansztamfater briansztamfater deleted the feat/swap-approve-token branch September 4, 2024 16:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

Approval label
7 participants