-
-
Notifications
You must be signed in to change notification settings - Fork 246
SWAPS-2839 update bridge controllers for bitcoin #6454
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
base: main
Are you sure you want to change the base?
Changes from 17 commits
7f345c7
d488050
299d7f3
e6b2bd5
9da14ea
98af283
5dc551b
249acca
eadbd8f
58cef05
f35d3a2
ca9c3d4
cae0407
7651b43
602645b
0c1028a
f2d9198
d2e73fc
45231de
30c24ec
5ac13fa
2e01ad7
b8815c6
acc60f5
644f6d3
81f4d09
ea57895
9e9ba9a
beb089d
3917e27
1af634d
41ce980
fb63f06
959ddbd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,6 +38,7 @@ import { hasSufficientBalance } from './utils/balance'; | |
import { | ||
getDefaultBridgeControllerState, | ||
isCrossChain, | ||
isNonEvmChainId, | ||
isSolanaChainId, | ||
sumHexes, | ||
} from './utils/bridge'; | ||
|
@@ -71,7 +72,7 @@ import type { | |
import { type CrossChainSwapsEventProperties } from './utils/metrics/types'; | ||
import { isValidQuoteRequest } from './utils/quote'; | ||
import { | ||
getFeeForTransactionRequest, | ||
computeFeeRequest, | ||
getMinimumBalanceForRentExemptionRequest, | ||
} from './utils/snaps'; | ||
import { FeatureId } from './utils/validators'; | ||
|
@@ -727,51 +728,88 @@ export class BridgeController extends StaticIntervalPollingController<BridgePoll | |
: undefined; | ||
}; | ||
|
||
/** | ||
* Appends transaction fees for non-EVM chains to quotes | ||
* | ||
* @param quotes - Array of quote responses to append fees to | ||
* @returns Array of quotes with fees appended, or undefined if quotes are for EVM chains | ||
*/ | ||
readonly #appendSolanaFees = async ( | ||
quotes: QuoteResponse[], | ||
): Promise<(QuoteResponse & SolanaFees)[] | undefined> => { | ||
// Return early if some of the quotes are not for solana | ||
if ( | ||
quotes.some(({ quote: { srcChainId } }) => !isSolanaChainId(srcChainId)) | ||
quotes.some(({ quote: { srcChainId } }) => !isNonEvmChainId(srcChainId)) | ||
) { | ||
return undefined; | ||
} | ||
|
||
const solanaFeePromises = Promise.allSettled( | ||
const nonEvmFeePromises = Promise.allSettled( | ||
quotes.map(async (quoteResponse) => { | ||
const { trade } = quoteResponse; | ||
const { trade, quote } = quoteResponse; | ||
const selectedAccount = this.#getMultichainSelectedAccount(); | ||
|
||
if (selectedAccount?.metadata?.snap?.id && typeof trade === 'string') { | ||
const { value: fees } = (await this.messagingSystem.call( | ||
const scope = formatChainIdToCaip(quote.srcChainId); | ||
|
||
const response = (await this.messagingSystem.call( | ||
'SnapController:handleRequest', | ||
getFeeForTransactionRequest( | ||
computeFeeRequest( | ||
selectedAccount.metadata.snap?.id, | ||
trade, | ||
selectedAccount.id, | ||
scope, | ||
), | ||
)) as { value: string }; | ||
)) as { | ||
type: 'base' | 'priority'; | ||
asset: { | ||
unit: string; | ||
type: string; | ||
amount: string; | ||
fungible: true; | ||
}; | ||
}[]; | ||
|
||
const baseFee = response?.find((fee) => fee.type === 'base'); | ||
let feeInNative = '0'; | ||
|
||
if (baseFee?.asset?.amount) { | ||
if (isSolanaChainId(quote.srcChainId)) { | ||
// Convert SOL to Lamports (1 SOL = 10^9 Lamports) | ||
// Use string manipulation to avoid floating point precision issues | ||
const parts = baseFee.asset.amount.split('.'); | ||
const wholePart = parts[0] || '0'; | ||
const decimalPart = (parts[1] || '').padEnd(9, '0').slice(0, 9); | ||
feeInNative = BigInt(wholePart + decimalPart).toString(); | ||
micaelae marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} else { | ||
// For other chains (BTC, Tron), use the fee as-is | ||
ghgoodreau marked this conversation as resolved.
Show resolved
Hide resolved
|
||
feeInNative = baseFee.asset.amount; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we should make this generic across chains so the metadata calculation can work for any non-EVM chain. If you search the package for Looks like network fees for Tron and BTC are not getting included in the metadata (see |
||
} | ||
} | ||
cursor[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
return { | ||
...quoteResponse, | ||
solanaFeesInLamports: fees, | ||
solanaFeesInLamports: feeInNative, | ||
}; | ||
} | ||
return quoteResponse; | ||
}), | ||
); | ||
|
||
const quotesWithSolanaFees = (await solanaFeePromises).reduce< | ||
const quotesWithNonEvmFees = (await nonEvmFeePromises).reduce< | ||
(QuoteResponse & SolanaFees)[] | ||
>((acc, result) => { | ||
if (result.status === 'fulfilled' && result.value) { | ||
acc.push(result.value); | ||
} else if (result.status === 'rejected') { | ||
console.error('Error calculating solana fees for quote', result.reason); | ||
console.error( | ||
'Error calculating non-EVM fees for quote', | ||
result.reason, | ||
); | ||
} | ||
return acc; | ||
}, []); | ||
|
||
return quotesWithSolanaFees; | ||
return quotesWithNonEvmFees; | ||
}; | ||
|
||
#getMultichainSelectedAccount() { | ||
|
Uh oh!
There was an error while loading. Please reload this page.