Skip to content

Commit

Permalink
Merge pull request #3 from nervina-labs/develop
Browse files Browse the repository at this point in the history
Release v0.2.0
  • Loading branch information
duanyytop authored Feb 5, 2024
2 parents b16477d + e94baa0 commit 8752ea6
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
Build contracts:

``` sh
make build-release
make build
```

Run tests:

``` sh
make test-release
make test
```
2 changes: 1 addition & 1 deletion contracts/dex-lock/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "dex-lock"
version = "0.1.0"
version = "0.2.0"
edition = "2021"

[dependencies]
Expand Down
8 changes: 7 additions & 1 deletion contracts/dex-lock/src/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,13 @@ pub fn main() -> Result<(), Error> {

let dex_input_capacity = load_cell_capacity(dex_index, Source::Input)? as u128;
let output_capacity = load_cell_capacity(dex_index, Source::Output)? as u128;
if (args.total_value + dex_input_capacity) > output_capacity {

// Prevent total_value(u128) from overflowing
let total_capacity = args
.total_value
.checked_add(dex_input_capacity)
.ok_or(Error::TotalValueOverflow)?;
if total_capacity > output_capacity {
return Err(Error::DexTotalValueNotMatch);
}

Expand Down
1 change: 1 addition & 0 deletions contracts/dex-lock/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub enum Error {
DexOwnerLockNotMatch,
DexTotalValueNotMatch,
DexSetupInvalid,
TotalValueOverflow,
}

impl From<SysError> for Error {
Expand Down
23 changes: 19 additions & 4 deletions tests/src/taker_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const LOCK_ARGS_INVALID: i8 = 5;
const DEX_OWNER_LOCK_NOT_MATCH: i8 = 6;
const DEX_TOTAL_VALUE_NOT_MATCH: i8 = 7;
const DEX_SETUP_INVALID: i8 = 8;
const TOTAL_VALUE_OVERFLOW: i8 = 9;

#[derive(PartialEq, Eq, Clone, Copy)]
enum DexError {
Expand All @@ -27,6 +28,7 @@ enum DexError {
DexOwnerLockNotMatch,
DexTotalValueNotMatch,
DexSetupInvalid,
TotalValueOverflow,
}

fn create_test_context(error: DexError) -> (Context, TransactionView) {
Expand Down Expand Up @@ -80,11 +82,16 @@ fn create_test_context(error: DexError) -> (Context, TransactionView) {
.build_script(&dex_out_point, dex_args1.to_vec().into())
.expect("script");

let total_value = if error == DexError::TotalValueOverflow {
u128::MAX - 1
} else {
9_8765_0000_1234u128
};
let dex_args2 = DexArgs {
owner_lock: owner_lock2.clone(),
setup: 0u8,
total_value: 9_8765_0000_1234u128,
receiver_lock: None,
owner_lock: owner_lock2.clone(),
setup: 0u8,
total_value,
receiver_lock: None,
unit_type_hash: None,
};
let mut dex_args2_vec = dex_args2.to_vec();
Expand Down Expand Up @@ -215,3 +222,11 @@ fn test_dex_taker_order_total_setup_invalid_error() {
let err = context.verify_tx(&tx, MAX_CYCLES).unwrap_err();
assert_script_error(err, DEX_SETUP_INVALID);
}

#[test]
fn test_dex_taker_order_total_value_overflow_error() {
let (context, tx) = create_test_context(DexError::TotalValueOverflow);
// run
let err = context.verify_tx(&tx, MAX_CYCLES).unwrap_err();
assert_script_error(err, TOTAL_VALUE_OVERFLOW);
}

0 comments on commit 8752ea6

Please sign in to comment.