-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
refactor: solana sign and send transaction #2646
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
069ec70
refactor: change solana signAndSendTransaction to be an RPC request f…
zoruka 2046bf8
refactor: remove blockhash param
zoruka 38eb8b8
fix: remove extra param
zoruka 144324c
fix: formating issue
zoruka c741a8e
feat: add signAndSendTransaction function for wallet adapter and send…
zoruka f9757a2
Merge branch 'main' of github.com:WalletConnect/web3modal into refact…
zoruka af0a2e0
fix: add missing .js for import
zoruka 99750af
Merge branch 'main' of github.com:WalletConnect/web3modal into refact…
zoruka d7aa413
Merge branch 'main' of github.com:WalletConnect/web3modal into refact…
zoruka 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
92 changes: 92 additions & 0 deletions
92
apps/laboratory/src/components/Solana/SolanaSignAndSendTransactionTest.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,92 @@ | ||
import { useState } from 'react' | ||
import { Button, Stack, Text, Spacer, Link } from '@chakra-ui/react' | ||
import { useWeb3ModalAccount, useWeb3ModalProvider } from '@web3modal/solana/react' | ||
import { PublicKey, Transaction, SystemProgram } from '@solana/web3.js' | ||
|
||
import { solana } from '../../utils/ChainsUtil' | ||
import { useChakraToast } from '../Toast' | ||
|
||
const PHANTOM_TESTNET_ADDRESS = '8vCyX7oB6Pc3pbWMGYYZF5pbSnAdQ7Gyr32JqxqCy8ZR' | ||
const recipientAddress = new PublicKey(PHANTOM_TESTNET_ADDRESS) | ||
const amountInLamports = 50000000 | ||
|
||
export function SolanaSignAndSendTransaction() { | ||
const toast = useChakraToast() | ||
const { address, chainId } = useWeb3ModalAccount() | ||
const { walletProvider, connection } = useWeb3ModalProvider() | ||
const [loading, setLoading] = useState(false) | ||
|
||
async function onSendTransaction() { | ||
try { | ||
setLoading(true) | ||
if (!walletProvider || !address) { | ||
throw Error('user is disconnected') | ||
} | ||
|
||
if (!connection) { | ||
throw Error('no connection set') | ||
} | ||
|
||
const balance = await connection.getBalance(walletProvider.publicKey) | ||
if (balance < amountInLamports) { | ||
throw Error('Not enough SOL in wallet') | ||
} | ||
|
||
// Create a new transaction | ||
const transaction = new Transaction().add( | ||
SystemProgram.transfer({ | ||
fromPubkey: walletProvider.publicKey, | ||
toPubkey: recipientAddress, | ||
lamports: amountInLamports | ||
}) | ||
) | ||
transaction.feePayer = walletProvider.publicKey | ||
const signature = await walletProvider.signAndSendTransaction(transaction) | ||
|
||
toast({ | ||
title: 'Success', | ||
description: signature, | ||
type: 'success' | ||
}) | ||
} catch (err) { | ||
toast({ | ||
title: 'Error', | ||
description: (err as Error).message, | ||
type: 'error' | ||
}) | ||
} finally { | ||
setLoading(false) | ||
} | ||
} | ||
|
||
if (!address) { | ||
return null | ||
} | ||
|
||
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={onSendTransaction} | ||
isDisabled={loading} | ||
> | ||
Sign and Send Transaction | ||
</Button> | ||
<Spacer /> | ||
|
||
<Link isExternal href="https://solfaucet.com/"> | ||
<Button variant="outline" colorScheme="blue"> | ||
Solana Faucet | ||
</Button> | ||
</Link> | ||
</Stack> | ||
) | ||
} |
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
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is the difference between Dapp and Wallet?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Dapp: the dapp request to wallet sign transaction and sends to blockchain
Wallet: the dapp request to wallet sign and send the transaction
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm I feel this is confusing, so you are doing basically
Why send the tx from lab in the first place? isn't just requesting the signing enough?
Just a nit tho
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is the two ways of doing it and both are valid: the first case is testing the
signTransaction
andsendTransaction
functions, the second is testingsignAndSendTransaction
which sends for wallet handle the blockchain connection.