-
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.
refactor: solana sign and send transaction (#2646)
- Loading branch information
Showing
8 changed files
with
413 additions
and
279 deletions.
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.