Skip to content

Commit 5d98977

Browse files
committed
Make register_transceiver init_if_needed to double as reenable
1 parent 0b3dde2 commit 5d98977

File tree

2 files changed

+25
-18
lines changed

2 files changed

+25
-18
lines changed

solana/programs/example-native-token-transfers/src/error.rs

+2
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ pub enum NTTError {
6363
InvalidMultisig,
6464
#[msg("ThresholdTooHigh")]
6565
ThresholdTooHigh,
66+
#[msg("InvalidTransceiverProgram")]
67+
InvalidTransceiverProgram,
6668
}
6769

6870
impl From<ScalingError> for NTTError {

solana/programs/example-native-token-transfers/src/instructions/admin.rs

+23-18
Original file line numberDiff line numberDiff line change
@@ -437,13 +437,16 @@ pub struct RegisterTransceiver<'info> {
437437
#[account(mut)]
438438
pub payer: Signer<'info>,
439439

440-
#[account(executable)]
440+
#[account(
441+
executable,
442+
constraint = transceiver.key() != Pubkey::default() @ NTTError::InvalidTransceiverProgram
443+
)]
441444
/// CHECK: transceiver is meant to be a transceiver program. Arguably a `Program` constraint could be
442445
/// used here that wraps the Transceiver account type.
443446
pub transceiver: UncheckedAccount<'info>,
444447

445448
#[account(
446-
init,
449+
init_if_needed,
447450
space = 8 + RegisteredTransceiver::INIT_SPACE,
448451
payer = payer,
449452
seeds = [RegisteredTransceiver::SEED_PREFIX, transceiver.key().as_ref()],
@@ -455,17 +458,23 @@ pub struct RegisterTransceiver<'info> {
455458
}
456459

457460
pub fn register_transceiver(ctx: Context<RegisterTransceiver>) -> Result<()> {
458-
let id = ctx.accounts.config.next_transceiver_id;
459-
ctx.accounts.config.next_transceiver_id += 1;
460-
ctx.accounts
461-
.registered_transceiver
462-
.set_inner(RegisteredTransceiver {
463-
bump: ctx.bumps.registered_transceiver,
464-
id,
465-
transceiver_address: ctx.accounts.transceiver.key(),
466-
});
461+
// initialize registered transceiver with new id on init
462+
if ctx.accounts.registered_transceiver.transceiver_address == Pubkey::default() {
463+
let id = ctx.accounts.config.next_transceiver_id;
464+
ctx.accounts.config.next_transceiver_id += 1;
465+
ctx.accounts
466+
.registered_transceiver
467+
.set_inner(RegisteredTransceiver {
468+
bump: ctx.bumps.registered_transceiver,
469+
id,
470+
transceiver_address: ctx.accounts.transceiver.key(),
471+
});
472+
}
467473

468-
ctx.accounts.config.enabled_transceivers.set(id, true)?;
474+
ctx.accounts
475+
.config
476+
.enabled_transceivers
477+
.set(ctx.accounts.registered_transceiver.id, true)?;
469478
Ok(())
470479
}
471480

@@ -486,12 +495,9 @@ pub struct DeregisterTransceiver<'info> {
486495
pub transceiver: UncheckedAccount<'info>,
487496

488497
#[account(
489-
mut,
490498
seeds = [RegisteredTransceiver::SEED_PREFIX, transceiver.key().as_ref()],
491499
bump,
492500
constraint = config.enabled_transceivers.get(registered_transceiver.id)? @ NTTError::DisabledTransceiver,
493-
// TODO: ideally, the rent should be reclaimed by original fee payer and not the owner
494-
close = owner,
495501
)]
496502
pub registered_transceiver: Account<'info, RegisteredTransceiver>,
497503
}
@@ -503,8 +509,7 @@ pub fn deregister_transceiver(ctx: Context<DeregisterTransceiver>) -> Result<()>
503509
.set(ctx.accounts.registered_transceiver.id, false)?;
504510

505511
// decrement threshold if too high
506-
let num_enabled_transceivers = u8::try_from(ctx.accounts.config.enabled_transceivers.len())
507-
.expect("Bitmap length must not exceed the bounds of u8");
512+
let num_enabled_transceivers = ctx.accounts.config.enabled_transceivers.len();
508513
if num_enabled_transceivers < ctx.accounts.config.threshold {
509514
// threshold should be at least 1
510515
ctx.accounts.config.threshold = num_enabled_transceivers.max(1);
@@ -597,7 +602,7 @@ pub struct SetThreshold<'info> {
597602
#[account(
598603
mut,
599604
has_one = owner,
600-
constraint = threshold <= u8::try_from(config.enabled_transceivers.len()).unwrap() @ NTTError::ThresholdTooHigh
605+
constraint = threshold <= config.enabled_transceivers.len() @ NTTError::ThresholdTooHigh
601606
)]
602607
pub config: Account<'info, Config>,
603608
}

0 commit comments

Comments
 (0)