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

Support multiple routes in astroport adapter #99

Merged
merged 10 commits into from
Apr 30, 2024
Merged

Conversation

thal0x
Copy link
Member

@thal0x thal0x commented Apr 20, 2024

Updates the Astroport adapter contract to support multiple routes.

The main change is to add an optional offer_asset to AstroportPoolSwap to allow manually setting the swap amount of an operation. With this we can set the offer_asset of a swap operation to the offer_asset of the route if it is the first operation in the route.

Most validation we will have to do regarding multiple routes appears to be done in the entry point contract, but correct me if I'm wrong.

@thal0x thal0x requested a review from NotJeremyLiu April 20, 2024 13:20
let return_denom = match routes
.last()
.and_then(|last_route| last_route.operations.last())
{
Some(last_op) => last_op.denom_out.clone(),
Copy link
Member

Choose a reason for hiding this comment

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

Yeah let's make sure we do validation on entrypoint that the first op denom / offer asset of all routes must be the same, and the last op denom of all routes must be the same in the entrypoint. Then this line is safe

let offer_asset = get_current_asset_available(&deps, &env, &operation.denom_in)?;
let offer_asset = match offer_asset {
Some(offer_asset) => offer_asset,
None => get_current_asset_available(&deps, &env, &operation.denom_in)?,
Copy link
Member

Choose a reason for hiding this comment

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

I believe there's an edge case here, ex:

  1. 100 OSMO in, 2 route trade, each route has an in of 50 OSMO
  2. First route is: (Osmo->Atom, Atom->Stars, Stars->Osmo, Osmo->USDC)
  3. That last operation (Osmo->USDC) would use all the available Osmo asset, leaving none for the 2nd route?

The only real reason why a certain route would be given (instead of just the last osmo->usdc) is if the prior operations net more Osmo and that's why it's the better swap route. So either we want to disallow these types of routes in validation or we will need a diff way to do accounting (accounting differences in balance after each swap)

Copy link
Member Author

Choose a reason for hiding this comment

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

damn I didn't know we did routes like this, ok I'll handle this differently

AstroportPoolSwap {
operation: SwapOperation,
offer_asset: Option<Asset>,
}, // Only used for the astroport swap adapter contract
Copy link
Member

Choose a reason for hiding this comment

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

let's move these comments above the variant now, inline comments look good in one liners, not so much w/ multiliner

@@ -171,22 +157,40 @@ fn execute_swap(
return Err(ContractError::Unauthorized);
}

// reset the pre swap out asset amount
PRE_SWAP_OUT_ASSET_AMOUNT.save(deps.storage, &Uint128::new(0))?;
Copy link
Member Author

Choose a reason for hiding this comment

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

I'm pretty sure in theory this isn't needed, but might be good to be safe

Copy link
Member

Choose a reason for hiding this comment

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

Why in theory is this not needed? Just because we don't expect coinhall to ever give us routes that touch the same denom out as the offer asset?

Copy link
Member

Choose a reason for hiding this comment

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

And for this to be correct shouldn't this query the contract's balance for the first hop's denom out instead of 0? Which ideally is 0 most of the time but doesn't seem like we should always assume it's 0

Copy link
Member Author

Choose a reason for hiding this comment

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

I believe it doesn't query the balance because it does it later, but let me recheck the logic

Copy link
Member Author

Choose a reason for hiding this comment

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

Ok now that I'm looking at the code again, it's not querying the balance in execute_swap because it queries the balance in execute_astroport_pool_swap.

And this is why I said that it's not needed in theory, because whatever value gets saved here, gets overwritten anyways.

@NotJeremyLiu
Copy link
Member

approved. left some comments in response to your PRE_SWAP_OUT_ASSET_AMOUNT comment

@thal0x thal0x merged commit a95f17d into main Apr 30, 2024
5 checks passed
@thal0x thal0x deleted the jw/astroport-multi-route branch April 30, 2024 10:54
thal0x added a commit that referenced this pull request May 7, 2024
thal0x added a commit that referenced this pull request May 7, 2024
* Revert "Add split route support in all adapters (#103)"

This reverts commit 1fa4f21.

* Revert "Support multiple routes in astroport adapter (#99)"

This reverts commit a95f17d.

* Revert "Support split routes in entry point contract (#101)"

This reverts commit 94c3bbf.

* Revert "Update contract types to support route splitting (#98)"

This reverts commit ec1fd57.

* Revert "add interface field to SwapOperation (#96)"

This reverts commit eb37618.
thal0x added a commit that referenced this pull request May 8, 2024
* add SmartSwapExactAssetIn swap type

* Revert "Add split route support in all adapters (#103)"

This reverts commit 1fa4f21.

* Revert "Support multiple routes in astroport adapter (#99)"

This reverts commit a95f17d.

* Revert "Support split routes in entry point contract (#101)"

This reverts commit 94c3bbf.

* Revert "Update contract types to support route splitting (#98)"

This reverts commit ec1fd57.

* Revert "add interface field to SwapOperation (#96)"

This reverts commit eb37618.

* remove deployed test contracts

* fix fmt issue

* update schema

* since user_swap is mutatable, we can mutate in place

* swap excess funds

* update schema

* lint change

* fix test

* add optional interface field to SwapOperation (#108)

* return error for largest_route_index

---------

Co-authored-by: Jeremy Liu <[email protected]>
thal0x added a commit that referenced this pull request May 8, 2024
* add SmartSwapExactAssetIn swap type

* Revert "Add split route support in all adapters (#103)"

This reverts commit 1fa4f21.

* Revert "Support multiple routes in astroport adapter (#99)"

This reverts commit a95f17d.

* Revert "Support split routes in entry point contract (#101)"

This reverts commit 94c3bbf.

* Revert "Update contract types to support route splitting (#98)"

This reverts commit ec1fd57.

* Revert "add interface field to SwapOperation (#96)"

This reverts commit eb37618.

* remove deployed test contracts

* fix fmt issue

* update schema

* since user_swap is mutatable, we can mutate in place

* swap excess funds

* update schema

* lint change

* fix test

* add optional interface field to SwapOperation (#108)

* return error for largest_route_index

* [API-2792] Add hallswap adapter (#102)

* feat: add hallswap adapter

* chore: add readme

* pr fixes

* add optional interface field to SwapOperation

* update hallswap adapter to implement updated interface

* remove get_hallswap_routes_from_skip_routes

---------

Co-authored-by: thal0x <[email protected]>

---------

Co-authored-by: Jeremy Liu <[email protected]>
Co-authored-by: Yonggiee <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants