1

The Solana docs states that Any program can read or credit another account.
Ref: https://docs.solana.com/developing/intro/programs.
Please explain this line. Does that mean I can extract all past transaction logs of a program in another program in Solana? If so, or Is there any way to read another program's data on-chain, Give an example(anchor-lang preferred) or github link where It was implemented.

1 Answer 1

1

Going back to the basics, accounts are like files in Solana, and can store any data. A program's code is stored in accounts, and other accounts store the program's data. For example, the token program has an account for its code, an account for each mint, and an account for each token account (the naming stinks there, sorry!).

When a program executes an instruction, it receives instruction data in the form of bytes, and it receives accounts in the form of AccountInfos https://github.com/solana-labs/solana/blob/4547549a732a6fd2f13b9743597b0b32199c7204/sdk/program/src/account_info.rs#L19

You'll notice that AccountInfo has a field called data, which is just a slice of bytes. No matter what program that account belongs to, any program can read those bytes and interpret them however they wish. For example, if it's a token mint, a program can interpret those bytes as a mint by calling spl_token::state::Mint::unpack(account_info.data.borrow()).

Here's a simple example program that does exactly that https://github.com/solana-labs/solana-program-library/blob/54ed0512cb5a9d8cb67eeff9077a1bc09e199666/examples/rust/transfer-tokens/src/processor.rs#L44

Note that a program's data does not include past transaction logs. In fact, those are not exposed on-chain by the runtime, so no program can see past transactions or logs from any other program.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.