2020-03-20 15:20:48 -07:00
|
|
|
use solana_sdk::pubkey::Pubkey;
|
2019-11-25 15:11:55 -08:00
|
|
|
|
|
|
|
#[derive(Default)]
|
|
|
|
pub struct AddressGenerator {
|
|
|
|
base_pubkey: Pubkey,
|
2019-12-17 15:14:59 -08:00
|
|
|
program_id: Pubkey,
|
2019-11-25 15:11:55 -08:00
|
|
|
nth: usize,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl AddressGenerator {
|
2020-01-15 10:50:37 -08:00
|
|
|
pub fn new(base_pubkey: &Pubkey, program_id: &Pubkey) -> Self {
|
2019-11-25 15:11:55 -08:00
|
|
|
Self {
|
|
|
|
base_pubkey: *base_pubkey,
|
2019-12-17 15:14:59 -08:00
|
|
|
program_id: *program_id,
|
2019-11-25 15:11:55 -08:00
|
|
|
nth: 0,
|
|
|
|
}
|
|
|
|
}
|
2019-12-17 15:14:59 -08:00
|
|
|
|
2019-11-25 15:11:55 -08:00
|
|
|
pub fn nth(&self, nth: usize) -> Pubkey {
|
2020-03-20 15:20:48 -07:00
|
|
|
Pubkey::create_with_seed(&self.base_pubkey, &format!("{}", nth), &self.program_id).unwrap()
|
2019-11-25 15:11:55 -08:00
|
|
|
}
|
2019-12-17 15:14:59 -08:00
|
|
|
|
2019-12-02 13:47:20 -08:00
|
|
|
#[allow(clippy::should_implement_trait)]
|
2019-11-25 15:11:55 -08:00
|
|
|
pub fn next(&mut self) -> Pubkey {
|
|
|
|
let nth = self.nth;
|
|
|
|
self.nth += 1;
|
|
|
|
self.nth(nth)
|
|
|
|
}
|
|
|
|
}
|