Ame Component is an onchain component based on EIP-7654 that decomposes complex onchain processes into independent, composable, reusable, and highly adaptable functional units.

Rationale

Type of request method

In order to enable the client to operate the contract in a standardized and predictable way, three request method types GET, POST, and PUT are set. The functions of each need to be defined in these three types to facilitate the contract caller to understand and process the information required for the request. However, there is no DELETE operation type because deleting data in the contract is an inefficient operation. Developers can add a PUT request method by themselves to set the data to be valid and invalid, and only return valid data in the GET method.

Request method parameter type

Some functions are defined in each request method type. They all include request parameter data type and response parameter data type, which need to be set in the constructor and then obtained according to the method name through getMethodReqAndRes. The data type of the parameter is defined by the enumeration of the data type. When processing the request parameter, abi.decode is used to decode according to the request parameter type and the request value. When returning the response, abi.encode is used to encode according to the response value and the response parameter type. In addition, we can get the method usage instructions by calling getMethodInstruction.

Interface

IComponent.sol
// SPDX-License-Identifier: CC0-1.0
pragma solidity >=0.8.0;
import "./Types.sol";
interface IComponent{

    /**
     * Requested method type.
     * GET, POST, PUT, OPTIONS
     */
    enum MethodTypes{
        GET,
        POST,
        PUT,
        OPTIONS
    }

    /**
     * Response data event.
     * @param _response is the response value of the post request or put request.
     */
    event Response(bytes _response);

    /**
     * Get method names based on request method type.
     * @param _methodTypes is the request method type.
     * @return Method names.
     */
    function getMethods(MethodTypes _methodTypes)external view returns (string[] memory);

    /**
     * Get the data types of request parameters and return parameters based on the requested method name.
     * @param _methodName is the method name.
     * @return Data types of request parameters and return parameters.
     */
    function getMethodReqAndRes(string memory _methodName) external view returns(Types.Type[] memory ,Types.Type[] memory );

    /**
     * Get the instruction of method based on the requested method name.
     * @param _methodName is the method name.
     * @return instruction
     */    
    function getMethodInstruction(string memory _methodName) external view returns(string memory);

    /**
     * Request the contract to retrieve records.
     * @param _methodName is the method name.
     * @param _methodReq is the method type.
     * @return The response to the get request.
     */
    function get(string memory _methodName,bytes memory _methodReq)external view returns(bytes memory);

    /**
     * Request the contract to create a new record.
     * @param _methodName is the method name.
     * @param _methodReq is the method type.
     * @return The response to the post request.
     */
    function post(string memory _methodName,bytes memory _methodReq)external payable returns(bytes memory) ;

    /**
     * Request the contract to update a record.
     * @param _methodName is the method name.
     * @param _methodReq is the method type.
     * @return The response to the put request.
     */
    function put(string memory _methodName,bytes memory _methodReq)external payable returns(bytes memory);

    /**
     * Supported request method types.
     * @return Method types.
     */
    function options()external returns(MethodTypes[] memory);
}
The library Types.sol contains an enumeration of Solidity types used in the above interfaces.
Types.sol
// SPDX-License-Identifier: CC0-1.0
pragma solidity >=0.8.0;
library Types {
    enum Type {
        BOOL, INT8, INT16, INT24, INT32, INT40, INT48, INT56, INT64, INT72, INT80, INT88, INT96, INT104, INT112, INT120, INT128, INT136, INT144, INT152, INT160, INT168, INT176, INT184, INT192, INT200, INT208, INT216, INT224, INT232, INT240, INT248, INT256, UINT8, UINT16, UINT24, UINT32, UINT40, UINT48, UINT56, UINT64, UINT72, UINT80, UINT88, UINT96, UINT104, UINT112, UINT120, UINT128, UINT136, UINT144, UINT152, UINT160, UINT168, UINT176, UINT184, UINT192, UINT200, UINT208, UINT216, UINT224, UINT232, UINT240, UINT248, UINT256, ADDRESS, BYTES1, BYTES2, BYTES3, BYTES4, BYTES5, BYTES6, BYTES7, BYTES8, BYTES9, BYTES10, BYTES11, BYTES12, BYTES13, BYTES14, BYTES15, BYTES16, BYTES17, BYTES18, BYTES19, BYTES20, BYTES21, BYTES22, BYTES23, BYTES24, BYTES25, BYTES26, BYTES27, BYTES28, BYTES29, BYTES30, BYTES31, BYTES32, BYTES, STRING, INT8_ARRAY, INT16_ARRAY, INT24_ARRAY, INT32_ARRAY, INT40_ARRAY, INT48_ARRAY, INT56_ARRAY, INT64_ARRAY, INT72_ARRAY, INT80_ARRAY, INT88_ARRAY, INT96_ARRAY, INT104_ARRAY, INT112_ARRAY, INT120_ARRAY, INT128_ARRAY, INT136_ARRAY, INT144_ARRAY, INT152_ARRAY, INT160_ARRAY, INT168_ARRAY, INT176_ARRAY, INT184_ARRAY, INT192_ARRAY, INT200_ARRAY, INT208_ARRAY, INT216_ARRAY, INT224_ARRAY, INT232_ARRAY, INT240_ARRAY, INT248_ARRAY, INT256_ARRAY, UINT8_ARRAY, UINT16_ARRAY, UINT24_ARRAY, UINT32_ARRAY, UINT40_ARRAY, UINT48_ARRAY, UINT56_ARRAY, UINT64_ARRAY, UINT72_ARRAY, UINT80_ARRAY, UINT88_ARRAY, UINT96_ARRAY, UINT104_ARRAY, UINT112_ARRAY, UINT120_ARRAY, UINT128_ARRAY, UINT136_ARRAY, UINT144_ARRAY, UINT152_ARRAY, UINT160_ARRAY, UINT168_ARRAY, UINT176_ARRAY, UINT184_ARRAY, UINT192_ARRAY, UINT200_ARRAY, UINT208_ARRAY, UINT216_ARRAY, UINT224_ARRAY, UINT232_ARRAY, UINT240_ARRAY, UINT248_ARRAY, UINT256_ARRAY, ADDRESS_ARRAY, BYTES1_ARRAY, BYTES2_ARRAY, BYTES3_ARRAY, BYTES4_ARRAY, BYTES5_ARRAY, BYTES6_ARRAY, BYTES7_ARRAY, BYTES8_ARRAY, BYTES9_ARRAY, BYTES10_ARRAY, BYTES11_ARRAY, BYTES12_ARRAY, BYTES13_ARRAY, BYTES14_ARRAY, BYTES15_ARRAY, BYTES16_ARRAY, BYTES17_ARRAY, BYTES18_ARRAY, BYTES19_ARRAY, BYTES20_ARRAY, BYTES21_ARRAY, BYTES22_ARRAY, BYTES23_ARRAY, BYTES24_ARRAY, BYTES25_ARRAY, BYTES26_ARRAY, BYTES27_ARRAY, BYTES28_ARRAY, BYTES29_ARRAY, BYTES30_ARRAY, BYTES31_ARRAY, BYTES32_ARRAY, BYTES_ARRAY, STRING_ARRAY
    }
}

