-
Notifications
You must be signed in to change notification settings - Fork 48
feat: use CCTP v2 for bungee instead of CCTP v1 #822
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
Draft
alfetopito
wants to merge
15
commits into
main
Choose a base branch
from
fix/bungee-cctp-v2
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 9 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
267435e
Merge pull request #2 from cowprotocol/main
rookmate 846edcf
Merge pull request #3 from cowprotocol/main
sebastiantf 8802f98
feat: use CCTP v2 for bungee instead of CCTP v1
alfetopito 1e6bee7
Merge pull request #5 from cowprotocol/main
rookmate 22491c4
feat(bridge): migrate bungee cctp support to cctp-v2
rookmate 1f74863
fix: take into account cctpv2 variable fees
rookmate 3c2ed51
fix: add missing Gnosis Native to enum BungeeBridgeName
rookmate 9964d4b
Merge branch 'feat-cctp-v2-via-bungee' into fix/bungee-cctp-v2
alfetopito ee5fd9b
Merge branch 'main' into fix/bungee-cctp-v2
alfetopito c3fb295
fix: don't share fixtures by exporting from a .test.ts module
alfetopito 945ade1
fix: add cctp back to avoid breaking api change
alfetopito 1ad6afd
fix: avoid txData being truncated
alfetopito 9a57584
Merge branch 'main' into fix/bungee-cctp-v2
alfetopito da5ecfa
Merge branch 'main' of https://github.com/cowprotocol/cow-sdk into fi…
shoom3301 1061355
chore: disable cctp
shoom3301 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
193 changes: 193 additions & 0 deletions
193
packages/bridging/src/providers/bungee/createBungeeDepositCall.test.ts
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,193 @@ | ||
| import { setGlobalAdapter } from '@cowprotocol/sdk-common' | ||
| import { SupportedChainId } from '@cowprotocol/sdk-config' | ||
| import { OrderKind } from '@cowprotocol/sdk-order-book' | ||
| import { createAdapters } from '../../../tests/setup' | ||
| import { QuoteBridgeRequest } from '../../types' | ||
| import { BUNGEE_APPROVE_AND_BRIDGE_V1_ABI } from './abi' | ||
| import { createBungeeDepositCall } from './createBungeeDepositCall' | ||
| import { BungeeBridge, BungeeQuoteWithBuildTx } from './types' | ||
| import { decodeAmountsBungeeTxData, toBridgeQuoteResult } from './util' | ||
|
|
||
| export const CCTP_V2_TX_DATA = | ||
| '0x000001a63ca7f5bc0000000000000000000000000000000000000000000000000000000005f5e1000000000000000000000000000000000000000000000000000000000000002713000000000000000000000000664b591ab924c6bb2caca533ed702386934a11d6000000000000000000000000833589fcd6edb6e08f4c7c32d4f71b54bda029130000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001e8480000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003e8' | ||
|
|
||
| function applyPctDiff(base: bigint, compare: bigint, target: bigint): bigint { | ||
| return (target * compare) / base | ||
| } | ||
|
|
||
| describe('createBungeeDepositCall', () => { | ||
| const adapters = createAdapters() | ||
|
|
||
| beforeEach(() => { | ||
| setGlobalAdapter(adapters.ethersV5Adapter) | ||
| }) | ||
|
|
||
| it('encodes cctp-v2 calldata rewrite metadata for feeAmount as the proportional secondary field', async () => { | ||
| const request: QuoteBridgeRequest = { | ||
| kind: OrderKind.SELL, | ||
| amount: 100000000n, | ||
| owner: '0x664B591AB924c6bb2caCA533Ed702386934A11d6', | ||
| sellTokenChainId: SupportedChainId.BASE, | ||
| sellTokenAddress: '0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913', | ||
| sellTokenDecimals: 6, | ||
| buyTokenChainId: SupportedChainId.MAINNET, | ||
| buyTokenAddress: '0xA0b86991c6218b36c1d19d4a2e9Eb0cE3606eB48', | ||
| buyTokenDecimals: 6, | ||
| appCode: 'bungee', | ||
| account: '0x664B591AB924c6bb2caCA533Ed702386934A11d6', | ||
| signer: '0x664B591AB924c6bb2caCA533Ed702386934A11d6', | ||
| } | ||
|
|
||
| const quoteWithBuildTx: BungeeQuoteWithBuildTx = { | ||
| bungeeQuote: { | ||
| originChainId: SupportedChainId.BASE, | ||
| destinationChainId: SupportedChainId.MAINNET, | ||
| userAddress: '0x664B591AB924c6bb2caCA533Ed702386934A11d6', | ||
| receiverAddress: '0x664B591AB924c6bb2caCA533Ed702386934A11d6', | ||
| input: { | ||
| token: { | ||
| chainId: SupportedChainId.BASE, | ||
| address: '0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913', | ||
| name: 'USD Coin', | ||
| symbol: 'USDC', | ||
| decimals: 6, | ||
| logoURI: '', | ||
| icon: '', | ||
| }, | ||
| amount: '100000000', | ||
| priceInUsd: 1, | ||
| valueInUsd: 100, | ||
| }, | ||
| route: { | ||
| quoteId: 'fd868dfdece0f3b1', | ||
| quoteExpiry: 0, | ||
| output: { | ||
| token: { | ||
| chainId: SupportedChainId.MAINNET, | ||
| address: '0xA0b86991c6218b36c1d19d4a2e9Eb0cE3606eB48', | ||
| name: 'USD Coin', | ||
| symbol: 'USDC', | ||
| decimals: 6, | ||
| logoURI: '', | ||
| icon: '', | ||
| }, | ||
| amount: '98000000', | ||
| priceInUsd: 1, | ||
| valueInUsd: 98, | ||
| minAmountOut: '98000000', | ||
| effectiveReceivedInUsd: 98, | ||
| }, | ||
| affiliateFee: null, | ||
| approvalData: { | ||
| spenderAddress: '0x3a23f943181408eac424116af7b7790c94cb97a5', | ||
| amount: '100000000', | ||
| tokenAddress: '0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913', | ||
| userAddress: '0x664B591AB924c6bb2caCA533Ed702386934A11d6', | ||
| }, | ||
| gasFee: { | ||
| gasToken: { | ||
| chainId: SupportedChainId.BASE, | ||
| address: '0x0000000000000000000000000000000000000000', | ||
| symbol: 'ETH', | ||
| name: 'Ethereum', | ||
| decimals: 18, | ||
| icon: '', | ||
| logoURI: '', | ||
| chainAgnosticId: null, | ||
| }, | ||
| gasLimit: '0', | ||
| gasPrice: '0', | ||
| estimatedFee: '0', | ||
| feeInUsd: 0, | ||
| }, | ||
| slippage: 0, | ||
| estimatedTime: 0, | ||
| routeDetails: { | ||
| name: 'Circle CCTP V2', | ||
| logoURI: '', | ||
| routeFee: { | ||
| token: { | ||
| chainId: SupportedChainId.BASE, | ||
| address: '0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913', | ||
| name: 'USD Coin', | ||
| symbol: 'USDC', | ||
| decimals: 6, | ||
| logoURI: '', | ||
| icon: '', | ||
| }, | ||
| amount: '0', | ||
| feeInUsd: 0, | ||
| priceInUsd: 1, | ||
| }, | ||
| dexDetails: null, | ||
| }, | ||
| refuel: null, | ||
| }, | ||
| routeBridge: BungeeBridge.CircleCCTPV2, | ||
| quoteTimestamp: 0, | ||
| }, | ||
| buildTx: { | ||
| approvalData: { | ||
| spenderAddress: '0x3a23f943181408eac424116af7b7790c94cb97a5', | ||
| amount: '100000000', | ||
| tokenAddress: '0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913', | ||
| userAddress: '0x664B591AB924c6bb2caCA533Ed702386934A11d6', | ||
| }, | ||
| txData: { | ||
| data: CCTP_V2_TX_DATA, | ||
| to: '0x3a23f943181408eac424116af7b7790c94cb97a5', | ||
| chainId: SupportedChainId.BASE, | ||
| value: '0', | ||
| }, | ||
| userOp: '', | ||
| }, | ||
| } | ||
|
|
||
| const quote = toBridgeQuoteResult(request, 0, quoteWithBuildTx) | ||
| const call = await createBungeeDepositCall({ request, quote }) | ||
| const decoded = adapters.ethersV5Adapter.utils.decodeFunctionData( | ||
| BUNGEE_APPROVE_AND_BRIDGE_V1_ABI, | ||
| 'approveAndBridge', | ||
| call.data, | ||
| ) as [string, { toString(): string }, { toString(): string }, string] | ||
|
|
||
| expect(decoded[0]).toBe(request.sellTokenAddress) | ||
| expect(decoded[1].toString()).toBe('100000000') | ||
| expect(decoded[2].toString()).toBe('0') | ||
| expect(decoded[3].startsWith(CCTP_V2_TX_DATA)).toBe(true) | ||
|
|
||
| const modifyCalldataParams = `0x${decoded[3].slice(-192)}` | ||
| const [inputAmountStartIndex, modifyOutputAmount, outputAmountStartIndex] = adapters.ethersV5Adapter.utils.decodeAbi( | ||
| ['uint256', 'bool', 'uint256'], | ||
| modifyCalldataParams, | ||
| ) as [bigint, boolean, bigint] | ||
|
|
||
| expect(inputAmountStartIndex).toBe(8n) | ||
| expect(modifyOutputAmount).toBe(true) | ||
| expect(outputAmountStartIndex).toBe(200n) | ||
| }) | ||
|
|
||
| it('would lower feeAmount proportionally when the actual bridged amount is below the quote', () => { | ||
| const result = decodeAmountsBungeeTxData(CCTP_V2_TX_DATA, BungeeBridge.CircleCCTPV2) | ||
|
|
||
| const quotedAmount = result.inputAmountBigNumber | ||
| const quotedFeeAmount = result.outputAmountBigNumber! | ||
| const actualBridgedAmount = 90000000n | ||
| const adjustedFeeAmount = applyPctDiff(quotedAmount, actualBridgedAmount, quotedFeeAmount) | ||
|
|
||
| expect(adjustedFeeAmount).toBe(1800000n) | ||
| expect(adjustedFeeAmount).toBeLessThan(quotedFeeAmount) | ||
| }) | ||
|
|
||
| it('would raise feeAmount proportionally when the actual bridged amount is above the quote', () => { | ||
| const result = decodeAmountsBungeeTxData(CCTP_V2_TX_DATA, BungeeBridge.CircleCCTPV2) | ||
|
|
||
| const quotedAmount = result.inputAmountBigNumber | ||
| const quotedFeeAmount = result.outputAmountBigNumber! | ||
| const actualBridgedAmount = 110000000n | ||
| const adjustedFeeAmount = applyPctDiff(quotedAmount, actualBridgedAmount, quotedFeeAmount) | ||
|
|
||
| expect(adjustedFeeAmount).toBe(2200000n) | ||
| expect(adjustedFeeAmount).toBeGreaterThan(quotedFeeAmount) | ||
| }) | ||
| }) | ||
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.