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
189 stars 94 forks source link

sdk creates trade but transaction doesn't happen #2

Closed hmtkvs closed 3 years ago

hmtkvs commented 3 years ago

it seems to retrieve tokens successfully and my balance from kovan testnet. but I can't see the transaction in my metamask wallet.

image

Is there an additional function that I need to use to confirm the transaction?

joshstevens19 commented 3 years ago

Hey yes you need to send the transaction using web3 or ethers have you used those frameworks before? Within the trade it has a transaction object which you need to send to execute the trade. Also you need to make sure the approvalTransaction if defined is set first to be able to approve uniswap to move your tokens on your behalf. I was thinking maybe in the readme if I did an example of how you could do this with web3 and ethers it may help people understand a bit more.

joshstevens19 commented 3 years ago

This isn’t a bug so will close the issue as your not executing the trade using your own web3 or ethers instance. Can carry on the chat and I can help you understand what your missing. This has already highlight the need for web3 and ethers examples in readme.

hmtkvs commented 3 years ago

Thanks a lot for your quick reply and your efforts on creating this module Josh. You are right this is not really a bug, I am just willing for discovering this sdk. I am using ethers.js instance and what I want to do is swap tokens like swapExactTokensForTokens contract of uniswap. This code below doesn't raise an error but it also doesn't do the job.

const { UniswapPair, ChainId, UniswapVersion, UniswapPairSettings, TokenFactoryPublic } = require('simple-uniswap-sdk'); const ethers = require('ethers');

const uniswapPair = new UniswapPair({ // the contract address of the token you want to convert FROM fromTokenContractAddress: '0x4f96fe3b7a6cf9725f59d353f723c1bdb64ca6aa', // the contract address of the token you want to convert TO toTokenContractAddress: '0x1f9840a85d5aF5bf1D1762F925BDADdC4201F984', // the ethereum address of the user using this part of the dApp ethereumAddress: '0xe1878181fBb2B38492a06246875d507895Eb5bb7', chainId: ChainId.KOVAN, providerUrl: 'https://kovan.infura.io/v3/INFURA_ID', settings: new UniswapPairSettings({ slippage: 0.005, deadlineMinutes: 20, disableMultihops: false, uniswapVersions: [UniswapVersion.v2, UniswapVersion.v3], }), });

// now to create the factory you just do async function calis(){

const uniswapPairFactory = await uniswapPair.createFactory(); const trade = await uniswapPairFactory.trade('0.00005');

const provider = new ethers.providers.JsonRpcProvider('https://kovan.infura.io/v3/INFURA_ID'); const wallet1 = new ethers.Wallet(PRIVATE_KEY, provider);

trade.transaction.gasLimit = 600000; trade.transaction.gasPrice = ethers.utils.parseUnits(10, 'gwei'); console.log(trade);

wallet1.sendTransaction(tx);

trade.destroy(); };

calis();

joshstevens19 commented 3 years ago

Hey do you get any errors? wallet1.sendTransaction is a promise so you should await it to not swallow any errors? ethers normally tell you what is wrong? I will extend the docs to include ethers + web3 examples of usage but your code looks fine. This looks more of an issue with your ethers awaiting stuff. If you give me some more error details il happy to look at you.

joshstevens19 commented 3 years ago

you also need to make sure you check that uniswap can move your tokens on your when using erc20 > eth/erc20! il update the readme with full examples which hopefully makes it clearer!

joshstevens19 commented 3 years ago

I have added some docs inline with ethers and web3 showing how you would hook it in - https://github.com/uniswap-integration/simple-uniswap-sdk#ethers-example hope this clear any issues up!

hmtkvs commented 3 years ago

Hey thanks a lot for adding this example into documentation. I am using your setup on Rinkeby provider forked by ganache-cli. Trade function raises error even tough I am using await, as you have suggested.

image

joshstevens19 commented 3 years ago

Can you remove your provider url and see if it works. I’m not sure if ganache rinkeby local had the same uniswap contract addresses