Releases: joshstevens19/simple-uniswap-sdk
upport custom route methods && uniswap v3 multi hop
What's Changed
- Support custom route methods && uniswap v3 multi hop by @liu-zhipeng in #45
New Contributors
- @liu-zhipeng made their first contribution in #45
Full Changelog: 3.6.2...3.7.0
Fix bug with custom network + base currencies
- routeText was still saying ETH instead of native token thanks @vm06007 for raising
- allow custom networks to define their own base currencies to allow multihops
Fix some dependencies
3.6.1 move ethereum-abi-types-generator to `dependencies`
Define a custom network which is not supported
Ability to define a custom network, allowing support for any EVM network uniswap fork.. example:
// Generate Uniswap pair
const pair = new UniswapPair({
fromTokenContractAddress: "0x0d500b1d8e8ef31e21c99d1db9a6444d3adf1270",
toTokenContractAddress: desiredTokenAddress,
ethereumAddress: this.wallet.address,
providerUrl: this.rpc,
chainId: 137,
settings: new UniswapPairSettings({
slippage: this.slippage, // Slippage config
deadlineMinutes: 5, // 5m max execution deadline
disableMultihops: false, // Allow multihops
uniswapVersions: [UniswapVersion.v2], // Only V2
cloneUniswapContractDetails: {
v2Override: {
routerAddress: "0x1b02da8cb0d097eb8d57a175b88c7d8b47997506",
factoryAddress: "0xc35dadb65012ec5796536bd9864ed8773abc74c4",
pairAddress: "0xc35dadb65012ec5796536bd9864ed8773abc74c4"
}
// v3Override exists here as well!
},
customNetwork: {
nameNetwork: "polygon",
multicallContractAddress:
"0x275617327c958bD06b5D6b871E7f491D76113dd8",
nativeCurrency: {
name: "Matic Token",
symbol: "MATIC"
},
nativeWrappedTokenInfo: {
chainId: 137,
contractAddress: "0x0d500b1d8e8ef31e21c99d1db9a6444d3adf1270",
decimals: 18,
symbol: "WMATIC",
name: "Wrapped Matic"
},
// can define your base tokens here if any!
baseTokens: {
usdt: {
chainId: 137,
contractAddress: 'CONTRACT_ADDRESS',
decimals: 18,
symbol: 'USDT',
name: 'Tether USD',
};
// dai...
// comp...
// usdc...
// wbtc...
};
}
})
});
Also thanks for the eth donations for this feature allowing it to be fast tracked - you can thank @vm06007 for that!
Support ability to override uniswap contract address
fix: resolve issue with v3 not unwrapped when using native eth
Thanks for @vm06007 for spotting this issue:
when you swapped from token > eth it would give you weth everytime and not unwrap it for you. This is now fixed.
Handle tokens which do not conform to the erc20 spec
Had a few cases raised about tokens that return bytes over string or uint256 over uint8 for name decimals etc.. we have now brought in override token info which right now holds MKR and dForce written so it can easily add others if cases are raised.
We now support ability to factor tx cost into the quotes
A lovely feature has been added:
Including gas fees in the trade response
The library has the ability to work out the best trade including gas fees. As expected this does add around about 700MS onto the response time due to the need to have to query eth_estimateGas
an the top 3 quotes to work out the best result. How it works is:
- It gets the best expected trade quotes as it normally does
- IF you do not have enough balance or enough allowance it will not estimate gas because it be a always failing transaction and the node will throw an error.
- ALSO IF the token your swapping does not have a fiat price in coin gecko then again it ignores the below as it can not do the math without a base currency.
- IF you have enough balance and allowance then finds the best 3 of the different hop options:
- best direct trade aka
ETH/TOKEN > TOKEN_YOU_WANT
- best trade which jumps 2 hops aka
ETH/TOKEN > TOKEN > TOKEN_YOU_WANT
- beat trade which jumps 3 hops aka
ETH/TOKEN > TOKEN > OTHER_TOKEN > TOKEN_YOU_WANT
- best direct trade aka
- It then
eth_estimateGas
those 3 transactions and takes off the tx fee from the expected quote - It then returns the trade which is the highest left amount, meaning it has taken into consideration gas within the quote
Do not worry if you want to use this feature but worried that first time customers before they approve ability for uniswap to move the tokens will not be able to benefit from this, as soon as you have approved uniswap to be able to move tokens on their behalf a new trade will be emitted within the quoteChanged$
stream so you can still get all the benefit on first time swaps.
The beauty of this is its very easy to setup just pass in a gasSettings
object including a getGasPrice
async function (IT MUST BE A PROMISE) which returns the gas price in Gwei
which you want to use for the working out. This must be a string number aka 30
= 30 Gwei
it does not handle passing in hex strings. This can be dynamic aka we call this everytime we go and work out the trades, so if you want this to hit an API or etherscan or return a fixed gas price, its completely up to you.
import {
ChainId,
TradeContext,
UniswapPair,
UniswapPairSettings,
} from 'simple-uniswap-sdk';
const uniswapPair = new UniswapPair({
// the contract address of the token you want to convert FROM
fromTokenContractAddress: '0x419D0d8BdD9aF5e606Ae2232ed285Aff190E711b',
// the contract address of the token you want to convert TO
toTokenContractAddress: '0x1985365e9f78359a9B6AD760e32412f4a445E862',
// the ethereum address of the user using this part of the dApp
ethereumAddress: '0xB1E6079212888f0bE0cf55874B2EB9d7a5e02cD9',
// you can pass in the provider url as well if you want
// providerUrl: YOUR_PROVIDER_URL,
// OR if you want to inject your own ethereum provider (no need for chainId if so)
// ethereumProvider: YOUR_WEB3_ETHERS_OR_CUSTOM_ETHEREUM_PROVIDER,
chainId: ChainId.RINKEBY,
settings: new UniswapPairSettings({
gasSettings: {
getGasPrice: async () => {
return 'GWEI_GAS_PRICE';
},
},
}),
});
That's it now you get trades which bring you back the best trades minus the tx cost.
Circular reference
Sorry about all the releases I have another package that is ready to ship and is dependent on this. Anyway, on using this package with a vue app it highlighted I had an import that went round in circles. This fixes it.
oops! remove random console.log
As it says above 🗡️