Swap tokens on Uniswap V3 with code, programmatically | A beginners guide with EthersJs & JavaScript

แชร์
ฝัง
  • เผยแพร่เมื่อ 2 พ.ย. 2024

ความคิดเห็น • 148

  • @blockmancodes
    @blockmancodes  ปีที่แล้ว +3

    Learn to use all of Uniswap's swap and liquidity functionality with code. bit.ly/3JkXYqL

    • @tricx6ft
      @tricx6ft ปีที่แล้ว

      in this video in the description i see 67 dollars and i want to pay that 67 dollars is it still okay

  • @jboon4175
    @jboon4175 2 ปีที่แล้ว +4

    Yeah... you did a great job explaining this. I've now got about 10 documents from the Uniswap docs and other tutorials that can go in the bin. Much appreciated.

  • @strawbryminiwheats
    @strawbryminiwheats ปีที่แล้ว

    Awesome, thanks. If anyone is having trouble doing the swap, If you don't have any WETH in the wallet to start with you'll have to add value manually to the transaction.

    • @amirnaderi-qe4qm
      @amirnaderi-qe4qm 3 หลายเดือนก่อน

      Hi
      How can I add Swaped value to tether to my wallet
      Many thanks

  • @AdamFergusonNZL
    @AdamFergusonNZL 2 ปีที่แล้ว +1

    Best explainer of code for uniswap trx I've found on youtube. Line by line function explainer with reference to documentation is such a benefit. Thanks! Subscribed and will be looking at all your vids.

  • @EEZOpen
    @EEZOpen 2 ปีที่แล้ว +2

    If I may suggest something I would love to see in one of the new videos: how to programmatically access the parameters displayed by Uniswap (i.e. exchange rate, expected price, price impact, ...). Thank you very much in advance for your exceptional efforts and the way of presenting the chosen topic.

    • @blockmancodes
      @blockmancodes  2 ปีที่แล้ว +2

      Thanks for the suggestion! Adding that to my todo list.

    • @EEZOpen
      @EEZOpen 2 ปีที่แล้ว

      @@blockmancodes Looking forward to see new great videos! Thank you very much.

  • @vilanovaalejandro500
    @vilanovaalejandro500 ปีที่แล้ว +1

    Is there any way you can create a video on how to also convert between WETH and stablecoins? Since WETH has 18 decimals and USDC has 6 decimals I'm not sure if I am doing something wrong. Also explaining how you would change the gas estimate and approval amount would be different in mainnet circumstances would be appreciated. Overall this is the clearest video Ive seen about making swaps. I'm still a beginner to defi so understanding the pools and how to swap between exchanges is something you've really helped me with. Thanks!

  • @Toninirulay
    @Toninirulay 5 หลายเดือนก่อน

    Hi there. Could you explain to me how to create the directories for the new blank note? Because I can't find the option in the Remix. Thank you so much.

  • @panchopacheco1247
    @panchopacheco1247 ปีที่แล้ว +1

    Can anyone help with the dreaded: "error\":{\"code\":-32000,\"message\":\"transaction underpriced\"} on Polygon.
    I raised the gas price, reset my MetaMask. One source suggested aligning my gpo.ignorePrice with the txPool.pricelimit
    but didn't explain how. Please share any solutions or suggestions

  • @wenrays2728
    @wenrays2728 2 ปีที่แล้ว

    Really helpful
    Eagerly waiting for your next tutorial on defi

  • @amirnaderi-qe4qm
    @amirnaderi-qe4qm 3 หลายเดือนก่อน

    Hi I hope you are doing well
    How can I add swaped value to to my crypto wallet

  • @yasinduneth
    @yasinduneth ปีที่แล้ว

    Do you have any video on how to transfer any other tokens except eth?
    I’ve been trying to make swapExactTokensForTokens function in uniswap router to make it work, but still failing. I tried doing this same thing with just using the explorer contract write with wagmi, is it possible to do this that way without uniswap sdk?

  • @internationalcompanymarine7745
    @internationalcompanymarine7745 2 ปีที่แล้ว

    To continue, we need clearer explanations and tips along with where we can contact you for programming tips. Token exchange and wallet transfer best explained so far.

  • @katokato384
    @katokato384 2 ปีที่แล้ว +1

    まじで神です。Goat🐏

  • @hongtinhang1601
    @hongtinhang1601 2 ปีที่แล้ว +1

    Nice video. Please do more video about trading bot and arbitrage bot.

  • @JV-gf3ee
    @JV-gf3ee ปีที่แล้ว +1

    i tried this last 3 weeks but i cant get it to work. but i do swaps with v2.

  • @ЮрійКуник-с4ч
    @ЮрійКуник-с4ч ปีที่แล้ว

    Amazing! Don’t forget please about making pancakeSwap V3 tutorials

  • @lastminuteprogrammer8511
    @lastminuteprogrammer8511 ปีที่แล้ว +2

    Hi, now that ropsten network is depreceated post the merge upgrade of ethereum, I tried to replicate above steps in Goerli testnet. I was also able to find the pool address of UNI/WETH pool in goerli testnet through your another video. Although the approve transaction is running successfully, but the swap transaction fails with error STF. Can you please replicate this tutorial for post merge upgrade viewers as well, preferrably on goerli? Thanks!

    • @dzsobacsi
      @dzsobacsi ปีที่แล้ว +1

      I was also struggling with this. It turned out that you have to have some WETH to do the transaction. It does not work with unwrapped ETH.

    • @daswassuup
      @daswassuup ปีที่แล้ว

      Can confirm that adding WETH solved this problem on Goerli 👌

  • @0xSoliditydev
    @0xSoliditydev 8 หลายเดือนก่อน

    Explain better than everyone in TH-cam!
    Thanks sir. 🫡

  • @alextradescrypto
    @alextradescrypto 7 หลายเดือนก่อน

    Really great video. Well done and thank you!

  • @pierreduprey9032
    @pierreduprey9032 ปีที่แล้ว

    Hello, firstly thanks for this, I am trying to execute it on mainnet with polygon.
    Only problem I have is that I am getting error: transaction underpriced. I adjusted the fee but it makes no difference. From reading forums it appears I need to set the gat price to at least 30 gwei. The response after executing the script is showing the gasPrice parameter = null, but the parameters 'gas price' is not taken by the ExactInputSingle function, so where is it specified?!

  • @danicodeve
    @danicodeve 7 หลายเดือนก่อน

    Is an await not required when calling the slot0() function at @17:15 ?

  • @0xsuperman
    @0xsuperman 3 หลายเดือนก่อน +1

    Any idea how I can find the pool address for uniswap's WETH/UNI pool on Sepolia?

    • @timrichter1980
      @timrichter1980 หลายเดือนก่อน

      Same problem... also, in general, how do I find suitable pools for token swaps?

    • @VitaliiBoiko-m3l
      @VitaliiBoiko-m3l 21 วันที่ผ่านมา

      did you find it?

  • @reactdev2838
    @reactdev2838 2 ปีที่แล้ว

    Highly Appreciated Bro

  • @mikoajagalarov3795
    @mikoajagalarov3795 ปีที่แล้ว

    Hi, thank you for your videos. I have a question about Uniswap SDK that you used in this video. You got ABI of swapRouter from uni sdk, but actually I am trying to create this functionality using regular java. And I stuck at this moment, because I can’t find actual smart contract that responsive for that functionality of swapping tokens on V3. Can you please provide more information where I can obtain this ABI to create wrapper of this smartcontract?

    • @blockmancodes
      @blockmancodes  ปีที่แล้ว

      You can get any popular ABI from etherscan in the contract code section at the bottom

  • @ryananderson8817
    @ryananderson8817 ปีที่แล้ว

    Hey Blockman, thanks again for doing this video. I have my trades working but sometime I wont know if a trade didnt go through until I look on etherscan. Is there a way to know if tx went through at all on the program? Thanks in advance.

    • @blockmancodes
      @blockmancodes  ปีที่แล้ว

      Yes. I can do a video on this soon

  • @JohnDoe-wi6nq
    @JohnDoe-wi6nq 2 ปีที่แล้ว

    Hello
    There are two videos you did, another with alpharouter. Can you pls tell which one is for what?

    • @blockmancodes
      @blockmancodes  2 ปีที่แล้ว

      Given a choice, use the alpharouter. It's more optimized.

  • @declannelson895
    @declannelson895 2 ปีที่แล้ว

    Great video! Where do we find pool addresses for test nets? I am connecting to Polygon Mumbai but can't find the correct pool and token addresses for the network.

    • @blockmancodes
      @blockmancodes  2 ปีที่แล้ว

      I have another video on finding token and pool addresses. Can you find it?

  • @blockmancodes
    @blockmancodes  2 ปีที่แล้ว +5

    What else do you want to learn to do with Uniswap?

    • @TN-jz5gb
      @TN-jz5gb 2 ปีที่แล้ว +1

      Can you show how to do a multihop swap with v3 like eth > usdc > uni? Im still struggling with a multihop swap

    • @blockmancodes
      @blockmancodes  2 ปีที่แล้ว +2

      @@TN-jz5gb Added to my to-do list!

    • @timgraf7793
      @timgraf7793 2 ปีที่แล้ว

      ​@@blockmancodes I know how to query Uniswap V3 pools directly for their reserves (within the tick) but I don't know how to interact with that specific pool via the Uniswap V3 router and quoter. Would be great if you could show how to do this!

    • @blockmancodes
      @blockmancodes  2 ปีที่แล้ว

      @@timgraf7793 Can you clarify "interact with that specific pool via the Uniswap V3 router and quoter"?
      You want to swap tokens, or do something else?

    • @jboon4175
      @jboon4175 2 ปีที่แล้ว +1

      I'm actually interested in v2 to v3 swaps and vice versa, for trading between Uniswap and uniswap clones (i.e. sushiswap). From what I can see, it's just the swap function with a bit of junk data in the callback, but I can't quite figure out if I need to write a specific contract for v2/v3 swaps or if I can do it with ethers.js.

  • @wordwonders01
    @wordwonders01 หลายเดือนก่อน

    thank you for sharing sir

  • @vincentli1994
    @vincentli1994 2 ปีที่แล้ว +1

    hi, when I update inputAmount from 0.001 to 0.01, I got "Error: invalid BigNumber string (argument="value", value="1e+21", code=INVALID_ARGUMENT, version=bignumber/5.6.2)". I wonder where went wrong?

    • @vincentli1994
      @vincentli1994 2 ปีที่แล้ว +1

      It seems have something to do with approvalAmount, after i change *1000 to *10, it works.

    • @blockmancodes
      @blockmancodes  2 ปีที่แล้ว +1

      @@vincentli1994 Good job!

  • @0xsuperman
    @0xsuperman ปีที่แล้ว

    Ropsten has now been deprecated, can this be converted and be done on Goerli testnet? How can I find the WETH/UNI pool address to replace the "0x4D7C363DED4B3b4e1F954494d2Bc3955e49699cC"?

    • @blockmancodes
      @blockmancodes  ปีที่แล้ว

      It will work on Goerli if you have the correct addresses. There's no perfect way to find tokens for testnets. Etherscan searches, google, checking how many wallets have the token, etc...

  • @Centori88
    @Centori88 2 ปีที่แล้ว

    This is a great tutorial!What parameters do I need to add Uniswap v3 in an arbitrage smart contract written in V2 (eg flashbots)?thanks(newby asking)

    • @blockmancodes
      @blockmancodes  2 ปีที่แล้ว

      It totally depends on the code in your contract. Is it public code or something you wrote yourself?

  • @zhaofour9833
    @zhaofour9833 2 ปีที่แล้ว

    Amazing stuff!! Sir, I wonder that is it possible to post some videos about using the Ganache fork network to do some transactions using Uniswap?? Thanks

  • @josephbagheri5682
    @josephbagheri5682 2 ปีที่แล้ว

    Hey can you make a video on swapping both ways and with one of the tokens being variable

    • @blockmancodes
      @blockmancodes  2 ปีที่แล้ว +1

      For sure, it's on my to-do list.

  • @SuperDividenz
    @SuperDividenz ปีที่แล้ว

    Great stuff thank you so much!

  • @unaiiglesias9901
    @unaiiglesias9901 ปีที่แล้ว

    Thanks. do exactInput demo please

    • @blockmancodes
      @blockmancodes  ปีที่แล้ว +1

      Thanks for watching!
      I'll be covering most V3 swap and liquidity functions in my Uniswap course so I probably won't be doing TH-cam videos on them.
      Though I'll continue making videos on other V3 topics.

  • @AnhTuan-uu7vh
    @AnhTuan-uu7vh ปีที่แล้ว

    can you guide me how to buy tokens fast on pankerswap on bsc or other networks with usdt usdc

  • @inaamullah9868
    @inaamullah9868 ปีที่แล้ว

    Thank you for this awesome tutorial. Does this still work for Goreli

    • @blockmancodes
      @blockmancodes  ปีที่แล้ว

      As of today, this should still work on Goerli

  • @0xHFSP
    @0xHFSP 2 ปีที่แล้ว

    i tried this directly on optimism mainnet. it works. thank you sir. quick question tho, on Line 34 of uniswapTrader.js file, why did u define state and not use it?

    • @0xHFSP
      @0xHFSP 2 ปีที่แล้ว

      is it for the swap params?

    • @blockmancodes
      @blockmancodes  2 ปีที่แล้ว +1

      Good catch brother! It's not needed. I was originally going to make the tutorial more complicated, but changed my mind and didn't remove that.

  • @KingKongHitDaBong
    @KingKongHitDaBong ปีที่แล้ว

    how would one tweak this if they have the contracts for both tokens but no pool has been created yet, so we would run the script so that once a pool with those 2 tokens has been detected it buys

    • @blockmancodes
      @blockmancodes  ปีที่แล้ว

      a) Continuously ping the factory getPool functions until you get a response indicating the pool exists
      b) Monitor events for new pool creations and check if the tokens match

  • @davidxiao924
    @davidxiao924 ปีที่แล้ว

    hi, mate, any idea to get real time data from dex(uniswap, etc) like cex data stream?

  • @jerrywang3225
    @jerrywang3225 ปีที่แล้ว

    great tutorial.

  • @franklee2924
    @franklee2924 ปีที่แล้ว

    Could we interact with pool contract directly without SwapRouter? and how

    • @blockmancodes
      @blockmancodes  ปีที่แล้ว +1

      Yes you can. The swap router calls swap on a pool under the hood. I'll try to do a quick video sometime soon

    • @franklee2924
      @franklee2924 ปีที่แล้ว

      @@blockmancodes great! can't wait!

  • @XaoCcHuK
    @XaoCcHuK ปีที่แล้ว

    How to setup "inputAmount" for USDC token for example?
    Got always this message "'{"jsonrpc":"2.0","id":65,"error":{"code":-32000,"message":"insufficient funds for gas * price + value"}}'"

    • @blockmancodes
      @blockmancodes  ปีที่แล้ว

      Sounds like you don't have enough ETH in your wallet for the transaction

    • @XaoCcHuK
      @XaoCcHuK ปีที่แล้ว

      @@blockmancodes I checked, i have more than 100 eth at Goerli network. Do you plan to create video about bridge tokens via script. (For example ETH-ARB bridge)?)

    • @XaoCcHuK
      @XaoCcHuK ปีที่แล้ว

      @@blockmancodes Also can you advice please. When i am tryng to swap more than 0.0099eth (for example 0.01) always got error "Error: invalid BigNumber string (argument="value", value="1e+21", code=INVALID_ARGUMENT, version=bignumber/5.7.0)" In the internet i found information, that js doesn't support so big numbers. Is it true?

  • @flimeth5333
    @flimeth5333 2 ปีที่แล้ว

    Thanks for the great video. Any plans to do this same content but written in Python?

    • @blockmancodes
      @blockmancodes  2 ปีที่แล้ว +2

      Thanks! TBH I have a few thousand tutorials on my to-do list so I probably won't get to Python for a long time

    • @flimeth5333
      @flimeth5333 2 ปีที่แล้ว

      @@blockmancodes pretty small to-do list

  • @LewisFarrell-my4td
    @LewisFarrell-my4td ปีที่แล้ว

    Do you have an equivalent video for Uniswap V2?

  • @viktorcortez5706
    @viktorcortez5706 2 ปีที่แล้ว

    I watched you video about Error: VM Exception while processing transaction: reverted with reason string 'STF' . But I'm still fighting this stuff))

    • @blockmancodes
      @blockmancodes  2 ปีที่แล้ว +1

      Do you have WETH (not ETH) in your wallet?

  • @tungthanh9613
    @tungthanh9613 2 ปีที่แล้ว +1

    I'm newbie,
    can someone tell me how can i get 'INFURA_URL_TESTNET=' value. tks

    • @blockmancodes
      @blockmancodes  2 ปีที่แล้ว

      Create a free account on Infura io, create a project then get the endpoint URL from settings

  • @YeasinRahaman
    @YeasinRahaman 2 ปีที่แล้ว

    Awesome, It is a great help for the community.
    I was looking for a solution how I can swap `UNI -> ETH`. I've tried similar approach, but got error with STF. Can you provide any clue?

    • @blockmancodes
      @blockmancodes  2 ปีที่แล้ว

      Try with WETH rather than ETH?

    • @YeasinRahaman
      @YeasinRahaman 2 ปีที่แล้ว

      ​@@blockmancodes Yes, I actually need to show the ETH balance to users. I found the idea of using WETH. I think I have to invoke the swapRouter multicall method to get the amount in ETH. But your videos helped a lot. Thank you very much.
      I am sorry for asking one more thing, By any chance do you have any code snippet/example how to invoke swapRouter multicall using ethers js? This will save a lot of time.

    • @blockmancodes
      @blockmancodes  2 ปีที่แล้ว

      @@YeasinRahaman I don't have any code examples sitting around for multicall. But will try to put together a quick video sometime soon

    • @YeasinRahaman
      @YeasinRahaman 2 ปีที่แล้ว

      @@blockmancodes Thanks a lot. Really appreciate your help.

  • @th3word487
    @th3word487 ปีที่แล้ว

    Much respect for these videos. You a coding monster.
    Does the wallet secret have spaces? And how can I send to test net and Mainnet?
    I need to make a swap with UNISWAP UNIVERSAL ROUTER.
    ASAP. Trying to learn as quick as possible.
    Thank you for the content!

    • @blockmancodes
      @blockmancodes  ปีที่แล้ว +1

      The wallet secret is a long string of "random" characters. You may be mixing it up with the pass phrase.
      Figure out how to fork mainnet. This will speed you up when testing.

    • @th3word487
      @th3word487 ปีที่แล้ว

      @@blockmancodes just bought your class.
      Does will this teach how to form main and test net?

  •  ปีที่แล้ว

    Whatever I do, I get 'Failed with STF' error. I have unlimited token approval already. Do I miss something?

    • @blockmancodes
      @blockmancodes  ปีที่แล้ว

      I've seen a lot of people get this error because they approve WETH but only have ETH in their wallet. Can you check that 1st if you haven't?

    •  ปีที่แล้ว

      @@blockmancodes I can't quite remember but I believe this message was misleading and was due to not enough gas supplied. I had the proper amount of assets to transfer, I just had too low gas limit as far as I remember.

  • @pravigyajain1153
    @pravigyajain1153 2 ปีที่แล้ว

    Can you please make a similar video for pancakeswap? I tried tweaking this one with corresponding addresses but couldn't get it to work 🙁

    • @blockmancodes
      @blockmancodes  2 ปีที่แล้ว +1

      On my todo list brother

    • @binbotaro3
      @binbotaro3 ปีที่แล้ว

      best uniswap tut on youtube! i also tried doing it on bsc testnet, deploying via hardhat ok, and approving approved amount ok, but getting gas estimate error on swapExactInputSingle...

  • @joaquinlopez1036
    @joaquinlopez1036 2 ปีที่แล้ว

    The videos are great. I'm trying to get it to work in Polygon but no way. It stops me in the approve of the contract. Do you have a Telegram group to be able to make more dynamic queries? Thank you

    • @blockmancodes
      @blockmancodes  2 ปีที่แล้ว

      Hey! I'll be doing some polygon videos in the near future. I'll be sure to cover this.
      Too busy to manage a Telegram group at the moment but it's on my future roadmap.

    • @julian007swiss
      @julian007swiss 2 ปีที่แล้ว

      @@blockmancodes Hi, I also tried to get this to work on Polygon, but failed ... Have you done a uniswap v3 polygon video yet, or some pointers on what is different there? thanks for the great video!

    • @blockmancodes
      @blockmancodes  2 ปีที่แล้ว

      @@julian007swiss Hey brother. I haven't done any Polygon videos yet. But hoping to do some this year sometime

  • @Centori88
    @Centori88 2 ปีที่แล้ว

    Please do multi hop swaps!

    • @blockmancodes
      @blockmancodes  2 ปีที่แล้ว +1

      Added to my roadmap brother. May take me a while to get to it though

  • @EEZOpen
    @EEZOpen 2 ปีที่แล้ว +1

    Just another excellent video. Yes, please move on with your suggestions to tweak gas fee and slippage. It's probably possible to get current gas fee for the selected network and depends of it set gas fee for swap transaction. Unfortunately, I still didn't manage to run this example successfully. For some reason I'm getting repeatedly the same error: 'insufficient funds for intrinsic transaction cost', code: 'INSUFFICIENT_FUNDS' although I have enough assets (over 0.2 ETH) and gas fee it seems that is currently 0.0062 ETH which is subtracted even if transaction failed (so your recommendation to run it on testnet is in place). Probably my problem is that I don't have WETH in my wallet but just ETH. Don't know currently how to wrap ETH on testnet. Any suggestion is welcome.

    • @EEZOpen
      @EEZOpen 2 ปีที่แล้ว

      Bingo! It seems to work. One thing is not very clear to me and that is the exchange rate difference. Namely, it turns out that for 0.001 WETH I get only 0.00045 UNI. Maybe it's because I'm on a testnet.
      By the way, I also tried testing using Pocket Network as a provider. It's reporting an error SERVER_ERROR, which was not the case with the examples from your previous examples.

    • @blockmancodes
      @blockmancodes  2 ปีที่แล้ว +1

      Glad you got it working! And you're correct, the weird swap rate is because it's on testnet.
      You probably know this but if you go to Uniswap and change to Ropsten in your Metamask you can also see these different prices.
      I haven't tried the code with Pocket but thanks for letting me know. I wonder if it's just some different configuration required.

    • @EEZOpen
      @EEZOpen 2 ปีที่แล้ว

      @@blockmancodes Tested also yesterday with Alchemy. Works fine.

  • @yashvinodmittal2864
    @yashvinodmittal2864 ปีที่แล้ว

    how do you get testnet url ?

    • @blockmancodes
      @blockmancodes  ปีที่แล้ว

      You can create an account on Infura to get one

  • @th3word487
    @th3word487 ปีที่แล้ว

    Can I use this on Remix?

    • @blockmancodes
      @blockmancodes  ปีที่แล้ว +1

      I'm not an expert on remix, but this is all javascript so you can easily run locally

    • @th3word487
      @th3word487 ปีที่แล้ว

      @@blockmancodes is there a website to code JavaScript like Remix? Or do I have to download software?
      I’m a super noob at the moment

  • @Androgamer-yx7tu
    @Androgamer-yx7tu 2 ปีที่แล้ว

    From where did u get address for uni/eth

    • @blockmancodes
      @blockmancodes  2 ปีที่แล้ว

      You can find addresses by searching Etherscan

    • @Androgamer-yx7tu
      @Androgamer-yx7tu 2 ปีที่แล้ว

      @@blockmancodes Actually got the pool address u hv explained in another video.
      It was after one video inside your uniswap playlist. Just in wrong order I guess.

  • @JV-gf3ee
    @JV-gf3ee ปีที่แล้ว

    Hi i finally did it thanks

  • @compilation_exe3821
    @compilation_exe3821 ปีที่แล้ว

    USEEEEEEEEEEEE TYPESCRIPTTTT MY FRIEND

  • @rezade8260
    @rezade8260 ปีที่แล้ว

    I love you man ❤️ no homo

  • @uncletan888
    @uncletan888 ปีที่แล้ว

    Can you do same video for polygon mainnet? Or how to use testnet and all popular mainnet.