Skip to content
This repository was archived by the owner on Mar 11, 2025. It is now read-only.

Commit 78e29e9

Browse files
authored
token-swap: Changed Wrap SwapCurve's calculator field into Arc instead of Box (#2860)
* box changed to arc * Imported Arc in all files * warnings removed * Updated fuzz
1 parent eb04855 commit 78e29e9

File tree

6 files changed

+55
-51
lines changed

6 files changed

+55
-51
lines changed

token-swap/program/fuzz/src/instructions.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::sync::Arc;
2+
13
use {
24
arbitrary::Arbitrary,
35
honggfuzz::fuzz,
@@ -459,12 +461,12 @@ fn get_swap_curve(curve_type: CurveType) -> SwapCurve {
459461
SwapCurve {
460462
curve_type,
461463
calculator: match curve_type {
462-
CurveType::ConstantProduct => Box::new(ConstantProductCurve),
463-
CurveType::ConstantPrice => Box::new(ConstantPriceCurve {
464+
CurveType::ConstantProduct => Arc::new(ConstantProductCurve),
465+
CurveType::ConstantPrice => Arc::new(ConstantPriceCurve {
464466
token_b_price: 10_000_000,
465467
}),
466-
CurveType::Stable => Box::new(StableCurve { amp: 100 }),
467-
CurveType::Offset => Box::new(OffsetCurve {
468+
CurveType::Stable => Arc::new(StableCurve { amp: 100 }),
469+
CurveType::Offset => Arc::new(OffsetCurve {
468470
token_b_offset: 100_000_000_000,
469471
}),
470472
},

token-swap/program/src/constraints.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ use crate::{
77
},
88
error::SwapError,
99
};
10-
1110
use solana_program::program_error::ProgramError;
1211

1312
#[cfg(feature = "production")]
@@ -17,7 +16,7 @@ use std::env;
1716
/// may be used by multiple frontends, to ensure that proper fees are being
1817
/// assessed.
1918
/// Since this struct needs to be created at compile-time, we only have access
20-
/// to const functions and constructors. Since SwapCurve contains a Box, it
19+
/// to const functions and constructors. Since SwapCurve contains a Arc, it
2120
/// cannot be used, so we have to split the curves based on their types.
2221
pub struct SwapConstraints<'a> {
2322
/// Owner of the program
@@ -100,8 +99,8 @@ pub const SWAP_CONSTRAINTS: Option<SwapConstraints> = {
10099
#[cfg(test)]
101100
mod tests {
102101
use super::*;
103-
104102
use crate::curve::{base::CurveType, constant_product::ConstantProductCurve};
103+
use std::sync::Arc;
105104

106105
#[test]
107106
fn validate_fees() {
@@ -128,7 +127,7 @@ mod tests {
128127
let calculator = ConstantProductCurve {};
129128
let swap_curve = SwapCurve {
130129
curve_type,
131-
calculator: Box::new(calculator.clone()),
130+
calculator: Arc::new(calculator.clone()),
132131
};
133132
let constraints = SwapConstraints {
134133
owner_key,
@@ -187,7 +186,7 @@ mod tests {
187186

188187
let swap_curve = SwapCurve {
189188
curve_type: CurveType::ConstantPrice,
190-
calculator: Box::new(calculator),
189+
calculator: Arc::new(calculator),
191190
};
192191
assert_eq!(
193192
Err(SwapError::UnsupportedCurveType.into()),

token-swap/program/src/curve/base.rs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use crate::curve::{
1616
use arrayref::{array_mut_ref, array_ref, array_refs, mut_array_refs};
1717
use std::convert::{TryFrom, TryInto};
1818
use std::fmt::Debug;
19+
use std::sync::Arc;
1920

2021
#[cfg(feature = "fuzz")]
2122
use arbitrary::Arbitrary;
@@ -61,7 +62,7 @@ pub struct SwapCurve {
6162
pub curve_type: CurveType,
6263
/// The actual calculator, represented as a trait object to allow for many
6364
/// different types of curves
64-
pub calculator: Box<dyn CurveCalculator>,
65+
pub calculator: Arc<dyn CurveCalculator>,
6566
}
6667

6768
impl SwapCurve {
@@ -162,14 +163,14 @@ impl SwapCurve {
162163
}
163164

164165
/// Default implementation for SwapCurve cannot be derived because of
165-
/// the contained Box.
166+
/// the contained Arc.
166167
impl Default for SwapCurve {
167168
fn default() -> Self {
168169
let curve_type: CurveType = Default::default();
169170
let calculator: ConstantProductCurve = Default::default();
170171
Self {
171172
curve_type,
172-
calculator: Box::new(calculator),
173+
calculator: Arc::new(calculator),
173174
}
174175
}
175176
}
@@ -216,13 +217,13 @@ impl Pack for SwapCurve {
216217
curve_type,
217218
calculator: match curve_type {
218219
CurveType::ConstantProduct => {
219-
Box::new(ConstantProductCurve::unpack_from_slice(calculator)?)
220+
Arc::new(ConstantProductCurve::unpack_from_slice(calculator)?)
220221
}
221222
CurveType::ConstantPrice => {
222-
Box::new(ConstantPriceCurve::unpack_from_slice(calculator)?)
223+
Arc::new(ConstantPriceCurve::unpack_from_slice(calculator)?)
223224
}
224-
CurveType::Stable => Box::new(StableCurve::unpack_from_slice(calculator)?),
225-
CurveType::Offset => Box::new(OffsetCurve::unpack_from_slice(calculator)?),
225+
CurveType::Stable => Arc::new(StableCurve::unpack_from_slice(calculator)?),
226+
CurveType::Offset => Arc::new(OffsetCurve::unpack_from_slice(calculator)?),
226227
},
227228
})
228229
}
@@ -268,7 +269,7 @@ mod tests {
268269
let curve_type = CurveType::ConstantProduct;
269270
let swap_curve = SwapCurve {
270271
curve_type,
271-
calculator: Box::new(curve),
272+
calculator: Arc::new(curve),
272273
};
273274

274275
let mut packed = [0u8; SwapCurve::LEN];
@@ -310,7 +311,7 @@ mod tests {
310311
let curve = ConstantProductCurve {};
311312
let swap_curve = SwapCurve {
312313
curve_type: CurveType::ConstantProduct,
313-
calculator: Box::new(curve),
314+
calculator: Arc::new(curve),
314315
};
315316
let result = swap_curve
316317
.swap(
@@ -355,7 +356,7 @@ mod tests {
355356
let curve = ConstantProductCurve {};
356357
let swap_curve = SwapCurve {
357358
curve_type: CurveType::ConstantProduct,
358-
calculator: Box::new(curve),
359+
calculator: Arc::new(curve),
359360
};
360361
let result = swap_curve
361362
.swap(
@@ -382,7 +383,7 @@ mod tests {
382383
let fees = Fees::default();
383384
let swap_curve = SwapCurve {
384385
curve_type: CurveType::ConstantProduct,
385-
calculator: Box::new(curve),
386+
calculator: Arc::new(curve),
386387
};
387388
let result = swap_curve
388389
.swap(

token-swap/program/src/instruction.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -566,8 +566,8 @@ pub fn unpack<T>(input: &[u8]) -> Result<&T, ProgramError> {
566566
#[cfg(test)]
567567
mod tests {
568568
use super::*;
569-
570569
use crate::curve::{base::CurveType, stable::StableCurve};
570+
use std::sync::Arc;
571571

572572
#[test]
573573
fn pack_intialize() {
@@ -591,7 +591,7 @@ mod tests {
591591
};
592592
let amp: u64 = 1;
593593
let curve_type = CurveType::Stable;
594-
let calculator = Box::new(StableCurve { amp });
594+
let calculator = Arc::new(StableCurve { amp });
595595
let swap_curve = SwapCurve {
596596
curve_type,
597597
calculator,

0 commit comments

Comments
 (0)