-
Notifications
You must be signed in to change notification settings - Fork 2
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
refactor: migrate bridge logic to frontend #4
Merged
Conversation
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
ryanml
previously approved these changes
Jul 29, 2024
bferenc
previously approved these changes
Aug 2, 2024
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.
👏 ✅
left a question below 👇
src/background/services/bridge/handlers/avalanche_bridgeAsset.ts
Outdated
Show resolved
Hide resolved
bferenc
approved these changes
Aug 2, 2024
ryanml
approved these changes
Aug 2, 2024
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Description
FAQ
WTF Michał?
Can you explain the changes a bit?
Migrating logic to the frontend
Our
BridgeService
andUnifiedBridgeService
used to be responsible for constructing the transaction data.We don't want that anymore.
The idea is that frontend should be responsible for constructing the transactions and only ask backend to sign them. As such, this PR migrates parts of the bridge backend logic to the frontend side of the extension:
🙃
avalanche_bridgeAsset.ts
handler updatesThis handler is used by Core Web to initiate bridge transactions in the extension. It used to heavily rely on
BridgeService
, but now it's using the Bridge SDK directly to construct the transactions.✂️
BridgeService.transferAsset()
removedThe logic that was there now lives in
BridgeProvider.tsx
file, intransferEVMAsset()
function. This function uses the Bridge SDK to construct the transaction data and sends aneth_sendTransaction
request to the backend for the transaction to be signed and dispatched.✂️
BridgeService.transferBtcAsset()
removedThe logic that was there is mostly removed. This method was mostly constructing a simple BTC send transaction, which we already have utils for in
bitcoin_sendTransaction
handler. So theuseBtcBridge()
now validates the input parameters and then re-uses the same utils we have in our Send flows to construct a transaction (based on the Bridge config & bridge addresses) and then sends abitcoin_sendTransaction
request to the backend.✅
BridgeService.estimateGas()
left unchangedThis was left unchanged, because our
avalanche_bridgeAsset
request handler is still using that part. However, extension's frontend uses the Bridge SDK directly to estimate gas limit.✂️
UnifiedBridgeService.getFee()
removedThe logic that was there now lives in
UnifiedBridgeProvider.tsx
, asgetFee()
function.✂️
UnifiedBridgeService.estimateGas()
removedThe logic that was there now lives in
UnifiedBridgeProvider.tsx
, asestimateGas()
function.✂️
UnifiedBridgeService.transfer()
removedThe logic that was there now lives in
UnifiedBridgeProvider.tsx
, astransferAsset()
function.✂️ Bunch of handlers removed
transferAsset.ts
getEthMaxTransferAmount.ts
- logic moved to theuseEthBridge()
hook.unifiedBridgeEstimateGas.ts
unifiedBridgeGetFee.ts
unifiedBridgeGetAssets.ts
unifiedBridgeTransferAsset.ts
unifiedBridgeEstimateGas.ts
🚀 One new handler
unifiedBridgeTrackTransfer.ts
- since the transaction building now lives on the frontend, once tx is confirmed the frontend asks the backend to start tracking the transfer.Changes to
eth_sendTransaction
andbitcoin_sendTransaction
Since those handlers are very generic and since bridge transactions cannot be simulated with Blockaid (they are cross-chain, use off-chain services), the approval windows for Bridge transactions could become pretty confusing (especially when they require two approvals).
As an idea to help with this a little bit, I added an optional parameter to
eth_sendTransaction
andbitcoin_sendTransaction
handlers. This parameter is calledTxDisplayOptions
. Here's the breakdown:Confirm Bridge
) and also some notice (i.e.This bridge transfer requires two approvals, you will be ask for one more signature
).displayData
object and can be used bySignTransaction.tsx
/BitcoinSignTransaction.tsx
screens.Frontend component structure changes
How things used to work
Bridge.tsx
file, that calleduseBridge()
hook, which in turn called all of the bridge adapter hooks (useEthBridge
,useBtcBridge
,useAvalancheBridge
anduseUnifiedBridge
).useBridge()
then decided which of the rendered hooks are relevant in the current state and merge them with the default values, then return back toBridge.tsx
.Here is a simplified diagram:
How things work now
As part of this PR, I changed the flow a little bit to prevent that unnecessary computation. Approach is very similar to the new Send flows. This is what it looks like right now:
BridgeForm
component was extracted out ofBridge.tsx
. It renders the form and accepts all of the state & state handlers returned by the specialised hooks (so called bridge adapters) thatBridge.tsx
used to do.Bridge.tsx
still callsuseBridge()
, but it only gets the data that is shared between all specialised flows in return (so things like: bridge provider (avalanche / cctp), current amount & amount setter, target chain id, etc.)Bridge.tsx
has this data, it then decides which of the the specialisedBridgeForm
components to render. There are four:BridgeFormETH
,BridgeFormBTC
,BridgeFormAVAX
,BridgeFormUnified
. Those specialised components render their respective specialised hooks (e.g.useBtcBridge()
) and then render the sharedBridgeForm
component.Here is a simplified diagram:
Testing
Screenshots:
I was not able to test ETH offboarding, as I don't have enough funds. I'm attaching screencast for some of the remaining scenarios below.
Ethereum.to.Avalanche.mov
Bitcoin.to.Avalanche.mov
Avalanche.to.Bitcoin.mov
CCTP.Ethereum.to.Avalanche.mov
CCTP.Avalanche.to.Ethereum.mov
Checklist for the author