Specifications

  • getMethods Get method names based on request method type.
  • getMethodReqAndRes Get the data types of request parameters and return parameters based on the requested method name.
  • getMethodInstruction Get the instruction of method.
  • get Request the contract to retrieve records.
  • post Request the contract to create a new record.
  • put Request the contract to update a record.
  • options Supported request method types.
The methodRequests and methodResponses are used to define the request parameter type and response data type of Method. Types.sol is a data type Library, which contains all data types of solidity.
mapping (string=>Types.Type[]) methodRequests;
mapping (string=>Types.Type[]) methodResponses;
Method Types has 4 request types, GET, POST, PUT, OPTIONS. methods is used to store the method name array of each request type.
mapping (MethodTypes=>string[]) methods;
instructions is used to store method instructions
mapping(string => string) instructions;
The options function will return the request method type supported by the component.
function options()public pure returns(MethodTypes[] memory){
  MethodTypes[] memory methodTypes=new MethodTypes[](4);
  methodTypes[0]=MethodTypes.GET;
  methodTypes[1]=MethodTypes.POST;
  methodTypes[2]=MethodTypes.PUT;
  methodTypes[3]=MethodTypes.OPTIONS;
  return methodTypes;
The setMethod is used to set the request parameter type and response data type of the method.
function setMethod(string memory _methodName,MethodTypes _methodType,Types.Type[] memory _methodReq,Types.Type[] memory _methodRes)  private  {
  methods[_methodType].push(_methodName);
  methodRequests[_methodName]=_methodReq;
  methodResponses[_methodName]=_methodRes;
 }
The getMethods returns the response method names according to the request type.
function getMethods(MethodTypes _methodTypes)public view returns (string[] memory){
    return methods[_methodTypes];
} 
The getMethodReqAndRes is used to obtain the request parameter type and response value type of the method based on the method name.
function getMethodReqAndRes(string memory _methodName)public view returns(Types.Type[] memory ,Types.Type[] memory ){
  return(
    methodRequests[_methodName],
    methodResponses[_methodName]
  );
}
The getMethodInstruction is used to query the usage of method.
  function getMethodInstruction(string memory _methodName)
        public
        view
        returns (string memory)
  {
        return instructions[_methodName];
  }
Implement the functions of each method in get, post, and put .
function get(string memory _methodName,bytes memory _methodReq)public pure returns(bytes memory){
  return abi.encode(_methodName,_methodReq);
}

function post(string memory _methodName,bytes memory _methodReq)public payable returns(bytes memory){
  return abi.encode(_methodName,_methodReq);
}

function put(string memory _methodName,bytes memory _methodReq)public payable returns(bytes memory){
  return abi.encode(_methodName,_methodReq);
}

Workflow

Component Workflow Pn
1

options

Call options to obtain supported request method types.
2

getMethods

Call getMethods to obtain the request method name.
3

getMethodInstruction

Call getMethodInstruction to obtain the request method instruction.
4

getMethodReqAndRes

Call getMethodReqAndRes to obtain the request parameter data type and response value data type.
5

Encode

Encode request parameters and call get, post, and put.
6

Decode

Decode response value.