gnosis / gnosis/dex-open-solver

Fee payment through USDC runs into issues due to min trade amount

Open
#53 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Python
Stars
14
Forks
7
PR merge metrics
No merged PRs in 30d

Description

This is not an issue with the solver, I post it here anyway:

In batch 5290401 someone wanted to trade their newly added token (T030) against USDC.
Since he sold only 1 USDC, only 2/1000 USDC in fees token imbalance was created. Since this is less than 10000 [inWEI], since USDC has only 6 digits, the rounding creating the right connection to the fee tokens fails.

```
python -m src.match --logging DEBUG ./data/instance_5290401.json token-pair T0004 T0030
DEBUG ::solver : === Order matching on token pair + fee token ===
DEBUG ::solver : b_buy_token : T0004
DEBUG ::solver : s_buy_token : T0030
DEBUG ::solver : fee_token : T0000
DEBUG ::solver :
DEBUG ::solver : === Solving T0004 -- T0030 (rational arithmetic) ===
DEBUG ::xrate : Exchange rate candidates in interval xrate ∈ [1.001e+09, 9.990e+11]:
DEBUG ::xrate : roots[3] : (1.335e+09, -7.453e+08)
DEBUG ::xrate : roots[4] : (7.075e+11, 1.168e+06) [local optimum]
DEBUG ::xrate : Exchange rate candidates for trivial solution:
DEBUG ::xrate : roots[1] : (9.990e+08, -9.990e+08)
DEBUG ::xrate : roots[2] : (1.001e+09, -9.950e+08)
DEBUG ::xrate : roots[1] : (9.990e+11, 9.980e+05) [local optimum]
DEBUG ::xrate : roots[2] : (1.001e+12, -9.980e+05)
DEBUG ::solver : p(T0004) / p(T0030) = 7.075e+11 (precise arithmetic)
DEBUG ::solver :
DEBUG ::solver : === Computing price of T0004 ===
DEBUG ::solver : Imbalance of T0004 : 1.999e+03 (due to fee)
DEBUG ::solver :
DEBUG ::solver : === Solving T0004 -- T0000 (nr_exec_f_orders ∈ [7, 7]) ===
DEBUG ::xrate : Exchange rate candidates in interval xrate ∈ [5.599e-13, 8.483e-13]:
DEBUG ::xrate : roots[4] : (5.627e-13, 3.982e+15) [local optimum]
DEBUG ::xrate : Exchange rate candidates for trivial solution:
DEBUG ::xrate : roots[2] : (5.599e-13, 8.851e+15) [local optimum]
DEBUG ::xrate : roots[2] : (8.483e-13, -3.288e+17)
DEBUG ::xrate : roots[2] : (8.509e-13, -1.273e+18)
DEBUG ::xrate : roots[2] : (9.009e-13, -3.746e+19)
DEBUG ::xrate : roots[2] : (1.001e-12, -1.010e+20)
DEBUG ::xrate : roots[2] : (1.001e-12, -1.010e+20)
DEBUG ::xrate : roots[2] : (1.001e-12, -1.010e+20)
DEBUG ::xrate : roots[1] : (1.110e-12, -1.684e+20)
DEBUG ::solver : Adjusted xrate : 5.599e-13
DEBUG ::solver :
DEBUG ::solver : === (Re)solving T0004 -- T0030 ===
DEBUG ::solver : With price for T0004 : 1786065471188743675468864534353
DEBUG ::solver : With maximum nr bs orders : 29
INFO ::round : Reducing max sell amount [T0030] of order <1771> : 1000000000000000000 --> 999929253940003712
INFO ::round : Reducing max sell amount [T0030] of order <1718> : 1000000000000000000000 --> 999999929253940035584
INFO ::round : Reducing max sell amount [T0004] of order <1772> : 1000000 --> 999994
INFO ::round : Reducing max sell amount [T0004] of order <1719> : 1000000 --> 999994
DEBUG ::solver : Adjusted xrate : 7.075e+11
DEBUG ::solver : Objective : 2.086e+36 [best=None]
DEBUG ::solver : Price of T0004 : 1786065471188743675468864534353
DEBUG ::solver : Price of T0030 : 2.525e+18
DEBUG ::solver : Amounts of T0004 bought in exchange for T0030:
DEBUG ::solver : [9.980e+05, 0]
DEBUG ::solver : Amounts of T0030 bought in exchange for T0004:
DEBUG ::solver : [7.067e+17, 0]
DEBUG ::solver : Amounts of T0004 bought in exchange for FEE (T0000):
DEBUG ::solver : [1.001e+04, 0, 0, 0, 0, 0, 0]
DEBUG ::solver :
DEBUG ::solver : === Rounding ===
DEBUG ::round : Token balances (initial):
DEBUG ::round : T0000 : 17894623925053188
DEBUG ::round : T0004 : -8011
DEBUG ::round : T0030 : -8493776212
DEBUG ::round : Directed edges: [('T0030', 'T0004'), ('T0004', 'T0030'), ('T0000', 'T0004')]
DEBUG ::round : 3 Touched tokens: ['T0000', 'T0004', 'T0030']
DEBUG ::round : Adjusting order 1772:
DEBUG ::round : (old) buy_amount : 706748898844548616 -- sell_amount: 999993
DEBUG ::round : (new) buy_amount : 706748890350772404 -- sell_amount: 999993
DEBUG ::round : Token balances (after balancing T0030):
DEBUG ::round : T0000 : 17894623925053188
DEBUG ::round : T0004 : -8011
DEBUG ::round : T0030 : 0
DEBUG ::round : Adjusting order 511:
DEBUG ::round : (old) buy_amount : 10009 -- sell_amount: 17894623925053188
DEBUG ::round : (new) buy_amount : 10000 -- sell_amount: 17878533245132569
DEBUG ::round : Token balances (after balancing T0004):
DEBUG ::round : T0000 : 17878533245132569
DEBUG ::round : T0004 : -8002
DEBUG ::round : T0030 : 0
```

The interesting fact is that the standard solver found a solution for some reason, which was valid, but generated neg. utility:

http://gnosis-dev-dfusion.s3-website.eu-central-1.amazonaws.com/#data/mainnet/standard-solver/results/2020-04-17/instance_5290412_2020-04-17T11:46:04.615799802+00:00/

Contributor guide

No contributing guide indexed for this repository

Research direction

Start by running the provided `python -m src.match --logging DEBUG ./data/instance_5290401.json token-pair T0004 T0030` command and inspect the solver and rounding paths involved in the logged minimum-trade and fee-token imbalance. Use the linked batch data and output to determine the intended handling; done should mean this USDC fee-payment case produces a valid, non-negative-utility result without the rounding failure.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.