@@ -22,6 +22,8 @@ const startChain = async () => {
2222 return wallet
2323}
2424
25+ jest . setTimeout ( 100000 )
26+ const uniswap = require ( "@studydefi/money-legos/uniswap" ) . default
2527const erc20 = require ( "@studydefi/money-legos/erc20" ) . default
2628
2729describe ( "do some tests" , ( ) => {
@@ -47,4 +49,45 @@ describe("do some tests", () => {
4749 const ethBalance = ethers . utils . formatEther ( ethBalanceWei )
4850 expect ( parseFloat ( ethBalance ) ) . toBe ( 1000 )
4951 } )
52+
53+ test ( "buy DAI from Uniswap" , async ( ) => {
54+ // 1. instantiate contracts
55+ const daiContract = new ethers . Contract (
56+ erc20 . dai . address ,
57+ erc20 . dai . abi ,
58+ wallet
59+ )
60+ const uniswapFactoryContract = new ethers . Contract (
61+ uniswap . factory . address ,
62+ uniswap . factory . abi ,
63+ wallet
64+ )
65+ const daiExchangeAddress = await uniswapFactoryContract . getExchange (
66+ erc20 . dai . address
67+ )
68+ const daiExchangeContract = new ethers . Contract (
69+ daiExchangeAddress ,
70+ uniswap . exchange . abi ,
71+ wallet
72+ )
73+
74+ // 2. do the actual swapping
75+ await daiExchangeContract . ethToTokenSwapInput (
76+ 1 , // min amount of token retrieved
77+ 2525644800 , // random timestamp in the future (year 2050)
78+ {
79+ gasLimit : 4000000 ,
80+ value : ethers . utils . parseEther ( "5" ) ,
81+ }
82+ )
83+
84+ // util function
85+ const fromWei = x => ethers . utils . formatUnits ( x , 18 )
86+
87+ // 3. check DAI balance
88+ const daiBalanceWei = await daiContract . balanceOf ( wallet . address )
89+ const daiBalance = parseFloat ( fromWei ( daiBalanceWei ) )
90+ expect ( daiBalance ) . toBeGreaterThan ( 0 )
91+ console . log ( "daiBalance" , daiBalance )
92+ } )
5093} )
0 commit comments