Documentation Index
Fetch the complete documentation index at: https://docs.stardust.gg/llms.txt
Use this file to discover all available pages before exploring further.
This guide will walk you through the process of sending a transaction using the Stardust API and the ethers library. By following this example, you’ll be able to create, sign, and send a transaction on the Ethereum Sepolia network.
Prerequisites
- Node.js installed on your system
- An Ethereum wallet with some Sepolia testnet ETH
- Stardust API key and wallet ID
dotenv, axios, and ethers libraries
Setup
-
Install Dependencies
First, you need to install the required dependencies:
npm install dotenv axios ethers
-
Create a
.env File
Create a .env file in your project root directory and add your Stardust API key and wallet ID:
PROD_SYSTEM_STARDUST_API_KEY=your_stardust_api_key
PROD_SYSTEM_STARDUST_WALLET_ID=your_stardust_wallet_id
Code Example
First, import the necessary libraries and configure your environment using the .env file:
import dotenv from 'dotenv';
import axios from 'axios';
import { ethers } from 'ethers';
dotenv.config();
This code imports dotenv, axios, and ethers libraries and sets up the environment variables.
2. Setup Configuration
Next, set up the API key and wallet ID from the environment variables:
const apiKey = process.env.PROD_SYSTEM_STARDUST_API_KEY!;
const walletId = process.env.PROD_SYSTEM_STARDUST_WALLET_ID!;
Next, set up the RPC provider for the Ethereum Sepolia network:
const rpcUrl = 'https://ethereum-sepolia-rpc.publicnode.com';
const rpcProvider = new ethers.providers.JsonRpcProvider(rpcUrl);
3. Create Transaction
Create and encode the transaction using the ethers library. This example uses an EIP-1559 transaction. More information about EIP-1559 can be found here or here.
If you are interacting with a contract, you will need to encode the data to interact with the contract according to the data field
const eip1559transaction = {
to: '0x355172E1AA17117DfCFDD2AcB4b0BFDA8308Cbc9', // recipient address, contract or EOA
value: ethers.utils.parseEther('0.01'), // amount to send
nonce: 2, // manage this or interact with the RPC directly to get the next nonce
maxFeePerGas: ethers.utils.parseUnits('10', 'gwei'), // based on the network you are interacting with
maxPriorityFeePerGas: ethers.utils.parseUnits('10', 'gwei'), // tip based on the network you are interacting with
gasLimit: 21000, // standard for basic transaction, if interacting with a contract you will need to estimate the gas
data: '0x', // hex encoded data to interact with a contract
chainId: 11155111, // chainId of the network you are interacting with, this example uses Sepolia
type: 2, // 1559 transaction type
};
4. Serialize/Encode the Transaction
Serialize the transaction using the ethers library.
source
const unsignedSerializedTransaction = ethers.utils.serializeTransaction(eip1559transaction);
5. Sign the Transaction
Send the unsigned serialized transaction to the Stardust API to get it signed for your given wallet:
const payload = {
walletId,
chainType: 'evm',
message: unsignedSerializedTransaction,
};
const resp = await axios.post('https://custodial-wallet.stardust.gg/v1/sign/message', payload, {
headers: { 'x-api-key': apiKey },
});
const { signature } = resp.data;
This code constructs a payload with the wallet ID, chain type, and encoded transaction, and sends it to the Stardust API to get a signature.
6. Serialize the Signed Transaction for Sending
This step appends the signature to the transaction for verification on the network:
const serializedSignedTx = ethers.utils.serializeTransaction(eip1559transaction, signature);
7. Send the Signed Transaction
Send the signed transaction to the Ethereum Sepolia network using eth_sendRawTransaction.
source
const sentTx = await rpcProvider.sendTransaction(serializedSignedTx);
console.log(`Sent tx object ${JSON.stringify(sentTx)}`);
This code sends the signed transaction using the RPC provider.
8. Output
Successful output should look like the following:
Sent tx object {"type":2,"chainId":11155111,"nonce":2,"maxPriorityFeePerGas":{"type":"BigNumber","hex":"0x02540be400"},"maxFeePerGas":{"type":"BigNumber","hex":"0x02540be400"},"gasPrice":null,"gasLimit":{"type":"BigNumber","hex":"0x5208"},"to":"0x355172E1AA17117DfCFDD2AcB4b0BFDA8308Cbc9","value":{"type":"BigNumber","hex":"0x2386f26fc10000"},"data":"0x","accessList":[],"hash":"0x93f06003a67a83764aa7cd2701ba827488a4b4d9d2a99385806543a872556479","v":0,"r":"0x4bf8227064515642ffd2a66be059b729abd2efbb3cd1f8450e6daa3726606b94","s":"0x58179b3af707c39a675066edc5f1d84698c9b7ad6f68e8d4402ad21ce8908fe7","from":"0x2210FA04B60d6846552F889DCde641022648F493","confirmations":0}
Conclusion
You have successfully created, signed, and sent a transaction using the Stardust API and the ethers library. You can now use this example to integrate transaction sending into your application. For more information, refer to the Stardust API documentation.