From 593199f072be1cdc5f29152da56f4a7b13094a58 Mon Sep 17 00:00:00 2001 From: baroooo Date: Mon, 17 Feb 2025 17:44:26 +0100 Subject: [PATCH] Release/v2.3.6 (#193) ### Description _A few sentences describing the overall effects and goals of the pull request's commits. What is the current behavior, and what is the updated/expected behavior with this PR?_ ### Other changes _Describe any minor or "drive-by" changes here._ ### Tested _An explanation of how the changes were tested or an explanation as to why they don't need to be._ ### Related issues - Fixes #issue number here ### Checklist before requesting a review - [ ] I have performed a self-review of my own code - [ ] I have added tests that prove my fix is effective or that my feature works - [ ] The PR title follows the [conventions](https://www.notion.so/Git-Branching-and-Commit-Message-Conventions-18f66f7d06444cfcbac5725ffbc7c04a?pvs=4#9355048863c549ef92fe210a8a1298aa) - [ ] I have run the [regression tests](https://www.notion.so/Mento-Web-App-Regression-Tests-37bd43a7da8d4e38b65993320a33d557) --------- Signed-off-by: dependabot[bot] Co-authored-by: Ryan Noble Co-authored-by: Bayological <6872903+bayological@users.noreply.github.com> Co-authored-by: Gabriel Temsten <57184013+gabrieltemtsen@users.noreply.github.com> Co-authored-by: Andrew718PLTS Co-authored-by: Andrew718PLTS Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Fraol Bereket <81419937+fraolb@users.noreply.github.com> Co-authored-by: chapati Co-authored-by: philbow61 <80156619+philbow61@users.noreply.github.com> Co-authored-by: boqdan <304771+bowd@users.noreply.github.com> --- next-env.d.ts | 2 +- package.json | 4 +- src/config/exchanges.ts | 391 --------------------- src/config/tokens.ts | 19 +- src/features/sdk.ts | 24 +- src/features/swap/SwapConfirm.tsx | 8 +- src/features/swap/useAllowance.tsx | 35 +- src/features/swap/useApproveTransaction.ts | 14 +- src/features/swap/useFormValidator.ts | 10 +- src/features/swap/useSwapQuote.ts | 11 +- src/features/swap/useSwapTransaction.ts | 18 +- yarn.lock | 38 +- 12 files changed, 117 insertions(+), 457 deletions(-) delete mode 100644 src/config/exchanges.ts diff --git a/next-env.d.ts b/next-env.d.ts index 4f11a03..a4a7b3f 100644 --- a/next-env.d.ts +++ b/next-env.d.ts @@ -2,4 +2,4 @@ /// // NOTE: This file should not be edited -// see https://nextjs.org/docs/basic-features/typescript for more information. +// see https://nextjs.org/docs/pages/building-your-application/configuring/typescript for more information. diff --git a/package.json b/package.json index 25f0a9a..0f0ce50 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@mento-protocol/mento-web", - "version": "2.3.5", + "version": "2.3.6", "description": "A simple DApp for Celo Mento exchanges", "keywords": [ "Celo", @@ -29,7 +29,7 @@ "@celo/rainbowkit-celo": "^0.11.0", "@headlessui-float/react": "^0.11.2", "@headlessui/react": "^1.7.13", - "@mento-protocol/mento-sdk": "^0.1.5", + "@mento-protocol/mento-sdk": "^1.0.2", "@metamask/jazzicon": "https://github.com/jmrossy/jazzicon#7a8df28", "@metamask/post-message-stream": "6.1.2", "@metamask/providers": "10.2.1", diff --git a/src/config/exchanges.ts b/src/config/exchanges.ts deleted file mode 100644 index 210810e..0000000 --- a/src/config/exchanges.ts +++ /dev/null @@ -1,391 +0,0 @@ -import { Exchange } from '@mento-protocol/mento-sdk' -import { ChainId } from 'src/config/chains' - -export const BrokerAddresses: Record = { - [ChainId.Alfajores]: '0xD3Dff18E465bCa6241A244144765b4421Ac14D09', - [ChainId.Baklava]: '0x6723749339e320E1EFcd9f1B0D997ecb45587208', - [ChainId.Celo]: '0x777A8255cA72412f0d706dc03C9D1987306B4CaD', -} - -export const AlfajoresExchanges: Exchange[] = [ - { - providerAddr: '0x9B64E8EaBD1a035b148cE970d3319c5C3Ad53EC3', - id: '0x3135b662c38265d0655177091f1b647b4fef511103d06c016efdf18b46930d2c', - assets: [ - '0x874069Fa1Eb16D44d622F2e0Ca25eeA172369bC1', - '0xF194afDf50B03e69Bd7D057c1Aa9e10c9954E4C9', - ], - }, - { - providerAddr: '0x9B64E8EaBD1a035b148cE970d3319c5C3Ad53EC3', - id: '0xb73ffc6b5123de3c8e460490543ab93a3be7d70824f1666343df49e219199b8c', - assets: [ - '0x10c892A6EC43a53E45D0B916B4b7D383B1b78C0F', - '0xF194afDf50B03e69Bd7D057c1Aa9e10c9954E4C9', - ], - }, - { - providerAddr: '0x9B64E8EaBD1a035b148cE970d3319c5C3Ad53EC3', - id: '0xed0528e42b9ecae538aab34b93813e08de03f8ac4a894b277ef193e67275bbae', - assets: [ - '0xE4D517785D091D3c54818832dB6094bcc2744545', - '0xF194afDf50B03e69Bd7D057c1Aa9e10c9954E4C9', - ], - }, - { - providerAddr: '0x9B64E8EaBD1a035b148cE970d3319c5C3Ad53EC3', - id: '0xe8693b17c0f002f6a2fe839525557cef10dfeacef9e16c9bbdcb01c57933ce58', - assets: [ - '0xE4D517785D091D3c54818832dB6094bcc2744545', - '0x2F25deB3848C207fc8E0c34035B3Ba7fC157602B', - ], - }, - { - providerAddr: '0x9B64E8EaBD1a035b148cE970d3319c5C3Ad53EC3', - id: '0x3e6d9109df536ba3f4c166e598bdfe132dca06573a54ca40c2b6f23ac6bd6cc6', - assets: [ - '0x10c892A6EC43a53E45D0B916B4b7D383B1b78C0F', - '0x87D61dA3d668797786D73BC674F053f87111570d', - ], - }, - { - providerAddr: '0x9B64E8EaBD1a035b148cE970d3319c5C3Ad53EC3', - id: '0xcfaa6be9334ee54fda94f2cfdf4c8bc376f24ce008ab9559b2a06b9fc388e78c', - assets: [ - '0xE4D517785D091D3c54818832dB6094bcc2744545', - '0x87D61dA3d668797786D73BC674F053f87111570d', - ], - }, - { - providerAddr: '0x9B64E8EaBD1a035b148cE970d3319c5C3Ad53EC3', - id: '0xe807b1ebe8b57ac4e5c1b8d51fcf8e3b21e919fd788bab807886c4f446a74d37', - assets: [ - '0x10c892A6EC43a53E45D0B916B4b7D383B1b78C0F', - '0x6e673502c5b55F3169657C004e5797fFE5be6653', - ], - }, - { - providerAddr: '0x9B64E8EaBD1a035b148cE970d3319c5C3Ad53EC3', - id: '0x269dcbdbc07fff1a4aaab9c7c03b3f629cd9bbed49aa0efebab874e4da1ffd07', - assets: [ - '0xB0FA15e002516d0301884059c0aaC0F0C72b019D', - '0xF194afDf50B03e69Bd7D057c1Aa9e10c9954E4C9', - ], - }, - { - providerAddr: '0x9B64E8EaBD1a035b148cE970d3319c5C3Ad53EC3', - id: '0x66c5917862c8dc589e789d43752aa17ad251126276ce88ea20d89865e67bdabe', - assets: [ - '0xB0FA15e002516d0301884059c0aaC0F0C72b019D', - '0x6e673502c5b55F3169657C004e5797fFE5be6653', - ], - }, - { - providerAddr: '0x9B64E8EaBD1a035b148cE970d3319c5C3Ad53EC3', - id: '0x89de88b8eb790de26f4649f543cb6893d93635c728ac857f0926e842fb0d298b', - assets: [ - '0x874069Fa1Eb16D44d622F2e0Ca25eeA172369bC1', - '0x1E0433C1769271ECcF4CFF9FDdD515eefE6CdF92', - ], - }, - { - providerAddr: '0x9B64E8EaBD1a035b148cE970d3319c5C3Ad53EC3', - id: '0x99be8b8341ba00914600cda701568ab27eea9aca7a32fa48c26e07b86841020c', - assets: [ - '0x10c892A6EC43a53E45D0B916B4b7D383B1b78C0F', - '0x2F25deB3848C207fc8E0c34035B3Ba7fC157602B', - ], - }, - { - providerAddr: '0x9B64E8EaBD1a035b148cE970d3319c5C3Ad53EC3', - id: '0xacc988382b66ee5456086643dcfd9a5ca43dd8f428f6ef22503d8b8013bcffd7', - assets: [ - '0x874069Fa1Eb16D44d622F2e0Ca25eeA172369bC1', - '0x2F25deB3848C207fc8E0c34035B3Ba7fC157602B', - ], - }, - { - providerAddr: '0x9B64E8EaBD1a035b148cE970d3319c5C3Ad53EC3', - id: '0xf77561650ba043a244ae9c58f778c141532c4afdb7cae5e6fd623b565c5584a0', - assets: [ - '0x874069Fa1Eb16D44d622F2e0Ca25eeA172369bC1', - '0x87D61dA3d668797786D73BC674F053f87111570d', - ], - }, - { - providerAddr: '0x9B64E8EaBD1a035b148cE970d3319c5C3Ad53EC3', - id: '0x36e12d20b7a59b463a1a113caf338cbad9d64ee017548d00a791681f2fbf0763', - assets: [ - '0x874069Fa1Eb16D44d622F2e0Ca25eeA172369bC1', - '0xBba91F588d031469ABCCA566FE80fB1Ad8Ee3287', - ], - }, - { - providerAddr: '0x9B64E8EaBD1a035b148cE970d3319c5C3Ad53EC3', - id: '0x7952984d7278ca3417febf52815c321984ac3147ced2c02bb6a02b0bcab08413', - assets: [ - '0x874069Fa1Eb16D44d622F2e0Ca25eeA172369bC1', - '0x5E0E3c9419C42a1B04e2525991FB1A2C467AB8bF', - ], - }, - { - providerAddr: '0x9B64E8EaBD1a035b148cE970d3319c5C3Ad53EC3', - id: '0x1c9378bd0973ff313a599d3effc654ba759f8ccca655ab6d6ce5bd39a212943b', - assets: [ - '0x874069Fa1Eb16D44d622F2e0Ca25eeA172369bC1', - '0xe6A57340f0df6E020c1c0a80bC6E13048601f0d4', - ], - }, -] - -export const BaklavaExchanges: Exchange[] = [ - { - providerAddr: '0xFF9a3da00F42839CD6D33AD7adf50bCc97B41411', - id: '0x3135b662c38265d0655177091f1b647b4fef511103d06c016efdf18b46930d2c', - assets: [ - '0x62492A644A588FD904270BeD06ad52B9abfEA1aE', - '0xdDc9bE57f553fe75752D61606B94CBD7e0264eF8', - ], - }, - { - providerAddr: '0xFF9a3da00F42839CD6D33AD7adf50bCc97B41411', - id: '0xb73ffc6b5123de3c8e460490543ab93a3be7d70824f1666343df49e219199b8c', - assets: [ - '0xf9ecE301247aD2CE21894941830A2470f4E774ca', - '0xdDc9bE57f553fe75752D61606B94CBD7e0264eF8', - ], - }, - { - providerAddr: '0xFF9a3da00F42839CD6D33AD7adf50bCc97B41411', - id: '0xed0528e42b9ecae538aab34b93813e08de03f8ac4a894b277ef193e67275bbae', - assets: [ - '0x6a0EEf2bed4C30Dc2CB42fe6c5f01F80f7EF16d1', - '0xdDc9bE57f553fe75752D61606B94CBD7e0264eF8', - ], - }, - { - providerAddr: '0xFF9a3da00F42839CD6D33AD7adf50bCc97B41411', - id: '0xbfd96ed0ed5098d2b2bff8b9d8423dc47001fccf2391ee6e532618ef79c12075', - assets: [ - '0x6a0EEf2bed4C30Dc2CB42fe6c5f01F80f7EF16d1', - '0xB407D37d76c417B6343310D42611FCA106B2abB8', - ], - }, - { - providerAddr: '0xFF9a3da00F42839CD6D33AD7adf50bCc97B41411', - id: '0x3e6d9109df536ba3f4c166e598bdfe132dca06573a54ca40c2b6f23ac6bd6cc6', - assets: [ - '0xf9ecE301247aD2CE21894941830A2470f4E774ca', - '0xD4079B322c392D6b196f90AA4c439fC2C16d6770', - ], - }, - { - providerAddr: '0xFF9a3da00F42839CD6D33AD7adf50bCc97B41411', - id: '0xcfaa6be9334ee54fda94f2cfdf4c8bc376f24ce008ab9559b2a06b9fc388e78c', - assets: [ - '0x6a0EEf2bed4C30Dc2CB42fe6c5f01F80f7EF16d1', - '0xD4079B322c392D6b196f90AA4c439fC2C16d6770', - ], - }, - { - providerAddr: '0xFF9a3da00F42839CD6D33AD7adf50bCc97B41411', - id: '0xe807b1ebe8b57ac4e5c1b8d51fcf8e3b21e919fd788bab807886c4f446a74d37', - assets: [ - '0xf9ecE301247aD2CE21894941830A2470f4E774ca', - '0x6f90ac394b1F45290d3023e4Ba0203005cAF2A4B', - ], - }, - { - providerAddr: '0xFF9a3da00F42839CD6D33AD7adf50bCc97B41411', - id: '0x269dcbdbc07fff1a4aaab9c7c03b3f629cd9bbed49aa0efebab874e4da1ffd07', - assets: [ - '0x64c1D812673E93Bc036AdC3D547d9950696DA5Af', - '0xdDc9bE57f553fe75752D61606B94CBD7e0264eF8', - ], - }, - { - providerAddr: '0xFF9a3da00F42839CD6D33AD7adf50bCc97B41411', - id: '0x66c5917862c8dc589e789d43752aa17ad251126276ce88ea20d89865e67bdabe', - assets: [ - '0x64c1D812673E93Bc036AdC3D547d9950696DA5Af', - '0x6f90ac394b1F45290d3023e4Ba0203005cAF2A4B', - ], - }, - { - providerAddr: '0xFF9a3da00F42839CD6D33AD7adf50bCc97B41411', - id: '0x89de88b8eb790de26f4649f543cb6893d93635c728ac857f0926e842fb0d298b', - assets: [ - '0x62492A644A588FD904270BeD06ad52B9abfEA1aE', - '0x8813Ae180017057d0Cf98C930cED1E7101B97370', - ], - }, - { - providerAddr: '0xFF9a3da00F42839CD6D33AD7adf50bCc97B41411', - id: '0x3839dd748e34fa87f5cd928e10d3e0ecafd9230ac9932c6cb2b0d5a358b72618', - assets: [ - '0xf9ecE301247aD2CE21894941830A2470f4E774ca', - '0xB407D37d76c417B6343310D42611FCA106B2abB8', - ], - }, - { - providerAddr: '0xFF9a3da00F42839CD6D33AD7adf50bCc97B41411', - id: '0xd8573fd7c4151f38cde9b2349a26a6bb03be4b3791105bc625731ad2d71c54ba', - assets: [ - '0x62492A644A588FD904270BeD06ad52B9abfEA1aE', - '0xB407D37d76c417B6343310D42611FCA106B2abB8', - ], - }, - { - providerAddr: '0xFF9a3da00F42839CD6D33AD7adf50bCc97B41411', - id: '0xf77561650ba043a244ae9c58f778c141532c4afdb7cae5e6fd623b565c5584a0', - assets: [ - '0x62492A644A588FD904270BeD06ad52B9abfEA1aE', - '0xD4079B322c392D6b196f90AA4c439fC2C16d6770', - ], - }, - { - providerAddr: '0xFF9a3da00F42839CD6D33AD7adf50bCc97B41411', - id: '0x36e12d20b7a59b463a1a113caf338cbad9d64ee017548d00a791681f2fbf0763', - assets: [ - '0x62492A644A588FD904270BeD06ad52B9abfEA1aE', - '0x27c586469038A1749B27BF5914DAff7A14227AfB', - ], - }, -] - -export const CeloExchanges: Exchange[] = [ - { - providerAddr: '0x22d9db95E6Ae61c104A7B6F6C78D7993B94ec901', - id: '0x3135b662c38265d0655177091f1b647b4fef511103d06c016efdf18b46930d2c', - assets: [ - '0x765DE816845861e75A25fCA122bb6898B8B1282a', - '0x471EcE3750Da237f93B8E339c536989b8978a438', - ], - }, - { - providerAddr: '0x22d9db95E6Ae61c104A7B6F6C78D7993B94ec901', - id: '0xb73ffc6b5123de3c8e460490543ab93a3be7d70824f1666343df49e219199b8c', - assets: [ - '0xD8763CBa276a3738E6DE85b4b3bF5FDed6D6cA73', - '0x471EcE3750Da237f93B8E339c536989b8978a438', - ], - }, - { - providerAddr: '0x22d9db95E6Ae61c104A7B6F6C78D7993B94ec901', - id: '0xed0528e42b9ecae538aab34b93813e08de03f8ac4a894b277ef193e67275bbae', - assets: [ - '0xe8537a3d056DA446677B9E9d6c5dB704EaAb4787', - '0x471EcE3750Da237f93B8E339c536989b8978a438', - ], - }, - { - providerAddr: '0x22d9db95E6Ae61c104A7B6F6C78D7993B94ec901', - id: '0xe8693b17c0f002f6a2fe839525557cef10dfeacef9e16c9bbdcb01c57933ce58', - assets: [ - '0xe8537a3d056DA446677B9E9d6c5dB704EaAb4787', - '0xcebA9300f2b948710d2653dD7B07f33A8B32118C', - ], - }, - { - providerAddr: '0x22d9db95E6Ae61c104A7B6F6C78D7993B94ec901', - id: '0xf418803158d881fda22694067bf6479476cec22ecfeeca2f6a65a6259bdbb9c0', - assets: [ - '0xD8763CBa276a3738E6DE85b4b3bF5FDed6D6cA73', - '0xEB466342C4d449BC9f53A865D5Cb90586f405215', - ], - }, - { - providerAddr: '0x22d9db95E6Ae61c104A7B6F6C78D7993B94ec901', - id: '0x40c8472edd23f2976b0503db2692e8f06f0eb52db690e84697cad36a6b44e2df', - assets: [ - '0xe8537a3d056DA446677B9E9d6c5dB704EaAb4787', - '0xEB466342C4d449BC9f53A865D5Cb90586f405215', - ], - }, - { - providerAddr: '0x22d9db95E6Ae61c104A7B6F6C78D7993B94ec901', - id: '0xfca6d94b46122eb9a4b86cf9d3e1e856fea8a826d0fc26c5baf17c43fbaf0f48', - assets: [ - '0xD8763CBa276a3738E6DE85b4b3bF5FDed6D6cA73', - '0x061cc5a2C863E0C1Cb404006D559dB18A34C762d', - ], - }, - { - providerAddr: '0x22d9db95E6Ae61c104A7B6F6C78D7993B94ec901', - id: '0x269dcbdbc07fff1a4aaab9c7c03b3f629cd9bbed49aa0efebab874e4da1ffd07', - assets: [ - '0x73F93dcc49cB8A239e2032663e9475dd5ef29A08', - '0x471EcE3750Da237f93B8E339c536989b8978a438', - ], - }, - { - providerAddr: '0x22d9db95E6Ae61c104A7B6F6C78D7993B94ec901', - id: '0xcc68743c58a31c4ec3c56bca3d579409b4e2424e5f37e54a85f917b22af74e7c', - assets: [ - '0x73F93dcc49cB8A239e2032663e9475dd5ef29A08', - '0x061cc5a2C863E0C1Cb404006D559dB18A34C762d', - ], - }, - { - providerAddr: '0x22d9db95E6Ae61c104A7B6F6C78D7993B94ec901', - id: '0x89de88b8eb790de26f4649f543cb6893d93635c728ac857f0926e842fb0d298b', - assets: [ - '0x765DE816845861e75A25fCA122bb6898B8B1282a', - '0x456a3D042C0DbD3db53D5489e98dFb038553B0d0', - ], - }, - { - providerAddr: '0x22d9db95E6Ae61c104A7B6F6C78D7993B94ec901', - id: '0x99be8b8341ba00914600cda701568ab27eea9aca7a32fa48c26e07b86841020c', - assets: [ - '0xD8763CBa276a3738E6DE85b4b3bF5FDed6D6cA73', - '0xcebA9300f2b948710d2653dD7B07f33A8B32118C', - ], - }, - { - providerAddr: '0x22d9db95E6Ae61c104A7B6F6C78D7993B94ec901', - id: '0xacc988382b66ee5456086643dcfd9a5ca43dd8f428f6ef22503d8b8013bcffd7', - assets: [ - '0x765DE816845861e75A25fCA122bb6898B8B1282a', - '0xcebA9300f2b948710d2653dD7B07f33A8B32118C', - ], - }, - { - providerAddr: '0x22d9db95E6Ae61c104A7B6F6C78D7993B94ec901', - id: '0x0d739efbfc30f303e8d1976c213b4040850d1af40f174f4169b846f6fd3d2f20', - assets: [ - '0x765DE816845861e75A25fCA122bb6898B8B1282a', - '0xEB466342C4d449BC9f53A865D5Cb90586f405215', - ], - }, - { - providerAddr: '0x22d9db95E6Ae61c104A7B6F6C78D7993B94ec901', - id: '0x773bcec109cee923b5e04706044fd9d6a5121b1a6a4c059c36fdbe5b845d4e9b', - assets: [ - '0x765DE816845861e75A25fCA122bb6898B8B1282a', - '0x48065fbBE25f71C9282ddf5e1cD6D6A887483D5e', - ], - }, - { - providerAddr: '0x22d9db95E6Ae61c104A7B6F6C78D7993B94ec901', - id: '0x7952984d7278ca3417febf52815c321984ac3147ced2c02bb6a02b0bcab08413', - assets: [ - '0x765DE816845861e75A25fCA122bb6898B8B1282a', - '0x105d4A9306D2E55a71d2Eb95B81553AE1dC20d7B', - ], - }, - { - providerAddr: '0x22d9db95E6Ae61c104A7B6F6C78D7993B94ec901', - id: '0x1c9378bd0973ff313a599d3effc654ba759f8ccca655ab6d6ce5bd39a212943b', - assets: [ - '0x765DE816845861e75A25fCA122bb6898B8B1282a', - '0x8A567e2aE79CA692Bd748aB832081C45de4041eA', - ], - }, -] - -export const MentoExchanges: Record> = { - [ChainId.Alfajores]: AlfajoresExchanges, - [ChainId.Baklava]: BaklavaExchanges, - [ChainId.Celo]: CeloExchanges, -} diff --git a/src/config/tokens.ts b/src/config/tokens.ts index 44fbe77..b85f386 100644 --- a/src/config/tokens.ts +++ b/src/config/tokens.ts @@ -1,5 +1,5 @@ import { ChainId } from 'src/config/chains' -import { MentoExchanges } from 'src/config/exchanges' +import { getMentoSdk } from 'src/features/sdk' import { Color } from 'src/styles/Color' import { areAddressesEqual } from 'src/utils/addresses' @@ -190,17 +190,16 @@ export function isNativeStableToken(tokenId: string) { return NativeStableTokenIds.includes(tokenId as TokenId) } -export function isSwappable(token_1: string, token_2: string, chainId: number) { - const exchanges = MentoExchanges[chainId as ChainId] - - if (!exchanges) return false - +export async function isSwappable(token_1: string, token_2: string, chainId: number) { + const sdk = await getMentoSdk(chainId) + const tradablePairs = await sdk.getTradablePairs() + if (!tradablePairs) return false if (token_1 === token_2) return false - return exchanges.some( - (obj) => - obj.assets.includes(getTokenAddress(token_1 as TokenId, chainId)) && - obj.assets.includes(getTokenAddress(token_2 as TokenId, chainId)) + return tradablePairs.some( + (assets) => + assets.find((asset) => asset.address === getTokenAddress(token_1 as TokenId, chainId)) && + assets.find((asset) => asset.address === getTokenAddress(token_2 as TokenId, chainId)) ) } diff --git a/src/features/sdk.ts b/src/features/sdk.ts index db37eb9..312b266 100644 --- a/src/features/sdk.ts +++ b/src/features/sdk.ts @@ -1,6 +1,6 @@ -import { Mento } from '@mento-protocol/mento-sdk' +import { Mento, TradablePair } from '@mento-protocol/mento-sdk' import { ChainId } from 'src/config/chains' -import { BrokerAddresses, MentoExchanges } from 'src/config/exchanges' +import { TokenId, getTokenAddress } from 'src/config/tokens' import { getProvider } from 'src/features/providers' const cache: Record = {} @@ -9,14 +9,18 @@ export async function getMentoSdk(chainId: ChainId): Promise { if (cache[chainId]) return cache[chainId] const provider = getProvider(chainId) - const brokerAddr = BrokerAddresses[chainId] - const exchanges = MentoExchanges[chainId] - let mento: Mento - if (brokerAddr) { - mento = await Mento.createWithParams(provider, brokerAddr, exchanges) - } else { - mento = await Mento.create(provider) - } + const mento = await Mento.create(provider) cache[chainId] = mento return mento } + +export async function getTradablePairForTokens( + chainId: ChainId, + tokenInId: TokenId, + tokenOutId: TokenId +): Promise { + const sdk = await getMentoSdk(chainId) + const tokenInAddr = getTokenAddress(tokenInId, chainId) + const tokenOutAddr = getTokenAddress(tokenOutId, chainId) + return await sdk.findPairForTokens(tokenInAddr, tokenOutAddr) +} diff --git a/src/features/swap/SwapConfirm.tsx b/src/features/swap/SwapConfirm.tsx index e2aa73f..21f8be8 100644 --- a/src/features/swap/SwapConfirm.tsx +++ b/src/features/swap/SwapConfirm.tsx @@ -87,12 +87,18 @@ export function SwapConfirmCard({ formValues }: Props) { const { sendApproveTx, isApproveTxSuccess, isApproveTxLoading } = useApproveTransaction( chainId, fromTokenId, + toTokenId, approveAmount, address ) const [isApproveConfirmed, setApproveConfirmed] = useState(false) - const { allowance, isLoading: isAllowanceLoading } = useAllowance(chainId, fromTokenId, address) + const { allowance, isLoading: isAllowanceLoading } = useAllowance( + chainId, + fromTokenId, + toTokenId, + address + ) const needsApproval = !isAllowanceLoading && new BigNumber(allowance).lte(approveAmount) const skipApprove = !isAllowanceLoading && !needsApproval diff --git a/src/features/swap/useAllowance.tsx b/src/features/swap/useAllowance.tsx index 467bab6..e1c6a35 100644 --- a/src/features/swap/useAllowance.tsx +++ b/src/features/swap/useAllowance.tsx @@ -1,37 +1,54 @@ +import { getAddress } from '@mento-protocol/mento-sdk' import { useQuery } from '@tanstack/react-query' import { Contract } from 'ethers' import { ERC20_ABI } from 'src/config/consts' -import { BrokerAddresses } from 'src/config/exchanges' import { TokenId, getTokenAddress } from 'src/config/tokens' import { getProvider } from 'src/features/providers' +import { getTradablePairForTokens } from 'src/features/sdk' import { logger } from 'src/utils/logger' async function fetchAllowance( - tokenAddr: string, + fromTokenId: TokenId, + toTokenId: TokenId, accountAddress: string, chainId: number ): Promise { + const tradablePair = await getTradablePairForTokens(chainId, fromTokenId, toTokenId) + const brokerAddress = getAddress('Broker', chainId) + const routerAddress = getAddress('MentoRouter', chainId) + const tokenAddr = getTokenAddress(fromTokenId, chainId) + logger.info(`Fetching allowance for token ${tokenAddr} on chain ${chainId}`) const provider = getProvider(chainId) const contract = new Contract(tokenAddr, ERC20_ABI, provider) - const brokerAddress = BrokerAddresses[chainId as keyof typeof BrokerAddresses] - const allowance = await contract.allowance(accountAddress, brokerAddress) + let allowedContractAddr: string + if (tradablePair.path.length === 1) { + allowedContractAddr = brokerAddress + } else { + allowedContractAddr = routerAddress + } + + const allowance = await contract.allowance(accountAddress, allowedContractAddr) logger.info(`Allowance: ${allowance.toString()}`) return allowance.toString() } -export function useAllowance(chainId: number, tokenId: TokenId, accountAddress?: string) { +export function useAllowance( + chainId: number, + fromTokenId: TokenId, + toTokenId: TokenId, + accountAddress?: string +) { const { data: allowance, isLoading } = useQuery( - ['tokenAllowance', chainId, tokenId, accountAddress], + ['tokenAllowance', chainId, fromTokenId, toTokenId, accountAddress], async () => { if (!accountAddress) return '0' - const tokenAddr = getTokenAddress(tokenId, chainId) - return fetchAllowance(tokenAddr, accountAddress, chainId) + return fetchAllowance(fromTokenId, toTokenId, accountAddress, chainId) }, { retry: false, - enabled: Boolean(accountAddress && chainId && tokenId), + enabled: Boolean(accountAddress && chainId && fromTokenId && toTokenId), staleTime: 5000, // Consider allowance stale after 5 seconds } ) diff --git a/src/features/swap/useApproveTransaction.ts b/src/features/swap/useApproveTransaction.ts index 0b91a03..798e68e 100644 --- a/src/features/swap/useApproveTransaction.ts +++ b/src/features/swap/useApproveTransaction.ts @@ -3,24 +3,26 @@ import BigNumber from 'bignumber.js' import { useEffect } from 'react' import { toast } from 'react-toastify' import { TokenId, getTokenAddress } from 'src/config/tokens' -import { getMentoSdk } from 'src/features/sdk' +import { getMentoSdk, getTradablePairForTokens } from 'src/features/sdk' import { logger } from 'src/utils/logger' import { usePrepareSendTransaction, useSendTransaction } from 'wagmi' export function useApproveTransaction( chainId: number, - tokenId: TokenId, + tokenInId: TokenId, + tokenOutId: TokenId, amountInWei: string, accountAddress?: Address ) { const { error: txPrepError, data: txRequest } = useQuery( - ['useApproveTransaction', chainId, tokenId, amountInWei, accountAddress], + ['useApproveTransaction', chainId, tokenInId, tokenOutId, amountInWei, accountAddress], async () => { if (!accountAddress || new BigNumber(amountInWei).lte(0)) return null const sdk = await getMentoSdk(chainId) - const tokenAddr = getTokenAddress(tokenId, chainId) - const txRequest = await sdk.increaseTradingAllowance(tokenAddr, amountInWei) - return { ...txRequest, to: tokenAddr } + const tokenInAddr = getTokenAddress(tokenInId, chainId) + const tradablePair = await getTradablePairForTokens(chainId, tokenInId, tokenOutId) + const txRequest = await sdk.increaseTradingAllowance(tokenInAddr, amountInWei, tradablePair) + return { ...txRequest, to: tokenInAddr } }, { retry: false, diff --git a/src/features/swap/useFormValidator.ts b/src/features/swap/useFormValidator.ts index 6aac3d8..fb32700 100644 --- a/src/features/swap/useFormValidator.ts +++ b/src/features/swap/useFormValidator.ts @@ -1,9 +1,9 @@ import { FormikErrors } from 'formik' import { useCallback } from 'react' import { MIN_ROUNDED_VALUE } from 'src/config/consts' -import { Tokens, getTokenAddress, getTokenByAddress } from 'src/config/tokens' +import { Tokens, getTokenByAddress } from 'src/config/tokens' import { AccountBalances } from 'src/features/accounts/fetchBalances' -import { getMentoSdk } from 'src/features/sdk' +import { getMentoSdk, getTradablePairForTokens } from 'src/features/sdk' import { SwapFormValues } from 'src/features/swap/types' import { parseAmount, toWei } from 'src/utils/amount' import { logger } from 'src/utils/logger' @@ -42,10 +42,10 @@ async function checkTradingLimits( values: SwapFormValues, chainId: number ): Promise<{ exceeds: boolean; errorMsg: string }> { - const token0 = getTokenAddress(values.fromTokenId, chainId) - const token1 = getTokenAddress(values.toTokenId, chainId) const mento = await getMentoSdk(chainId) - const exchangeId = (await mento.getExchangeForTokens(token0, token1)).id + const tradablePair = await getTradablePairForTokens(chainId, values.fromTokenId, values.toTokenId) + // TODO: handle multiple hops + const exchangeId = tradablePair.path[0].id const tradingLimits = await mento.getTradingLimits(exchangeId) let timestampIn = 0 diff --git a/src/features/swap/useSwapQuote.ts b/src/features/swap/useSwapQuote.ts index f6842ba..d6a8e59 100644 --- a/src/features/swap/useSwapQuote.ts +++ b/src/features/swap/useSwapQuote.ts @@ -4,7 +4,7 @@ import { useEffect } from 'react' import { toast } from 'react-toastify' import { SWAP_QUOTE_REFETCH_INTERVAL } from 'src/config/consts' import { TokenId, Tokens, getTokenAddress } from 'src/config/tokens' -import { getMentoSdk } from 'src/features/sdk' +import { getMentoSdk, getTradablePairForTokens } from 'src/features/sdk' import { SwapDirection } from 'src/features/swap/types' import { calcExchangeRate, @@ -40,12 +40,17 @@ export function useSwapQuote( const fromTokenAddr = getTokenAddress(fromTokenId, chainId) const toTokenAddr = getTokenAddress(toTokenId, chainId) const mento = await getMentoSdk(chainId) + const tradablePair = await getTradablePairForTokens(chainId, fromTokenId, toTokenId) let quoteWei: string if (isSwapIn) { - quoteWei = (await mento.getAmountOut(fromTokenAddr, toTokenAddr, amountWeiBN)).toString() + quoteWei = ( + await mento.getAmountOut(fromTokenAddr, toTokenAddr, amountWeiBN, tradablePair) + ).toString() } else { - quoteWei = (await mento.getAmountIn(fromTokenAddr, toTokenAddr, amountWeiBN)).toString() + quoteWei = ( + await mento.getAmountIn(fromTokenAddr, toTokenAddr, amountWeiBN, tradablePair) + ).toString() } const quote = fromWei(quoteWei, quoteDecimals) diff --git a/src/features/swap/useSwapTransaction.ts b/src/features/swap/useSwapTransaction.ts index 3532161..baf0352 100644 --- a/src/features/swap/useSwapTransaction.ts +++ b/src/features/swap/useSwapTransaction.ts @@ -3,7 +3,7 @@ import BigNumber from 'bignumber.js' import { useEffect } from 'react' import { toast } from 'react-toastify' import { TokenId, getTokenAddress } from 'src/config/tokens' -import { getMentoSdk } from 'src/features/sdk' +import { getMentoSdk, getTradablePairForTokens } from 'src/features/sdk' import { SwapDirection } from 'src/features/swap/types' import { logger } from 'src/utils/logger' import { usePrepareSendTransaction, useSendTransaction } from 'wagmi' @@ -43,10 +43,20 @@ export function useSwapTransaction( const sdk = await getMentoSdk(chainId) const fromTokenAddr = getTokenAddress(fromToken, chainId) const toTokenAddr = getTokenAddress(toToken, chainId) - const brokerAddr = sdk.getBroker().address + const tradablePair = await getTradablePairForTokens(chainId, fromToken, toToken) const swapFn = direction === 'in' ? sdk.swapIn.bind(sdk) : sdk.swapOut.bind(sdk) - const txRequest = await swapFn(fromTokenAddr, toTokenAddr, amountInWei, thresholdAmountInWei) - return { ...txRequest, to: brokerAddr } + const txRequest = await swapFn( + fromTokenAddr, + toTokenAddr, + amountInWei, + thresholdAmountInWei, + tradablePair + ) + // This should be populated by the SDK as either broker or router, but if it's not, throw an error + if (!txRequest.to) { + throw new Error('Swap transaction to address is undefined') + } + return { ...txRequest, to: txRequest.to! } } ) diff --git a/yarn.lock b/yarn.lock index 75fb354..65dc0a7 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1885,21 +1885,22 @@ __metadata: languageName: node linkType: hard -"@mento-protocol/mento-core-ts@npm:^0.1.1": - version: 0.1.1 - resolution: "@mento-protocol/mento-core-ts@npm:0.1.1" - checksum: 336df2ae8338bc23de39327f346f29f26b2be2d335aa8ca125291a9408ba0bfac3699b17920dc269eaa988c535b0ad7bf4332da2d2947983335023239ea6f1e3 +"@mento-protocol/mento-core-ts@npm:^0.2.0": + version: 0.2.3 + resolution: "@mento-protocol/mento-core-ts@npm:0.2.3" + checksum: 334877806d44e8e2ff905f22cc6ed695b3a61b22ec0456bfa6a7b6ee6cb068492af10da62cb939e2ebe17d87b2e4610826448f57cd390390f4e409b768513a49 languageName: node linkType: hard -"@mento-protocol/mento-sdk@npm:^0.1.5": - version: 0.1.5 - resolution: "@mento-protocol/mento-sdk@npm:0.1.5" +"@mento-protocol/mento-sdk@npm:^1.0.2": + version: 1.0.2 + resolution: "@mento-protocol/mento-sdk@npm:1.0.2" dependencies: - "@mento-protocol/mento-core-ts": ^0.1.1 + "@mento-protocol/mento-core-ts": ^0.2.0 + mento-router-ts: ^0.2.0 peerDependencies: ethers: ^5.7 - checksum: 91e00199f88f5f369fca9daf699218a1d652166ca0d15e8a539662170952b53498969e23bf0e039f32251633c8e87318adc333a6617869b790ab266893e88095 + checksum: 3aa3e1798f9abaaa3a0c0362c241711e1365a82906ed8e11baedebe285a3b16524110a6fe92e39c7ad12ad66c1b4ebb83a79e308c6b210f964c1e3e4a80eac22 languageName: node linkType: hard @@ -1910,7 +1911,7 @@ __metadata: "@celo/rainbowkit-celo": ^0.11.0 "@headlessui-float/react": ^0.11.2 "@headlessui/react": ^1.7.13 - "@mento-protocol/mento-sdk": ^0.1.5 + "@mento-protocol/mento-sdk": ^1.0.2 "@metamask/jazzicon": "https://github.com/jmrossy/jazzicon#7a8df28" "@metamask/post-message-stream": 6.1.2 "@metamask/providers": 10.2.1 @@ -2996,11 +2997,11 @@ __metadata: linkType: hard "@types/react-dom@npm:^18.2.5": - version: 18.2.5 - resolution: "@types/react-dom@npm:18.2.5" - dependencies: - "@types/react": "*" - checksum: c48209f8c60cb9054f3deee5365bc9fd6dadd8f901b67f1612a334057b2671518fc5145f14aca63ff276a926ccb5358308a6cf58ec700178f382bb3ebde96d91 + version: 18.3.5 + resolution: "@types/react-dom@npm:18.3.5" + peerDependencies: + "@types/react": ^18.0.0 + checksum: 95c757684f71e761515c5a11299e5feec550c72bb52975487f360e6f0d359b26454c26eaf2ce45dd22748205aa9b2c2fe0abe7005ebcbd233a7615283ac39a7d languageName: node linkType: hard @@ -8189,6 +8190,13 @@ __metadata: languageName: node linkType: hard +"mento-router-ts@npm:^0.2.0": + version: 0.2.0 + resolution: "mento-router-ts@npm:0.2.0" + checksum: 94a01a2cd898dc20379a2961f17b3c8bb29b01cd2bb9e5efb6b5d67d0270d9e347411b89eb8afb7c6b6f272b4242a3dd21465802e78fc724067316bc7661ad61 + languageName: node + linkType: hard + "merge-stream@npm:^2.0.0": version: 2.0.0 resolution: "merge-stream@npm:2.0.0"