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

How to use native currency on custom network #40

Closed niZmosis closed 2 years ago

niZmosis commented 2 years ago

Using ETH.MAINNET().contractAddress works fine for eth, and I am trying to reproduce that for Polygon and BSC. I see it prints out WETHs address with a suffix _ETH. I tried to make my input token the same way for Polygon 0x0d500B1d8E8eF31E21C99d1Db9A6444d3ADf1270_MATIC and it didn't work. My config is below, I have the nativeCurrency setup for the customNetwork. I have swaped tokens and everything so I know it works, I just don't know how to make the Native Coin of each chain as an input.

A few off topic questions. What does the baseTokens parameter do? How do I access them? Does the baseTokens have anything to do with the NativeCurrency?

Is there anyway I can swap using my wallet, but have the swap send the output to a different wallet?

Thanks

`const config = { name: 'Polygon Mainnet', nativeCurrency: { name: 'Polygon', symbol: 'MATIC', decimals: 18, logoUrl: [tokenImageUrlForContract({ tokenContractChainId: maticMainChainId })] }, nativeWrappedTokenInfo: { chainId: maticMainChainId, contractAddress: '0x0d500B1d8E8eF31E21C99d1Db9A6444d3ADf1270', decimals: 18, symbol: 'WMATIC', name: 'Wrapped MATIC' }, multicallContractAddress: '0x275617327c958bD06b5D6b871E7f491D76113dd8', defaultPairs: { inputAddress: '0x0d500B1d8E8eF31E21C99d1Db9A6444d3ADf1270', outputAddress: '0x8f3cf7ad23cd3cadbd9735aff958023239c6a063' }, uniswapVersions: [UniswapVersion.v3], cloneUniswapContractDetails: { v3Override: { routerAddress: '0x68b3465833fb72A70ecDF485E0e4C7bD8665Fc45', factoryAddress: '0x1f98431c8ad98523631ae4a59f267346ea31f984', quoterAddress: '0xb27308f9f90d607463bb33ea1bebb41c27ce5ab6' } } }

async function quote({ chainId, library, account, fromTokenContractAddress, toTokenContractAddress, slippage, deadlineMinutes, tradeAmount, tradeDirection }) { try { const { name, nativeCurrency, nativeWrappedTokenInfo, multicallContractAddress, uniswapVersions, cloneUniswapContractDetails } = config

    let settings = new UniswapPairSettings({
        slippage,
        deadlineMinutes,
        disableMultihops: false,
        uniswapVersions,
        gasSettings: {
            getGasPrice: async () => {
                // TODO update
                const gasPrice = await library.getGasPrice()
                return formatUnits(gasPrice, 'gwei')
            }
        }
    })

    if (!ethChains.includes(chainId)) {
        settings = {
            ...settings,
            cloneUniswapContractDetails,
            customNetwork: {
                nameNetwork: name,
                multicallContractAddress,
                nativeCurrency,
                nativeWrappedTokenInfo
            }
        }
    }

    const uniswapPair = new UniswapPair({
        fromTokenContractAddress,
        toTokenContractAddress,
        ethereumAddress: account,
        ethereumProvider: library,
        settings
    })

    const uniswapPairFactory = await uniswapPair.createFactory()

    const trade = await uniswapPairFactory.trade(tradeAmount, tradeDirection)

    //.......
} catch (error) {
    throw error
}

} `

niZmosis commented 2 years ago

Some one must know how to get this to work

joshstevens19 commented 2 years ago

Sorry been super busy.. il look into this tomorrow for you and come back with an answer 👍

yalamber commented 2 years ago

Hello, any update on this, can we do same for local network on ganache.

niZmosis commented 2 years ago

I was over thinking it. The library is expecting the _ETH suffix no matter what chain you are on. You can use the appendEthToContractAddress(wrappedNativeCoinAddress) function to wrap the address or just append _ETH yourself.