How can you transfer SOL using the web3.js sdk for Solana?
Asked
Active
Viewed 1.1k times
2 Answers
20
var web3 = require("@solana/web3.js");
// Address: 9vpsmXhZYMpvhCKiVoX5U8b1iKpfwJaFpPEEXF7hRm9N
const DEMO_FROM_SECRET_KEY = new Uint8Array([
37, 21, 197, 185, 105, 201, 212, 148, 164, 108, 251, 159, 174, 252, 43, 246,
225, 156, 38, 203, 99, 42, 244, 73, 252, 143, 34, 239, 15, 222, 217, 91, 132,
167, 105, 60, 17, 211, 120, 243, 197, 99, 113, 34, 76, 127, 190, 18, 91, 246,
121, 93, 189, 55, 165, 129, 196, 104, 25, 157, 209, 168, 165, 149,
]);
(async () => {
// Connect to cluster
var connection = new web3.Connection(web3.clusterApiUrl("devnet"));
// Construct a `Keypair` from secret key
var from = web3.Keypair.fromSecretKey(DEMO_FROM_SECRET_KEY);
// Generate a new random public key
var to = web3.Keypair.generate();
// Add transfer instruction to transaction
var transaction = new web3.Transaction().add(
web3.SystemProgram.transfer({
fromPubkey: from.publicKey,
toPubkey: to.publicKey,
lamports: web3.LAMPORTS_PER_SOL / 100,
})
);
// Sign transaction, broadcast, and confirm
var signature = await web3.sendAndConfirmTransaction(
connection,
transaction,
[from]
);
console.log("SIGNATURE", signature);
console.log("SUCCESS");
})();

Chase Barker
- 947
- 5
- 7
-
How to make or construct a Signer from the existing Pvt key and public key? or is it possible to sign the transaction from the PVT key string directly? – dush88c Sep 01 '21 at 03:55
-
https://github.com/solana-labs/solana-web3.js/blob/master/examples/send_sol.js – Chase Barker Sep 02 '21 at 04:03
-
You CANNOT make a signer from the public key alone. The only options you have to generate signers are by using the privateKey or the seed phrase. The relevant functions would be: import * as web3 from '@solana/web3.js'; const signer1 = web3.Keypair.fromSeed(); OR const signer2 = web3.Keypair.fromSecretKey(); OR const singer3 = web3.Keypair.generate(); //Random public and private keys are generated – Rahul Saxena Sep 03 '21 at 21:06
-
asking outside the scope, but anyone knows how to link an image with solana token in samrt contract? – Sudarshan Sep 20 '21 at 03:22
-
@RahulSaxena there are reports that you can `let wallet = useAnchorWallet()` and `wallet.signTransaction` to sign the transaction using your wallet's internals. Currently researching this – Eugene Gr. Philippov Jan 14 '22 at 22:51
-
unrelated to the topic, this line web3.clusterApiUrl("devnet") - no provider is specified. Doing the same in ethereum, I would have to get node from a provider - Infura for example to access the testnets. But that doesn't seem to be the case in solana? – Gervasius Twinklewinkleson Jan 25 '22 at 15:08
-
how to generate a keypair from the secret phrase (12 words string) – Said Pc Feb 10 '22 at 02:54
9
Here's an example of how you can do that. Assumptions:
- You have a Phantom wallet.
- You're working on devnet
- You have a string of the public key (address) of the destination wallet.
"9fuYBoRvgptU4fVZ8ZqvWTTc6oC68P4tjuSA2ySzn6Nv"
would be used here.
Code
import * as web3 from '@solana/web3.js';
import * as splToken from '@solana/spl-token';
const getProvider = async () => {
if ("solana" in window) {
const provider = window.solana;
if (provider.isPhantom) {
console.log("Is Phantom installed? ", provider.isPhantom);
return provider;
}
} else {
window.open("https://www.phantom.app/", "_blank");
}
};
async function transferSOL() {
// Detecing and storing the phantom wallet of the user (creator in this case)
var provider = await getProvider();
console.log("Public key of the emitter: ",provider.publicKey.toString());
// Establishing connection
var connection = new web3.Connection(
web3.clusterApiUrl('devnet'),
);
// I have hardcoded my secondary wallet address here. You can take this address either from user input or your DB or wherever
var recieverWallet = new web3.PublicKey("9fuYBoRvgptU4fVZ8ZqvWTTc6oC68P4tjuSA2ySzn6Nv");
// Airdrop some SOL to the sender's wallet, so that it can handle the txn fee
var airdropSignature = await connection.requestAirdrop(
provider.publicKey,
web3.LAMPORTS_PER_SOL,
);
// Confirming that the airdrop went through
await connection.confirmTransaction(airdropSignature);
console.log("Airdropped");
var transaction = new web3.Transaction().add(
web3.SystemProgram.transfer({
fromPubkey: provider.publicKey,
toPubkey: recieverWallet,
lamports: web3.LAMPORTS_PER_SOL //Investing 1 SOL. Remember 1 Lamport = 10^-9 SOL.
}),
);
// Setting the variables for the transaction
transaction.feePayer = await provider.publicKey;
let blockhashObj = await connection.getRecentBlockhash();
transaction.recentBlockhash = await blockhashObj.blockhash;
// Transaction constructor initialized successfully
if(transaction) {
console.log("Txn created successfully");
}
// Request creator to sign the transaction (allow the transaction)
let signed = await provider.signTransaction(transaction);
// The signature is generated
let signature = await connection.sendRawTransaction(signed.serialize());
// Confirm whether the transaction went through or not
await connection.confirmTransaction(signature);
//Print the signature here
console.log("Signature: ", signature);
}

TylerH
- 20,799
- 66
- 75
- 101

Rahul Saxena
- 630
- 7
- 10