1. 编写Solidity智能合约
2. 使用Web3j-OpenAPI生成相应的OpenAPI项目
3. 将生成的项目作为独立服务器运行
4. 使用Swagger-UI或使用我们的客户端实现从客户端应用程序发送HTTP请求
//SPDX-License-Identifier:Apache-2.0pragmasolidity^0.7.0;//ModifiedGreetercontract.Basedonexampleathttps://www.ethereum.org/greeter.contractMortal{/*Definevariableownerofthetypeaddress*/addressowner;/*thisfunctionisexecutedatinitializationandsetstheownerofthecontract*/constructor(){owner=msg.sender;}modifieronlyOwner{require(msg.sender==owner,"Onlyownercancallthisfunction.");_;}/*Functiontorecoverthefundsonthecontract*/functionkill()onlyOwnerpublic{selfdestruct(msg.sender);}}contractHelloWorldisMortal{/*definevariablegreetingofthetypestring*/stringgreet;/*thisrunswhenthecontractisexecuted*/constructor(stringmemory_greet){greet=_greet;}functionnewGreeting(stringmemory_greet)onlyOwnerpublic{emitModified(greet,_greet,greet,_greet);greet=_greet;}/*mainfunction*/functiongreeting()publicviewreturns(stringmemory){returngreet;}eventModified(stringindexedoldGreetingIdx,stringindexednewGreetingIdx,stringoldGreeting,stringnewGreeting);}
$curl-Lget.epirus.io|sh
$epirusopenapiimport--help
$epirusopenapiimport-s=HelloWorld.sol--package=com.tutorial--project-name=HelloWorldProject--output-dir=.
$cdHelloWorldProject$./gradlewrun--args="--help"
环境变量
配置文件
直接从CLI
$exportWEB3J_ENDPOINT=<link_to_your_Ethereum_node>$exportWEB3J_PRIVATE_KEY=<your_private_key>$exportWEB3J_OPENAPI_HOST=localhost$exportWEB3J_OPENAPI_PORT=9090
$./gradlewrun
dependencies{implementation"org.web3j.openapi:web3j-openapi-client:4.7.1"}
varservice=newClientService("http://localhost:9090");varhelloWorldProject=ClientFactory.create(HelloWorldProjectApi.class,service);System.out.println("DeployingtheHelloWorldcontract...");vardeployParams=newHelloWorldDeployParameters("Hello");varreceipt=helloWorldProject.getContracts().getHelloWorld().deploy(deployParams);System.out.println("Deployedcontractaddress:"+receipt.getContractAddress());//LoadthecontractfromthereturnedtransactionreceiptvarhelloWorld=helloWorldProject.getContracts().getHelloWorld().load(receipt.getContractAddress());//ChangethegreetingmessagevarnewReceipt=helloWorld.newGreeting(newNewGreetingParameters("HelloWeb3j-OpenAPI"));System.out.println("NewGreetingtransactionhash:"+newReceipt.getTransactionHash());//Checkthatthesmartcontracthasbeenupdatedvargreeting=helloWorld.greeting().getResult();System.out.println("Greetingmethodresult:"+greeting);
varcountdownLatch=newCountDownLatch(1);//StartlisteningforeventshelloWorld.getEvents().getModified().onEvent(e->{System.out.println("Receivedevent:"+modifiedEvent.getNewGreeting());countdownLatch.countDown();});helloWorld.newGreeting(newNewGreetingParameters("Thiswilltriggeramodifiedevent"));countdownLatch.await();//Waitsfortheevent
作者:链三丰,来源:区块链研究实验室
郑重声明:本文版权归原作者所有,转载文章仅为传播更多信息之目的,如作者信息标记有误,请第一时间联系我们修改或删除,多谢。
郑重声明:本文版权归原作者所有,转载文章仅为传播更多信息之目的,如作者信息标记有误,请第一时间联系我们修改或删除,多谢。