Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: api integration tests #133

Merged
merged 38 commits into from
Aug 6, 2024
Merged
Show file tree
Hide file tree
Changes from 34 commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
360bb2a
feat: pop api fungibles pallet
Daanvdplas Jul 19, 2024
e097f80
refactor: comments
Daanvdplas Jul 20, 2024
c8d8b2a
refactor: rebase main, refactor extension with enums
evilrobot-01 Jul 20, 2024
c88387d
refactor
Daanvdplas Jul 21, 2024
224390a
refactor 2
Daanvdplas Jul 22, 2024
43ae445
feat: add fungibles pallet functions
Daanvdplas Jul 22, 2024
bbe01a1
tests: remove transfer_from (transfer copy) and add approve
Daanvdplas Jul 22, 2024
68264da
refactor: primitives
Daanvdplas Jul 22, 2024
2481773
fix: pallet api error + missing docs
Daanvdplas Jul 22, 2024
c10a8d4
fix: integration test after removal fungibles feature primitives
Daanvdplas Jul 22, 2024
a8c4bb5
chore: benchmark approve
Daanvdplas Jul 23, 2024
4419bad
refactor: pallet
Daanvdplas Jul 24, 2024
792fa07
refactor: integration-tests
Daanvdplas Jul 24, 2024
96da2f0
refactor: ink api library
Daanvdplas Jul 24, 2024
cb54a57
refactor: devnet runtime
Daanvdplas Jul 24, 2024
9ec2744
chore: benchmark fungibles approve
Daanvdplas Jul 24, 2024
f0fff97
test: ensure Lookup configuration
Daanvdplas Jul 24, 2024
4a476b0
fix: approve benchmark + weights
Daanvdplas Jul 24, 2024
f3cc83a
refactor: primitives
Daanvdplas Jul 25, 2024
bd85489
refactor: bits and pieces
Daanvdplas Jul 25, 2024
0e49f63
fix: test devnet
Daanvdplas Jul 25, 2024
8f6139c
refactor: apply final comments
Daanvdplas Jul 25, 2024
948e144
chore: benchmark approve
Daanvdplas Jul 23, 2024
88577e4
refactor: read state
Daanvdplas Jul 23, 2024
809e192
refactor: add state query filter
Daanvdplas Jul 23, 2024
ac9714c
fix: test UnknownCall
Daanvdplas Jul 23, 2024
77711ec
refactor: read state functions
Daanvdplas Jul 25, 2024
b50830c
refactor: enums
Daanvdplas Jul 25, 2024
2a8800c
refactor: style docs and using constants for errors in chain extension
Daanvdplas Jul 26, 2024
b93f009
refactor: api integration tests
Daanvdplas Jul 26, 2024
b639e8b
refactor: IT better error handling
Daanvdplas Jul 26, 2024
074b8cf
merge daan/api
Daanvdplas Jul 28, 2024
5aec185
merge daan/api
Daanvdplas Jul 31, 2024
51c38cc
refactor: match statement
Daanvdplas Jul 31, 2024
60c744b
refactor: resolve clippy
Daanvdplas Aug 5, 2024
5b78e4c
feat: asset management (#151)
Daanvdplas Aug 6, 2024
9efb839
style: consistency docs
Daanvdplas Aug 6, 2024
e623419
style: explanation test
Daanvdplas Aug 6, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions pallets/api/src/fungibles/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,30 +153,30 @@
let id: AssetIdParameterOf<T> = id.into();

// If the new value is equal to the current allowance, do nothing.
let return_weight = if value == current_allowance {
Self::weight_approve(0, 0)
}
// If the new value is greater than the current allowance, approve the difference
// because `approve_transfer` works additively (see `pallet-assets`).
else if value > current_allowance {
AssetsOf::<T>::approve_transfer(
origin,
id,
spender,
value.saturating_sub(current_allowance),
)
.map_err(|e| e.with_weight(Self::weight_approve(1, 0)))?;
Self::weight_approve(1, 0)
} else {
// If the new value is less than the current allowance, cancel the approval and set the new value
AssetsOf::<T>::cancel_approval(origin.clone(), id.clone(), spender.clone())
.map_err(|e| e.with_weight(Self::weight_approve(0, 1)))?;
if value.is_zero() {
return Ok(Some(Self::weight_approve(0, 1)).into());
}
AssetsOf::<T>::approve_transfer(origin, id, spender, value)?;
Self::weight_approve(1, 1)
};

Check warning on line 179 in pallets/api/src/fungibles/mod.rs

View workflow job for this annotation

GitHub Actions / clippy

`if` chain can be rewritten with `match`

warning: `if` chain can be rewritten with `match` --> pallets/api/src/fungibles/mod.rs:156:24 | 156 | let return_weight = if value == current_allowance { | _________________________________^ 157 | | Self::weight_approve(0, 0) 158 | | } 159 | | // If the new value is greater than the current allowance, approve the difference ... | 178 | | Self::weight_approve(1, 1) 179 | | }; | |_____________^ | = help: consider rewriting the `if` chain to use `cmp` and `match` = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#comparison_chain = note: `#[warn(clippy::comparison_chain)]` on by default
Ok(Some(return_weight).into())
}

