This guide provides a simple example of how to claim rewards on something.cool using js.
npm install @solana/web3.js bs58 tweetnacl axios
const { Connection, PublicKey, Transaction, Keypair } = require("@solana/web3.js")
const bs58 = require("bs58")
const nacl = require("tweetnacl")
const axios = require("axios")
// Configuration
const API_URL = "TBA/api/v1"
const RPC_URL = "https://api.mainnet-beta.solana.com"
// Your wallet's private key (in production, use secure key management)
const privateKey = "YOUR_PRIVATE_KEY_HERE" // Replace with your private key
const wallet = Keypair.fromSecretKey(bs58.decode(privateKey))
const walletAddress = wallet.publicKey.toString()
// Token you want to claim rewards for
const tokenAddress = "TokenAddressHere" // Replace with token address
const claimType = "SOL" // or 'TOKEN'
// Create Solana connection
const connection = new Connection(RPC_URL)
// Function to claim rewards
async function claimRewards() {
// Step 1: Generate and sign timestamp
const timestamp = Date.now().toString()
const timestampBytes = Buffer.from(timestamp)
const signature = nacl.sign.detached(timestampBytes, wallet.secretKey)
const signatureBase58 = bs58.encode(signature)
// Step 2: Get claimable rewards for the token
const tokenRewardsResponse = await axios.get(
`${API_URL}/rewards/${walletAddress}/tokens/${tokenAddress}`,
{
headers: {
"X-Signature": signatureBase58,
"X-Timestamp": timestamp,
},
}
)
console.log("Claimable rewards:", tokenRewardsResponse.data)
// Check if there are rewards to claim
if (
tokenRewardsResponse.data.status === "success" &&
tokenRewardsResponse.data.data.unclaimedRewards[claimType.toLowerCase()] > 0
) {
console.log("Timestamp signed successfully")
// Step 3: Initiate claim
const claimResponse = await axios.post(
`${API_URL}/rewards/${walletAddress}/initiate`,
{
tokenAddress,
claimType,
burnToken: false,
},
{
headers: {
"Content-Type": "application/json",
"X-Signature": signatureBase58,
"X-Timestamp": timestamp,
},
}
)
if (claimResponse.data.status === "success") {
// Step 4: Process and sign the transaction
const transactionBase64 = claimResponse.data.data.tx
const transactionBuffer = Buffer.from(transactionBase64, "base64")
const transaction = Transaction.from(transactionBuffer)
// Sign the transaction
transaction.partialSign(wallet)
// Step 5: Send transaction to the network
const rawTransaction = transaction.serialize()
const txSignature = await connection.sendRawTransaction(rawTransaction)
console.log("Transaction sent:", txSignature)
// Wait for confirmation
const confirmation = await connection.confirmTransaction(txSignature)
console.log("Transaction confirmed:", confirmation)
// Step 6: Check claim status
const statusResponse = await axios.get(`${API_URL}/rewards/${txSignature}/status`)
console.log("Claim status:", statusResponse.data)
return txSignature
} else {
console.error("Failed to initiate claim:", claimResponse.data)
}
} else {
console.log("No rewards available to claim")
}
}
// Run the claim function
claimRewards()
.then((signature) => console.log("Claim process complete! Signature:", signature))
.catch((error) => console.error("Claim process failed:", error))