I have an account with definition like this. How do i get result from implemented method "can_buy"?

pub struct Data {
    pub time_start: i64,
    pub time_end: i64,
    pub total_sale: u64,

impl Data {
    pub fn can_buy(&self) {
        let now: i64 = Clock::get().unwrap().unix_timestamp;
        return self.time_start <= now <= self.time_end

1 Answer 1


You can only call can_buy from an instance of Data. This means you have to fetch a Data account, deserialize its data, and then call the function. This can be done on or off-chain.

Solana does not have a strong concept of read-only contract calls, like on Ethereum. You can create an instruction that writes the result into the transaction's return data, and then read that from a simulated transaction. Here's the program code:

mod my_program {
    pub fn get(ctx: Context<Get>) -> Result<()> {
        let my_account = ctx.accounts.my_account;
        solana_program::program::set_return_data(&[my_account.can_buy() as u8]);

pub struct Get<'info> {
    pub my_account: Account<'info, Data>,

After that, you'll have to read the returnData field from the simulation response: https://docs.solana.com/developing/clients/jsonrpc-api#simulatetransaction

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.