Getting Started

In this guide we will look at leveraging the Solana Web3.js library in conjunction with the Stardust Custodial SDK, using it to sign and send a transaction with SOL from one wallet address to another

Step 1: Installation

npm install @stardust-gg/stardust-custodial-sdk-solana-js
npm install @solana/web3.js

Step 2: Instantiate the Stardust Custodial SDK & Create a Wallet

const solanaWeb3 = require('@solana/web3.js');
const { StardustCustodialSDK } = require('@stardust-gg/stardust-custodial-sdk-solana-js');

const API_KEY = 'your_api_key';

// Initializing the SDK
const StardustSDK = new StardustCustodialSDK(API_KEY);

// Create two wallets
const sender = await StardustSDK.createWallet();
const receiver = await StardustSDK.createWallet();

console.log(`Sender: ${sender}`);
console.log(`Receiver: ${receiver}`);

// Make sure to save both wallet Ids!
const senderId = sender.id;
const receiverId = receiver.id;

📘 Send a 0.01 SOL to the sender PublicKey to continue with this tutorial!

Step 3: Create a transfer transaction on Solana, sign it, and send it!

const solanaWeb3 = require('@solana/web3.js');
const { StardustCustodialSDK } = require('@stardust-gg/stardust-custodial-sdk-solana-js');

const API_KEY = 'your_api_key';

// Initializing the SDK
const StardustSDK = new StardustCustodialSDK(API_KEY);

// From the above step!
const senderId = sender.id;
const receiverId = receiver.id;

// Get both wallets
const sender = await StardustSDK.getWallet(senderId);
const receiver = await StardustSDK.getWallet(receiverId);

// Get each wallets Public Key
const senderPublicKey = await sender.getPublicKey();
const receiverPublicKey = await receiver.getPublicKey();

// Get their existing balances on Solana, make sure sender has above 0 SOL! 
const senderBalance = await connection.getBalance(senderPublicKey);
const receiverBalance = await connection.getBalance(receiverPublicKey);

console.log(`Sending from wallet with address ${senderPublicKey} and balance ${senderBalance}`)
console.log(`Receiving from wallet with address ${receiverPublicKey} and balance ${receiverBalance}`)

// Setting up the transaction
const instruction = solanaWeb3.SystemProgram.transfer({
  fromPubkey: senderPublicKey,
  toPubkey: receiverPublicKey,
  lamports: solanaWeb3.LAMPORTS_PER_SOL / 1000, // Amount to send, in lamports (1 SOL = 1e9 lamports)
});
const ind = await connection.getLatestBlockhash();
ind.feePayer = senderPublicKey;

const transaction = new solanaWeb3.Transaction(ind).add(instruction);
const serializedTx = transaction.serializeMessage().toString("hex");
console.log(`Serialized Transaction: ${serializedTx}`);

// Signing the transaction and adding the signature
let signature = await sender.sign(serializedTx);
transaction.addSignature(senderPublicKey, Buffer.from(signature, "hex"));

// Sending transaction
const sent = await connection.sendRawTransaction(transaction.serialize());
console.log(`Sent transaction with hash: ${sent}`);

Example Output

Sending from wallet with address 3NtuCZMou8Epqk1xhUZQRLMiMCSoxPTWk5qaLu7MEmbi and balance 112915000

Receiving from wallet with address 9GMLUrYV5a3MofW1SgHtrSHe7zKNmPzv5xcGXCLvgKKD and balance 0

Serialized Transaction: 010001032353411cd57647d7d7ae44440094ab5d4370a55aac6dd967e7c07c83db482a957acc338deb3a7691762b8b3629342683fe00bf411f86b8e7235683250501e4b00000000000000000000000000000000000000000000000000000000000000000cf9fb2a8dfe92185e513ad68ac1d41ae21ba05133f4020dae31e1f416a10d32201020200010c0200000040420f0000000000

Sent transaction with hash: 25UPi4W9zjjZR6UtRiWD1TvppfkGiAdXnerGVbpoPkpd6HBQ3gkee9JCc5h15TJ9YikVsw2PhskvscM8EKAGdMXv

Feel free to check out this transaction address on the Solana explorer! https://explorer.solana.com/