github.com/cwntr/go-defi@v0.0.0-20210629134751-07f9ec2f7e66/client/client_test.go (about) 1 package client 2 3 import ( 4 "context" 5 "crypto/ecdsa" 6 "log" 7 "math/big" 8 "testing" 9 10 "github.com/524119574/go-defi/binding/erc20" 11 12 "github.com/ethereum/go-ethereum/accounts/abi/bind" 13 "github.com/ethereum/go-ethereum/common" 14 "github.com/ethereum/go-ethereum/crypto" 15 "github.com/ethereum/go-ethereum/ethclient" 16 ) 17 18 var key *ecdsa.PrivateKey 19 var ethClient *ethclient.Client 20 var publicKey *ecdsa.PublicKey 21 var fromAddr common.Address 22 var defiClient *DefiClient 23 24 func init() { 25 var err error 26 key, err = crypto.HexToECDSA("b8c1b5c1d81f9475fdf2e334517d29f733bdfa40682207571b12fc1142cbf329") 27 if err != nil { 28 log.Fatalf("Failed to create private key: %v", err) 29 } 30 ethClient, err = ethclient.Dial("http://127.0.0.1:8545") 31 if err != nil { 32 log.Fatalf("Failed to connect to ETH: %v", err) 33 } 34 35 publicKeyECDSA, ok := (key.Public()).(*ecdsa.PublicKey) 36 if !ok { 37 log.Fatal("cannot assert type") 38 } 39 fromAddr = crypto.PubkeyToAddress(*publicKeyECDSA) 40 defiClient = NewClient(bind.NewKeyedTransactor(key), ethClient) 41 _ = fromAddr 42 _ = ethClient 43 _ = defiClient 44 } 45 46 func TestInteractWithCompound(t *testing.T) { 47 48 beforeETH, err := ethClient.BalanceAt(context.Background(), fromAddr, nil) 49 50 err = defiClient.Compound().Supply(int64(1e18), ETH) 51 if err != nil { 52 t.Errorf("Failed to supply in compound: %v", err) 53 } 54 55 cETH, err := defiClient.Compound().BalanceOf(ETH) 56 if err != nil { 57 t.Errorf("Failed to get balance: %v", err) 58 } 59 60 if cETH.Cmp(big.NewInt(0)) == 0 { 61 t.Errorf("CETH minting is not successful") 62 } 63 64 afterETH, err := ethClient.BalanceAt(context.Background(), fromAddr, nil) 65 66 if beforeETH.Cmp(afterETH) != 1 { 67 t.Errorf("ETH balance not decreasing.") 68 } 69 } 70 71 func TestInteractWithUniswap(t *testing.T) { 72 beforeETH, err := ethClient.BalanceAt(context.Background(), fromAddr, nil) 73 beforeDAI, err := defiClient.BalanceOf(DAI) 74 75 err = defiClient.Uniswap().Swap(1e18, DAI, ETH, fromAddr) 76 if err != nil { 77 t.Errorf("Failed to swap in uniswap: %v", err) 78 } 79 80 afterETH, err := ethClient.BalanceAt(context.Background(), fromAddr, nil) 81 afterDAI, err := defiClient.BalanceOf(DAI) 82 83 if beforeETH.Cmp(afterETH) != 1 { 84 t.Errorf("ETH balance not decreasing.") 85 } 86 if afterDAI.Cmp(beforeDAI) != 1 { 87 t.Errorf("Dai hasn't increased!") 88 } 89 90 err = defiClient.Compound().Supply(int64(1e18), DAI) 91 if err != nil { 92 t.Errorf("Failed to supply Dai in compound: %v", err) 93 } 94 95 cDai, err := defiClient.Compound().BalanceOf(DAI) 96 if err != nil { 97 t.Errorf("Failed to get balance: %v", err) 98 } 99 100 if cDai.Cmp(big.NewInt(0)) != 1 { 101 t.Errorf("cDAI minting is not successful: %v", cDai) 102 } 103 104 } 105 106 func TestInteractWithCompoundInDai(t *testing.T) { 107 108 beforeDAI, err := defiClient.BalanceOf(DAI) 109 110 err = defiClient.Compound().Supply(int64(1e18), DAI) 111 if err != nil { 112 t.Errorf("Failed to supply in compound: %v", err) 113 } 114 115 cDAI, err := defiClient.Compound().BalanceOf(DAI) 116 if err != nil { 117 t.Errorf("Failed to get balance: %v", err) 118 } 119 120 if cDAI.Cmp(big.NewInt(0)) == 0 { 121 t.Errorf("CDai minting is not successful") 122 } 123 124 afterDAI, err := defiClient.BalanceOf(DAI) 125 126 if beforeDAI.Cmp(afterDAI) != 1 { 127 t.Errorf("Dai balance not decreasing.") 128 } 129 } 130 131 func TestMintSomeUSDC(t *testing.T) { 132 _, err := erc20.NewErc20(CoinToAddressMap[USDC], ethClient) 133 if err != nil { 134 t.Errorf("Error getting USDC Contract") 135 } 136 beforeUSDC, err := defiClient.BalanceOf(USDC) 137 if err != nil { 138 t.Errorf("Error getting USDC balance") 139 } 140 141 if beforeUSDC.Cmp(big.NewInt(0)) != 0 { 142 t.Errorf("Before USDC not 0, %v", beforeUSDC) 143 } 144 } 145 146 func TestInteractWithYearn(t *testing.T) { 147 beforeETH, err := ethClient.BalanceAt(context.Background(), fromAddr, nil) 148 149 err = defiClient.Yearn().addLiquidity(big.NewInt(1e18), ETH) 150 if err != nil { 151 t.Errorf("Failed to add liquidity in yearn: %v", err) 152 } 153 154 afterETH, err := ethClient.BalanceAt(context.Background(), fromAddr, nil) 155 if beforeETH.Cmp(afterETH) != 1 { 156 t.Errorf("ETH balance not decreasing.") 157 } 158 } 159 160 func TestInteractWithFurucomboWithYearn(t *testing.T) { 161 beforeETH, err := ethClient.BalanceAt(context.Background(), fromAddr, nil) 162 163 actions := new(Actions) 164 actions.Add( 165 defiClient.Yearn().AddLiquidityActions(big.NewInt(1e18), ETH), 166 ) 167 168 err = defiClient.ExecuteActions(actions) 169 if err != nil { 170 t.Errorf("Failed to add liquidity in yearn: %v", err) 171 } 172 173 afterETH, err := ethClient.BalanceAt(context.Background(), fromAddr, nil) 174 if beforeETH.Cmp(afterETH) != 1 { 175 t.Errorf("ETH balance not decreasing.") 176 } 177 178 actions = new(Actions) 179 actions.Add( 180 defiClient.Yearn().RemoveLiquidityActions(big.NewInt(1e18), ETH), 181 ) 182 Approve(defiClient, yWETH, common.HexToAddress(ProxyAddr), big.NewInt(1e18)) 183 err = defiClient.ExecuteActions(actions) 184 if err != nil { 185 t.Errorf("Failed to remove liquidity in yearn: %v", err) 186 } 187 188 // Not sure why this is not increasing... 189 afterafterETH, err := ethClient.BalanceAt(context.Background(), fromAddr, nil) 190 191 if afterETH.Cmp(afterafterETH) != -1 { 192 t.Errorf("ETH balance not increasing. %v %v, %v", afterETH, afterafterETH, actions.Actions[0].data) 193 } 194 } 195 196 func TestInteractWithFurucomboWithCompoundNew(t *testing.T) { 197 198 beforeCETH, err := defiClient.Compound().BalanceOf(ETH) 199 200 if err != nil { 201 log.Fatalf("Failed to get balance: %v", err) 202 } 203 204 actions := new(Actions) 205 206 actions.Add( 207 defiClient.Compound().SupplyActions(big.NewInt(1e18), ETH), 208 ) 209 210 defiClient.ExecuteActions(actions) 211 212 if err != nil { 213 t.Errorf("Failed to interact with Furucombo: %v", err) 214 } 215 216 afterCETH, err := defiClient.Compound().BalanceOf(ETH) 217 if err != nil { 218 t.Errorf("Failed to get balance: %v", err) 219 } 220 221 if afterCETH.Cmp(beforeCETH) != 1 { 222 t.Errorf("cETH minting is not successful via Furucombo: %v %v", afterCETH, beforeCETH) 223 } 224 225 } 226 227 func TestInteractWithFurucomboWithCompoundERC20New(t *testing.T) { 228 Approve(defiClient, DAI, common.HexToAddress(ProxyAddr), big.NewInt(1e18)) 229 beforeCDai, err := defiClient.Compound().BalanceOf(DAI) 230 231 if err != nil { 232 log.Fatalf("Failed to get balance: %v", err) 233 } 234 235 actions := new(Actions) 236 237 actions.Add( 238 defiClient.Compound().SupplyActions(big.NewInt(1e18), DAI), 239 ) 240 241 defiClient.ExecuteActions(actions) 242 243 if err != nil { 244 t.Errorf("Failed to interact with Furucombo: %v", err) 245 } 246 247 afterCDai, err := defiClient.Compound().BalanceOf(DAI) 248 if err != nil { 249 t.Errorf("Failed to get balance: %v", err) 250 } 251 252 if afterCDai.Cmp(beforeCDai) != 1 { 253 t.Errorf("cDai minting is not successful via Furucombo: %v %v", afterCDai, beforeCDai) 254 } 255 256 } 257 258 func TestInteractWithFurucomboWithCompoundERC20withRedeem(t *testing.T) { 259 Approve(defiClient, DAI, common.HexToAddress(ProxyAddr), big.NewInt(1e18)) 260 Approve(defiClient, cDAI, common.HexToAddress(ProxyAddr), big.NewInt(1e18)) 261 262 beforeCDai, err := defiClient.Compound().BalanceOf(DAI) 263 264 if err != nil { 265 log.Fatalf("Failed to get balance: %v", err) 266 } 267 268 actions := new(Actions) 269 270 actions.Add( 271 defiClient.Compound().SupplyActions(big.NewInt(1e18), DAI), 272 defiClient.Compound().RedeemActions(big.NewInt(100000), DAI), 273 ) 274 275 defiClient.ExecuteActions(actions) 276 277 if err != nil { 278 t.Errorf("Failed to interact with Furucombo: %v", err) 279 } 280 281 afterCDai, err := defiClient.Compound().BalanceOf(DAI) 282 if err != nil { 283 t.Errorf("Failed to get balance: %v", err) 284 } 285 286 if afterCDai.Cmp(beforeCDai) != 1 { 287 t.Errorf("cDai minting is not successful via Furucombo: %v %v", afterCDai, beforeCDai) 288 } 289 290 } 291 292 func TestInteractWithFurucomboFlashLoan(t *testing.T) { 293 Approve(defiClient, DAI, common.HexToAddress(ProxyAddr), big.NewInt(1e18)) 294 295 actions := new(Actions) 296 flashLoanActions := new(Actions) 297 298 actions.Add( 299 defiClient.SupplyFundActions(big.NewInt(1e18), DAI), 300 defiClient.Aave().FlashLoanActions( 301 big.NewInt(5e18), 302 DAI, 303 flashLoanActions, 304 ), 305 ) 306 307 err := defiClient.ExecuteActions(actions) 308 309 if err != nil { 310 t.Errorf("Failed to interact with Furucombo: %v", err) 311 } 312 313 } 314 315 // func TestInteractWithFurucomboFlashSwap(t *testing.T) { 316 // Approve(defiClient, DAI, common.HexToAddress(FurucomboAddr), big.NewInt(1e18)) 317 318 // actions := new(Actions) 319 // flashSwapActions := new(Actions) 320 321 // actions.Add( 322 // defiClient.SupplyFundActions(big.NewInt(1e18), DAI), 323 // defiClient.Uniswap().FlashSwapActions( 324 // big.NewInt(2e18), 325 // DAI, 326 // DAI, 327 // flashSwapActions, 328 // ), 329 // ) 330 331 // err := defiClient.ExecuteActions(actions) 332 333 // if err != nil { 334 // t.Errorf("Failed to interact with Furucombo..: %v", err) 335 // } 336 337 // } 338 339 func TestInteractWithFurucomboUniswap(t *testing.T) { 340 beforeETH, err := ethClient.BalanceAt(context.Background(), fromAddr, nil) 341 if err != nil { 342 t.Errorf("Error getting ETH balance") 343 } 344 beforeDAI, err := defiClient.BalanceOf(DAI) 345 if err != nil { 346 t.Errorf("Error getting DAI balance") 347 } 348 349 actions := new(Actions) 350 351 actions.Add( 352 defiClient.Uniswap().SwapActions(big.NewInt(1e18), DAI, ETH), 353 ) 354 355 err = defiClient.ExecuteActions(actions) 356 357 afterETH, err := ethClient.BalanceAt(context.Background(), fromAddr, nil) 358 afterDAI, err := defiClient.BalanceOf(DAI) 359 360 if beforeETH.Cmp(afterETH) != 1 { 361 t.Errorf("ETH balance not decreasing.") 362 } 363 if afterDAI.Cmp(beforeDAI) != 1 { 364 t.Errorf("DAI hasn't increased!") 365 } 366 367 } 368 369 func TestInteractWithFurucomboKyber(t *testing.T) { 370 beforeETH, err := ethClient.BalanceAt(context.Background(), fromAddr, nil) 371 if err != nil { 372 t.Errorf("Error getting ETH balance") 373 } 374 beforeDAI, err := defiClient.BalanceOf(DAI) 375 if err != nil { 376 t.Errorf("Error getting DAI balance") 377 } 378 379 actions := new(Actions) 380 381 actions.Add( 382 defiClient.Kyberswap().SwapActions(big.NewInt(1e18), DAI, ETH), 383 ) 384 385 err = defiClient.ExecuteActions(actions) 386 387 afterETH, err := ethClient.BalanceAt(context.Background(), fromAddr, nil) 388 afterDAI, err := defiClient.BalanceOf(DAI) 389 390 if beforeETH.Cmp(afterETH) != 1 { 391 t.Errorf("ETH balance not decreasing.") 392 } 393 if afterDAI.Cmp(beforeDAI) != 1 { 394 t.Errorf("DAI hasn't increased!") 395 } 396 397 } 398 399 func TestInteractWithFurucomboFlashLoanCompound(t *testing.T) { 400 Approve(defiClient, DAI, common.HexToAddress(ProxyAddr), big.NewInt(3e18)) 401 beforecDAI, err := defiClient.BalanceOf(cDAI) 402 if err != nil { 403 t.Errorf("Error getting DAI balance") 404 } 405 406 actions := new(Actions) 407 flashLoanActions := new(Actions) 408 409 flashLoanActions.Add( 410 defiClient.Compound().SupplyActions(big.NewInt(1e18), DAI), 411 defiClient.SupplyFundActions(big.NewInt(2e18), DAI), 412 defiClient.Compound().RedeemActions(big.NewInt(1), DAI), 413 ) 414 415 actions.Add( 416 // defiClient.SupplyFundActions(big.NewInt(1e18), DAI), 417 defiClient.Aave().FlashLoanActions( 418 big.NewInt(1e18), 419 DAI, 420 flashLoanActions, 421 ), 422 ) 423 424 err = defiClient.ExecuteActions(actions) 425 426 if err != nil { 427 t.Errorf("Failed to interact with Furucombo: %v", err) 428 } 429 430 aftercDAI, err := defiClient.BalanceOf(cDAI) 431 if beforecDAI.Cmp(aftercDAI) != -1 { 432 t.Errorf("cdai balance not increasing.") 433 } 434 } 435 436 // func TestInteractWithFurucomboFlashSwapCompound(t *testing.T) { 437 // Approve(defiClient, DAI, common.HexToAddress(FurucomboAddr), big.NewInt(2e18)) 438 // beforecDAI, err := defiClient.BalanceOf(cDAI) 439 // if err != nil { 440 // t.Errorf("Error getting DAI balance") 441 // } 442 443 // actions := new(Actions) 444 // flashLoanActions := new(Actions) 445 446 // flashLoanActions.Add( 447 // defiClient.Compound().SupplyActions(big.NewInt(1e18), DAI), 448 // defiClient.SupplyFundActions(big.NewInt(2e18), DAI), 449 // defiClient.Compound().RedeemActions(big.NewInt(1), DAI), 450 // ) 451 452 // actions.Add( 453 // defiClient.Uniswap().FlashSwapActions( 454 // big.NewInt(1e18), 455 // DAI, 456 // DAI, 457 // flashLoanActions, 458 // ), 459 // ) 460 461 // err = defiClient.ExecuteActions(actions) 462 463 // if err != nil { 464 // t.Errorf("Failed to interact with Furucombo: %v", err) 465 // } 466 467 // aftercDAI, err := defiClient.BalanceOf(cDAI) 468 // if beforecDAI.Cmp(aftercDAI) != -1 { 469 // t.Errorf("cdai balance not increasing.") 470 // } 471 // } 472 473 func TestInteractWithFurucomboCurve(t *testing.T) { 474 Approve(defiClient, DAI, common.HexToAddress(ProxyAddr), big.NewInt(2e18)) 475 beforeUSDC, err := defiClient.BalanceOf(USDC) 476 if err != nil { 477 t.Errorf("Error getting DAI balance") 478 } 479 480 actions := new(Actions) 481 482 actions.Add( 483 defiClient.Curve().ExchangeActions( 484 common.HexToAddress(c3Pool), 485 CoinToAddressMap[DAI], 486 CoinToAddressMap[USDC], 487 big.NewInt(0), 488 big.NewInt(1), 489 big.NewInt(1e18), 490 big.NewInt(1e5)), 491 ) 492 493 err = defiClient.ExecuteActions(actions) 494 495 if err != nil { 496 t.Errorf("Failed to interact with Furucombo: %v", err) 497 } 498 499 afterUSDC, err := defiClient.BalanceOf(USDC) 500 if beforeUSDC.Cmp(afterUSDC) != -1 { 501 t.Errorf("USDC balance not increasing. %v %v", beforeUSDC, afterUSDC) 502 } 503 } 504 505 // Supplying DAI to the Curve 3 pool 506 func TestInteractWithFurucomboCurveAddLiquidity(t *testing.T) { 507 Approve(defiClient, DAI, common.HexToAddress(ProxyAddr), big.NewInt(2e18)) 508 beforeDAI, err := defiClient.BalanceOf(DAI) 509 if err != nil { 510 t.Errorf("Error getting DAI balance") 511 } 512 513 actions := new(Actions) 514 515 actions.Add( 516 defiClient.Curve().AddLiquidityActions( 517 common.HexToAddress(c3Pool), 518 common.HexToAddress(threePoolCrv), 519 []common.Address{CoinToAddressMap[DAI], CoinToAddressMap[USDC], CoinToAddressMap[USDT]}, 520 []*big.Int{big.NewInt(1e18), big.NewInt(0), big.NewInt(0)}, 521 big.NewInt(0)), 522 ) 523 524 err = defiClient.ExecuteActions(actions) 525 526 if err != nil { 527 t.Errorf("Failed to interact with Furucombo: %v", err) 528 } 529 530 afterDAI, err := defiClient.BalanceOf(DAI) 531 if beforeDAI.Cmp(afterDAI) != 1 { 532 t.Errorf("USDC balance not decreasing. %v %v", beforeDAI, afterDAI) 533 } 534 } 535 536 func TestInteractWithFurucomboMaker(t *testing.T) { 537 beforeDAI, err := defiClient.BalanceOf(DAI) 538 if err != nil { 539 t.Errorf("Error getting DAI balance") 540 } 541 542 actions := new(Actions) 543 544 collateralAmount := big.NewInt(0) 545 collateralAmount.SetString("2000000000000000000", 10) 546 547 outputAmount := big.NewInt(0) 548 outputAmount.SetString("511000000000000000000", 10) 549 actions.Add( 550 defiClient.Maker().GenerateDaiAction(collateralAmount, outputAmount, ETH), 551 ) 552 553 err = defiClient.ExecuteActions(actions) 554 555 if err != nil { 556 t.Errorf("Failed to interact with Furucombo: %v", err) 557 } 558 559 afterDAI, err := defiClient.BalanceOf(DAI) 560 if beforeDAI.Cmp(afterDAI) != -1 { 561 t.Errorf("dai balance not increasing: %v, %v.", beforeDAI, afterDAI) 562 } 563 } 564 565 func TestInteractWithFurucomboMakerUSDC(t *testing.T) { 566 beforeDAI, err := defiClient.BalanceOf(DAI) 567 568 if err != nil { 569 t.Errorf("Error getting DAI balance") 570 } 571 572 Approve(defiClient, USDC, common.HexToAddress(ProxyAddr), big.NewInt(1e18)) 573 actions := new(Actions) 574 575 collateralAmount := big.NewInt(0) 576 collateralAmount.SetString("1000000000", 10) 577 outputAmount := big.NewInt(0) 578 outputAmount.SetString("520000000000000000000", 10) 579 actions.Add( 580 defiClient.Uniswap().SwapActions(big.NewInt(5e18), USDC, ETH), 581 defiClient.Maker().GenerateDaiAction(collateralAmount, outputAmount, USDC), 582 ) 583 584 err = defiClient.ExecuteActions(actions) 585 586 if err != nil { 587 t.Errorf("Failed to interact with Furucombo: %v", err) 588 } 589 590 afterDAI, err := defiClient.BalanceOf(DAI) 591 if beforeDAI.Cmp(afterDAI) != -1 { 592 t.Errorf("dai balance not increasing: %v, %v.", beforeDAI, afterDAI) 593 } 594 } 595 596 func TestInteractWithFurucomboBalancer(t *testing.T) { 597 beforeDAI, err := defiClient.BalanceOf(ETH) 598 Approve(defiClient, DAI, common.HexToAddress(ProxyAddr), big.NewInt(6e18)) 599 600 if err != nil { 601 t.Errorf("Error getting DAI balance") 602 } 603 604 actions := new(Actions) 605 606 actions.Add( 607 defiClient.Balancer().Swap(DAI, ETH, big.NewInt(6e18)), 608 ) 609 610 err = defiClient.ExecuteActions(actions) 611 612 if err != nil { 613 t.Errorf("Failed to interact with Furucombo: %v", err) 614 } 615 616 afterDAI, err := defiClient.BalanceOf(ETH) 617 if beforeDAI.Cmp(afterDAI) != -1 { 618 t.Errorf("dai balance not increasing: %v, %v.", beforeDAI, afterDAI) 619 } 620 }