Skip to main content

go-patract

Intruduction#

Most contract behaviors are highly related to context. In addition to interacting with the chain, user-oriented contract applications also need to provide users with current relevant context status information:

+--DAPP-Front-End--------------+        +---Chain-------------------------+|                              |        |                                 || +----+  +------------------+ |        | +-------+     +-------+         || |    |  |                  | | Commit | |       |     |       |         || |    |  |   Polkadot-JS    +------------> Node  +---->+ Node  |         || |    +->+                  | |   Tx   | |       |     |       |         || |    |  |                  | |        | +-------+     +----+-++         || |    |  +------------------+ |        |                    ^ |          || | UI |                       |        +---------------------------------+| |    |  +------------------+ |                             | || |    |  |                  | |        +--DAPP-Server--------------------+| |    |  |                  | |  Push  | +--------+     +-----v-------+  || |    +<-+   Model          +<-----------+        +-----+             |  || |    |  |                  | |        | | Server |     |  PatractGo  |  || |    |  |                  +------------>        +-----+             |  || +----+  +------------------+ | Query  | +----+---+     +-----+-------+  |+------------------------------+        |      |               |          |                                        |      |         +-----v-------+  |                                        |      |         |             |  |                                        |      +-------->+   DataBase  |  |                                        |                |             |  |                                        |                +-------------+  |                                        |                                 |                                        +---------------------------------+

PatractGo is mainly responsible for implementing micro-services in a DApp. Unlike querying the state of the chain API, PatractGo can monitor the calls and events generated by the specified contract. Developers can obtain the state storage based on this information to maintain consistent state with the chain. Through data services based on a typical API-DB architecture, the front-end DApp can efficiently and concisely obtain the state on the chain as context information.

Based on the API of chain nodes, PatractGo obtains block information and summarizes and filters it, and sends contract-related messages and events based on metadata analysis to the handler protocol specified by the developer. For example, for a typical ERC20 contract, the developer can use the channel to subscribe to all transfer events that occur, and then synchronize them into the database, so that other microservices can provide services corresponding to the token data of the account, such as querying the current token holding distribution and other logics.

Therefor, PatractGo will achieve the following support:

  • Complete the secondary packaging of the contract module interface, complete operations such as put_code, call, instantiate, etc.
  • Parse the metadata.json information of the contract, and support the automatic generation of http service interface for the metadata corresponding contract
  • Scanning and monitoring support of the contract status on the chain for statistics and analysis
  • Basic command line tool support for native interaction with the contract, mainly used to test the security of the contract
  • SDK development examples for ERC20 contract support

Design#

PatractGo consists of the following packages:

  • patractgo/metadata contract metadata processing, and metadata-based contract processing
  • patractgo/rpc/native re-encapsulation of the contract module interface to provide the contract-related interaction based on chain RPC
  • patractgo/rpc implement the interaction with the contract based on metadata
  • patractgo/rest implements an http service based on metadata to interact with the contract
  • patractgo/observer Monitoring and Scanning support for contract status on the chain
  • patractgo/contracts/erc20 supports ERC20 contracts and examples
  • patractgo/tools some tools for contracts develop

Currently, we haven't designed the module which could auto-gen code for a contract based on a metadata, thus we provide patractgo/contracts/erc20 as an example to show how to warp a contract as a go source file.

This auto contract code generator feature would be developed with java-patract repo later (in next version), for their have same logic to generate the code for contracts.