Day 4 of RareSkills Solana Course.
- Configure Solana to run on localhost:
solana config set --url localhost
- Run the test validator node on another terminal:
solana-test-validator
- Run Solana logs on another terminal:
solana logs
- Build Anchor program:
anchor build
- Sync program_id with Anchor key:
anchor keys sync
- Run tests:
anchor test --skip-local-validator
- When stopping transactions with invalid parameters:
Ethereum
triggers arevert
- while
Solana
returns anerror
.
require!
macro can be used as an alternative toif statements
.- Solana programs should always return an
Ok(())
or anError
.- All functions in Solana have a return type of
Result<()>
.
- All functions in Solana have a return type of
- In Anchor, errors are an enum with the
#[error_code]
attribute.
-
What pattern do you notice with the Error number? What happens to the error codes if you change the order of the errors in the Enum MyError?
- The
Anchor error codes
start at6000
, and increments by1
from the first record in theMyError enum
. (6000
,6001
, ...)
- The
-
Use this code block which adds the new func and error to the existing code: Before you run this, what do you think the new error code will be?
- The new error code will be
6002
, since there are three records in theMyError enum
.
- The new error code will be
-
What happens if you put a msg! macro before the return error statements in a Solana program function? What happens if you replace
return err!
withOk(())
?- The
msg!
will not print when there's an error. - Replacing
return err!
withOk(())
will result in the transaction succeeding, printing the messageWill this print?"
.
- The