github.com/frostornge/solgen@v0.1.3/main.go (about) 1 package main 2 3 import ( 4 "os" 5 "path" 6 "path/filepath" 7 8 "github.com/frostornge/solgen/bind" 9 "github.com/frostornge/solgen/deployment" 10 "github.com/frostornge/solgen/proto" 11 "github.com/frostornge/solgen/utils" 12 "github.com/spf13/cobra" 13 ) 14 15 var ( 16 rootCmd = &cobra.Command{ 17 Use: "solgen", 18 Short: "solgen is solidity bind generator", 19 Long: "Solgen is a tool for generate solidity binds.\n" + "This application helps to generate go/proto bind of solidity.\n", 20 Version: "0.0.0", 21 Run: run(), 22 } 23 24 rootFlags struct { 25 typeFlag string 26 inputPath string 27 outputPath string 28 optionPath string 29 } 30 ) 31 32 const ( 33 BindTypeKlay = "klay" 34 BindTypeProto = "proto" 35 ) 36 37 func init() { 38 rflags := rootCmd.PersistentFlags() 39 rflags.StringVarP(&rootFlags.typeFlag, "type", "t", BindTypeKlay, "Bind type") 40 rflags.StringVarP(&rootFlags.inputPath, "input", "i", "http://localhost:8500", "Input path") 41 rflags.StringVarP(&rootFlags.outputPath, "output", "o", "./out/", "Output path") 42 rflags.StringVarP(&rootFlags.optionPath, "option", "p", "./option.json", "Option path") 43 } 44 45 func openFile(path string) (*os.File, error) { 46 file, err := os.OpenFile(path, os.O_WRONLY, os.ModePerm) 47 if os.IsNotExist(err) { 48 file, err = os.Create(path) 49 if err != nil { 50 return nil, err 51 } 52 } else if err != nil { 53 return nil, err 54 } 55 56 return file, nil 57 } 58 59 func writeFile(path, filename string, contract deployment.Deployment, option bind.Customs) error { 60 bindFile, err := openFile(filepath.Join(path, filename+".go")) 61 if err != nil { 62 return err 63 } 64 defer bindFile.Close() 65 66 wrapFile, err := openFile(filepath.Join(path, filename+"_wrapper.go")) 67 if err != nil { 68 return err 69 } 70 defer wrapFile.Close() 71 72 // Generate the contract binding 73 if err := bind.Bind( 74 bindFile, wrapFile, 75 filename, contract.RawABI, "adapter", 76 option, bind.PlatformKlay, bind.LangGo, 77 ); err != nil { 78 return err 79 } 80 return nil 81 } 82 83 func main() { 84 contracts, err := deployment.GetDeploymentsFrom("http://localhost:8500") 85 if err != nil { 86 panic(err) 87 } 88 89 options := map[string]bind.Customs{ 90 "Accounts": { 91 Structs: map[string]string{"(address,uint8,address,address)": "types.Account"}, 92 Methods: map[string]bool{ 93 "create": true, 94 "createTemporary": true, 95 "unlockTemporary": true, 96 "setController": true, 97 "getAccount": true, 98 "getAccountByIdentityHash": true, 99 "getAccountId": true, 100 "getAccountIdByIdentityHash": true, 101 "getAccountIdFromSignature": true, 102 "isTemporary": true, 103 "isControllerOf": true, 104 "exists": true, 105 }, 106 }, 107 "AppRegistry": { 108 Structs: map[string]string{"(string,address,address)": "types.App"}, 109 Methods: map[string]bool{ 110 "register": true, 111 "unregister": true, 112 "get": true, 113 "exists": true, 114 "isOwner": true, 115 "transferAppOwner": true, 116 }, 117 }, 118 "ControllerRegistry": { 119 Structs: map[string]string{"(address,uint256)": "types.DataController"}, 120 Methods: map[string]bool{ 121 "register": true, 122 "get": true, 123 "exists": true, 124 }, 125 }, 126 "Consents": { 127 Structs: map[string]string{"(uint8,string,bool)": "types.ConsentData", "(uint8,string,bool)[]": "[]types.ConsentData"}, 128 Methods: map[string]bool{ 129 "consent": true, 130 "consentMany": true, 131 "consentByController": true, 132 "consentManyByController": true, 133 "modifyConsentByController": true, 134 "modifyConsentManyByController": true, 135 "isAllowed": true, 136 "isAllowedAt": true, 137 }, 138 }, 139 "DataTypeRegistry": { 140 Structs: map[string]string{"(string,address,bytes32)": "types.DataType"}, 141 Methods: map[string]bool{ 142 "register": true, 143 "unregister": true, 144 "get": true, 145 "exists": true, 146 "isOwner": true, 147 }, 148 }, 149 "Exchange": { 150 Structs: map[string]string{ 151 "(string,address,bytes20[],uint256,uint256,(address,bytes4,bytes),uint8)": "types.Offer", 152 "(address,bytes4,bytes)": "types.Escrow", 153 }, 154 Methods: map[string]bool{ 155 "prepare": true, 156 "addDataIds": true, 157 "order": true, 158 "cancel": true, 159 "settle": true, 160 "reject": true, 161 "offerExists": true, 162 "getOffer": true, 163 "getOfferMembers": true, 164 }, 165 }, 166 } 167 168 if err := os.RemoveAll("./test/bind"); err != nil { 169 panic(err) 170 } 171 172 if err := os.MkdirAll("./test/bind", os.ModePerm); err != nil { 173 panic(err) 174 } 175 176 for name, contract := range contracts { 177 fn := utils.ToSnakeCase(name) 178 fp, _ := filepath.Abs(path.Join("./test", "bind")) 179 180 if err := writeFile(fp, fn, contract, options[name]); err != nil { 181 panic(err) 182 } 183 } 184 185 //if err := rootCmd.Execute(); err != nil { 186 // panic(err) 187 //} 188 } 189 190 func run() func(*cobra.Command, []string) { 191 return func(cmd *cobra.Command, args []string) { 192 deployments, err := deployment.GetDeploymentsFrom(rootFlags.inputPath) 193 if err != nil { 194 panic(err) 195 } 196 197 switch rootFlags.typeFlag { 198 case BindTypeKlay: 199 case BindTypeProto: 200 if err := proto.GenerateBind(rootFlags.outputPath, deployments, proto.Options{}); err != nil { 201 panic(err) 202 } 203 } 204 } 205 }