gasless-sdk
v1.1.0
Published
Gasless transactions SDK for Solana
Maintainers
Readme
Solana Gasless SDK
Enable gasless transactions in your Solana dApp with one line of code. Let your users interact with your dApp without needing SOL or creating a wallet.
Features
- 🚀 Zero Gas Fees - Users don't need SOL to transact
- 🔒 No Private Keys - Backend handles all signatures
- 💸 Simple Integration - Just 2 lines of code
- ⚡ Fast Processing - Transactions confirm quickly
- 🛡️ Rate Limited - Protected against abuse
Installation
npm install solana-gasless-sdkQuick Start
import { GaslessSDK } from 'solana-gasless-sdk';
// Initialize SDK - no private keys needed!
const sdk = new GaslessSDK();
// Send a gasless transaction
const result = await sdk.sendTransaction(
'RECIPIENT_ADDRESS',
GaslessSDK.solToLamports(0.1) // 0.1 SOL
);
if (result.success) {
console.log('Transaction successful:', result.signature);
} else {
console.error('Error:', result.error);
}How It Works
- Your dApp calls the SDK with recipient address and amount
- SDK sends request to our secure relayer service
- Relayer (backend) processes and pays for the transaction
- Transaction signature is returned to your dApp
Your dApp → SDK → Relayer → Solana Network
↓
Private Key
(Managed by Relayer)API Reference
new GaslessSDK(config?)
Initialize the SDK.
const sdk = new GaslessSDK({
relayerUrl?: string // Optional: Custom relayer URL
});sendTransaction(recipientAddress: string, lamports: number)
Send a gasless transaction.
const result = await sdk.sendTransaction(address, lamports);
// Returns: { success: boolean, signature?: string, error?: string }Utility Functions
// Convert SOL to lamports
const lamports = GaslessSDK.solToLamports(1); // 1 SOL = 1000000000 lamports
// Convert lamports to SOL
const sol = GaslessSDK.lamportsToSol(1000000000); // 1 SOLSecurity Model
- ✅ No private keys required from users
- ✅ All transactions signed by secure backend
- ✅ Rate limiting prevents abuse
- ✅ Input validation on all parameters
- ✅ Error handling for failed transactions
Limitations
- Maximum transaction: 15 SOL per transfer
- Rate limit: 3 requests per wallet per 3 hours
- Only supports native SOL transfers (not tokens)
Error Handling
try {
const result = await sdk.sendTransaction(address, lamports);
if (!result.success) {
switch(result.error) {
case 'Too many requests':
// Handle rate limit
break;
case 'Invalid recipient address':
// Handle invalid address
break;
default:
// Handle other errors
}
}
} catch (error) {
// Handle network errors
}Best Practices
- Always check success
if (result.success) {
// Process success
} else {
// Handle error
}- Use utility functions
// Good
const lamports = GaslessSDK.solToLamports(0.1);
// Not recommended
const lamports = 100000000; // Hard to verify amount- Handle rate limits
// Inform users about remaining requests
if (result.error === 'Too many requests') {
alert('Please try again in 3 hours');
}Examples
React Example
function SendButton() {
const handleSend = async () => {
const sdk = new GaslessSDK();
const result = await sdk.sendTransaction(
'Address',
GaslessSDK.solToLamports(0.1)
);
if (result.success) {
console.log(`Sent! Signature: ${result.signature}`);
}
};
return <button onClick={handleSend}>Send 0.1 SOL</button>;
}Next.js Example
export default function TransactionPage() {
const [status, setStatus] = useState('');
const sendTransaction = async () => {
const sdk = new GaslessSDK();
setStatus('Sending...');
const result = await sdk.sendTransaction(
'Address',
GaslessSDK.solToLamports(0.1)
);
setStatus(result.success ? 'Sent!' : 'Failed');
};
}Complete Example
1. Simple HTML + JavaScript Example
<!DOCTYPE html>
<html>
<head>
<title>Solana Gasless Transfer</title>
</head>
<body>
<h1>Solana Gasless Transfer</h1>
<input type="text" id="recipient" placeholder="Recipient Address">
<input type="number" id="amount" placeholder="Amount in SOL" step="0.1">
<button onclick="sendTransaction()">Send SOL</button>
<div id="status"></div>
<script type="module">
import { GaslessSDK } from 'solana-gasless-sdk-dev';
window.sendTransaction = async function() {
const status = document.getElementById('status');
const recipient = document.getElementById('recipient').value;
const amount = parseFloat(document.getElementById('amount').value);
status.textContent = 'Sending transaction...';
try {
const sdk = new GaslessSDK();
const lamports = GaslessSDK.solToLamports(amount);
const result = await sdk.sendTransaction(recipient, lamports);
if (result.success) {
status.textContent = `Success! Transaction signature: ${result.signature}`;
} else {
status.textContent = `Error: ${result.error}`;
}
} catch (error) {
status.textContent = `Error: ${error.message}`;
}
}
</script>
</body>
</html>2. React Component Example
import { useState } from 'react';
import { GaslessSDK } from 'solana-gasless-sdk-dev';
export function GaslessTransfer() {
const [recipient, setRecipient] = useState('');
const [amount, setAmount] = useState('');
const [status, setStatus] = useState('');
const handleTransfer = async (e) => {
e.preventDefault();
setStatus('Sending transaction...');
try {
const sdk = new GaslessSDK();
const lamports = GaslessSDK.solToLamports(parseFloat(amount));
const result = await sdk.sendTransaction(recipient, lamports);
if (result.success) {
setStatus(`Success! Tx: ${result.signature}`);
} else {
setStatus(`Error: ${result.error}`);
}
} catch (error) {
setStatus(`Error: ${error.message}`);
}
};
return (
<div>
<h2>Send SOL (Gasless)</h2>
<form onSubmit={handleTransfer}>
<input
type="text"
placeholder="Recipient Address"
value={recipient}
onChange={(e) => setRecipient(e.target.value)}
/>
<input
type="number"
step="0.1"
placeholder="Amount in SOL"
value={amount}
onChange={(e) => setAmount(e.target.value)}
/>
<button type="submit">Send</button>
</form>
<div>{status}</div>
</div>
);
}3. Node.js Script Example
const { GaslessSDK } = require('solana-gasless-sdk-dev');
async function sendGaslessTransaction() {
try {
const sdk = new GaslessSDK();
// Example values
const recipientAddress = 'RECIPIENT_SOLANA_ADDRESS';
const amountInSol = 0.1;
const result = await sdk.sendTransaction(
recipientAddress,
GaslessSDK.solToLamports(amountInSol)
);
if (result.success) {
console.log('Transaction successful!');
console.log('Signature:', result.signature);
console.log('Amount:', result.lamports, 'lamports');
console.log('Recipient:', result.recipient);
} else {
console.error('Transaction failed:', result.error);
}
} catch (error) {
console.error('Error:', error.message);
}
}
sendGaslessTransaction();Quick Test
To quickly test if the SDK is working:
// In Node.js or browser console
import { GaslessSDK } from 'solana-gasless-sdk-dev';
const test = async () => {
const sdk = new GaslessSDK();
const result = await sdk.sendTransaction(
'9zPVXhH7Ean3dexNvRqer1TyrmhLGh2yWxEtgQC3sQcN', // Example address
GaslessSDK.solToLamports(0.1)
);
console.log(result);
};
test();Note: Make sure to:
- Replace
RECIPIENT_SOLANA_ADDRESSwith a real Solana address - Keep amounts below 15 SOL
- Handle rate limits (3 requests per wallet per 3 hours)
Support
- GitHub Issues: Report a bug
- X: @adxtya_jha
- Email: [email protected]
License
MIT License - feel free to use in your projects!
