How to use New method of launcher Package

Best Rod code snippet using launcher.New

build-tool-detector_test.go

Source:build-tool-detector_test.go Github

copy

Full Screen

...19 Context("Internal Server Error", func() {20 var service *goa.Service21 var configuration *config.Configuration22 BeforeEach(func() {23 service = goa.New("build-tool-detector")24 configuration = config.New()25 // Mock auth service with success response26 authBodyString, err := ioutil.ReadFile("../controllers/test/mock/fabric8_auth_backend/return_token.json")27 Expect(err).Should(BeNil())28 configuration = config.New()29 gock.New(configuration.GetAuthServiceURL()).30 Get("/api/token").31 Reply(200).32 BodyString(string(authBodyString))33 })34 AfterEach(func() {35 gock.Off()36 })37 It("Non-existent owner name -- 404 Repository Not Found", func() {38 // Fail instead of expect- and make this a method39 bodyString, err := ioutil.ReadFile("../controllers/test/mock/fabric8_launcher_backend/not_found_repo_branch.json")40 Expect(err).Should(BeNil())41 gock.New("https://api.github.com").42 Get("/repos/fabric8-launcherz/launcher-backend/branches/master").43 Reply(404).44 BodyString(string(bodyString))45 branch := "master"46 test.ShowBuildToolDetectorNotFound(GinkgoT(), nil, nil, controllers.NewBuildToolDetectorController(service, *configuration), "https://github.com/fabric8-launcherz/launcher-backend", &branch)47 })48 It("Non-existent owner name -- 404 Owner Not Found", func() {49 bodyString, err := ioutil.ReadFile("../controllers/test/mock/fabric8_launcher_backend/not_found_repo_branch.json")50 Expect(err).Should(BeNil())51 gock.New("https://api.github.com").52 Get("/repos/fabric8-launcher/launcher-backendz/branches/master").53 Reply(404).54 BodyString(string(bodyString))55 branch := "master"56 test.ShowBuildToolDetectorNotFound(GinkgoT(), nil, nil, controllers.NewBuildToolDetectorController(service, *configuration), "https://github.com/fabric8-launcher/launcher-backendz", &branch)57 })58 It("Non-existent branch name -- 404 Branch Not Found", func() {59 bodyString, err := ioutil.ReadFile("../controllers/test/mock/fabric8_launcher_backend/not_found_branch.json")60 Expect(err).Should(BeNil())61 gock.New("https://api.github.com").62 Get("/repos/fabric8-launcher/launcher-backend/branches/masterz").63 Reply(404).64 BodyString(string(bodyString))65 test.ShowBuildToolDetectorNotFound(GinkgoT(), nil, nil, controllers.NewBuildToolDetectorController(service, *configuration), "https://github.com/fabric8-launcher/launcher-backend/tree/masterz", nil)66 })67 It("Invalid URL -- 400 Bad Request", func() {68 branch := "master"69 test.ShowBuildToolDetectorBadRequest(GinkgoT(), nil, nil, controllers.NewBuildToolDetectorController(service, *configuration), "fabric8-launcher/launcher-backend", &branch)70 })71 It("Unsupported Git Service -- 500 Internal Server Error", func() {72 branch := "master"73 test.ShowBuildToolDetectorInternalServerError(GinkgoT(), nil, nil, controllers.NewBuildToolDetectorController(service, *configuration), "http://gitlab.com/fabric8-launcher/launcher-backend", &branch)74 })75 It("Invalid URL and Branch -- 500 Internal Server Error", func() {76 test.ShowBuildToolDetectorBadRequest(GinkgoT(), nil, nil, controllers.NewBuildToolDetectorController(service, *configuration), "", nil)77 })78 })79 Context("Okay", func() {80 var service *goa.Service81 var configuration *config.Configuration82 BeforeEach(func() {83 service = goa.New("build-tool-detector")84 // Mock auth service with success response85 authBodyString, err := ioutil.ReadFile("../controllers/test/mock/fabric8_auth_backend/return_token.json")86 Expect(err).Should(BeNil())87 configuration = config.New()88 gock.New(configuration.GetAuthServiceURL()).89 Get("/api/token").90 Reply(200).91 BodyString(string(authBodyString))92 })93 AfterEach(func() {94 os.Unsetenv("BUILD_TOOL_DETECTOR_GITHUB_CLIENT_ID")95 os.Unsetenv("BUILD_TOOL_DETECTOR_GITHUB_CLIENT_SECRET")96 gock.Off()97 })98 It("Recognize Unknown - Branch field populated", func() {99 bodyString, err := ioutil.ReadFile("../controllers/test/mock/fabric8_wit/ok_branch.json")100 Expect(err).Should(BeNil())101 gock.New("https://api.github.com").102 Get("/repos/fabric8-services/fabric8-wit/branches/master").103 Reply(200).104 BodyString(string(bodyString))105 bodyString, err = ioutil.ReadFile("../controllers/test/mock/fabric8_wit/not_found_contents.json")106 Expect(err).Should(BeNil())107 gock.New("https://api.github.com").108 Get("/repos/fabric8-services/fabric8-wit/contents/pom.xml").109 Reply(404).110 BodyString(string(bodyString))111 branch := "master"112 _, buildTool := test.ShowBuildToolDetectorOK(GinkgoT(), nil, nil, controllers.NewBuildToolDetectorController(service, *configuration), "https://github.com/fabric8-services/fabric8-wit", &branch)113 Expect(buildTool.BuildToolType).Should(Equal("unknown"), "buildTool should not be empty")114 })115 It("Recognize Unknown - Branch included in URL", func() {116 bodyString, err := ioutil.ReadFile("../controllers/test/mock/fabric8_wit/ok_branch.json")117 Expect(err).Should(BeNil())118 gock.New("https://api.github.com").119 Get("/repos/fabric8-services/fabric8-wit/branches/master").120 Reply(200).121 BodyString(string(bodyString))122 bodyString, err = ioutil.ReadFile("../controllers/test/mock/fabric8_wit/not_found_contents.json")123 Expect(err).Should(BeNil())124 gock.New("https://api.github.com").125 Get("/repos/fabric8-services/fabric8-wit/contents/pom.xml").126 Reply(404).127 BodyString(string(bodyString))128 _, buildTool := test.ShowBuildToolDetectorOK(GinkgoT(), nil, nil, controllers.NewBuildToolDetectorController(service, *configuration), "https://github.com/fabric8-services/fabric8-wit/tree/master", nil)129 Expect(buildTool.BuildToolType).Should(Equal("unknown"), "buildTool should not be empty")130 })131 It("Recognize Maven - Branch field populated", func() {132 bodyString, err := ioutil.ReadFile("../controllers/test/mock/fabric8_launcher_backend/ok_branch.json")133 Expect(err).Should(BeNil())134 gock.New("https://api.github.com").135 Get("/repos/fabric8-launcher/launcher-backend/branches/master").136 Reply(200).137 BodyString(string(bodyString))138 bodyString, err = ioutil.ReadFile("../controllers/test/mock/fabric8_launcher_backend/ok_contents.json")139 Expect(err).Should(BeNil())140 gock.New("https://api.github.com").141 Get("/repos/fabric8-launcher/launcher-backend/contents/pom.xml").142 Reply(200).143 BodyString(string(bodyString))144 branch := "master"145 _, buildTool := test.ShowBuildToolDetectorOK(GinkgoT(), nil, nil, controllers.NewBuildToolDetectorController(service, *configuration), "https://github.com/fabric8-launcher/launcher-backend", &branch)146 Expect(buildTool.BuildToolType).Should(Equal("maven"), "buildTool should not be empty")147 })148 It("Recognize Maven - Branch included in URL", func() {149 bodyString, err := ioutil.ReadFile("../controllers/test/mock/fabric8_launcher_backend/ok_branch.json")150 Expect(err).Should(BeNil())151 gock.New("https://api.github.com").152 Get("/repos/fabric8-launcher/launcher-backend/branches/master").153 Reply(200).154 BodyString(string(bodyString))155 bodyString, err = ioutil.ReadFile("../controllers/test/mock/fabric8_launcher_backend/ok_contents.json")156 Expect(err).Should(BeNil())157 gock.New("https://api.github.com").158 Get("/repos/fabric8-launcher/launcher-backend/contents/pom.xml").159 Reply(200).160 BodyString(string(bodyString))161 _, buildTool := test.ShowBuildToolDetectorOK(GinkgoT(), nil, nil, controllers.NewBuildToolDetectorController(service, *configuration), "https://github.com/fabric8-launcher/launcher-backend/tree/master", nil)162 Expect(buildTool.BuildToolType).Should(Equal("maven"), "buildTool should not be empty")163 })164 It("Recognize NodeJS - Branch included in URL", func() {165 bodyString, err := ioutil.ReadFile("../controllers/test/mock/fabric8_ui/ok_branch.json")166 Expect(err).Should(BeNil())167 gock.New("https://api.github.com").168 Get("/repos/fabric8-ui/fabric8-ui/branches/master").169 Reply(200).170 BodyString(string(bodyString))171 bodyString, err = ioutil.ReadFile("../controllers/test/mock/fabric8_ui/ok_contents.json")172 Expect(err).Should(BeNil())173 gock.New("https://api.github.com").174 Get("/repos/fabric8-ui/fabric8-ui/contents/package.json").175 Reply(200).176 BodyString(string(bodyString))177 _, buildTool := test.ShowBuildToolDetectorOK(GinkgoT(), nil, nil, controllers.NewBuildToolDetectorController(service, *configuration), "https://github.com/fabric8-ui/fabric8-ui/tree/master", nil)178 Expect(buildTool.BuildToolType).Should(Equal("nodejs"), "buildTool should be nodejs")179 })180 It("Recognize Golang - Branch included in URL", func() {181 bodyString, err := ioutil.ReadFile("../controllers/test/mock/fabric8_wit/ok_branch.json")182 Expect(err).Should(BeNil())183 gock.New("https://api.github.com").184 Get("/repos/fabric8-services/fabric8-wit/branches/master").185 Reply(200).186 BodyString(string(bodyString))187 bodyString, err = ioutil.ReadFile("../controllers/test/mock/fabric8_wit/ok_contents.json")188 Expect(err).Should(BeNil())189 gock.New("https://api.github.com").190 Get("/repos/fabric8-services/fabric8-wit/contents/main.go").191 Reply(200).192 BodyString(string(bodyString))193 _, buildTool := test.ShowBuildToolDetectorOK(GinkgoT(), nil, nil, controllers.NewBuildToolDetectorController(service, *configuration), "https://github.com/fabric8-services/fabric8-wit/tree/master", nil)194 Expect(buildTool.BuildToolType).Should(Equal("golang"), "buildTool should be golang")195 })196 })197})...

Full Screen

Full Screen

api_crowd_sale.go

Source:api_crowd_sale.go Github

copy

Full Screen

...8)9type CrowdSaleAPIHandler struct {10 launcher *launcher.Launcher11}12func NewCrowdSaleAPIHandler(opts *launcher.Opts) (*CrowdSaleAPIHandler,error) {13 h := &CrowdSaleAPIHandler{}14 la,err := launcher.NewLauncherDefault(opts)15 if err != nil {16 return nil, err17 }18 h.launcher = la19 return h,nil20}21type CrowdSaleReq struct {22 TokenAddress string23 TargetFunds string24 Price string25 StartTime string26 EndTime string27}28type CrowdSaleValueGet struct {29 Address string30}31type CrowdSaleValueGot struct {32 Value string `json:"value"`33}34type CrowdSaleWithdrawSet struct {35 Address string36}37type CrowdSaleTxGot struct {38 Hash string `json:"hash"`39}40type CrowdSaleTimeSet struct {41 Address string42 Time string43}44type CrowdSalePriceSet struct {45 Address string46 Price string47}48type CrowdSaleTargetFundsSet struct {49 Address string50 Amount string51}52func (handler *CrowdSaleAPIHandler) NewAndDeploy(req CrowdSaleReq, resp *ContractResp) error {53 tokenAddr := common.HexToAddress(req.TokenAddress)54 targetFunds := *uint256.NewUInt256(req.TargetFunds)55 price := *uint256.NewUInt256(req.Price)56 startTime := *uint256.NewUInt256(req.StartTime)57 endTime := *uint256.NewUInt256(req.EndTime)58 opts := &launcher.CrowdSaleOpts{59 TokenAddress: tokenAddr,60 TargetFunds: targetFunds,61 Price: price,62 StartTime: startTime,63 EndTime: endTime,64 }65 contract, err := handler.launcher.DeployCrowdSale(opts)66 if err != nil {67 return jsonerrs.New(-32001,err.Error())68 }69 resp.Address = contract.Address70 resp.TxHash = contract.TxHash71 resp.ABI = contract.ABI72 resp.Data = contract.Data73 return nil74}75func (handler *CrowdSaleAPIHandler) GetFundsSoldTotal(get CrowdSaleValueGet, got *CrowdSaleValueGot) error {76 contractAddr := get.Address77 crowdSale, err := handler.launcher.LoadCrowdSaleByContractAddr(contractAddr)78 if err != nil {79 return jsonerrs.New(-32001,err.Error())80 }81 value,err := crowdSale.FundsSoldTotal(nil)82 if err != nil {83 return jsonerrs.New(-32001,err.Error())84 }85 got.Value = "0x" + value.Text(16)86 return nil87}88func (handler *CrowdSaleAPIHandler) GetFundsRaisedTotal(get CrowdSaleValueGet, got *CrowdSaleValueGot) error {89 contractAddr := get.Address90 crowdSale, err := handler.launcher.LoadCrowdSaleByContractAddr(contractAddr)91 if err != nil {92 return jsonerrs.New(-32001,err.Error())93 }94 value,err := crowdSale.FundsRaisedTotal(nil)95 if err != nil {96 return jsonerrs.New(-32001,err.Error())97 }98 got.Value = "0x" + value.Text(16)99 return nil100}101func (handler *CrowdSaleAPIHandler) GetToken(get CrowdSaleValueGet, got *CrowdSaleValueGot) error {102 contractAddr := get.Address103 crowdSale, err := handler.launcher.LoadCrowdSaleByContractAddr(contractAddr)104 if err != nil {105 return jsonerrs.New(-32001,err.Error())106 }107 value,err := crowdSale.Token(nil)108 if err != nil {109 return jsonerrs.New(-32001,err.Error())110 }111 got.Value = value.Hex()112 return nil113}114func (handler *CrowdSaleAPIHandler) GetPrice(get CrowdSaleValueGet, got *CrowdSaleValueGot) error {115 contractAddr := get.Address116 crowdSale, err := handler.launcher.LoadCrowdSaleByContractAddr(contractAddr)117 if err != nil {118 return jsonerrs.New(-32001,err.Error())119 }120 value,err := crowdSale.Price(nil)121 if err != nil {122 return jsonerrs.New(-32001,err.Error())123 }124 got.Value = "0x" + value.Text(16)125 return nil126}127func (handler *CrowdSaleAPIHandler) GetTargetFunds(get CrowdSaleValueGet, got *CrowdSaleValueGot) error {128 contractAddr := get.Address129 crowdSale, err := handler.launcher.LoadCrowdSaleByContractAddr(contractAddr)130 if err != nil {131 return jsonerrs.New(-32001,err.Error())132 }133 value,err := crowdSale.TargetFunds(nil)134 if err != nil {135 return jsonerrs.New(-32001,err.Error())136 }137 got.Value = "0x" + value.Text(16)138 return nil139}140func (handler *CrowdSaleAPIHandler) GetStartTime(get CrowdSaleValueGet, got *CrowdSaleValueGot) error {141 contractAddr := get.Address142 crowdSale, err := handler.launcher.LoadCrowdSaleByContractAddr(contractAddr)143 if err != nil {144 return jsonerrs.New(-32001,err.Error())145 }146 value,err := crowdSale.StartTime(nil)147 if err != nil {148 return jsonerrs.New(-32001,err.Error())149 }150 got.Value = "0x" + value.Text(16)151 return nil152}153func (handler *CrowdSaleAPIHandler) GetEndTime(get CrowdSaleValueGet, got *CrowdSaleValueGot) error {154 contractAddr := get.Address155 crowdSale, err := handler.launcher.LoadCrowdSaleByContractAddr(contractAddr)156 if err != nil {157 return jsonerrs.New(-32001,err.Error())158 }159 value,err := crowdSale.EndTime(nil)160 if err != nil {161 return jsonerrs.New(-32001,err.Error())162 }163 got.Value = "0x" + value.Text(16)164 return nil165}166func (handler *CrowdSaleAPIHandler) GetTotalAmount(get CrowdSaleValueGet, got *CrowdSaleValueGot) error {167 contractAddr := get.Address168 crowdSale, err := handler.launcher.LoadCrowdSaleByContractAddr(contractAddr)169 if err != nil {170 return jsonerrs.New(-32001,err.Error())171 }172 value,err := crowdSale.TotalAmount(nil)173 if err != nil {174 return jsonerrs.New(-32001,err.Error())175 }176 got.Value = "0x" + value.Text(16)177 return nil178}179func btoi(b bool) int {180 i := 0181 if b {182 i = 1183 }184 return i185}186func (handler *CrowdSaleAPIHandler) IsActive(get CrowdSaleValueGet, got *CrowdSaleValueGot) error {187 contractAddr := get.Address188 crowdSale, err := handler.launcher.LoadCrowdSaleByContractAddr(contractAddr)189 if err != nil {190 return jsonerrs.New(-32001,err.Error())191 }192 value,err := crowdSale.IsActive(nil)193 if err != nil {194 return jsonerrs.New(-32001,err.Error())195 }196 got.Value = strconv.Itoa(btoi(value))197 return nil198}199func (handler *CrowdSaleAPIHandler) IsFinished(get CrowdSaleValueGet, got *CrowdSaleValueGot) error {200 contractAddr := get.Address201 crowdSale, err := handler.launcher.LoadCrowdSaleByContractAddr(contractAddr)202 if err != nil {203 return jsonerrs.New(-32001,err.Error())204 }205 value,err := crowdSale.IsFinished(nil)206 if err != nil {207 return jsonerrs.New(-32001,err.Error())208 }209 got.Value = strconv.Itoa(btoi(value))210 return nil211}212func (handler *CrowdSaleAPIHandler) IsUncompleted(get CrowdSaleValueGet, got *CrowdSaleValueGot) error {213 contractAddr := get.Address214 crowdSale, err := handler.launcher.LoadCrowdSaleByContractAddr(contractAddr)215 if err != nil {216 return jsonerrs.New(-32001,err.Error())217 }218 value,err := crowdSale.IsUncompleted(nil)219 if err != nil {220 return jsonerrs.New(-32001,err.Error())221 }222 got.Value = strconv.Itoa(btoi(value))223 return nil224}225func (handler *CrowdSaleAPIHandler) IsSalesCompleted(get CrowdSaleValueGet, got *CrowdSaleValueGot) error {226 contractAddr := get.Address227 crowdSale, err := handler.launcher.LoadCrowdSaleByContractAddr(contractAddr)228 if err != nil {229 return jsonerrs.New(-32001,err.Error())230 }231 value,err := crowdSale.IsSalesCompleted(nil)232 if err != nil {233 return jsonerrs.New(-32001,err.Error())234 }235 got.Value = strconv.Itoa(btoi(value))236 return nil237}238func (handler *CrowdSaleAPIHandler) Withdraw(set CrowdSaleWithdrawSet, got *CrowdSaleTxGot) error {239 contractAddr := set.Address240 crowdSale, err := handler.launcher.LoadCrowdSaleByContractAddr(contractAddr)241 if err != nil {242 return jsonerrs.New(-32001,err.Error())243 }244 txOpts, err := handler.launcher.DefaultTxOpts()245 if err != nil {246 return jsonerrs.New(-32001,err.Error())247 }248 tx, err := crowdSale.Withdraw(txOpts)249 if err != nil {250 return jsonerrs.New(-32001,err.Error())251 }252 got.Hash = tx.Hash().Hex()253 return nil254}255func (handler *CrowdSaleAPIHandler) SetStartTime(set CrowdSaleTimeSet, got *CrowdSaleTxGot) error {256 contractAddr := set.Address257 crowdSale, err := handler.launcher.LoadCrowdSaleByContractAddr(contractAddr)258 if err != nil {259 return jsonerrs.New(-32001,err.Error())260 }261 txOpts, err := handler.launcher.DefaultTxOpts()262 if err != nil {263 return jsonerrs.New(-32001,err.Error())264 }265 time := uint256.NewUInt256(set.Time)266 tx, err := crowdSale.SetStartTime(txOpts,time.ToBigInt())267 if err != nil {268 return jsonerrs.New(-32001,err.Error())269 }270 got.Hash = tx.Hash().Hex()271 return nil272}273func (handler *CrowdSaleAPIHandler) SetEndTime(set CrowdSaleTimeSet, got *CrowdSaleTxGot) error {274 contractAddr := set.Address275 crowdSale, err := handler.launcher.LoadCrowdSaleByContractAddr(contractAddr)276 if err != nil {277 return jsonerrs.New(-32001,err.Error())278 }279 txOpts, err := handler.launcher.DefaultTxOpts()280 if err != nil {281 return jsonerrs.New(-32001,err.Error())282 }283 time := uint256.NewUInt256(set.Time)284 tx, err := crowdSale.SetEndTime(txOpts,time.ToBigInt())285 if err != nil {286 return jsonerrs.New(-32001,err.Error())287 }288 got.Hash = tx.Hash().Hex()289 return nil290}291func (handler *CrowdSaleAPIHandler) SetPrice(set CrowdSalePriceSet, got *CrowdSaleTxGot) error {292 contractAddr := set.Address293 crowdSale, err := handler.launcher.LoadCrowdSaleByContractAddr(contractAddr)294 if err != nil {295 return jsonerrs.New(-32001,err.Error())296 }297 txOpts, err := handler.launcher.DefaultTxOpts()298 if err != nil {299 return jsonerrs.New(-32001,err.Error())300 }301 price := uint256.NewUInt256(set.Price)302 tx, err := crowdSale.SetPrice(txOpts,price.ToBigInt())303 if err != nil {304 return jsonerrs.New(-32001,err.Error())305 }306 got.Hash = tx.Hash().Hex()307 return nil308}309func (handler *CrowdSaleAPIHandler) SetTargetFunds(set CrowdSaleTargetFundsSet, got *CrowdSaleTxGot) error {310 contractAddr := set.Address311 crowdSale, err := handler.launcher.LoadCrowdSaleByContractAddr(contractAddr)312 if err != nil {313 return jsonerrs.New(-32001,err.Error())314 }315 txOpts, err := handler.launcher.DefaultTxOpts()316 if err != nil {317 return jsonerrs.New(-32001,err.Error())318 }319 amount := uint256.NewUInt256(set.Amount)320 tx, err := crowdSale.SetTargetFunds(txOpts,amount.ToBigInt())321 if err != nil {322 return jsonerrs.New(-32001,err.Error())323 }324 got.Hash = tx.Hash().Hex()325 return nil326}...

Full Screen

Full Screen

launcher.go

Source:launcher.go Github

copy

Full Screen

...34 Value *big.Int35 GasPrice *big.Int36 GasLimit uint6437}38func NewKeyFromECDSA(privateKeyECDSA *ecdsa.PrivateKey) *keystore.Key {39 id, err := uuid.NewRandom()40 if err != nil {41 panic(fmt.Sprintf("Could not create random uuid: %v", err))42 }43 key := &keystore.Key{44 Id: id,45 Address: crypto.PubkeyToAddress(privateKeyECDSA.PublicKey),46 PrivateKey: privateKeyECDSA,47 }48 return key49}50func ImportKeyStoreByFilepath(keyFilepath string, password string) (key *keystore.Key, err error) {51 jsonBytes, err := ioutil.ReadFile(keyFilepath)52 if err != nil {53 return nil, err54 }55 return keystore.DecryptKey(jsonBytes, password)56}57func NewLauncherDefault(opts *Opts) (*Launcher, error) {58 //data, err := ioutil.ReadFile("./config.json")59 //if err != nil {60 // return nil, err61 //}62 //err = json.Unmarshal(data, &c)63 //if err != nil{64 // return nil, err65 //}66 if opts.PrivateKey == "" {67 return nil, fmt.Errorf("private Key not be empyt")68 }69 privateKey,err := crypto.HexToECDSA(opts.PrivateKey)70 if err != nil {71 return nil, err72 }73 apiurl := opts.PRCApiPrefix74 apiurl = fmt.Sprintf("%s%s",apiurl,opts.RPCApiPath)75 if apiurl == "" {76 return nil, fmt.Errorf("RPC URL not be empyt")77 }78 return NewLauncher(apiurl, new(big.Int).SetUint64(opts.ChainId), privateKey)79}80// 创建启动器81func NewLauncher(rpcUrl string, chainId *big.Int, privateKey *ecdsa.PrivateKey) (*Launcher, error) {82 client, err := ethclient.Dial(rpcUrl)83 if err != nil {84 return nil, err85 }86 key := NewKeyFromECDSA(privateKey)87 return &Launcher{88 CLI: client,89 key: key,90 chainId: chainId,91 }, nil92}93func (launcher *Launcher) DefaultTxOpts() (*bind.TransactOpts, error) {94 privateKey := launcher.key.PrivateKey95 return bind.NewKeyedTransactorWithChainID(96 privateKey, launcher.chainId)97}98// 部署合约99func (launcher *Launcher) DeployERC20Token(opts *ERC20TokenOpts) (*Contract, error) {100 address := launcher.key.Address101 txOpts, err := launcher.DefaultTxOpts()102 if err != nil {103 return nil, err104 }105 initialSupply := uint256.NewUInt256(opts.InitialSupply).ToBigInt()106 address, tx, _, err := contract.DeployERC20Token(107 txOpts, launcher.CLI,opts.Name, opts.Symbol,108 initialSupply, opts.Decimals)109 if err != nil {110 return nil, err111 }112 txdata := "0x" + hex.EncodeToString(tx.Data())113 txhash := tx.Hash().Hex()114 con := &Contract{115 Address: address.Hex(),116 TxHash: txhash,117 ABI: contract.ERC20TokenABI,118 Data: txdata,119 }120 return con,nil121}122func (launcher *Launcher) LoadERC20TokenByContractAddr(addr string) (*contract.ERC20Token, error) {123 contractAddr := common.HexToAddress(addr)124 return contract.NewERC20Token(contractAddr,launcher.CLI)125}126func (launcher *Launcher) GetBalance(address string) (*big.Int,error) {127 addr := common.HexToAddress(address)128 value, err := launcher.CLI.BalanceAt(context.Background(),addr,nil)129 if err != nil {130 return nil,err131 }132 return value,nil133}134func (launcher *Launcher) Transfer(to string, val uint256.UInt256) (*types.Transaction,error) {135 privateKey := launcher.key.PrivateKey136 toAddr := common.HexToAddress(to)137 valBigInt := val.ToBigInt()138 txOpts := &TxOpts{139 From: launcher.key.Address,140 Value: valBigInt,141 }142 tx, err := launcher.transfer(txOpts, toAddr,privateKey)143 if err != nil {144 return nil,err145 }146 return tx,nil147}148func (launcher *Launcher) TransferFrom(from string, to string, val uint256.UInt256) (*types.Transaction,error) {149 return nil,nil150}151func (launcher *Launcher) transfer(txOps *TxOpts, to common.Address, key *ecdsa.PrivateKey) (*types.Transaction,error) {152 var err error153 value := txOps.Value154 if value == nil {155 value = new(big.Int)156 }157 var nonce uint64158 if txOps.Nonce == nil {159 nonce, err = launcher.CLI.PendingNonceAt(context.Background(), txOps.From)160 if err != nil {161 return nil,err162 }163 } else {164 nonce = txOps.Nonce.Uint64()165 }166 gasPrice := txOps.GasPrice167 if gasPrice == nil {168 gasPrice, err = launcher.CLI.SuggestGasPrice(context.Background())169 if err != nil {170 return nil,err171 }172 }173 gasLimit := txOps.GasLimit174 if gasLimit == 0 {175 msg := ethereum.CallMsg{From: txOps.From, To: &to, GasPrice: gasPrice, Value: value, Data: nil}176 gasLimit, err = launcher.CLI.EstimateGas(context.Background(), msg)177 if err != nil {178 return nil,err179 }180 }181 rawTx := types.NewTx(&types.LegacyTx{182 Nonce: nonce,183 To: &to,184 Value: value,185 Gas: gasLimit,186 GasPrice: gasPrice,187 Data: nil,188 })189 signer := types.LatestSignerForChainID(launcher.chainId)190 signature, err := crypto.Sign(signer.Hash(rawTx).Bytes(), key)191 if err != nil {192 return nil,err193 }194 signedTx, err := rawTx.WithSignature(signer, signature)195 if err != nil {196 return nil,err197 }198 err = launcher.CLI.SendTransaction(context.Background(), signedTx)199 if err != nil {200 return nil, err201 }202 return signedTx,nil203}204func (launcher *Launcher) LoadCrowdSaleByContractAddr(addr string) (*contract.CrowdSale, error) {205 contractAddr := common.HexToAddress(addr)206 return contract.NewCrowdSale(contractAddr,launcher.CLI)207}208func (launcher *Launcher) DeployCrowdSale(opts *CrowdSaleOpts) (*Contract, error){209 address := launcher.key.Address210 privateKey := launcher.key.PrivateKey211 txOpts, err := bind.NewKeyedTransactorWithChainID(212 privateKey, launcher.chainId)213 if err != nil {214 return nil, err215 }216 address, tx, _, err := contract.DeployCrowdSale(217 txOpts, launcher.CLI,opts.TokenAddress, opts.TargetFunds.ToBigInt(),218 opts.Price.ToBigInt(), opts.StartTime.ToBigInt(), opts.EndTime.ToBigInt())219 if err != nil {220 return nil, err221 }222 txdata := "0x" + hex.EncodeToString(tx.Data())223 txhash := tx.Hash().Hex()224 // 合约地址225 con := &Contract{...

Full Screen

Full Screen

New

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 launcher := launcher.New()4 launcher.Launch()5}6import (7func main() {8 launcher := launcher.New()9 launcher.Launch()10}11import (12func main() {13 launcher := launcher.New()14 launcher.Launch()15}16import (17func main() {18 launcher := launcher.New()19 launcher.Launch()20}21import (22func main() {23 launcher := launcher.New()24 launcher.Launch()25}26import (27func main() {28 launcher := launcher.New()29 launcher.Launch()30}31import (32func main() {33 launcher := launcher.New()34 launcher.Launch()35}36import (37func main() {38 launcher := launcher.New()39 launcher.Launch()40}41import (42func main() {

Full Screen

Full Screen

New

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 launcher := launcher.New()4 fmt.Println(launcher)5}61. Fork it (

Full Screen

Full Screen

New

Using AI Code Generation

copy

Full Screen

1import "fmt"2func main() {3 launcher := New(10)4 launcher.Launch()5}6import "fmt"7func main() {8 launcher := New(10)9 launcher.Launch()10}11import "fmt"12func main() {13 launcher := New(10)14 launcher.Launch()15}16import "fmt"17func main() {18 launcher := New(10)19 launcher.Launch()20}21import "fmt"22func main() {23 launcher := New(10)24 launcher.Launch()25}26import "fmt"27func main() {28 launcher := New(10)29 launcher.Launch()30}31import "fmt"32func main() {33 launcher := New(10)34 launcher.Launch()35}36import "fmt"37func main() {38 launcher := New(10)39 launcher.Launch()40}41import "fmt"42func main() {43 launcher := New(10)44 launcher.Launch()45}

Full Screen

Full Screen

New

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 cmd := exec.Command("ls", "-l", "-a")4 err := cmd.Run()5 if err != nil {6 fmt.Println(err)7 }8}

Full Screen

Full Screen

New

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 l := launcher.New("SpaceX", 2010, 3, 2, 0, 0)4 l.Launch()5 fmt.Println("After launch")6 l.Status()7}

Full Screen

Full Screen

New

Using AI Code Generation

copy

Full Screen

1import (2type launcher struct {3}4func (l *launcher) new() {5 fmt.Println("New method of launcher class")6 fmt.Println("Name of the launcher is:", l.launcherName)7 fmt.Println("Type of the launcher is:", l.launcherType)8 fmt.Println("Cost of the launcher is:", l.launcherCost)9}10func main() {11 launcher1 := launcher{"Brahmos", "Surface to Surface", 20000000}12 launcher1.new()13}

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful