joshstevens19 / simple-uniswap-sdk

Uniswap SDK which handles the routes automatically for you, changes in trade quotes reactive subscriptions, exposure to formatted easy to understand information, bringing back the best trade quotes automatically, generating transactions for you and much more.
MIT License
188 stars 95 forks source link

feat: AVALANCHE support #46

Closed mburger81 closed 2 years ago

mburger81 commented 2 years ago

Hello,

can you help on configuring the UniswapPair for Avalanche Main- and Testnet?

Can you maybe share an example?

@joshstevens19 @vm06007

vm06007 commented 2 years ago

you'll need to deploy multicall contract or use the one available on that chain first, do you know the address?

mburger81 commented 2 years ago

maybe here?

https://github.com/makerdao/multicall/pull/32

mburger81 commented 2 years ago

This is my example code with just the AVALANCHE MAINNET configurations, which I'm not sure if all the addresses are right, but at the end I'm running into the issue at the bottom

private chainDetails(provider: any, chainId: any) {
    ....

    if (chainId === ChainId.AVALANCHE)
      return {
        routerAddress: '0x60aE616a2155Ee3d9A68541Ba4544862310933d4',
        factoryAddress: '0x9Ad6C38BE94206cA50bb0d90783181662f0Cfa10',
        pairAddress: '0x9Ad6C38BE94206cA50bb0d90783181662f0Cfa10'
      }

      .......
  }

  private async init(testnet: boolean,  grf: boolean) {

    const chainId = this.wallet.chainId;
    const userAddress = this.wallet.address;
    const disableMultihops = false;
    const provider = {};

    const details = this.chainDetails(
        provider,
        chainId
    );

    // const WETH_ADDRESS = details.wethAddress;
    // const DAI_ADDRESS = details.daiAddress;

    // const fromToken = findTokenStrict(details.tokensIn, fromCurrency, WETH_ADDRESS);
    // const toToken = findTokenStrict(details.tokensOut, toCurrency, DAI_ADDRESS);

    const targets = {
        v2Override: {
            routerAddress: details.routerAddress,
            factoryAddress: details.factoryAddress,
            pairAddress: details.pairAddress,
        },
    };

    let customNetworkData;
    let providerUrl;

    ........

    if (chainId === ChainId.AVALANCHE) {
      // 0xB31f66AA3C1e785363F0875A1B74E27b85FD66c7 WAVAX
      // 0xB97EF9Ef8734C71904D8002F8b6Bc66Dd9c48a6E USDC
      // 0x95923F63dA09B4f7520f7C65a31F318D8228B744 FOOK

      (providerUrl = 'https://api.avax.network/ext/bc/C/rpc'),
      (customNetworkData = {
          nameNetwork: 'Avalanche Mainnet C-Chain',
          multicallContractAddress: '0x82979a6f8D628270B29F5687bEA2F73D5D0eC77d', // ?????
          nativeCurrency: {
              name: 'AVAX Token',
              symbol: 'AVAX',
          },
          nativeWrappedTokenInfo: {
              chainId,
              contractAddress: '0xB31f66AA3C1e785363F0875A1B74E27b85FD66c7',
              decimals: 18,
              symbol: 'WAVAX',
              name: 'Wrapped AVAX',
          },
      });
    }

    this.uniswapDappSharedLogicContext  = {
        supportedNetworkTokens: [
          .......
          {
            chainId: 43114,
            defaultInputToken: '0xB31f66AA3C1e785363F0875A1B74E27b85FD66c7', // WAVAX
            defaultOutputToken: '0xB97EF9Ef8734C71904D8002F8b6Bc66Dd9c48a6E', // USDC
            supportedTokens: [
              { contractAddress: '0xB31f66AA3C1e785363F0875A1B74E27b85FD66c7' } // WAVAX
              // { contractAddress: '0xB97EF9Ef8734C71904D8002F8b6Bc66Dd9c48a6E' } // USDC
              // { contractAddress: '0x95923F63dA09B4f7520f7C65a31F318D8228B744' } // FOOK
            ]
          }
        ],
        ethereumAddress: userAddress,
        ethereumProvider: this.web3Service.provider,
        providerUrl,
        settings: new UniswapPairSettings({
            slippage: 0.01,
            deadlineMinutes: 15,
            disableMultihops,
            uniswapVersions: [UniswapVersion.v2],
            cloneUniswapContractDetails: targets,
            customNetwork: customNetworkData
        }),
    }

  }

image

vm06007 commented 2 years ago

