Skip to content

Commit ad8e4a3

Browse files
a5-picklekcsongor
authored andcommitted
solana: fail on wrong mode earlier; one less timestamp call
1 parent 79eefa0 commit ad8e4a3

File tree

3 files changed

+57
-41
lines changed

3 files changed

+57
-41
lines changed

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

+9
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,12 @@ pub enum Mode {
6868
Burning,
6969
Locking,
7070
}
71+
72+
impl std::fmt::Display for Mode {
73+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
74+
match self {
75+
Mode::Burning => write!(f, "Burning"),
76+
Mode::Locking => write!(f, "Locking"),
77+
}
78+
}
79+
}

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

+44-38
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,12 @@ pub struct TransferBurn<'info> {
9595

9696
// TODO: fees for relaying?
9797
pub fn transfer_burn(ctx: Context<TransferBurn>, args: TransferArgs) -> Result<()> {
98+
require_eq!(
99+
ctx.accounts.common.config.mode,
100+
Mode::Burning,
101+
NTTError::InvalidMode
102+
);
103+
98104
let accs = ctx.accounts;
99105
let TransferArgs {
100106
amount,
@@ -106,24 +112,21 @@ pub fn transfer_burn(ctx: Context<TransferBurn>, args: TransferArgs) -> Result<(
106112
// TODO: should we revert if we have dust?
107113
let amount = NormalizedAmount::remove_dust(amount, accs.common.mint.decimals);
108114

109-
match accs.common.config.mode {
110-
Mode::Burning => token_interface::burn(
111-
CpiContext::new_with_signer(
112-
accs.common.token_program.to_account_info(),
113-
token_interface::Burn {
114-
mint: accs.common.mint.to_account_info(),
115-
from: accs.common.from.to_account_info(),
116-
authority: accs.common.token_authority.to_account_info(),
117-
},
118-
&[&[
119-
crate::TOKEN_AUTHORITY_SEED,
120-
&[ctx.bumps.common.token_authority],
121-
]],
122-
),
123-
amount,
124-
)?,
125-
Mode::Locking => return Err(NTTError::InvalidMode.into()),
126-
}
115+
token_interface::burn(
116+
CpiContext::new_with_signer(
117+
accs.common.token_program.to_account_info(),
118+
token_interface::Burn {
119+
mint: accs.common.mint.to_account_info(),
120+
from: accs.common.from.to_account_info(),
121+
authority: accs.common.token_authority.to_account_info(),
122+
},
123+
&[&[
124+
crate::TOKEN_AUTHORITY_SEED,
125+
&[ctx.bumps.common.token_authority],
126+
]],
127+
),
128+
amount,
129+
)?;
127130

128131
insert_into_outbox(
129132
&mut accs.common,
@@ -162,6 +165,12 @@ pub struct TransferLock<'info> {
162165
// TODO: fees for relaying?
163166
// TODO: factor out common bits
164167
pub fn transfer_lock(ctx: Context<TransferLock>, args: TransferArgs) -> Result<()> {
168+
require_eq!(
169+
ctx.accounts.common.config.mode,
170+
Mode::Locking,
171+
NTTError::InvalidMode
172+
);
173+
165174
let accs = ctx.accounts;
166175
let TransferArgs {
167176
amount,
@@ -173,26 +182,23 @@ pub fn transfer_lock(ctx: Context<TransferLock>, args: TransferArgs) -> Result<(
173182
// TODO: should we revert if we have dust?
174183
let amount = NormalizedAmount::remove_dust(amount, accs.common.mint.decimals);
175184

176-
match accs.common.config.mode {
177-
Mode::Burning => return Err(NTTError::InvalidMode.into()),
178-
Mode::Locking => token_interface::transfer_checked(
179-
CpiContext::new_with_signer(
180-
accs.common.token_program.to_account_info(),
181-
token_interface::TransferChecked {
182-
from: accs.common.from.to_account_info(),
183-
to: accs.custody.to_account_info(),
184-
authority: accs.common.token_authority.to_account_info(),
185-
mint: accs.common.mint.to_account_info(),
186-
},
187-
&[&[
188-
crate::TOKEN_AUTHORITY_SEED,
189-
&[ctx.bumps.common.token_authority],
190-
]],
191-
),
192-
amount,
193-
accs.common.mint.decimals,
194-
)?,
195-
}
185+
token_interface::transfer_checked(
186+
CpiContext::new_with_signer(
187+
accs.common.token_program.to_account_info(),
188+
token_interface::TransferChecked {
189+
from: accs.common.from.to_account_info(),
190+
to: accs.custody.to_account_info(),
191+
authority: accs.common.token_authority.to_account_info(),
192+
mint: accs.common.mint.to_account_info(),
193+
},
194+
&[&[
195+
crate::TOKEN_AUTHORITY_SEED,
196+
&[ctx.bumps.common.token_authority],
197+
]],
198+
),
199+
amount,
200+
accs.common.mint.decimals,
201+
)?;
196202

197203
insert_into_outbox(
198204
&mut accs.common,

solana/programs/example-native-token-transfers/src/queue/rate_limit.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -94,13 +94,14 @@ impl RateLimitState {
9494
/// Refills the capacity by the given amount.
9595
/// This is used to replenish the capacity via backflows.
9696
pub fn refill(&mut self, now: UnixTimestamp, amount: u64) {
97-
self.capacity_at_last_tx = self.capacity().saturating_add(amount).min(self.limit);
97+
self.capacity_at_last_tx = self.capacity_at(now).saturating_add(amount).min(self.limit);
9898
self.last_tx_timestamp = now;
9999
}
100100

101101
pub fn set_limit(&mut self, limit: u64) {
102102
let old_limit = self.limit;
103-
let current_capacity = self.capacity();
103+
let now = current_timestamp();
104+
let current_capacity = self.capacity_at(now);
104105

105106
self.limit = limit;
106107

@@ -115,7 +116,7 @@ impl RateLimitState {
115116
};
116117

117118
self.capacity_at_last_tx = new_capacity.min(limit);
118-
self.last_tx_timestamp = current_timestamp();
119+
self.last_tx_timestamp = now;
119120
}
120121
}
121122

0 commit comments

Comments
 (0)