README.md in solana-ruby-web3js-1.0.1 vs README.md in solana-ruby-web3js-2.0.0beta1
- old
+ new
@@ -55,40 +55,101 @@
result = client.get_parsed_account_info(pubkey, options)
puts result
+### More Information on Solana Methods
+
+For a more detailed overview of Solana's available RPC methods, visit the official documentation:
+
+- [Solana HTTP RPC Methods](https://solana.com/docs/rpc/http)
+- [Solana WebSocket RPC Methods](https://solana.com/docs/rpc/websocket)
+- [Solana web3.js Connection Methods](https://solana-labs.github.io/solana-web3.js/classes/Connection.html)
+
### Options Parameter
-The options parameter is a hash that can include the following fields:
+The options parameter is a hash that can include the following fields and more, allowing for customized responses:
-commitment: Specifies the level of commitment desired when querying state.
+- **commitment**: Specifies the level of commitment desired when querying state. Options include:
-Options include:
+ - 'finalized': Query the most recent block confirmed by supermajority of the cluster.
+ - 'confirmed': Query the most recent block that has been voted on by supermajority of the cluster.
+ - 'processed': Query the most recent block regardless of cluster voting.
- 'finalized': Query the most recent block confirmed by supermajority of the cluster.
- 'confirmed': Query the most recent block that has been voted on by supermajority of the cluster.
- 'processed': Query the most recent block regardless of cluster voting.
+- **encoding**: Defines the format of the returned account data. Possible values include:
-encoding: Defines the format of the returned account data. Possible values include:
+ - 'jsonParsed': Returns data in a JSON-parsed format.
+ - 'base64': Returns raw account data in Base64 encoding.
+ - 'base64+zstd': Returns compressed Base64 data.
- 'jsonParsed': Returns data in a JSON-parsed format.
- 'base64': Returns raw account data in Base64 encoding.
- 'base64+zstd': Returns compressed Base64 data.
+- **epoch**: Specify the epoch when querying for certain information like epoch details.
+- **skipPreflight**: If true, skip the preflight transaction verification. Preflight ensures that a transaction is valid before sending it to the network, but skipping this can result in faster submission.
+
+- **maxRetries**: Specify how many times to retry sending a transaction before giving up.
+
+- **recentBlockhash**: Provide a custom recent blockhash for a transaction if not relying on the default.
+
By providing options, you can control the nature of the returned data and the reliability of the query.
### Filters Parameter
-The filters parameter allows you to specify conditions for querying token accounts. Some common filter types include:
+The filters parameter allows you to specify conditions when querying accounts and other resources. Here are some common filters:
- # Mint Filter: Filter by a specific token mint. This retrieves accounts holding tokens of that mint.
- filters = { mint: 'TokenMintPublicKey' }
+#### Token Accounts by Owner
- # Program Filter: Filter by a specific program (e.g., the token program).
- filters = { programId: 'TokenProgramPublicKey' }
+ # Replace 'owner_pubkey' with the owner's public key
+ owner_pubkey = 'Fg6PaFpoGXkYsidMpWxTWqSKJf6KJkUxX92cnv7WMd2J'
+
+ # Query for token accounts owned by this public key
+ filters = [{ mint: 'TokenMintPublicKey' }]
+
+ result = client.get_token_accounts_by_owner(owner_pubkey, filters)
+
+ puts result
+#### Account Filters
+
+You can use the filters parameter to apply conditions for certain queries, such as fetching token accounts by a specific owner or a specific token program. Below are examples of filters that can be used in different queries.
+
+#### Mint Filter
+
+- Filter accounts by a specific token mint.
+
+ ``filters = [{ mint: 'TokenMintPublicKey' }]``
+
+ ``result = client.get_token_accounts_by_owner(owner_pubkey, filters)``
+
+#### Program Filter
+
+- Filter accounts associated with a particular program, such as the token program.
+
+ ``filters = [{ programId: 'TokenProgramPublicKey' }]``
+
+ ``result = client.get_token_accounts_by_owner(owner_pubkey, filters)``
+
+#### Data Size Filter
+
+- Filter accounts by the exact size of the account data.
+
+ ``filters = [{ dataSize: 165 }]``
+
+ ``result = client.get_program_accounts('ProgramPublicKey', filters)``
+
+#### Memcmp Filter
+
+- Filter by matching a specific slice of bytes at a given offset in account data.
+
+ ``filters = [{
+ memcmp: {
+ offset: 0,
+ bytes: 'Base58EncodedBytes'
+ }
+ }]``
+
+ ``result = client.get_program_accounts('ProgramPublicKey', filters)``
+
## WebSocket Methods
The SolanaRuby gem also provides WebSocket methods to handle real-time notifications and updates from the Solana blockchain. To use the WebSocket client:
# Initialize the WebSocket client
@@ -268,6 +329,78 @@
remove_slot_change_listener(subscription_id)
## Default Options
Several methods have optional parameters where default options are defined in the client. These options can be customized or overridden when calling the methods, but if left unspecified, the client will use its internal defaults.
+
+## Transaction Helpers
+
+### Transfer SOL Between Accounts
+
+To transfer SOL (the native cryptocurrency of the Solana blockchain) from one account to another, follow these steps:
+
+#### Requirements:
+
+- **Sender's Keypair:** Either generate a new keypair or provide the private key for an existing sender account. This keypair is used to sign the transaction.
+- **Receiver's Public Key:** Specify the public key of the destination account. You can generate a new keypair for the receiver or use an existing public key.
+- **Airdrop Functionality:** For Devnet or Testnet transactions, ensure that the sender's account is funded with sufficient lamports using the Solana airdrop feature.
+- An initialized client to interact with the Solana blockchain.
+
+#### Example Usage:
+
+ require 'solana_ruby'
+
+ # Initialize the client (defaults to Mainnet(https://api.mainnet-beta.solana.com))
+ client = SolanaRuby::HttpClient.new('https://api.devnet.solana.com')
+
+ # Fetch the recent blockhash
+ recent_blockhash = client.get_latest_blockhash["blockhash"]
+
+ # Generate or fetch the sender's keypair
+ # Option 1: Generate a new keypair
+ sender_keypair = SolanaRuby::Keypair.generate
+ # Option 2: Use an existing private key
+ # sender_keypair = SolanaRuby::Keypair.from_private_key("InsertPrivateKeyHere")
+
+ sender_pubkey = sender_keypair[:public_key]
+
+
+ # Airdrop some lamports to the sender's account
+ lamports = 10 * 1_000_000_000
+ sleep(1)
+ result = client.request_airdrop(sender_pubkey, lamports)
+ puts "Solana Balance #{lamports} lamports added sucessfully for the public key: #{sender_pubkey}"
+ sleep(10)
+
+
+ # Generate or use an existing receiver's public key
+ # Option 1: Generate a new keypair for the receiver
+ receiver_keypair = SolanaRuby::Keypair.generate # generate receiver keypair
+ receiver_pubkey = receiver_keypair[:public_key]
+ # Option 2: Use an existing public key
+ # receiver_pubkey = 'InsertExistingPublicKeyHere'
+
+ transfer_lamports = 1 * 1_000_000
+ puts "Payer's full private key: #{sender_keypair[:full_private_key]}"
+ puts "Receiver's full private key: #{receiver_keypair[:full_private_key]}"
+ puts "Receiver's Public Key: #{receiver_keypair[:public_key]}"
+
+ # Create a new transaction
+ transaction = SolanaRuby::TransactionHelper.new_sol_transaction(
+ sender_pubkey,
+ receiver_pubkey,
+ transfer_lamports,
+ recent_blockhash
+ )
+
+ # Get the sender's private key (ensure it's a string)
+ private_key = sender_keypair[:private_key]
+ puts "Private key type: #{private_key.class}, Value: #{private_key.inspect}"
+
+ # Sign the transaction
+ signed_transaction = transaction.sign([sender_keypair])
+
+ # Send the transaction to the Solana network
+ sleep(5)
+ response = client.send_transaction(transaction.to_base64, { encoding: 'base64' })
+ puts "Response: #{response}"