From 348b15c13770e53eda01188562d37f8da03a2e0a Mon Sep 17 00:00:00 2001 From: Felipe Mendes Date: Mon, 9 Sep 2024 13:25:43 -0300 Subject: [PATCH] fix: remove deprecated params from sign transaction wc rpc request (#2816) --- apps/laboratory/next.config.mjs | 2 +- .../Solana/SolanaSignJupiterSwapTest.tsx | 122 ++++++++++++++++++ .../src/components/Solana/SolanaTests.tsx | 14 ++ .../web3js/providers/WalletConnectProvider.ts | 14 +- .../tests/WalletConnectProvider.test.ts | 40 ------ 5 files changed, 139 insertions(+), 53 deletions(-) create mode 100644 apps/laboratory/src/components/Solana/SolanaSignJupiterSwapTest.tsx diff --git a/apps/laboratory/next.config.mjs b/apps/laboratory/next.config.mjs index d93cf2747a..81435953fb 100644 --- a/apps/laboratory/next.config.mjs +++ b/apps/laboratory/next.config.mjs @@ -11,7 +11,7 @@ const cspHeader = ` style-src 'self' 'unsafe-inline' https://fonts.googleapis.com; img-src * 'self' data: blob: https://walletconnect.org https://walletconnect.com https://secure.walletconnect.com https://secure.walletconnect.org https://tokens-data.1inch.io https://tokens.1inch.io https://ipfs.io https://lab.web3modal.com; font-src 'self' https://fonts.gstatic.com; - connect-src 'self' https://react-wallet.walletconnect.com https://rpc.walletconnect.com https://rpc.walletconnect.org https://relay.walletconnect.com https://relay.walletconnect.org wss://relay.walletconnect.com wss://relay.walletconnect.org https://pulse.walletconnect.com https://pulse.walletconnect.org https://api.web3modal.com https://api.web3modal.org wss://www.walletlink.org https://o1095249.ingest.sentry.io; + connect-src 'self' https://react-wallet.walletconnect.com https://rpc.walletconnect.com https://rpc.walletconnect.org https://relay.walletconnect.com https://relay.walletconnect.org wss://relay.walletconnect.com wss://relay.walletconnect.org https://pulse.walletconnect.com https://pulse.walletconnect.org https://api.web3modal.com https://api.web3modal.org wss://www.walletlink.org https://o1095249.ingest.sentry.io https://quote-api.jup.ag; frame-src 'self' https://verify.walletconnect.com https://verify.walletconnect.org https://secure.walletconnect.com https://secure.walletconnect.org ${ process.env.NEXT_PUBLIC_SECURE_SITE_SDK_URL || '' }; diff --git a/apps/laboratory/src/components/Solana/SolanaSignJupiterSwapTest.tsx b/apps/laboratory/src/components/Solana/SolanaSignJupiterSwapTest.tsx new file mode 100644 index 0000000000..087da33757 --- /dev/null +++ b/apps/laboratory/src/components/Solana/SolanaSignJupiterSwapTest.tsx @@ -0,0 +1,122 @@ +import { useState } from 'react' +import { Button, Stack, Text, Spacer } from '@chakra-ui/react' +import { PublicKey, VersionedTransaction } from '@solana/web3.js' + +import { useWeb3ModalAccount, useWeb3ModalProvider } from '@web3modal/solana/react' + +import { solana } from '../../utils/ChainsUtil' +import { useChakraToast } from '../Toast' +import bs58 from 'bs58' + +export function SolanaSignJupiterSwapTest() { + const toast = useChakraToast() + const { chainId } = useWeb3ModalAccount() + const { walletProvider, connection } = useWeb3ModalProvider() + const [loading, setLoading] = useState(false) + + async function onSignVersionedTransaction() { + try { + setLoading(true) + if (!walletProvider?.publicKey) { + throw Error('user is disconnected') + } + + if (!connection) { + throw Error('no connection set') + } + + const transaction = await createJupiterSwapTransaction({ + publicKey: walletProvider.publicKey + }) + const signedTransaction = await walletProvider.signTransaction(transaction) + const signature = signedTransaction.signatures[0] + + if (!signature) { + throw Error('Empty signature') + } + + toast({ + title: 'Success', + description: bs58.encode(signature), + type: 'success' + }) + } catch (err) { + toast({ + title: 'Error', + description: (err as Error).message, + type: 'error' + }) + } finally { + setLoading(false) + } + } + + if (chainId === solana.chainId) { + return ( + + Switch to Solana Devnet or Testnet to test this feature + + ) + } + + return ( + + + + + ) +} + +type CreateJupiterSwapTransactionParams = { + publicKey: PublicKey +} + +async function createJupiterSwapTransaction({ publicKey }: CreateJupiterSwapTransactionParams) { + const qs = new URLSearchParams({ + inputMint: 'EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v', + outputMint: 'So11111111111111111111111111111111111111112', + amount: '100000', + slippageBps: '300', + swapMode: 'ExactIn', + onlyDirectRoutes: 'false', + asLegacyTransaction: 'false', + maxAccounts: '64', + minimizeSlippage: 'false' + }) + + const quoteResponse = await ( + await fetch(`https://quote-api.jup.ag/v6/quote?${qs.toString()}`) + ).json() + const swapResponse = await ( + await fetch('https://quote-api.jup.ag/v6/swap', { + headers: { + 'Content-Type': 'application/json' + }, + body: JSON.stringify({ + userPublicKey: publicKey.toString(), + wrapAndUnwrapSol: true, + prioritizationFeeLamports: { + priorityLevelWithMaxLamports: { + maxLamports: 4000000, + global: false, + priorityLevel: 'high' + } + }, + asLegacyTransaction: false, + dynamicComputeUnitLimit: true, + allowOptimizedWrappedSolTokenAccount: false, + quoteResponse, + dynamicSlippage: { maxBps: 300 } + }), + method: 'POST' + }) + ).json() + + return VersionedTransaction.deserialize(Buffer.from(swapResponse.swapTransaction, 'base64')) +} diff --git a/apps/laboratory/src/components/Solana/SolanaTests.tsx b/apps/laboratory/src/components/Solana/SolanaTests.tsx index 0414d51e28..844a6e12c9 100644 --- a/apps/laboratory/src/components/Solana/SolanaTests.tsx +++ b/apps/laboratory/src/components/Solana/SolanaTests.tsx @@ -18,6 +18,7 @@ import { SolanaWriteContractTest } from './SolanaWriteContractTest' import { solana, solanaDevnet, solanaTestnet } from '../../utils/ChainsUtil' import { SolanaSignAndSendTransaction } from './SolanaSignAndSendTransactionTest' import { SolanaSignAllTransactionsTest } from './SolanaSignAllTransactionsTest' +import { SolanaSignJupiterSwapTest } from './SolanaSignJupiterSwapTest' export function SolanaTests() { const { isConnected, currentChain } = useWeb3ModalAccount() @@ -83,6 +84,19 @@ export function SolanaTests() { + + + + Sign Jupiter Swap Transaction + + + ℹ️ + + + + + + {(currentChain?.chainId === solanaTestnet.chainId || currentChain?.chainId === solanaDevnet.chainId) && ( } spacing="4"> diff --git a/packages/base/adapters/solana/web3js/providers/WalletConnectProvider.ts b/packages/base/adapters/solana/web3js/providers/WalletConnectProvider.ts index cc12dc8b78..60eb696962 100644 --- a/packages/base/adapters/solana/web3js/providers/WalletConnectProvider.ts +++ b/packages/base/adapters/solana/web3js/providers/WalletConnectProvider.ts @@ -12,7 +12,6 @@ import { Connection, PublicKey, Transaction, - TransactionMessage, VersionedTransaction, type SendOptions } from '@solana/web3.js' @@ -284,18 +283,9 @@ export class WalletConnectProvider extends ProviderEventEmitter implements Provi * This is a deprecated method that is used to support older versions of the * WalletConnect RPC API. It should be removed in the future */ - private getRawRPCParams(_transaction: AnyTransaction) { - let transaction = _transaction - + private getRawRPCParams(transaction: AnyTransaction) { if (isVersionedTransaction(transaction)) { - const instructions = TransactionMessage.decompile(transaction.message).instructions - const legacyMessage = new TransactionMessage({ - payerKey: new PublicKey(this.getAccount(true).publicKey), - recentBlockhash: transaction.message.recentBlockhash, - instructions: [...instructions] - }).compileToLegacyMessage() - - transaction = Transaction.populate(legacyMessage) + return {} } return { diff --git a/packages/base/adapters/solana/web3js/tests/WalletConnectProvider.test.ts b/packages/base/adapters/solana/web3js/tests/WalletConnectProvider.test.ts index 1400a9460b..79876bf81a 100644 --- a/packages/base/adapters/solana/web3js/tests/WalletConnectProvider.test.ts +++ b/packages/base/adapters/solana/web3js/tests/WalletConnectProvider.test.ts @@ -100,26 +100,6 @@ describe('WalletConnectProvider specific tests', () => { { method: 'solana_signTransaction', params: { - feePayer: '2VqKhjZ766ZN3uBtBpb7Ls3cN4HrocP1rzxzekhVEgoP', - instructions: [ - { - data: '3Bxs4NN8M2Yn4TLb', - keys: [ - { - isSigner: true, - isWritable: true, - pubkey: '2VqKhjZ766ZN3uBtBpb7Ls3cN4HrocP1rzxzekhVEgoP' - }, - { - isSigner: true, - isWritable: true, - pubkey: '2VqKhjZ766ZN3uBtBpb7Ls3cN4HrocP1rzxzekhVEgoP' - } - ], - programId: '11111111111111111111111111111111' - } - ], - recentBlockhash: 'EZySCpmzXRuUtM95P2JGv9SitqYph6Nv6HaYBK7a8PKJ', transaction: 'AQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACAAQABAhY+loQT/3nqQuE+Gw4GLiTIklVzYfSLz1r9WQ1FedoGAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAADJmlOhYicUv7MMXLfh/CK3nuxRET//L6xrgHRfdADXpQEBAgAADAIAAACAlpgAAAAAAAA=', pubkey: TestConstants.accounts[0].address @@ -301,26 +281,6 @@ describe('WalletConnectProvider specific tests', () => { { method: 'solana_signTransaction', params: { - feePayer: '2VqKhjZ766ZN3uBtBpb7Ls3cN4HrocP1rzxzekhVEgoP', - instructions: [ - { - data: '3Bxs4NN8M2Yn4TLb', - keys: [ - { - isSigner: true, - isWritable: true, - pubkey: '2VqKhjZ766ZN3uBtBpb7Ls3cN4HrocP1rzxzekhVEgoP' - }, - { - isSigner: true, - isWritable: true, - pubkey: '2VqKhjZ766ZN3uBtBpb7Ls3cN4HrocP1rzxzekhVEgoP' - } - ], - programId: '11111111111111111111111111111111' - } - ], - recentBlockhash: 'EZySCpmzXRuUtM95P2JGv9SitqYph6Nv6HaYBK7a8PKJ', transaction: 'AQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACAAQABAhY+loQT/3nqQuE+Gw4GLiTIklVzYfSLz1r9WQ1FedoGAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAADJmlOhYicUv7MMXLfh/CK3nuxRET//L6xrgHRfdADXpQEBAgAADAIAAACAlpgAAAAAAAA=', pubkey: TestConstants.accounts[0].address