Solan: What is the difference between .rpc ()
and .instruction () in the
anchor test?
As a Sorana developer, you are likely to know the “Anchor” library, which provides a convenient interface to interact with the Sorana Blockchain. Two common functions that can be used to send instructions are .rpc ()
(remote invitation) and .instruction ()
. Although they may seem the same, there is an important difference between them.
. Rpc ()
The .rpc () function is designed to send requests to Sorana -blockchain in a certain way. When you call
.rpc (), it creates a request that can be made in the block chain. The request includes metadata, such as its name, signature and arguments. This metadata allows the request to be adapted to Blockchain with existing instructions.
Here's an example of how to use.rpc ():
Javascript
Const Anchor = Require (‘@anchor-protocol/anchors’);
// Create a new account
Const Accountid = anchor.web3.eth.ccounts._new ();
// Specify the instruction
Const instructions = {
Name: ‘my instruction’,
Args: [1, 2, 3],
};
// Send your request .rpc ()
Anchor.rpc (). Help ({
Address: Accountid,
Signature: ‘Your_Signature_here’,
Data: Help,
}). then ((answer) => console.log (answer);
`
. Help ()
.Instruction ()
function is designed to send instructions directly from the program or from the script. When you call .instruction ()
, it creates a guide that can be performed in a block chain without extra metadata.
Here is an example of how to use .instruction ()
:
`Javascript
Const Anchor = Require (‘@anchor-protocol/anchors’);
// Create a new account
Const Accountid = anchor.web3.eth.ccounts._new ();
// Specify the manuscript
Const script = {
Code: ‘Function () {self.addaccount (Accountid); } ‘,
};
// Submit instruction .instruction ()
Anchor.rpc (). Help (script) .en ((answer) => console.log (answer));
`
The most important differences
So what is the key difference .rpc ()
and .instruction ()?
- Metadata
:
.rpc ()
Requires metadata from the instruction, while.instruction ()
No.
2
If you send the instruction directly to .rpc ()
, it is likely that the problem will be related to how you create and complete the instruction. Make sure you give the correct metadata, such as the “address” of the account and any additional criteria.
Here is an example of how to create a new instruction .instruction ()
and run it:
`Javascript
Const Anchor = Require (‘@anchor-protocol/anchors’);
// Create a new account
Const Accountid = anchor.web3.eth.ccounts._new ();
// Specify the manuscript
Const script = {
Code: ‘Function () {self.addaccount (Accountid); } ‘,
};
// Submit instruction .instruction ()
Anchor.rpc (). Help (script) .en ((answer) => console.log (answer));
`
In summary, `. Make sure you are using the right function and give the correct metadata when creating and performing instructions.