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

Get token list for self-sell task on Gnosis Chain #82

Merged
merged 2 commits into from
Apr 16, 2024
Merged

Conversation

fedgiac
Copy link
Contributor

@fedgiac fedgiac commented Apr 15, 2024

This fixes (at least on Gnosis Chain) issue #74.

Adds support for fetching the token lists on Gnosis Chain/xdai. Api source is blockscout, see docs here.

Test Plan

Try the script on xdai. Note the updated parameters.

npx hardhat self-sell --network xdai --origin 0xA03be496e67Ec29bC62F01a428683D7F9c204930 --receiver 0xA03be496e67Ec29bC62F01a428683D7F9c204930 --to-token 0xe91d153e0b41518a2ce8dd3d7944fa863463a97d --min-value 500 --leftover 100 --fee-slippage-bps 10000 --price-slippage-bps 500 --max-fee-percent 10 --validity 7200 --api-url "https://api.cow.fi/xdai"
Output
$ npx hardhat self-sell --network xdai --origin 0xA03be496e67Ec29bC62F01a428683D7F9c204930 --receiver 0xA03be496e67Ec29bC62F01a428683D7F9c204930 --to-token 0xe91d153e0b41518a2ce8dd3d7944fa863463a97d --min-value 10 --leftover 5 --fee-slippage-bps 10000 --price-slippage-bps 500 --max-fee-percent 10 --validity 7200 --api-url "https://api.cow.fi/xdai"
Using account 0xA03be496e67Ec29bC62F01a428683D7F9c204930
Warning: price retrieval failed for token MPS (0xfa57AA7beED63D03Aaf85fFd1753f5f6242588fb): UnsupportedToken (Token 0xfa57aa7beed63d03aaf85ffd1753f5f6242588fb is unsupported: Could not find on chain source of the token with at least 100000 balance.)
Ignored 3 units of MPS (0xfa57AA7beED63D03Aaf85fFd1753f5f6242588fb) with value 0.00 USD, does not satisfy conditions on min value and leftover
Ignored 3.428515320952721743 units of OLAS (0xcE11e14225575945b8E6Dc0D4F2dD4C570f79d9f) with value 13.23 USD, does not satisfy conditions on min value and leftover
Amounts to sell:
 address                                    | value (USD) | balance         | sold amount     | symbol | buy amount (WXDAI) | fee % | needs allowance? 
--------------------------------------------+-------------+-----------------+-----------------+--------+--------------------+-------+------------------
 0x37b60f4E9A31A64cCc0024dce7D0fD07eAA0F7B3 |       26.82 |  944.4376981095 |  768.3724911123 | PNK    |      20.7225771341 | <0.01 | yes              
 0xaBEf652195F98A91E490f047A5006B71c85f058d |       37.02 |   37.2741871030 |   32.2398700605 | crvUSD |      30.4180069757 | <0.01 | yes              
 0x7eF541E2a22058048904fE5744f9c7E4C57AF717 |       56.91 |   14.4030001840 |   13.1377869560 | BAL    |      49.3235893530 | <0.01 | yes              
 0x21a42669643f45Bc0e086b8Fc2ed70c23D67509d |       94.99 | 1008.7745440583 |  955.6762529824 | FOX    |      85.4619920833 | <0.01 | yes              
 0xaf204776c7245bF4147c2612BF6e5972Ee483701 |      121.19 |  113.8535953146 |  109.1566157273 | sDAI   |     110.3879500162 | <0.01 | yes              
 0xb90D6bec20993Be5d72A5ab353343f7a0281f158 |      123.64 |    0.1002337701 |    0.0961803551 | DXD    |     112.7410064779 | <0.01 | yes              
 0x3a97704a1b25F08aa230ae53B352e2e72ef52843 |      132.82 |    2.8147454445 |    2.7087876183 | AGVE   |     117.6747378526 | <0.01 | yes              
 0xcB444e90D8198415266c6a2724b7900fb12FC56E |      241.41 |  227.4279638550 |  222.7176836210 | EURe   |     224.5954407393 | <0.01 | yes              
 0x9C58BAcC331c9aa871AFD802DB6379a98e80CEdb |      248.94 |    0.7390962527 |    0.7242516127 | GNO    |     231.7497339565 | <0.01 | yes              
 0x177127622c4A00F3d409B75571e12cB3c8973d3c |     1569.13 | 6572.5875746238 | 6551.6442848856 | COW    |    1485.9549528645 | <0.01 |                  

The settlement transaction will cost approximately 0.001110169506661017 XDAI and will create 10 orders for an estimated total value of 2602.91 USD. The proceeds of the orders will be sent to 0xA03be496e67Ec29bC62F01a428683D7F9c204930.
Submit orders to API? (y/N) 

Notice that the tokens that the script suggests to withdraw are consistent with those on Gnosisscan (and the ignored tokens are next in the row).

@fedgiac fedgiac requested a review from a team April 15, 2024 15:43
Copy link
Contributor

@mfw78 mfw78 left a comment

Choose a reason for hiding this comment

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

Minor nit

src/tasks/withdraw/token_balances.ts Show resolved Hide resolved
Copy link
Contributor

@MartinquaXD MartinquaXD left a comment

Choose a reason for hiding this comment

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

Thanks for implementing this so quickly. 🚀
Just nits, overall the rounding issues probably shouldn't cause problems but if it's easy enough to use bigints it should probably be done.

Comment on lines +92 to +93
parseFloat(token.exchange_rate) *
(parseInt(value) / Math.pow(10, parseInt(token.decimals)));
Copy link
Contributor

Choose a reason for hiding this comment

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

Parsing the value and 10**decimals can both overflow the safe integer number range so the numbers would get converted to floats resulting in rounding issues.
Could we use BigInts here instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't think I can avoid a conversion to float anywhere here. I don't know the precision of exchange_rate, so the best I can do is saying it's a float, which still offers some decent precision. Once this is a float, everything will need to be converted to float and so parsing as BigInt doesn't really help for precision. At first I tried to replace everything with Number(BigInt(value) / 10n ** BigInt(token.decimals)), which I realized was just going to create much worse issues!

Copy link
Contributor

Choose a reason for hiding this comment

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

True. Unless we have something like a BigRational indeed the best option would be to use floats here. Doesn't look like ethers has something like that.
Also IIRC we end up getting quotes for all these tokens which should be more accurate anyway so the worst thing that could happen is that some of the tokens should get drained but doesn't. But then it will probably be drained the next time if the buffers keep accumulating that token.

src/tasks/withdraw/token_balances.ts Outdated Show resolved Hide resolved
Base automatically changed from self-sell-arbitrary-tokens to main April 16, 2024 08:48
@fedgiac fedgiac force-pushed the self-sell-on-xdai branch from 274d433 to def777c Compare April 16, 2024 08:59
@coveralls
Copy link

Pull Request Test Coverage Report for Build 8702820726

Details

  • 0 of 0 changed or added relevant lines in 0 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage remained the same at 97.398%

Totals Coverage Status
Change from base Build 8702691843: 0.0%
Covered Lines: 364
Relevant Lines: 366

💛 - Coveralls

@fedgiac fedgiac merged commit 45a0792 into main Apr 16, 2024
5 checks passed
@fedgiac fedgiac deleted the self-sell-on-xdai branch April 16, 2024 09:48
@github-actions github-actions bot locked and limited conversation to collaborators Apr 16, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants