Exercise a Smart Contract
Now that you have an environment with an active node, it's time to deploy a basic smart contract and issue some transactions.
Deploy a Smart Contract
- Navigate to your environmental dashboard. It should look similar to the following:
- Click the Click here to connect your nodes hyperlink at the top of the page. This will open the Kaleido Connect panel.
- Click View Details beneath the REST API Gateway window.
The REST API Gateway is a fantastic development accelerator. It abstracts the core Ethereum JSON/RPC API and surfaces a convenient REST interface for interaction with the blockchain. In doing so the service removes the need for heavy client libraries and complex API programming, and instead allows transactions to be submitted as lightweight JSON/YAML payloads via familiar REST methods.
- Click Generate New on the Application Credentials panel. This will create a basic authentication username password combination to be used within the REST API Gateway for secure connection to your node.
- From the REST API Gateway panel make sure you are on the Quick Start tab.
- An autogenerated payload is supplied with the Simple Storage solidity smart contract embedded in the payload.
- Simple Storage is a trivial smart contract with a single global variable -
storedData- specified as type uint. It exposes a
setmethod for write operations and a
getmethod for queries. A value of 12345 is passed to the constructor upon deployment.
- Click DEPLOY to instantiate the Simple Storage smart contract and issue your first Ethereum transaction! No APIs and no custom programming required.
- You should see a Deploy Response similar to the following upon a successful instantiation:
- Give the transaction a few seconds to be mined and then you can click CHECK REPLY to see the full Ethereum transaction receipt. The receipt will contain all of the low level transaction details, as well as a callable address for future invocations and queries. Follow along with the next section to interact with your smart contract.
Access the Automatically Generated REST API
Now that you have an active smart contract on the network, it's time to update the
storedData global variable and perform a read. We'll do so by using the built-in smart contract API interface to talk to the chain.
- In the left hand navigation of the REST API Gateway panel, switch to the Smart Contracts tab. This is the second option directly beneath the Quick Start screen.
- The Smart Contracts interface generates and exposes RESTful APIs for any smart contract deployed via the EthConnect Kafka bridge. It also supports the uploading of solidity smart contracts and the autogeneration of REST endpoints for each of the contract's methods. This means that once again there is no need for client-side Ethereum programming in order to communicate with the blockchain. In fact, by using the Smart Contract API interface, you don't even need to specify values for the smart contract address or for gas!
- Since you deployed the Simple Storage smart contract in the first section, there will be an existing set of REST endpoints to communicate with your specific contract.
- At the bottom of the Smart Contracts panel, locate the Deployed REST Gateway APIs section. You should see a row with the contract name, timestamp of instantiation and a single API Deployment.
- Click the
simplestoragelink or the dropdown arrow to see the API deployment.
- You should see a single instance defined by an address. This is the callable address of your recently deployed Simple Storage smart contract without a prepended
Invoke the Smart Contract
- Click the View button to open up the deployment. This will open a new tab with an interactive swagger console for interaction with the smart contract.
Let's begin by querying for the value of the
storedData variable. An argument of "12345" was passed to the constructor upon deployment, so we should expect to see "12345" returned on our first query.
- In the first section expand GET to invoke the smart contract's
getmethod and click the TRY button.
- You should see "12345" as the retVal
Now let's update the global variable to a new value of "99"
- Close GET and expand POST to invoke the smart contract's
setmethod. Edit the JSON payload and replace the
- Click the TRY button to invoke the smart contract's
setmethod and update the state. Wait a few seconds for the transaction receipt to be returned. By default, the
kld-syncparameter is set to true. This means that the response will not be returned until the transaction goes through an attempted execution. If the
kld-syncparameter is set to
false, the response will return prior to block mining and simply indicate the status of the sent message.
- If you see TransactionSuccess in the response body then the invocation was properly executed. You can proceed to issue another query, expecting to see 99 as the returned state of the variable.
- Note that any GET operations will not result in chain-layer transactions. These simply return the state for enumerated variables.