7

I am trying to update value of string. I can update if its type is u8, u32, u64, or i64 but cannot update if it's a string type. Any guesses what am I doing wrong?

Code snippet:

  1. Program Function
    pub fn set_uri(
        ctx: Context<ModifyLedger>,
        new_uri: String,
    ) -> Result<()> {
        let ledger_account = &mut ctx.accounts.ledger_account;
        ledger_account.uri = new_uri;    
        Ok(())
    }
  1. derive Account:
#[derive(Accounts)]
pub struct ModifyLedger<'info> {
    #[account(mut)]
    pub ledger_account: Account<'info, Ledger>,
    #[account(mut)]
    pub wallet: Signer<'info>,
}
  1. Account:
#[account]
pub struct Ledger {
    pub uri: String,
}
1
  • How many bytes of space have you allotted to the Ledger account?
    – 0xShuk
    Commented Oct 30, 2022 at 7:05

3 Answers 3

3

I increased the space of my account and redeployed the programs with new keys.

#[account(
  init,
  payer = payer,
  space = 82+36,
  seeds = [b"my_custom_seed"],
  bump)
 ]    
 pub ledger_account: Account<'info, Ledger>
3
  • Hi, iam also facing this same issue can u let me know how much max size of space I can provide at space value
    – Mojo
    Commented Nov 2, 2022 at 2:29
  • It depends as per you needs. If you don't know how much exact space is required then you can push it to a some higher value like if you have space = 82 you can do something like space = 1024 Commented Nov 2, 2022 at 10:32
  • Hi, in my current project requirement, I need to store some data of users who enters to my Dapp, Before I was only using one PDA account to store all the user details then I was getting serialized Error 3004 Then, I came up with a solution for storing all user details by creating an account for every user, where i can have a n number of accounts in a program
    – Mojo
    Commented Nov 3, 2022 at 2:17
1

Instead of this

#[account]
pub struct Ledger {
    pub uri: String,
}

use

// Implements an Accounts deserializer on the given struct.   
#[derive(Accounts)]
pub struct Ledger {
    pub uri: String,
}
0

Space was the issue for me as well. In my case, I solved it with the equivalent of the following

#[derive(Accounts)]
#[instruction(new_uri: String)]
pub struct ModifyLedger<'info> {
    #[account(
        mut,
        // 8 for discriminator, 4 for the string length, and uri.len() for the content of string
        space = 8 + 4 + new_uri.len() 
    )]
    pub ledger_account: Account<'info, Ledger>,
    
    #[account(mut)]
    pub wallet: Signer<'info>,
}

Essentially, I am computing the space required to store the ledger account struct, from the data passed in to the instruction. Remember to add 8 bytes at the start for anchor's discriminator.

#[account]
pub struct Ledger {
    pub uri: String,
}

Using the exact space required helps you be more efficient in terms of SOL usage.

References:

  1. Solana Quick Start Tutorial - Program Derived Address - Implement Create Instruction (open the Explanation accordion)

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.