Skip to content

Commit 65f4773

Browse files
authored
fix: zero fee in dex (#43)
* fix zero fee * fix lint * remove comment
1 parent a2f09ee commit 65f4773

File tree

8 files changed

+24
-30
lines changed

8 files changed

+24
-30
lines changed

crates/compiler/src/built_package.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,10 @@ impl BuiltPackage {
7777
new_config.architecture = None;
7878
new_config.generate_docs = false;
7979
new_config.generate_move_model = true;
80-
new_config.compiler_config.known_attributes = metadata::get_all_attribute_names().clone();
80+
new_config
81+
.compiler_config
82+
.known_attributes
83+
.clone_from(metadata::get_all_attribute_names());
8184

8285
let (mut package, model_opt) =
8386
new_config.compile_package_no_exit(&package_path, &mut stderr())?;

crates/compiler/src/test_package.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,10 @@ impl TestPackage {
2828
new_build_config.test_mode = true;
2929
new_build_config.generate_docs = false;
3030
new_build_config.generate_move_model = true;
31-
new_build_config.compiler_config.known_attributes =
32-
metadata::get_all_attribute_names().clone();
31+
new_build_config
32+
.compiler_config
33+
.known_attributes
34+
.clone_from(metadata::get_all_attribute_names());
3335

3436
configure_for_unit_test();
3537

crates/e2e-move-tests/src/lib.rs

-25
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,7 @@
11
pub mod harness;
22
pub mod test_utils;
33

4-
use anyhow::bail;
54
pub use harness::*;
6-
use move_package::package_hooks::PackageHooks;
7-
use move_package::source_package::parsed_manifest::CustomDepInfo;
8-
use move_symbol_pool::Symbol;
95

106
#[cfg(test)]
117
mod tests;
12-
13-
pub(crate) struct InitiaPackageHooks {}
14-
pub const UPGRADE_POLICY_CUSTOM_FIELD: &str = "upgrade_policy";
15-
16-
impl PackageHooks for InitiaPackageHooks {
17-
fn custom_package_info_fields(&self) -> Vec<String> {
18-
vec![UPGRADE_POLICY_CUSTOM_FIELD.to_string()]
19-
}
20-
21-
fn custom_dependency_key(&self) -> Option<String> {
22-
Some("initia".to_string())
23-
}
24-
25-
fn resolve_custom_dependency(
26-
&self,
27-
_dep_name: Symbol,
28-
_info: &CustomDepInfo,
29-
) -> anyhow::Result<()> {
30-
bail!("not used")
31-
}
32-
}

crates/e2e-move-tests/src/test_utils/mock_chain.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,7 @@ impl MockStakingAPI {
391391
metadata: AccountAddress,
392392
amount: u64,
393393
) -> anyhow::Result<u64> {
394-
match self.validators.get(&validator.to_vec()) {
394+
match self.validators.get(validator) {
395395
Some(ratios) => match ratios.get(&metadata) {
396396
Some((s, a)) => Ok(amount * s / a),
397397
None => Err(anyhow!("ratio not found")),
@@ -406,7 +406,7 @@ impl MockStakingAPI {
406406
metadata: AccountAddress,
407407
share: u64,
408408
) -> anyhow::Result<u64> {
409-
match self.validators.get(&validator.to_vec()) {
409+
match self.validators.get(validator) {
410410
Some(ratios) => match ratios.get(&metadata) {
411411
Some((s, a)) => Ok(share * a / s),
412412
None => Err(anyhow!("ratio not found")),

libmovevm/src/move_api/bytecode.rs

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ pub trait Bytecode {
3232

3333
fn address_identifier_at(&self, idx: AddressIdentifierIndex) -> &AccountAddress;
3434

35+
#[allow(dead_code)]
3536
fn find_entry_function(&self, name: &IdentStr) -> Option<MoveFunction>;
3637

3738
fn new_move_struct_field(&self, def: &FieldDefinition) -> MoveStructField {

libmovevm/src/storage.rs

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use anyhow::anyhow;
1111

1212
/// Access to the VM's backend storage, i.e. the chain
1313
pub trait Storage {
14+
#[allow(dead_code)]
1415
/// Returns Err on error.
1516
/// Returns Ok(None) when key does not exist.
1617
/// Returns Ok(Some(Vec<u8>)) when key exists.

precompile/binaries/stdlib/dex.mv

130 Bytes
Binary file not shown.

precompile/modules/initia_stdlib/sources/dex.move

+12
Original file line numberDiff line numberDiff line change
@@ -204,8 +204,12 @@ module initia_std::dex {
204204
/// All start_after must be provided or not
205205
const ESTART_AFTER: u64 = 17;
206206

207+
// Cannot create pair with the same coin type
207208
const ESAME_COIN_TYPE: u64 = 19;
208209

210+
/// Zero amount in the swap simulation is not allowed
211+
const EZERO_AMOUNT_IN: u64 = 20;
212+
209213
// Constants
210214
const MAX_LIMIT: u8 = 30;
211215

@@ -1359,9 +1363,17 @@ module initia_std::dex {
13591363
amount_in: u64,
13601364
swap_fee_rate: Decimal128,
13611365
): (u64, u64) {
1366+
assert!(amount_in > 0, error::invalid_argument(EZERO_AMOUNT_IN));
1367+
13621368
let one = decimal128::one();
13631369
let exp = decimal128::from_ratio(decimal128::val(&weight_in), decimal128::val(&weight_out));
1370+
1371+
// avoid zero fee amount to prevent fee bypass attack
13641372
let fee_amount = decimal128::mul_u64(&swap_fee_rate, amount_in);
1373+
if (fee_amount == 0) {
1374+
fee_amount = 1;
1375+
};
1376+
13651377
let adjusted_amount_in = amount_in - fee_amount;
13661378
let base = decimal128::from_ratio_u64(pool_amount_in, pool_amount_in + adjusted_amount_in);
13671379
let sub_amount = pow(&base, &exp);

0 commit comments

Comments
 (0)