Skip to content

Commit

Permalink
fix: remove deprecated params from sign transaction wc rpc request (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
zoruka authored Sep 9, 2024
1 parent 07a931c commit 348b15c
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 53 deletions.
2 changes: 1 addition & 1 deletion apps/laboratory/next.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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 || ''
};
Expand Down
122 changes: 122 additions & 0 deletions apps/laboratory/src/components/Solana/SolanaSignJupiterSwapTest.tsx
Original file line number Diff line number Diff line change
@@ -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 (
<Text fontSize="md" color="yellow">
Switch to Solana Devnet or Testnet to test this feature
</Text>
)
}

return (
<Stack direction={['column', 'column', 'row']}>
<Button
data-test-id="sign-transaction-button"
onClick={onSignVersionedTransaction}
isDisabled={loading}
>
Sign Jupiter Swap Transaction
</Button>
<Spacer />
</Stack>
)
}

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'))
}
14 changes: 14 additions & 0 deletions apps/laboratory/src/components/Solana/SolanaTests.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -83,6 +84,19 @@ export function SolanaTests() {
</Heading>
<SolanaSignAndSendTransaction />
</Box>

<Box>
<Heading size="xs" textTransform="uppercase" pb="2">
Sign Jupiter Swap Transaction
<Tooltip label="Use Jupiter Swap API to create a transaction that has Address Lookup Tables and requests for the wallet to sign it">
<Text as="span" fontSize="sm" ml="2">
ℹ️
</Text>
</Tooltip>
</Heading>
<SolanaSignJupiterSwapTest />
</Box>

{(currentChain?.chainId === solanaTestnet.chainId ||
currentChain?.chainId === solanaDevnet.chainId) && (
<Stack divider={<StackDivider />} spacing="4">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {
Connection,
PublicKey,
Transaction,
TransactionMessage,
VersionedTransaction,
type SendOptions
} from '@solana/web3.js'
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 348b15c

Please sign in to comment.