Skip to content

Commit

Permalink
Merge pull request #35 from WHELP-project/multihop/implement-contract
Browse files Browse the repository at this point in the history
Multihop: Implement new contract
  • Loading branch information
ueco-jb authored Dec 1, 2023
2 parents 7574127 + 5de65f1 commit cf072f3
Show file tree
Hide file tree
Showing 18 changed files with 2,643 additions and 1 deletion.
20 changes: 20 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ cw-storage-plus = "1.1"
cw-utils = "1.0"
derivative = "2.2"
dex = { path = "./packages/dex", default-features = false }
dex-pool = { path = "./contracts/pool", default-features = false }
dex-stake = { path = "./contracts/stake", default-features = false }
itertools = "0.10"
proptest = "1.0"
schemars = "0.8"
Expand Down
6 changes: 6 additions & 0 deletions contracts/multi-hop/.cargo/config
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[alias]
wasm = "build --release --lib --target wasm32-unknown-unknown"
wasm-debug = "build --lib --target wasm32-unknown-unknown"
unit-test = "test --lib"
integration-test = "test --test integration"
schema = "run --bin multi_hop_schema"
11 changes: 11 additions & 0 deletions contracts/multi-hop/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
root = true

[*]
indent_style = space
indent_size = 2
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.rs]
indent_size = 4
12 changes: 12 additions & 0 deletions contracts/multi-hop/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Build results
/target

# Text file backups
**/*.rs.bk

# macOS
.DS_Store

# IDEs
*.iml
.idea
33 changes: 33 additions & 0 deletions contracts/multi-hop/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
[package]
name = "dex-multi-hop"
version = { workspace = true }
authors = ["Jakub <[email protected]>"]
edition = { workspace = true }
description = "Dex Multi-hop - provides multi-hop swap functionality"
license = { workspace = true }

[lib]
crate-type = ["cdylib", "rlib"]

[features]
backtraces = ["cosmwasm-std/backtraces"]
library = []

[dependencies]
coreum-wasm-sdk = { workspace = true }
cosmwasm-schema = { workspace = true }
cosmwasm-std = { workspace = true }
cw-storage-plus = { workspace = true }
cw2 = { workspace = true }
cw20 = { workspace = true }
cw-utils = { workspace = true }
thiserror = { workspace = true }
dex = { workspace = true }

[dev-dependencies]
anyhow = { workspace = true }
cw-multi-test = { workspace = true }
cw20-base = { workspace = true }
# dex-factory = { workspace = true }
dex-pool = { workspace = true }
dex-stake = { workspace = true }
209 changes: 209 additions & 0 deletions contracts/multi-hop/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,209 @@
# Dex Router

The Router contract contains logic to facilitate multi-hop swaps for smart tokens and cw20.

---

### Operations Assertion

For every swap, the contract checks if the resulting token is the one that was asked for and whether the receiving amount exceeds the minimum to receive.

## InstantiateMsg

Initializes the contract with the dex factory contract address.

```json
{
"dex_factory": "core..."
}
```

## ExecuteMsg

### `receive`

CW20 receive msg.

```json
{
"receive": {
"sender": "core...",
"amount": "123",
"msg": "<base64_encoded_json_string>"
}
}
```

### `execute_swap_operation`

Swaps one token to another. _single_ defines whether this swap is single or part of a multi hop route.
This message is for internal use.

### Example

Swap UST => mABNB

```json
{
"execute_swap_operation": {
"operation": {
"dex_swap": {
"offer_asset_info": {
"smart_token": {
"denom": "uusd"
}
},
"ask_asset_info": {
"cw20_token": {
"contract_addr": "core..."
}
}
}
},
"to": "terra...",
"max_spread": "0.05",
"single": false
}
}
```

### `execute_swap_operations`

Performs multi-hop swap operations for native & cw20 tokens. Swaps execute one-by-one and the last swap will return the ask token. This function is public (can be called by anyone).

### Example

Swap KRT => UST => mABNB

```json
{
"execute_swap_operations": {
"operations": [
{
"native_swap":{
"offer_denom":"ukrw",
"ask_denom":"uusd"
}
},
{
"dex_swap": {
"offer_asset_info": {
"native_token": {
"denom": "uusd"
}
},
"ask_asset_info": {
"token": {
"contract_addr": "core..."
}
}
}
}
],
"minimum_receive": "123",
"to": "core...",
"max_spread": "0.05"
}
}
```

### `assert_minimum_receive`

Checks that an amount of ask tokens exceeds `minimum_receive`. This message is for internal use.

```json
{
"assert_minimum_receive": {
"asset_info": {
"smart_token": {
"contract_addr": "core..."
}
},
"prev_balance": "123",
"minimum_receive": "123",
"receiver": "core..."
}
}
```

## QueryMsg

All query messages are described below. A custom struct is defined for each query response.

### `config`

Returns the general configuration for the router contract.

```json
{
"config": {}
}
```

### `simulate_swap_operations`

Simulates multi-hop swap operations. Examples:

- KRT => UST => mABNB

```json
{
"simulate_swap_operations" : {
"offer_amount": "123",
"operations": [
{
"smart_swap": {
"offer_denom": "ukrw",
"ask_denom": "uusd"
}
},
{
"dex_swap": {
"offer_asset_info": {
"smart_token": {
"denom": "uusd"
}
},
"ask_asset_info": {
"cw20_token": {
"contract_addr": "core..."
}
}
}
}
]
}
}
```

- mABNB => UST => KRT

```json
{
"simulate_swap_operations" : {
"offer_amount": "123",
"operations": [
{
"dex_swap": {
"offer_denom": "uusd",
"ask_denom": "ukrw"
}
},
{
"dex_swap": {
"offer_asset_info": {
"cw20_token": {
"contract_addr": "core..."
}
},
"ask_asset_info": {
"smart_token": {
"denom": "uusd"
}
}
}
}
]
}
}
```
15 changes: 15 additions & 0 deletions contracts/multi-hop/rustfmt.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# stable
newline_style = "unix"
hard_tabs = false
tab_spaces = 4

# unstable... should we require `rustup run nightly cargo fmt` ?
# or just update the style guide when they are stable?
#fn_single_line = true
#format_code_in_doc_comments = true
#overflow_delimited_expr = true
#reorder_impl_items = true
#struct_field_align_threshold = 20
#struct_lit_single_line = true
#report_todo = "Always"

10 changes: 10 additions & 0 deletions contracts/multi-hop/src/bin/multi_hop_schema.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use cosmwasm_schema::write_api;
use dex_multi_hop::msg::{ExecuteMsg, InstantiateMsg, QueryMsg};

fn main() {
write_api! {
instantiate: InstantiateMsg,
query: QueryMsg,
execute: ExecuteMsg
}
}
Loading

0 comments on commit cf072f3

Please sign in to comment.