-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: remove deprecated params from sign transaction wc rpc request (#…
- Loading branch information
Showing
5 changed files
with
139 additions
and
53 deletions.
There are no files selected for viewing
This file contains 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
122 changes: 122 additions & 0 deletions
122
apps/laboratory/src/components/Solana/SolanaSignJupiterSwapTest.tsx
This file contains 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 { 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')) | ||
} |
This file contains 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 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 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