HomeDeveloper MaterialsEnterprise App Gateway via KafkaSmart Contract API Generator

Smart Contract API Generator

The Smart Contracts service will generate an interactive Swagger Interface and downloadable Swagger specification for any uploaded smart contract.  The service automatically generates the solidity ABI for any uploaded contract and serves RESTful endpoints for any invokable or queryable method in the code.  This allows developers to forgo web3 libraries during dApp development and instead leverage modern REST APIs and common authorization techniques to communicate with their smart contracts.  The Kafka-backed EthConnect layer transforms the submitted payloads into properly formed signed Ethereum transaction objects and provides reliable delivery at an optimal rate.  This simplicity and reliability helps enterprises easily weave Kaleido into their existing business processes without reassembling their hardened stacks with heavy SDKs.  Moreover, the service can be utilized against existing on-chain smart contracts by simply specifying an ABI resource ID and the contract address as a path parameter in the client call.  Couple this with the customizable event streams service and you have a cohesive unified view of any business-pertinent on-chain events that can be reacted to and automated.

Uploading Contracts

Access your REST API Gateway by selecting Connect in the node dropdown panel or by clicking a node and selecting + CONNECT on the node details screen.

  • Click View Details beneath the REST API Gateway Panel
  • Supply existing application credentials or click Regenerate to access the interface
  • Switch to the Smart Contracts tab in the lefthand navigation
  • Click the CHOOSE FILES button to upload a single or multi-part smart contract.  The service accepts .sol, .zip and .tgz extensions.  As such, you can upload an entire smart contract directory if you wish.  For example, Open-Zeppelin token directories.
  • This documentation will make use of the SimpleEvents.sol contract outlined in the Configuring Event Streams article.

The code is as follows:

pragma solidity >=0.5.2 <0.6.0;
/**
  * @title Simple Storage with events
  * @dev Read and write values to the chain
  */
contract SimpleEvents {
  int64 public storedI;
  string public storedS;
  event Changed (
    address indexed from,
    int64 indexed i,
    string indexed s,
    bytes32 h,
    string m
  );
  /**
    * @dev Constructor sets the default value
    * @param i The initial value integer
    * @param s The initial value string
    */
  constructor(int64 i, string memory s) public {
    set(i,s);
  }
  /**
    * @dev Set the value
    * @param i The new value integer
    * @param s The new value string
    */
  function set(int64 i, string memory s) public {
    storedI = i;
    storedS = s;
    emit Changed(msg.sender, i, s, keccak256(bytes(s)), s);
  }
  /**
    * @dev Get the value
    */
  function get() public view returns (int64 i, string memory s) {
    return (storedI, storedS);
  }
}
  • If the pragma for your contract is below .5, select the Solidity .4 compatibility mode checkbox
  • Otherwise, click the UPLOAD button to build the ABI and generate the Swagger interface
  • The contract will appear underneath the Deployed REST Gateway APIs section at the bottom of the page
  • Note that the contract itself is NOT deployed through an upload.  As such, the Deployments field will be listed as 0
  • Click the VIEW button to see the contract ABIs

  • This will open a new page with a unique ABI-specific resource ID served as a targetable API Server URL
  • You can use this API to deploy your smart contract programmatically or directly through the console
  • Alternatively, you can leverage the API to interact with existing on-chain smart contracts by passing the contract address as a parameter in the call
  • To deploy, call the POST method and supply any required arguments to your constructor.
  • The SimpleEvents.sol contract requires an integer and a string upon instantiation.  For example:

  • The contract address will be returned in the response payload
  • To continue using the ABI endpoint, simply pass the address as a parameter on any subsequent calls
  • The ABI interface is especially useful for any client side dApps that need to call multiple contracts with the same syntax.  For example, you may have multiple bilateral payment smart contracts for private transaction processing in your environment
  • Notice that the contract address must be explicitly defined as a path parameter if sending transactions against the ABI interface

Interacting with a Specific Deployment

Once the contract has been deployed through the API Gateway, there will also be a separate Swagger instance available that targets the specific address.

  • Return to your Smart Contract tab in the REST Gateway and click Refresh next to your contract.
  • You should see the Deployments field change from 0 to 1
  • You will also notice the contract specific address displayed
  • Click the View button next to the address to access the swagger interface for that particular contract address

  • Open this swagger interface and you will see a targetable URL with the smart contract address explicitly defined
  • Kaleido is abstracting the ABI resource ID on this call because the interface has been accessed directly within the console
  • Notice that /abis has been removed from the path and replaced by /contracts/{contract_address}

  • You can use the interactive swagger interface for the specific contract instance to invoke any of the available methods.  For this example you have a set and a get that can be called.

Programmatically Calling Contracts via the Gateway

If you want to use a REST client like Postman or write your own program to communicate via EthConnect the syntax for the target URL is as follows:

https://{kaleido_connect_URL}/abis/{abi_long_ID}/{contract_address}/{contract_method}

An actual API endpoint might resemble the following:

https://u0l3fmedb5-u0dl657t5d-connect.us0-aws.kaleido.io/abis/ad7e3bbf-91a8-42f9-7350-927f63f6e452/0xa1614d32387813409cd6e7fb551a9e7f2b22d9b9/set

Note that the ABI long from ID is required with this approach in order to inform Kaleido about the interfaces and methods for the smart contract.

Note also that any node you are targeting MUST have the contract source uploaded via the Gateway.  This will produce a unique ABI long form ID specific to that node’s Gateway service.

The same smart contract address can be accessed by any node via its Kaleido Connect URL, however each URL instance must contain an ABI resource ID specific to that node.  So if you want to communicate with multiple nodes, just upload the solidity source and generate a separate ABI resource ID for the node.

ADDITIONAL REQUIREMENTS 

  • Supply application credentials specified as Basic Auth (the same set of application credentials can be leveraged if the nodes are owned by the same member)
  • Supply an x-kaleido-from Header with a user account on the targeted node supplied as a value.  This will be used to sign the transaction on the server
  • Submit the same basic payload for the method’s required parameters
  • Using Postman, a proper transaction might resemble the following

Notice also the requirement for x-kaleido-from as a Header: