Skip to content

Commit

Permalink
[ECO-1755][Coverage][Audit] Add abort for empty market bytes (#85)
Browse files Browse the repository at this point in the history
  • Loading branch information
alnoki authored Jun 3, 2024
1 parent e7a572d commit 6d65d23
Show file tree
Hide file tree
Showing 7 changed files with 942 additions and 914 deletions.
939 changes: 475 additions & 464 deletions doc/move/emojicoin_dot_fun/emojicoin_dot_fun.md

Large diffs are not rendered by default.

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
846 changes: 426 additions & 420 deletions doc/move/emojicoin_dot_fun/img/emojicoin_dot_fun_forward_dep.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion doc/move/emojicoin_dot_fun/img/hex_codes_backward_dep.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion doc/move/emojicoin_dot_fun/img/hex_codes_forward_dep.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
57 changes: 30 additions & 27 deletions src/move/emojicoin_dot_fun/sources/emojicoin_dot_fun.move
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ module emojicoin_dot_fun::emojicoin_dot_fun {
const E_CHAT_MESSAGE_EMPTY: u64 = 13;
/// The given emoji index is out of bounds.
const E_INVALID_EMOJI_INDEX: u64 = 14;
/// No emoji bytes specified.
const E_EMOJI_BYTES_EMPTY: u64 = 15;

/// Exists at package address, tracks the address of the registry object.
struct RegistryAddress has key {
Expand Down Expand Up @@ -1768,7 +1770,7 @@ module emojicoin_dot_fun::emojicoin_dot_fun {
};
}

inline fun assert_valid_coin_types<Emojicoin, EmojicoinLP>(market_address: address) {
/*inline*/ fun assert_valid_coin_types<Emojicoin, EmojicoinLP>(market_address: address) {
assert!(
exists<LPCoinCapabilities<Emojicoin, EmojicoinLP>>(market_address),
E_INVALID_COIN_TYPES
Expand Down Expand Up @@ -1813,7 +1815,7 @@ module emojicoin_dot_fun::emojicoin_dot_fun {
)
}

inline fun bump_market_state(
/*inline*/ fun bump_market_state(
market_ref: &Market,
trigger: u8,
instantaneous_stats: InstantaneousStats,
Expand All @@ -1835,15 +1837,15 @@ module emojicoin_dot_fun::emojicoin_dot_fun {
});
}

inline fun burn_lp_coins<Emojicoin, EmojicoinLP>(
/*inline*/ fun burn_lp_coins<Emojicoin, EmojicoinLP>(
market_address: address,
coin: Coin<EmojicoinLP>,
) acquires LPCoinCapabilities {
let coin_caps = borrow_global<LPCoinCapabilities<Emojicoin, EmojicoinLP>>(market_address);
coin::burn<EmojicoinLP>(coin, &coin_caps.burn);
}

inline fun cpamm_simple_swap_output_amount(
/*inline*/ fun cpamm_simple_swap_output_amount(
input_amount: u64,
is_sell: bool,
reserves: Reserves
Expand All @@ -1857,7 +1859,7 @@ module emojicoin_dot_fun::emojicoin_dot_fun {
(result as u64)
}

inline fun create_market(
/*inline*/ fun create_market(
registry_ref_mut: &mut Registry,
emoji_bytes: vector<u8>,
): (address, signer) {
Expand Down Expand Up @@ -1944,7 +1946,7 @@ module emojicoin_dot_fun::emojicoin_dot_fun {
(market_address, market_signer)
}

inline fun emit_periodic_state(
/*inline*/ fun emit_periodic_state(
market_metadata_ref: &MarketMetadata,
nonce: u64,
time: u64,
Expand Down Expand Up @@ -1980,7 +1982,7 @@ module emojicoin_dot_fun::emojicoin_dot_fun {
});
}

inline fun ensure_coins_initialized<Emojicoin, EmojicoinLP>(
/*inline*/ fun ensure_coins_initialized<Emojicoin, EmojicoinLP>(
market_ref: &Market,
market_signer: &signer,
market_address: address,
Expand Down Expand Up @@ -2044,13 +2046,13 @@ module emojicoin_dot_fun::emojicoin_dot_fun {
}
}

inline fun fdv(
/*inline*/ fun fdv(
reserves: Reserves,
): u128 {
mul_div(reserves.quote, EMOJICOIN_SUPPLY, reserves.base)
}

inline fun fdv_market_cap(
/*inline*/ fun fdv_market_cap(
reserves: Reserves,
supply_minuend: u64,
): (
Expand All @@ -2065,7 +2067,7 @@ module emojicoin_dot_fun::emojicoin_dot_fun {
)
}

inline fun fdv_market_cap_start_end(
/*inline*/ fun fdv_market_cap_start_end(
reserves_start: Reserves,
reserves_end: Reserves,
supply_minuend: u64,
Expand All @@ -2080,14 +2082,14 @@ module emojicoin_dot_fun::emojicoin_dot_fun {
(fdv_start, market_cap_start, fdv_end, market_cap_end)
}

inline fun get_bps_fee(
/*inline*/ fun get_bps_fee(
principal: u64,
fee_rate_bps: u8,
): u64 {
((((principal as u128) * (fee_rate_bps as u128)) / BASIS_POINTS_PER_UNIT) as u64)
}

inline fun get_concatenation(base: String, additional: String): String {
/*inline*/ fun get_concatenation(base: String, additional: String): String {
string::append(&mut base, additional);
base
}
Expand Down Expand Up @@ -2116,7 +2118,7 @@ module emojicoin_dot_fun::emojicoin_dot_fun {
borrow_global<RegistryAddress>(@emojicoin_dot_fun).registry_address
}

inline fun get_verified_symbol_emoji_bytes(
/*inline*/ fun get_verified_symbol_emoji_bytes(
registry_ref: &Registry,
emojis: vector<vector<u8>>,
): vector<u8> {
Expand All @@ -2127,6 +2129,7 @@ module emojicoin_dot_fun::emojicoin_dot_fun {
assert!(table::contains(coin_symbol_emojis_ref, emoji), E_NOT_SUPPORTED_SYMBOL_EMOJI);
vector::append(&mut verified_bytes, emoji);
};
assert!(!vector::is_empty(&verified_bytes), E_EMOJI_BYTES_EMPTY);
assert!(
vector::length(&verified_bytes) <= (MAX_SYMBOL_LENGTH as u64),
E_EMOJI_BYTES_TOO_LONG
Expand All @@ -2135,7 +2138,7 @@ module emojicoin_dot_fun::emojicoin_dot_fun {
verified_bytes
}

inline fun instantaneous_stats(market_ref: &Market): InstantaneousStats {
/*inline*/ fun instantaneous_stats(market_ref: &Market): InstantaneousStats {
let lp_coin_supply = market_ref.lp_coin_supply;
let in_bonding_curve = lp_coin_supply == 0;
let total_quote_locked = total_quote_locked(market_ref, in_bonding_curve);
Expand All @@ -2157,14 +2160,14 @@ module emojicoin_dot_fun::emojicoin_dot_fun {
table::contains(&registry_ref.supplemental_chat_emojis, emoji)
}

inline fun last_period_boundary(
/*inline*/ fun last_period_boundary(
time: u64,
period: u64,
): u64 {
(time / period) * period
}

inline fun liquidity_provision_operation_epilogue(
/*inline*/ fun liquidity_provision_operation_epilogue(
tvl: u128,
fdv: u128,
market_cap: u128,
Expand Down Expand Up @@ -2195,23 +2198,23 @@ module emojicoin_dot_fun::emojicoin_dot_fun {
);
}

inline fun mint_lp_coins<Emojicoin, EmojicoinLP>(
/*inline*/ fun mint_lp_coins<Emojicoin, EmojicoinLP>(
market_address: address,
amount: u64,
): Coin<EmojicoinLP> acquires LPCoinCapabilities {
let coin_caps = borrow_global<LPCoinCapabilities<Emojicoin, EmojicoinLP>>(market_address);
coin::mint<EmojicoinLP>(amount, &coin_caps.mint)
}

inline fun mul_div(
/*inline*/ fun mul_div(
a: u64,
b: u64,
c: u64,
): u128 {
(a as u128) * (b as u128) / (c as u128)
}

inline fun simulate_provide_liquidity_inner(
/*inline*/ fun simulate_provide_liquidity_inner(
provider: address,
quote_amount: u64,
market_ref: &Market,
Expand Down Expand Up @@ -2250,7 +2253,7 @@ module emojicoin_dot_fun::emojicoin_dot_fun {
}
}

inline fun simulate_remove_liquidity_inner<Emojicoin>(
/*inline*/ fun simulate_remove_liquidity_inner<Emojicoin>(
provider: address,
lp_coin_amount: u64,
market_ref: &Market,
Expand Down Expand Up @@ -2296,7 +2299,7 @@ module emojicoin_dot_fun::emojicoin_dot_fun {
}
}

inline fun total_quote_locked(
/*inline*/ fun total_quote_locked(
market_ref: &Market,
in_bonding_curve: bool,
): u64 {
Expand All @@ -2307,7 +2310,7 @@ module emojicoin_dot_fun::emojicoin_dot_fun {
}
}

inline fun tvl(
/*inline*/ fun tvl(
market_ref: &Market,
in_bonding_curve: bool,
): u128 {
Expand All @@ -2318,7 +2321,7 @@ module emojicoin_dot_fun::emojicoin_dot_fun {
}
}

inline fun tvl_clamm(
/*inline*/ fun tvl_clamm(
virtual_reserves: Reserves,
): u128 {
let quote_virtual = virtual_reserves.quote;
Expand All @@ -2335,7 +2338,7 @@ module emojicoin_dot_fun::emojicoin_dot_fun {
}
}

inline fun tvl_cpamm(
/*inline*/ fun tvl_cpamm(
real_quote_reserves: u64,
): u128 {
// Base reserves priced in quote are equal to the value of quote reserves.
Expand All @@ -2355,7 +2358,7 @@ module emojicoin_dot_fun::emojicoin_dot_fun {
///
/// While all terms can technically be `u128`, in practice they will all be `u64`, and even if
/// a few terms require a few extra bits, there should not be any overflow.
inline fun tvl_per_lp_coin_growth_q64_inline(
/*inline*/ fun tvl_per_lp_coin_growth_q64_inline(
start: TVLtoLPCoinRatio,
end: TVLtoLPCoinRatio,
): u128 {
Expand All @@ -2372,7 +2375,7 @@ module emojicoin_dot_fun::emojicoin_dot_fun {
}
}

inline fun update_global_fdv_market_cap_for_liquidity_operation(
/*inline*/ fun update_global_fdv_market_cap_for_liquidity_operation(
fdv_start: u128,
market_cap_start: u128,
fdv_end: u128,
Expand Down Expand Up @@ -2400,7 +2403,7 @@ module emojicoin_dot_fun::emojicoin_dot_fun {
};
}

inline fun valid_coin_types<Emojicoin, EmojicoinLP>(market_address: address): bool {
/*inline*/ fun valid_coin_types<Emojicoin, EmojicoinLP>(market_address: address): bool {
let emoji_type = &type_info::type_of<Emojicoin>();
let lp_type = &type_info::type_of<EmojicoinLP>();

Expand Down
8 changes: 8 additions & 0 deletions src/move/emojicoin_dot_fun/tests/tests.move
Original file line number Diff line number Diff line change
Expand Up @@ -4272,6 +4272,14 @@
);
}

#[test, expected_failure(
abort_code = emojicoin_dot_fun::emojicoin_dot_fun::E_EMOJI_BYTES_EMPTY,
location = emojicoin_dot_fun::emojicoin_dot_fun,
)] fun verified_symbol_emoji_bytes_emoji_bytes_empty() {
init_package();
verified_symbol_emoji_bytes(vector[]);
}

#[test, expected_failure(
abort_code = emojicoin_dot_fun::emojicoin_dot_fun::E_EMOJI_BYTES_TOO_LONG,
location = emojicoin_dot_fun::emojicoin_dot_fun,
Expand Down

0 comments on commit 6d65d23

Please sign in to comment.