您的 USDC 正以昨天的利率產生收益Quicknode 會自動將其移至今日最佳的 Morpho 錢庫中。目前已在 7 條區塊鏈上上線。
制定您的策略Solana :與Ethereum 方法的比較
一篇針對Ethereum RPC 方法與Solana RPC 呼叫所進行的全面比較。透過釐清兩者之間的細微差異、相似之處與區別,我們將彌合這兩大領先區塊鏈之間的知識鴻溝。

2023年8月31日 — 閱讀時間 11 分鐘

在探索廣闊的區塊鏈世界時,許多熟悉Ethereum RPC 方法的開發者,往往會想了解這些方法在其他區塊鏈(例如Solana)上的對應方式。本入門指南Ethereum RPC 方法與Solana 方法進行比較,闡明兩者的功能與對應關係。 無論您是希望釐清Solana 運作原理的Ethereum ,還是剛踏入Solana 領域的新手開發者,這份入門指南都將提供有助於深化您區塊鏈知識的見解。
與Ethereum 對應的Solana 方法 eth_getBlockByNumber 的對應Solana 方法 getBlock。然而,必須注意的是Solana Ethereum 在區塊鏈的結構和術語Ethereum 不同,因此回傳的資料以及與這些方法互動的方式可能會有些差異。
在Ethereum中, eth_getBlockByNumber 可讓您透過區塊編號或區塊標籤來擷取區塊資訊。同樣地,在Solana 中, getBlock 則可讓您透過提供區塊的槽位(相當於Ethereum區塊編號)來檢索區塊資訊。
以下是一個關於如何使用 getBlock 方法的Solana:
# Using solana-cli solana block <block_slot>
若您是透過程式方式與Solana API 進行互動,可以使用像solana.js這樣的 JavaScript 函式庫來呼叫此方法:
const web3 = require('@solana/web3.js');
const solanaRpcUrl = 'QUICKNODE_URL'; // Replace with the quicknode url
const connection = new web3.Connection(solanaRpcUrl, 'confirmed');
async function getSolanaBlock(slot) {
const block = await connection.getBlock(slot);
console.log(block);
}
const blockSlot = 12345678; // Replace with the desired block slot
getSolanaBlock(blockSlot);Ethereum eth_getTransactionReceipt 可擷取交易的收據,其中包含合約事件與日誌的相關資訊。在Solana 中,由於Solana Ethereum 交易模型及資料結構Ethereum 沒有直接對應的方法。不過,您可以透過結合使用Solana RPC 方法與程式設計功能,來實現類似的功能。
若要取得與 eth_getTransactionReceipt 在Solana 中取得類似的資訊,您需要按照以下步驟操作:
使用Solana RPC 方法取得交易資訊:
請使用 getTransactionSolana 方法來擷取交易詳細資訊。
解碼日誌與事件:
Solana 本身Solana Ethereum那樣具備日誌的概念。相反地,您需要設計您的Solana 使其發送您有興趣追蹤的自訂事件或資料。
從交易訊息或交易日誌中解析並解碼這些自訂事件。
建立自訂回應:
根據解碼後的事件和資訊,您可以建立一個自訂回應物件,其內容與從Ethereum 收據中獲得的資訊相似。
以下是一個簡化的範例,說明您如何使用 JavaScript 和Solana solana.js函式庫來處理此問題:
const web3 = require('@solana/web3.js');
async function getSolanaTransactionReceipt(transactionSignature) {
const solanaRpcUrl = 'QUICKNODE_URL'; // Replace with the quicknode url
const connection = new web3.Connection(solanaRpcUrl, 'confirmed');
const transaction = await connection.getTransaction(transactionSignature);
if (!transaction) {
return null; // Transaction not found
}
// Parse and decode custom events/logs from transaction.message
const decodedEvents = parseAndDecodeEvents(transaction.message.logs);
const receipt = {
transactionHash: transactionSignature,
blockNumber: transaction.slot, // Solana's equivalent of block number
// Other relevant information based on your decoded events
decodedEvents,
};
return receipt;
}
function parseAndDecodeEvents(logs) {
// Implement logic to parse and decode logs into custom event data
// This could involve iterating through logs and extracting data
// Refer to Solana's documentation and your program's event structure
return decodedEvents;
}
const transactionSignature = '...'; // Replace with the Solana transaction signature
getSolanaTransactionReceipt(transactionSignature)
.then((receipt) => {
console.log(receipt);
})
.catch((error) => {
console.error('Error:', error);
});Solana 的設計與Ethereum因此您需要據此調整您的做法。所提供的範例僅為高層次的指引,您需要根據您具體的Solana 使用情境進行客製化調整。
Ethereum eth_getCode 用於擷取部署於Ethereum 智慧合約的位元組碼。對應的Solana 方法則是 getAccountInfo,可用於擷取帳戶相關資訊,包括與該帳戶相關聯的資料。
以下是您如何使用Solana getAccountInfo 方法,並與Ethereumeth_getCode的比較說明:
Ethereum eth_getCode:
// Using web3.js for Ethereum
const Web3 = require('web3');
const web3 = new Web3('QUICKNDOE_URL'); // Replace with the quicknode url
async function getEthContractBytecode(address) {
const bytecode = await web3.eth.getCode(address);
return bytecode;
}
const contractAddress = '0x...'; // Replace with the Ethereum contract address
getEthContractBytecode(contractAddress)
.then((bytecode) => {
console.log(bytecode);
})
.catch((error) => {
console.error('Error:', error);
});Solana getAccountInfo:
const web3 = require('@solana/web3.js');
async function getSolanaContractData(address) {
const solanaRpcUrl = 'QUICKNODE_URL'; // Replace with the quicknode url const connection = new web3.Connection(solanaRpcUrl, 'confirmed');
const publicKey = new web3.PublicKey(address);
const accountInfo = await connection.getAccountInfo(publicKey);
if (!accountInfo) {
return null; // Account not found
}
const contractData = accountInfo.data;
return contractData;
}
const contractAddress = '...'; // Replace with the Solana contract address
getSolanaContractData(contractAddress)
.then((contractData) => {
console.log(contractData);
})
.catch((error) => {
console.error('Error:', error);
});在Solana , getAccountInfo 會擷取指定帳戶的相關資訊,而帳戶資訊物件的`data`欄位則包含與合約相關的位元組碼或資料。請注意Solana 儲存各種類型的資料,而不僅限於位元組碼,因此根據您的具體使用情境,`data`欄位的內容可能需要進一步處理。
請根據您的使用情境及所使用的函式庫,對範例進行調整。Solana設計與術語與Ethereum 不同,因此您處理合約及其資料的方式也會有些許差異。
Ethereum eth_call 用於呼叫合約函式或擷取合約狀態,而無需在Ethereum 實際進行交易。對應的Solana 方法則是 simulateTransaction,該方法可讓您模擬一筆交易,並取得在Solana 執行程式(智慧合約)的結果。
以下是您如何使用Solana simulateTransaction 方法,並與Ethereumeth_call的比較:
Ethereum eth_call:
// Using web3.js for Ethereum
const Web3 = require('web3');
const web3 = new Web3('QUICKNODE_URL'); // Replace with the quicknode url
async function ethCall(contractAddress, data) {
const result = await web3.eth.call({
to: contractAddress,
data: data,
});
return result;
}
const contractAddress = '0x...'; // Replace with the Ethereum contract address
const inputData = '0x...'; // Replace with the input data for the function call
ethCall(contractAddress, inputData)
.then((result) => {
console.log(result);
})
.catch((error) => {
console.error('Error:', error);
});Solana simulateTransaction:
const web3 = require('@solana/web3.js');
async function simulateSolanaTransaction(sender, programId, data) {
const solanaRpcUrl = 'QUICKNODE_URL'; // Replace with the quicknode url
const connection = new web3.Connection(solanaRpcUrl, 'confirmed');
const instruction = new web3.TransactionInstruction({
keys: [{ pubkey: sender, isSigner: true, isWritable: true }],
programId,
data,
});
const simulatedTransactionResponse = await connection.simulateTransaction(
new web3.Transaction().add(instruction)
);
const simulatedResult = simulatedTransactionResponse.value;
return simulatedResult;
}
const senderPublicKey = new web3.PublicKey('...'); // Replace with the sender's public key
const programId = new web3.PublicKey('...'); // Replace with the Solana program ID
const inputData = Buffer.from('...'); // Replace with the input data for the program
simulateSolanaTransaction(senderPublicKey, programId, inputData)
.then((simulatedResult) => {
console.log(simulatedResult);
})
.catch((error) => {
console.error('Error:', error);
});在Solana , `simulateTransaction` 可讓您透過交易指令模擬執行程式(智慧合約)。請注意Solana 交易設定方式Ethereum不同,因此您需要據此調整程式碼。相較於Ethereum 的Solana架構與設計可能需要額外的考量與調整。
Ethereum eth_getBalance 用於查詢特定Ethereum 餘額。對應的Solana 方法則是 getBalance,可讓您查詢特定Solana 地址的餘額。
以下是您如何使用Solana getBalance 方法,並與Ethereumeth_getBalance進行比較:
Ethereum eth_getBalance:
// Using web3.js for Ethereum
const Web3 = require('web3');
const web3 = new Web3('QUICKNODE_URL'); // Replace with the quicknode url
async function getEthBalance(address) {
const balance = await web3.eth.getBalance(address);
return balance;
}
const ethAddress = '0x...'; // Replace with the Ethereum address
getEthBalance(ethAddress)
.then((balance) => {
console.log(balance);
})
.catch((error) => {
console.error('Error:', error);
});Solana getBalance:
const web3 = require('@solana/web3.js');
async function getSolanaBalance(address) {
const solanaRpcUrl = 'QUICKNODE_URL'; // Replace with the quicknode url
const connection = new web3.Connection(solanaRpcUrl, 'confirmed');
const publicKey = new web3.PublicKey(address);
const balance = await connection.getBalance(publicKey);
return balance;
}
const solanaAddress = '...'; // Replace with the Solana wallet address
getSolanaBalance(solanaAddress)
.then((balance) => {
console.log(balance);
})
.catch((error) => {
console.error('Error:', error);
});這兩種情況下,您都是在查詢特定地址的餘額,但請注意,Ethereum Solana的地址格式及網路相關細節會有所不同。Solana Ed25519 公鑰格式,而Ethereum 十六進位格式。
請將佔位符替換為實際地址,並根據您所使用的函式庫和工具調整程式碼。Solana Ethereum 架構與設計Ethereum ,因此程式碼和做法在某些方面可能會有所差異。
Solana 的架構與設計與Ethereum大不相同,雖然Ethereum debug_traceBlockByHash 方法來追蹤和除錯區塊,但Solana RPC 方法和工具可能沒有具備同等詳細程度的直接對應功能。
在Ethereum中, `debug_traceBlockByHash` 可對區塊內的執行過程及狀態變化進行詳細追蹤與分析。另一方面Solana 則主要著重於高吞吐量與效能,這可能導致其除錯與追蹤的實作方式有所不同。
Solana RPC 方法並未提供與Ethereum同等詳細程度的區塊追蹤功能 debug_traceBlockByHash。Solana 的設計優先考量速度與可擴展性,且其著重於平行處理的特性,可能使得追蹤區塊中的每個細節變得較不切實際。
不過,若要進行除錯和追蹤,以下這些Solana 方法可能會對您有所幫助:
交易瀏覽器: Solana 在其區塊瀏覽器網站(例如Solana )Solana 交易瀏覽器。雖然其詳細程度可能不如Ethereum區塊追蹤功能,但它能協助您視覺化區塊內的交易及程式執行狀況。
Solana 與日誌記錄: Solana 介面(CLI)提供用於與Solana 互動的指令。雖然它可能無法提供Ethereum方法相同的追蹤功能,但確實提供了用於監控交易和程式日誌的工具。
程式日誌: Solana 可產生日誌,並可透過 getLogs RPC 方法擷取這些日誌。這些日誌能提供有關Solana 執行的相關資訊。
開發者社群:隨著Solana 生態系統不斷演進,可能會出現用於除錯和追蹤的新工具與技術。建議您關注Solana官方管道及開發者社群討論,以掌握最新資訊與最佳實務。
在Solana 中,跨程式呼叫的概念類似於Ethereum中的內部交易,但在運作方式及回傳的資料方面仍存在一些差異。
在Ethereum,當您執行追蹤呼叫以分析內部交易時(使用 debug_traceTransaction RPC 方法)來分析內部交易時,你可以看到交易內部發生的呼叫與互動序列,包括合約呼叫及其影響。
在Solana 中,跨程式呼叫是指從Solana 內部呼叫其他Solana (智慧合約)。Solana 以 Rust 語言編寫,並編譯成一種稱為 BPF(Berkeley Packet Filter)字節碼的自訂字節碼,該字節碼會在Solana 區塊鏈上執行。當一個Solana 呼叫另一個Solana ,即為跨程式呼叫。
然而,您可從Solana 的跨程式呼叫中擷取的資訊與資料Solana 主要Solana 於Solana 之間的互動,其詳細程度可能不如Ethereum內部交易那般詳盡。Solana原生交易結構與程式設計模型是為追求高效能與並行處理而設計,這可能與Ethereum智慧合約互動機制並不完全一致。
若您正在尋找Solana Ethereum追蹤功能完全相當的機制,請務必注意:Solana架構與設計可能無法提供完全相同細度的資訊。Solana 更著重於實現高吞吐量與效率,而Ethereum著重於提供合約互動的全面性視圖。
若要探索Solana 中的跨程式呼叫,您可以參閱Solana 了解如何在Solana 使用 `invoke`方法,以及可用於與Solana 互動並擷取其執行資訊的 RPC 方法。
若想進一步了解如何使用Solana 進行開發,請參閱以下資源:
Quicknode 成立於 2017 年,為開發者與企業Quicknode 機構級的區塊鏈基礎設施。憑藉 99.99% 的正常運行時間以及對 80 多條區塊鏈的支持,各團隊得以在無需妥協的情況下,開發並擴展鏈上應用程式。
將最新的工程見解、產品更新及 Web3 新聞直接送至您的收件匣。
已通過 SOC 2 Type II 認證 · ISO 27001