github.com/cwntr/go-defi@v0.0.0-20210629134751-07f9ec2f7e66/README.md (about) 1 # Go Defi Framework 2 3 This is a framework to combine multiple interactions with Ethereum Defi framework into one atomic transaction. 4 5 This library support flash loan, so you can use this library to do things like arbitrage. The things that you can do includes: 6 - Arbitrage yCRV ([here](https://furucombo.app/explore/combo_curve_00015)) 7 - Leverage Short DAI ([here](https://furucombo.app/explore/combo_maker_00009)) 8 9 ### Supported Protocols 10 - Compound 11 - Supply token: `client.Compound().SupplyActions()` 12 - Redeem token: `client.Compound().RedeemActions()` 13 - Aave 14 - Flash loan: `client.Aave().FlashLoanActions()` 15 - Uniswap 16 - Swap: `client.Uniswap().SwapActions()` 17 - Kyberswap 18 - Swap: `client.Kyberswap().SwapActions()` 19 - Yearn 20 - Supply to Vault: `client.Yearn().AddLiquidityActions()` 21 - Withdraw from Vault: `client.Yearn().RemoveLiquidityActions()` 22 - Curve 23 - Exchange token: `client.Curve().ExchangeActions()` 24 - Exchange underlying token `client.Curve().ExchangeUnderlyingActions` 25 - Add Liquidity: `client.Curve().AddLiquidityActions()` 26 - Remove Liquidity: `client.Curve().RemoveLiquidityActions()` 27 - Sushiswap 28 - Swap: `client.Sushiswap().SwapActions()` 29 - MakerDao 30 - Create New Vault: `client.Maker().GenerateDaiAction()` 31 - Burn DAI and reduce debt: `client.Maker().WipeAction()` 32 - Add more collateral to vault: `client.Maker().DepositCollateralActions()` 33 34 ### APIs 35 36 The main API for this tool is the `ExecuteActions` API. 37 38 The tool sends the transaction into a proxy contract, and let the proxy 39 contract to actually interact with the underlying protocols. 40 41 A graph illustration of the idea is the following: 42  43 44 The `Client`, i.e. this tool interact with the proxy contract, the proxy contract does the following: 45 1. check if the handler is valid through the `isValid` function of the `Registry` contract 46 2. If step 1 is successful, delegate call the compound handler contract to interact with the underlyng compound code. 47 48 In the client we create an empty `Actions` by doing: 49 ```go 50 actions := new(client.Actions) 51 ``` 52 We then add more `Actions` by calling the `Add` function of the `Actions` struct: 53 ```go 54 actions.Add( 55 defiClient.Compound().SupplyActions(big.NewInt(1e18), client.DAI), 56 defiClient.Uniswap().SwapActions(big.NewInt(1e18), client.DAI, client.ETH), 57 ) 58 ``` 59 After that we send the `actions` by calling the `ExecuteActions` function: 60 ```go 61 err = defiClient.ExecuteActions(actions) 62 ``` 63 And done we have executed a transaction 64 65 ## Complete Working Example for flash loan 66 Initialize a flash loan 67 68 ```go 69 package main 70 71 import ( 72 "log" 73 "math/big" 74 75 "github.com/524119574/go-defi/client" 76 "github.com/ethereum/go-ethereum/accounts/abi/bind" 77 "github.com/ethereum/go-ethereum/common" 78 "github.com/ethereum/go-ethereum/crypto" 79 "github.com/ethereum/go-ethereum/ethclient" 80 ) 81 82 func main() { 83 key, err := crypto.HexToECDSA("... your private key here ...") 84 if err != nil { 85 log.Fatalf("Failed to create private key: %v", err) 86 } 87 ethClient, err := ethclient.Dial("... ETH gateway, could be Infura ...") 88 if err != nil { 89 log.Fatalf("Failed to connect to ETH: %v", err) 90 } 91 92 // Create a New Defi Client 93 defiClient := client.NewClient(bind.NewKeyedTransactor(key), ethClient) 94 if err != nil { 95 log.Fatalf("Error creating client: %v.", err) 96 } 97 98 // Approve the Transaction. 99 client.Approve(defiClient, client.DAI, common.HexToAddress(client.FurucomboAddr), big.NewInt(2e18)) 100 if err != nil { 101 log.Fatal("Error getting DAI balance") 102 } 103 104 actions := new(client.Actions) 105 106 // The action that you want to take before returning the loaned fund 107 flashLoanActions := new(client.Actions) 108 109 // Supply 1 Dai to Compound 110 flashLoanActions.Add( 111 defiClient.Compound().SupplyActions(big.NewInt(1e18), client.DAI), 112 ) 113 // Transfer some fund so that we have enough to pay back the principal and interest 114 flashLoanActions.Add( 115 defiClient.SupplyFundActions(big.NewInt(2e18), client.DAI), 116 ) 117 // Redeem some cDAI 118 flashLoanActions.Add( 119 defiClient.Compound().RedeemActions(big.NewInt(1), client.DAI), 120 ) 121 122 // Add the flash loan action 123 actions.Add( 124 defiClient.Aave().FlashLoanActions( 125 big.NewInt(1e18), 126 client.DAI, 127 flashLoanActions, 128 ), 129 ) 130 131 err = defiClient.ExecuteActions(actions) 132 133 if err != nil { 134 log.Fatalf("Failed to interact with Furucombo: %v", err) 135 } 136 } 137 138 ``` 139 Go Version: 1.13 140 141 ## Documentation 142 A more thorough documentation can be found [here](https://godoc.org/github.com/524119574/go-defi/client). 143 144 ## Contact 145 - Leonard Ge [Twitter](https://twitter.com/ge_leonard) 146 - Kaihua Qin 147 - Liyi Zhou 148 - Arthur Gervais 149 150 ## Acknowledgement 151 This is project is inspired by [Furucombo](https://furucombo.app/).