I've checked the router you're using (https://snowtrace.io/address/0x60ae616a2155ee3d9a68541ba4544862310933d4#code) it does not look like standard implementation of Uniswap contract code, in fact it has some token wrapper and some functions have different names such as swapExactAVAXForTokens and swapTokensForExactAVAX that means it won't work with this SDK, this SDK only works with standard implementations so it expects swapExactETHForTokens and swapTokensForExactETH if contract uses different ABI these functions won't be found and thus most certainly lead to an error.

liu-zhipeng commented 2 years ago

I've checked the router you're using (https://snowtrace.io/address/0x60ae616a2155ee3d9a68541ba4544862310933d4#code) it does not look like standard implementation of Uniswap contract code, in fact it has some token wrapper and some functions have different names such as swapExactAVAXForTokens and swapTokensForExactAVAX that means it won't work with this SDK, this SDK only works with standard implementations so it expects swapExactETHForTokens and swapTokensForExactETH if contract uses different ABI these functions won't be found and thus most certainly lead to an error.

Yep! Exactly. Trader Joe and Pangolin swap have a bit different swap methods and abi as well. So to use the avalanche dex, we need customized ABI and method names.

45

I am trying to add some features to support customized ABI and method names, Uniswap V3 multihop as well in this PR. Not confirmed by @joshstevens19 yet.

liu-zhipeng commented 2 years ago
new UniswapPairSettings({
          slippage: 0.005,
          deadlineMinutes: DEFAULT_DEADLINE_MINUTES,
          disableMultihops: false,
          uniswapVersions: [UniswapVersion.v2],
          cloneUniswapContractDetails: {
            v2Override: {
              routerAddress: "0x60aE616a2155Ee3d9A68541Ba4544862310933d4",
              factoryAddress: "0x9ad6c38be94206ca50bb0d90783181662f0cfa10",
              pairAddress: "0x9ad6c38be94206ca50bb0d90783181662f0cfa10",
              routerAbi: TraderJoeRouterABI,
              routerMethods: {
                swapETHForExactTokens: "swapAVAXForExactTokens",
                swapExactETHForTokens: "swapExactAVAXForTokens",
                swapExactETHForTokensSupportingFeeOnTransferTokens:
                  "swapExactAVAXForTokensSupportingFeeOnTransferTokens",
                swapExactTokensForETH: "swapExactTokensForAVAX",
                swapExactTokensForETHSupportingFeeOnTransferTokens:
                  "swapExactTokensForAVAXSupportingFeeOnTransferTokens",
                swapExactTokensForTokens: "swapExactTokensForTokens",
                swapExactTokensForTokensSupportingFeeOnTransferTokens:
                  "swapExactTokensForTokensSupportingFeeOnTransferTokens",
                swapTokensForExactETH: "swapTokensForExactAVAX",
                swapTokensForExactTokens: "swapTokensForExactTokens",
              },
            },
          },
          customNetwork: {
            nameNetwork: "Avalanche",
            multicallContractAddress: "0xed386Fe855C1EFf2f843B910923Dd8846E45C5A4",
            nativeCurrency: {
              name: "AVAX",
              symbol: "AVAX",
            },
            nativeWrappedTokenInfo: {
              chainId: 43114,
              contractAddress: "0xB31f66AA3C1e785363F0875A1B74E27b85FD66c7",
              decimals: 18,
              symbol: "WAVAX",
              name: "Wrapped AVAX",
            },
            baseTokens: {
              usdt: {
                chainId: 43114,
                contractAddress: "0xc7198437980c041c805A1EDcbA50c1Ce5db95118",
                decimals: 6,
                symbol: "USDT",
                name: "USDT",
              },
              usdc: {
                chainId: 43114,
                contractAddress: "0xa7d7079b0fead91f3e65f86e8915cb59c1a4c664",
                decimals: 6,
                symbol: "USDC.e",
                name: "USDC.e",
              },
              wbtc: {
                chainId: 43114,
                contractAddress: "0x152b9d0fdc40c096757f570a51e494bd4b943e50",
                decimals: 8,
                symbol: "BTC.b",
                name: "BTC.b",
              },
            },
          },
        }),
liu-zhipeng commented 2 years ago

^ is the customized UniswapPairSetting. It worked with Trader Joe swap

vm06007 commented 2 years ago

cool

liu-zhipeng commented 2 years ago

cool

@vm06007 if you have a chance, could you review my changes?

45

joshstevens19 commented 2 years ago

Yes will re-review it tonight or tomorrow the main thing I seen is you turning liquidity fee turned into an array which through me off! Will review and give more details on the stuff I’m not fully sold on.. but I’m sure we can get this in just needs to be correct! @liu-zhipeng

joshstevens19 commented 2 years ago

Defo more eyes and a review from @vm06007 would be great as he knows how how v2 works as well! The main thing is breaking changes to the return interface which is the problem!

mburger81 commented 2 years ago

@joshstevens19 yeah using the new implementation is a braking change for example on the #uniswap-dapp-integration-shared lib, so if this could be avoid would be probably better. I hope you have a little bit time to have a look today and give us a solutions thx

joshstevens19 commented 2 years ago

https://github.com/joshstevens19/simple-uniswap-sdk/pull/45