thedatadudech / lottery

Team project of the Encode Solana Bootcamp
3 stars 0 forks source link

Error while adapting to new generate_random_numbers() using Solana clock #5

Open thedatadudech opened 2 months ago

thedatadudech commented 2 months ago

pub fn generate_random_numbers() -> Result<[u8; 6]> { // Fetch the current clock sysvar; return an error if it fails let clock = Clock::get()?;

// Combine slot and timestamp to form a seed
let seed = clock.slot.to_le_bytes().to_vec().into_iter()
    .chain(clock.unix_timestamp.to_le_bytes().to_vec())
    .collect::<Vec<u8>>();

// Hash the seed to generate a "random" byte array
let hash_seed: Hash = hash(&seed);

// Use a set to keep track of unique numbers
let mut unique_numbers: HashSet<u8> = HashSet::new();

// Convert the hash into a list of unique numbers in the range 1-60
let mut i = 0;
while unique_numbers.len() < 6 {
    let number = (hash_seed.as_ref()[i] % 60) + 1;  // Range 1-60
    unique_numbers.insert(number);
    i = (i + 1) % hash_seed.as_ref().len();  // Wrap around hash bytes if necessary
}

// Convert the unique numbers set into an array
let mut chosen_numbers: [u8; 6] = [0; 6];
for (i, &num) in unique_numbers.iter().enumerate() {
    chosen_numbers[i] = num;
}

validate_numbers(&chosen_numbers)?;

Ok(chosen_numbers)

}

Error message Error: Deploying program failed: RPC response error -32002: Transaction simulation failed: Error processing Instruction 0: account data too small for instruction [3 log messages] There was a problem deploying: Output { status: ExitStatus(unix_wait_status(256)), stdout: "", stderr: "" }.

found in commit 208f4e486ac9b9b61ae5052bce086b376f6e5ed5