@@ -437,13 +437,16 @@ pub struct RegisterTransceiver<'info> {
437
437
#[ account( mut ) ]
438
438
pub payer : Signer < ' info > ,
439
439
440
- #[ account( executable) ]
440
+ #[ account(
441
+ executable,
442
+ constraint = transceiver. key( ) != Pubkey :: default ( ) @ NTTError :: InvalidTransceiverProgram
443
+ ) ]
441
444
/// CHECK: transceiver is meant to be a transceiver program. Arguably a `Program` constraint could be
442
445
/// used here that wraps the Transceiver account type.
443
446
pub transceiver : UncheckedAccount < ' info > ,
444
447
445
448
#[ account(
446
- init ,
449
+ init_if_needed ,
447
450
space = 8 + RegisteredTransceiver :: INIT_SPACE ,
448
451
payer = payer,
449
452
seeds = [ RegisteredTransceiver :: SEED_PREFIX , transceiver. key( ) . as_ref( ) ] ,
@@ -455,17 +458,23 @@ pub struct RegisterTransceiver<'info> {
455
458
}
456
459
457
460
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
+ }
467
473
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 ) ?;
469
478
Ok ( ( ) )
470
479
}
471
480
@@ -486,12 +495,9 @@ pub struct DeregisterTransceiver<'info> {
486
495
pub transceiver : UncheckedAccount < ' info > ,
487
496
488
497
#[ account(
489
- mut ,
490
498
seeds = [ RegisteredTransceiver :: SEED_PREFIX , transceiver. key( ) . as_ref( ) ] ,
491
499
bump,
492
500
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,
495
501
) ]
496
502
pub registered_transceiver : Account < ' info , RegisteredTransceiver > ,
497
503
}
@@ -503,8 +509,7 @@ pub fn deregister_transceiver(ctx: Context<DeregisterTransceiver>) -> Result<()>
503
509
. set ( ctx. accounts . registered_transceiver . id , false ) ?;
504
510
505
511
// 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 ( ) ;
508
513
if num_enabled_transceivers < ctx. accounts . config . threshold {
509
514
// threshold should be at least 1
510
515
ctx. accounts . config . threshold = num_enabled_transceivers. max ( 1 ) ;
@@ -597,7 +602,7 @@ pub struct SetThreshold<'info> {
597
602
#[ account(
598
603
mut ,
599
604
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
601
606
) ]
602
607
pub config : Account < ' info , Config > ,
603
608
}
0 commit comments