-
Notifications
You must be signed in to change notification settings - Fork 100
improve: add solana withdrawal step to finalizer #2552
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
Merged
Merged
Changes from 15 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
1afb5bc
improve: add solana withdrawal step to finalizer
bmzig 1741ea5
fix
bmzig 66e8d20
log logs
bmzig a9afb7d
Merge branch 'master' into bz/solanaStep
bmzig 939a49a
combine sdk bump
bmzig 60aa009
Merge branch 'master' into bz/solanaStep
bmzig f2a14b3
Merge branch 'master' into bz/solanaStep
bmzig 546292e
Merge branch 'master' into bz/solanaStep
bmzig e3fd271
Merge branch 'master' into bz/solanaStep
bmzig f7714bb
Merge branch 'master' into bz/solanaStep
bmzig 2d720a3
update
bmzig aaa4a2d
Merge branch 'master' into bz/solanaStep
bmzig 43ff0dd
Merge branch 'master' into bz/solanaStep
bmzig a45e420
Merge branch 'master' into bz/solanaStep
bmzig a878a6d
comment
bmzig c861d69
Apply suggestions from code review
bmzig 2a9aa09
fix
bmzig 6b7379e
Merge branch 'master' into bz/solanaStep
bmzig 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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,2 @@ | ||
| export * from "./l1Tol2"; | ||
| export * from "./l2Tol1"; |
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,122 @@ | ||
| import { KeyPairSigner, appendTransactionMessageInstructions, pipe, address, generateKeyPairSigner } from "@solana/kit"; | ||
| import { SVMSpokePoolClient } from "../../../../clients"; | ||
| import { CONTRACT_ADDRESSES, CCTP_MAX_SEND_AMOUNT } from "../../../../common"; | ||
| import { | ||
| winston, | ||
| SvmAddress, | ||
| sendAndConfirmSolanaTransaction, | ||
| simulateSolanaTransaction, | ||
| toKitAddress, | ||
| CHAIN_IDs, | ||
| getCCTPDepositAccounts, | ||
| createDefaultTransaction, | ||
| getAssociatedTokenAddress, | ||
| getStatePda, | ||
| getTransferLiabilityPda, | ||
| TOKEN_SYMBOLS_MAP, | ||
| PUBLIC_NETWORKS, | ||
| chainIsProd, | ||
| getEventAuthority, | ||
| createFormatFunction, | ||
| } from "../../../../utils"; | ||
| import { SvmSpokeClient } from "@across-protocol/contracts"; | ||
|
|
||
| /** | ||
| * Initiates a withdrawal from the Solana spoke pool. | ||
| * | ||
| * @param solanaClient The Solana client. | ||
| * @param signer A base signer to be converted into a Solana signer. | ||
| * @param simulate Whether to simulate the transaction. | ||
| * @param hubChainId The chain ID of the hub. | ||
| * @returns A list of executed transaction signatures. | ||
| */ | ||
| export async function bridgeTokensToHubPool( | ||
| solanaClient: SVMSpokePoolClient, | ||
| signer: KeyPairSigner, | ||
| logger: winston.Logger, | ||
| hubChainId = 1 | ||
| ): Promise<{ message: string; signature?: string }> { | ||
| const svmProvider = solanaClient.svmEventsClient.getRpc(); | ||
| const svmSpoke = solanaClient.spokePoolAddress; | ||
| const svmSpokeProgramId = toKitAddress(svmSpoke); | ||
|
|
||
| const l2ChainId = chainIsProd(hubChainId) ? CHAIN_IDs.SOLANA : CHAIN_IDs.SOLANA_DEVNET; | ||
| const { address: tokenMessengerAddress } = CONTRACT_ADDRESSES[l2ChainId].cctpTokenMessenger; | ||
| const { address: messageTransmitterAddress } = CONTRACT_ADDRESSES[l2ChainId].cctpMessageTransmitter; | ||
| const l2Usdc = SvmAddress.from(TOKEN_SYMBOLS_MAP.USDC.addresses[l2ChainId]); | ||
| const destinationDomain = PUBLIC_NETWORKS[hubChainId].cctpDomain; | ||
|
|
||
| // Before we get all the information necessary to initiate a CCTP withdrawal, we need to check whether | ||
| // the transfer liability PDA has any pending withdraw amounts. | ||
| const transferLiability = await getTransferLiabilityPda(svmSpokeProgramId, toKitAddress(l2Usdc)); | ||
| const transferLiabilityAccount = await SvmSpokeClient.fetchTransferLiability(svmProvider, transferLiability); | ||
| const pendingWithdrawAmount = Math.min( | ||
| Number(transferLiabilityAccount.data.pendingToHubPool), | ||
| CCTP_MAX_SEND_AMOUNT.toNumber() | ||
| ); | ||
| if (pendingWithdrawAmount === 0) { | ||
| return { | ||
| message: "Transfer liability account has no pending withdraw amount.", | ||
| }; | ||
| } | ||
|
|
||
| // If there is an amount to withdraw, then fetch all accounts and send the transaction. | ||
| const [state, eventAuthority, cctpDepositAccounts, messageSentEventData] = await Promise.all([ | ||
| getStatePda(svmSpokeProgramId), | ||
| getEventAuthority(svmSpokeProgramId), | ||
| getCCTPDepositAccounts( | ||
| hubChainId, | ||
| destinationDomain, | ||
| address(tokenMessengerAddress), | ||
| address(messageTransmitterAddress) | ||
| ), | ||
| generateKeyPairSigner(), | ||
| ]); | ||
| const { | ||
| tokenMessengerMinterSenderAuthority, | ||
| messageTransmitter, | ||
| tokenMessenger, | ||
| remoteTokenMessenger, | ||
| tokenMinter, | ||
| localToken, | ||
| cctpEventAuthority, | ||
| } = cctpDepositAccounts; | ||
| const vault = await getAssociatedTokenAddress(SvmAddress.from(state.toString()), l2Usdc); | ||
| const bridgeTokensToHubPoolIx = SvmSpokeClient.getBridgeTokensToHubPoolInstruction({ | ||
| signer, | ||
| payer: signer, | ||
| mint: toKitAddress(l2Usdc), | ||
| state, | ||
| transferLiability, | ||
| vault, | ||
| tokenMessengerMinterSenderAuthority, | ||
| messageTransmitter, | ||
| tokenMessenger, | ||
| remoteTokenMessenger, | ||
| tokenMinter, | ||
| localToken, | ||
| cctpEventAuthority, | ||
| messageSentEventData, | ||
| eventAuthority, | ||
| program: svmSpokeProgramId, | ||
| amount: pendingWithdrawAmount, | ||
| }); | ||
| const bridgeTokensToHubPoolTx = pipe(await createDefaultTransaction(svmProvider, signer), (tx) => | ||
| appendTransactionMessageInstructions([bridgeTokensToHubPoolIx], tx) | ||
| ); | ||
| const formatUsdc = createFormatFunction(2, 4, false, 6); | ||
| if ((process.env["SEND_TRANSACTIONS"] ?? "false") === "true") { | ||
bmzig marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| const withdrawSignature = await sendAndConfirmSolanaTransaction(bridgeTokensToHubPoolTx, signer, svmProvider); | ||
| return { | ||
| message: `Withdrew ${formatUsdc(pendingWithdrawAmount.toString())} USDC from Solana to the hub pool.`, | ||
| signature: withdrawSignature, | ||
| }; | ||
| } | ||
| const withdrawSimulation = await simulateSolanaTransaction(bridgeTokensToHubPoolTx, svmProvider); | ||
| return { | ||
| message: `Simulated withdrawal of ${formatUsdc(pendingWithdrawAmount.toString())} USDC with result ${ | ||
| withdrawSimulation?.value?.logs | ||
| }`, | ||
| signature: "", | ||
| }; | ||
| } | ||
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
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.