Expand Down Expand Up @@ -214,23 +214,21 @@
) -> DispatchResultWithPostInfo {
let who = ensure_signed(origin.clone())
.map_err(|e| e.with_weight(Self::weight_approve(0, 0)))?;
let mut current_allowance = AssetsOf::<T>::allowance(id.clone(), &who, &spender);
let current_allowance = AssetsOf::<T>::allowance(id.clone(), &who, &spender);
let spender = T::Lookup::unlookup(spender);
let id: AssetIdParameterOf<T> = id.into();

if value.is_zero() {
return Ok(Some(Self::weight_approve(0, 0)).into());
}

current_allowance.saturating_reduce(value);
// Cancel the aproval and set the new value if `current_allowance` is more than zero.
Daanvdplas marked this conversation as resolved.
Show resolved Hide resolved
AssetsOf::<T>::cancel_approval(origin.clone(), id.clone(), spender.clone())
.map_err(|e| e.with_weight(Self::weight_approve(0, 1)))?;

if current_allowance.is_zero() {
let new_allowance = current_allowance.saturating_sub(value);
if new_allowance.is_zero() {
Daanvdplas marked this conversation as resolved.
Show resolved Hide resolved
return Ok(Some(Self::weight_approve(0, 1)).into());
}
AssetsOf::<T>::approve_transfer(origin, id, spender, current_allowance)?;
AssetsOf::<T>::approve_transfer(origin, id, spender, new_allowance)?;
Ok(().into())
}
}
Expand Down
36 changes: 18 additions & 18 deletions pallets/api/src/fungibles/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,24 +38,6 @@ fn transfer_from_works() {
});
}

#[test]
fn decrease_allowance_works() {
new_test_ext().execute_with(|| {
let amount: Balance = 100 * UNIT;
create_asset_mint_and_approve(ALICE, ASSET, ALICE, amount, BOB, amount);
assert_eq!(Assets::allowance(ASSET, &ALICE, &BOB), amount);
// Owner balance is not changed if decreased by zero.
assert_ok!(Fungibles::decrease_allowance(signed(ALICE), ASSET, BOB, 0));
assert_eq!(Assets::allowance(ASSET, &ALICE, &BOB), amount);
// Decrease allowance successfully.
assert_ok!(Fungibles::decrease_allowance(signed(ALICE), ASSET, BOB, amount / 2 - 1 * UNIT));
assert_eq!(Assets::allowance(ASSET, &ALICE, &BOB), amount / 2 + 1 * UNIT);
// Saturating if current allowance is decreased more than the owner balance.
assert_ok!(Fungibles::decrease_allowance(signed(ALICE), ASSET, BOB, amount));
assert_eq!(Assets::allowance(ASSET, &ALICE, &BOB), 0);
});
}

// Non-additive, sets new value.
#[test]
fn approve_works() {
Expand Down Expand Up @@ -94,6 +76,24 @@ fn increase_allowance_works() {
});
}

#[test]
fn decrease_allowance_works() {
new_test_ext().execute_with(|| {
let amount: Balance = 100 * UNIT;
create_asset_mint_and_approve(ALICE, ASSET, ALICE, amount, BOB, amount);
assert_eq!(Assets::allowance(ASSET, &ALICE, &BOB), amount);
// Owner balance is not changed if decreased by zero.
assert_ok!(Fungibles::decrease_allowance(signed(ALICE), ASSET, BOB, 0));
assert_eq!(Assets::allowance(ASSET, &ALICE, &BOB), amount);
// Decrease allowance successfully.
assert_ok!(Fungibles::decrease_allowance(signed(ALICE), ASSET, BOB, amount / 2 - 1 * UNIT));
assert_eq!(Assets::allowance(ASSET, &ALICE, &BOB), amount / 2 + 1 * UNIT);
// Saturating if current allowance is decreased more than the owner balance.
assert_ok!(Fungibles::decrease_allowance(signed(ALICE), ASSET, BOB, amount));
assert_eq!(Assets::allowance(ASSET, &ALICE, &BOB), 0);
});
}

#[test]
fn total_supply_works() {
new_test_ext().execute_with(|| {
Expand Down
Loading
Loading