πŸ’Έ
Sending SOLs through browser JavaScript
Contributors: @rajkaria, @kb24x7​
Now that we have figured out how to connect the wallet to our website. Let's see how we can create and sign a transaction through the wallets.
Prerequisites:
1
yarn add @solana/web3.js
Copied!
OR
1
npm i @solana/web3.js
Copied!
Let's import some constructors from this module:
1
// wallet.ts
2
​
3
import {
4
Connection,
5
Transaction,
6
SystemProgram,
7
PublicKey
8
} from '@solana/web3.js';
Copied!
If you're coming from the last article, you know that we have a file that exports the user's wallet and a method that connects to the wallet in this file itself, if you've missed that I highly recommend that you check out the last article. So let's import the getWallet function in this file.
1
import { getWallet } from './exportWallet.ts'
Copied!
We now have everything we need, let's create a function to send SOLs.
1
export const sendMoney = async (to: PublicKey, amount: number) => {
2
const wallet = getWallet()
3
const sender = connectWallet()
4
const environment = "mainnet-beta"
5
const network = `https://api.${environment}.solana.com`
6
​
7
// This method connects to the mainnet-beta solana network,
8
// you can change it to devnet as well.
9
​
10
const connection = new Connection(network)
11
12
// Let's construct the transaction here
13
const transaction = new Transaction()
14
.add(
15
SystemProgram.transfer({
16
fromPubkey: sender,
17
toPubkey: to,
18
lamports: amount * 1000000000,
19
})
20
);
21
​
22
// These are the transaction instructions,
23
// 1 SOL = 1000000000 LAMPs
24
25
const { blockhash } = await connection.getRecentBlockhash()
26
transaction.recentBlockhash = blockhash
27
transaction.feePayer = publicKey
28
29
// This transaction needs to be signed by the user before it can
30
// be sent
31
let signedTransaction = await wallet.signTransaction(transaction)
32
​
33
// Let's send this transaction to the network now
34
​
35
try {
36
const txid = await connection.sendRawTransaction(
37
signedTransaction.serialize()
38
)
39
return txid;
40
catch (error) {
41
console.error(error)
42
}
43
44
}
Copied!
Now we can utilize this function anywhere in our working directory. There are
1
import { sendMoney } from './wallet.ts'
2
import { PublicKey } from '@solana/web3.js'
3
​
4
async function main() {
5
const receiver = new PublicKey("....") // Receiver's Address
6
const txid = await sendMoney(receiver, 1) // Send 1 SOL
7
}
Copied!

FAQs

If you have any doubts related to this topic or you have a suggestion, feel free to create an issue here.
If you want to make edits to this topic or update the topic, please click on the "Edit on Github" link you see on the right section, make the changes and create a pull request to the master branch. One of the moderators will look at your changes & merge your changes if all looks good.
And yes, if you update anything in this topic, don't forget to add your GitHub username in the contributors' list!
Have a happy learning! ✌️
Copy link
Contents
FAQs