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

Fix dex ed #1196

Merged
merged 2 commits into from
Jul 11, 2023
Merged

Fix dex ed #1196

merged 2 commits into from
Jul 11, 2023

Conversation

zqhxuyuan
Copy link
Contributor

@zqhxuyuan zqhxuyuan commented Jul 10, 2023

Description

fix dex ed when last user remove liquidity failed.

DEX commit: Manta-Network/Zenlink@853f41c#diff-48b4d8abc9afad5d3c79f61c7ad9b831eb830423e5709aaff6b6fd67acc3be3cR105-R110

PS: did some refactor on zenlink

i.e. A pair of(KMA,KSM), when add liquidity, user's KMA and KSM transfer to pair acocunt. And the liquidity amount is record as total_supply on pair account. when user remove liquidity, KMA and KSM return back to user account.

https://github.com/Manta-Network/Zenlink/blob/c2acc36bea02e6c2aa38f3b290e255dfd5c164dd/zenlink-protocol/src/swap/mod.rs#L135-L143

let reserve_0 = T::MultiAssetsHandler::balance_of(asset_0, lp_account);
let reserve_1 = T::MultiAssetsHandler::balance_of(asset_1, lp_account);

let (amount_0, amount_1) = calculate_share_amounts(
	remove_liquidity,
	status.total_supply,
	reserve_0,
	reserve_1,
);

amount = remove_liquidity * reserve / total_supply, Let's say there's only one user add liquidity, now when user remove liquidity, remove_liquidity=total_supply, so the amount=reserve. amount_0 = KMA_balance(lp_account).

deposit ED of KMA to pair account is not going to work, because amount = KMA balance of pair account, and all the KMA on pair account will be transfer to user.

https://github.com/Manta-Network/Zenlink/blob/c2acc36bea02e6c2aa38f3b290e255dfd5c164dd/zenlink-protocol/src/swap/mod.rs#L176

The T::MultiAssetsHandler::transfer on original zenlink when transfer native token is using KeepAlive:

https://github.com/zenlinkpro/Zenlink-DEX-Module/blob/7662acbff71cea3b35db918ceb9370e8929be991/zenlink-protocol/src/multiassets.rs#L100-L106

T::MultiAssetsHandler::transfer(asset_0, lp_account, recipient, amount_0)?; 

This will failed, because pair account can't transfer all KMA while keep alive at the same time.

For non native token, we already set as AllowDeath:

<MantaConcreteFungibleLedger as FungibleLedger>::withdraw_burning(
manta_asset_id,
origin,
amount,
ExistenceRequirement::AllowDeath,

transfer on LocalAssetHandler is local_withdraw + local_deposit: https://github.com/zenlinkpro/Zenlink-DEX-Module/blob/7662acbff71cea3b35db918ceb9370e8929be991/zenlink-protocol/src/traits.rs#L17-L24


Before we can approve this PR for merge, please make sure that all the following items have been checked off:

  • Connected to an issue with discussion and accepted design using zenhub "Connect issue" button below
  • Added one label out of the L- group to this PR
  • Added one or more labels from the A- and C- groups to this PR
  • Explicitly labelled A-calamari and/or A-manta if your changes are meant for/impact either of these (CI depends on it)
  • Re-reviewed Files changed in the Github PR explorer.

Situational Notes:

  • If adding functionality, write unit tests!
  • If importing a new pallet, choose a proper module index for it, and allow it in BaseFilter. Ensure every extrinsic works from front-end. If there's corresponding tool, ensure both work for each other.
  • If needed, update our Javascript/Typescript APIs. These APIs are officially used by exchanges or community developers.
  • If modifying existing runtime storage items, make sure to implement storage migrations for the runtime and test them with try-runtime. This includes migrations inherited from upstream changes, and you can search the diffs for modifications of #[pallet::storage] items to check for any.

Signed-off-by: zqhxuyuan <zqhxuyuan@gmail.com>
@zqhxuyuan zqhxuyuan added A-manta Area: Issues and PRs related to the Manta Runtime L-fixed Log: Issues and PRs related to bug fixes C-bug Category: Issues documenting a bug labels Jul 10, 2023
@github-actions
Copy link

github-actions bot commented Jul 10, 2023

⚠️ Congestion test: 1-day congestion cost (manta-runtime) is NOT above target_daily_congestion_cost_kma

@github-actions
Copy link

github-actions bot commented Jul 10, 2023

⚠️ Congestion test: 1-day congestion cost (calamari-runtime) is NOT above target_daily_congestion_cost_kma

Copy link
Contributor

@ferrell-code ferrell-code left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could reaping the dex account cause any other problems? I'd have to dive deeper but I feel like then the state of the dex could not match the account balance of dex account.

Maybe we could just send ed of manta, or create allowlist for non-reapable accounts

@Apokalip
Copy link
Contributor

Could reaping the dex account cause any other problems? I'd have to dive deeper but I feel like then the state of the dex could not match the account balance of dex account.

Maybe we could just send ed of manta, or create allowlist for non-reapable accounts

How big would that list grow to be? 🤔

Copy link
Contributor

@ghzlatarev ghzlatarev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we have a real description please what problem this is solving and how exactly.

@ghzlatarev
Copy link
Contributor

Could reaping the dex account cause any other problems? I'd have to dive deeper but I feel like then the state of the dex could not match the account balance of dex account.

Maybe we could just send ed of manta, or create allowlist for non-reapable accounts

what is the concrete scenario you're worried about ?

@Dengjianping
Copy link
Contributor

Does zenlink team know this issue or have they fixed it already in later release?

@zqhxuyuan
Copy link
Contributor Author

Does zenlink team know this issue or have they fixed it already in later release?

yes, I've told them this issue. We may consider add some integration test into our chain. As zenlink's testcase is using tokens module not pallet_assets.

@zqhxuyuan
Copy link
Contributor Author

Can we have a real description please what problem this is solving and how exactly.

add more info in the description.

@ghzlatarev ghzlatarev merged commit c33d83a into manta Jul 11, 2023
35 checks passed
@ghzlatarev ghzlatarev deleted the fix_dex_ed branch July 11, 2023 12:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-manta Area: Issues and PRs related to the Manta Runtime C-bug Category: Issues documenting a bug L-fixed Log: Issues and PRs related to bug fixes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants