-
Notifications
You must be signed in to change notification settings - Fork 198
feat(swapper): integrate Garden Finance for BTC and wrapped BTC swaps #12355
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: develop
Are you sure you want to change the base?
Changes from all commits
8c12860
cd25497
093e26f
729dd56
8f980a5
ba36446
faff23b
ed8e1d8
60c116f
eb2a8a4
ab867e4
cf7aebb
5cbf758
3567316
0d72d8e
6d58e30
bbb880c
2c2551b
d8b3b61
72e38a3
13a1b13
6835458
673f284
b2d8b4f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| import type { Csp } from '../../../types' | ||
|
|
||
| export const csp: Csp = { | ||
| 'connect-src': ['https://api.garden.finance'], | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,168 @@ | ||
| import type { starknet } from '@shapeshiftoss/chain-adapters' | ||
| import { toAddressNList } from '@shapeshiftoss/chain-adapters' | ||
| import { hash, num } from 'starknet' | ||
|
|
||
| export const toHexString = (value: unknown): string => { | ||
| if (typeof value !== 'string') { | ||
| throw new Error(`toHexString: expected string, got ${typeof value}`) | ||
| } | ||
| if (value.startsWith('0x')) return value | ||
| if (/^[0-9a-fA-F]+$/.test(value) && /[a-fA-F]/.test(value)) { | ||
| return `0x${value}` | ||
| } | ||
| if (/^\d+$/.test(value)) { | ||
| return num.toHex(value) | ||
| } | ||
| throw new Error(`toHexString: ambiguous input ${JSON.stringify(value)}`) | ||
| } | ||
|
|
||
| type StarknetEstimateResult = { | ||
| result?: { | ||
| l1_gas_consumed?: string | ||
| l1_gas_price?: string | ||
| l2_gas_consumed?: string | ||
| l2_gas_price?: string | ||
| l1_data_gas_consumed?: string | ||
| l1_data_gas_price?: string | ||
| }[] | ||
| error?: unknown | ||
| } | ||
|
|
||
| export const buildStarknetInvokeTx = async ({ | ||
| formattedCalldata, | ||
| normalizedFrom, | ||
| accountNumber, | ||
| adapter, | ||
| }: { | ||
| formattedCalldata: string[] | ||
| normalizedFrom: string | ||
| accountNumber: number | ||
| adapter: starknet.ChainAdapter | ||
| }) => { | ||
| const chainIdHex = await adapter.getStarknetProvider().getChainId() | ||
| const nonce = await adapter.getNonce(normalizedFrom) | ||
|
|
||
| const version = '0x3' as const | ||
| const estimateTx = { | ||
| type: 'INVOKE', | ||
| version, | ||
| sender_address: normalizedFrom, | ||
| calldata: formattedCalldata, | ||
| signature: [], | ||
| nonce, | ||
| resource_bounds: { | ||
| l1_gas: { max_amount: '0x186a0', max_price_per_unit: '0x5f5e100' }, | ||
| l2_gas: { max_amount: '0x0', max_price_per_unit: '0x0' }, | ||
| l1_data_gas: { max_amount: '0x186a0', max_price_per_unit: '0x1' }, | ||
| }, | ||
| tip: '0x0', | ||
| paymaster_data: [], | ||
| account_deployment_data: [], | ||
| nonce_data_availability_mode: 'L1', | ||
| fee_data_availability_mode: 'L1', | ||
| } | ||
|
|
||
| const estimateResult: StarknetEstimateResult = await (async () => { | ||
| try { | ||
| const response = await adapter | ||
| .getStarknetProvider() | ||
| .fetch('starknet_estimateFee', [[estimateTx], ['SKIP_VALIDATE'], 'latest']) | ||
| return (await response.json()) as StarknetEstimateResult | ||
| } catch (error) { | ||
| const message = error instanceof Error ? error.message : String(error) | ||
| throw new Error(`starknet_estimateFee RPC call failed for ${normalizedFrom}: ${message}`) | ||
| } | ||
| })() | ||
|
|
||
| if (estimateResult.error) { | ||
| throw new Error( | ||
| `starknet_estimateFee returned error for ${normalizedFrom}: ${JSON.stringify( | ||
| estimateResult.error, | ||
| )}`, | ||
| ) | ||
| } | ||
|
|
||
| const feeEstimate = estimateResult.result?.[0] | ||
| if (!feeEstimate) { | ||
| throw new Error(`starknet_estimateFee returned no estimate for ${normalizedFrom}`) | ||
| } | ||
|
Comment on lines
+42
to
+88
Contributor
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. Preserve typed swapper errors in the shared RPC path. This helper now performs the provider calls for both Avnu and Garden, but RPC/json failures and the fee-estimation branches still escape as raw As per coding guidelines, "ALWAYS wrap async operations in try-catch blocks and use AsyncResultOf utility for converting promises to Results" and "ALWAYS use custom error classes from 🤖 Prompt for AI Agents |
||
|
|
||
| const l1GasConsumed = feeEstimate.l1_gas_consumed | ||
| ? BigInt(feeEstimate.l1_gas_consumed) | ||
| : BigInt('0x186a0') | ||
| const l1GasPrice = feeEstimate.l1_gas_price | ||
| ? BigInt(feeEstimate.l1_gas_price) | ||
| : BigInt('0x5f5e100') | ||
| const l2GasConsumed = feeEstimate.l2_gas_consumed | ||
| ? BigInt(feeEstimate.l2_gas_consumed) | ||
| : BigInt('0x0') | ||
| const l2GasPrice = feeEstimate.l2_gas_price ? BigInt(feeEstimate.l2_gas_price) : BigInt('0x0') | ||
| const l1DataGasConsumed = feeEstimate.l1_data_gas_consumed | ||
| ? BigInt(feeEstimate.l1_data_gas_consumed) | ||
| : BigInt('0x186a0') | ||
| const l1DataGasPrice = feeEstimate.l1_data_gas_price | ||
| ? BigInt(feeEstimate.l1_data_gas_price) | ||
| : BigInt('0x1') | ||
|
|
||
| const resourceBounds = { | ||
| l1_gas: { | ||
| max_amount: (l1GasConsumed * BigInt(500)) / BigInt(100), | ||
| max_price_per_unit: (l1GasPrice * BigInt(200)) / BigInt(100), | ||
| }, | ||
| l2_gas: { | ||
| max_amount: (l2GasConsumed * BigInt(500)) / BigInt(100), | ||
| max_price_per_unit: (l2GasPrice * BigInt(200)) / BigInt(100), | ||
| }, | ||
| l1_data_gas: { | ||
| max_amount: (l1DataGasConsumed * BigInt(500)) / BigInt(100), | ||
| max_price_per_unit: (l1DataGasPrice * BigInt(200)) / BigInt(100), | ||
| }, | ||
| } | ||
|
|
||
| const invokeHashInputs = { | ||
| senderAddress: normalizedFrom, | ||
| version, | ||
| compiledCalldata: formattedCalldata, | ||
| chainId: chainIdHex, | ||
| nonce, | ||
| nonceDataAvailabilityMode: 0 as const, | ||
| feeDataAvailabilityMode: 0 as const, | ||
| resourceBounds: { | ||
| l1_gas: { | ||
| max_amount: resourceBounds.l1_gas.max_amount, | ||
| max_price_per_unit: resourceBounds.l1_gas.max_price_per_unit, | ||
| }, | ||
| l2_gas: { | ||
| max_amount: resourceBounds.l2_gas.max_amount, | ||
| max_price_per_unit: resourceBounds.l2_gas.max_price_per_unit, | ||
| }, | ||
| l1_data_gas: { | ||
| max_amount: resourceBounds.l1_data_gas.max_amount, | ||
| max_price_per_unit: resourceBounds.l1_data_gas.max_price_per_unit, | ||
| }, | ||
| }, | ||
| tip: '0x0', | ||
| paymasterData: [], | ||
| accountDeploymentData: [], | ||
| } | ||
|
|
||
| const txHash = hash.calculateInvokeTransactionHash(invokeHashInputs) | ||
|
|
||
| return { | ||
| addressNList: toAddressNList(adapter.getBip44Params({ accountNumber })), | ||
| txHash, | ||
| _txDetails: { | ||
| fromAddress: normalizedFrom, | ||
| calldata: formattedCalldata, | ||
| nonce, | ||
| version, | ||
| resourceBounds, | ||
| chainId: chainIdHex, | ||
| nonceDataAvailabilityMode: 0 as const, | ||
| feeDataAvailabilityMode: 0 as const, | ||
| tip: '0x0', | ||
| paymasterData: [], | ||
| accountDeploymentData: [], | ||
| }, | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export * from './buildStarknetInvokeTx' |
Uh oh!
There was an error while loading. Please reload this page.