From 7e61b471f05df834949da0ed69b0bab2b184eda8 Mon Sep 17 00:00:00 2001 From: rickey <12867336+HelloRickey@users.noreply.github.com> Date: Wed, 13 Mar 2024 10:31:31 +0800 Subject: [PATCH 1/7] add Request Method Types --- ERCS/erc-xxxx.md | 176 +++++++++++++++++++++ assets/erc-xxxx/RequestMethodTypes.sol | 96 ++++++++++++ assets/erc-xxxx/Types.sol | 205 +++++++++++++++++++++++++ 3 files changed, 477 insertions(+) create mode 100644 ERCS/erc-xxxx.md create mode 100644 assets/erc-xxxx/RequestMethodTypes.sol create mode 100644 assets/erc-xxxx/Types.sol diff --git a/ERCS/erc-xxxx.md b/ERCS/erc-xxxx.md new file mode 100644 index 0000000000..727eb84411 --- /dev/null +++ b/ERCS/erc-xxxx.md @@ -0,0 +1,176 @@ +--- +eip: xxxx +title: Request Method Types +description: Use a set of request methods to indicate the type of action to take on the contract. +author: Rickey (@HelloRickey) +discussions-to: +status: Draft +type: Standards Track +category: ERC +created: 2024-03-13 +--- + +## Abstract + +This proposal standardizes a set of request and response communication standards between clients and smart contracts, using POST, GET, and PUT requests to create, read, and update the states of smart contracts. You can customize different request method names, request parameters and response values, and each request method will be mapped to a specific operation. + +## Motivation + +In Web2.0, we often use HTTP methods to define how the client interacts with the server. We use the POST, GET, and PUT methods to perform different operations on the resources in the server. This standard effectively simplifies the interaction between the client and the server. Similar standards can also be used to wrap functions in smart contracts. When the client makes a request to the contract, it indicates the type and method of operation to be taken, and the corresponding method in the contract performs the operation and returns a response. + +Advantages of using this standard include: + +- Standardization: Provide a unified and standardized communication method for clients and contracts, so that all parties can conduct more standardized interactions. + +- Composability: It can customize request methods, request parameters and response values, and easily build complex applications with multiple contract request methods components. + +- Interoperability: Easily extend plugins or middleware in request methods to enable interoperability with onchain applications from all parties. + +- State Management: Create, retrieve, and modify smart contract states according to the application use cases of each party. + +- RESTful: It can adopt RESTful design principles to make it more predictable and make calls to all parties clearer and easier to understand. + +## Specification + +The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", "SHOULD", "SHOULD NOT", "RECOMMENDED", "NOT RECOMMENDED", "MAY", and "OPTIONAL" in this document are to be interpreted as described in RFC 2119 and RFC 8174. + +It consists of four request method types: + +**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. + +Workflow: + +1. Call ```options``` to obtain supported request method types. +2. Call ```getMethods``` to obtain the request method name. +3. Call ```getMethodReqAndRes``` to obtain the request parameter data type and response value data type. +4. Encode request parameters and call ```get```, ```pot```, and ```put```. +5. Decode response value. + +### Interfaces + +#### `IRequestMethodTypes.sol` + +```solidity +// SPDX-License-Identifier: CC0-1.0 +pragma solidity >=0.8.0; +import "./Types.sol"; +interface IRequestMethodTypes{ + + /** + * Requested method type. + * GET, POST, PUT, OPTIONS + */ + enum MethodTypes{ + GET, + POST, + PUT, + OPTIONS + } + + /** + * 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 responses based on the requested method name. + * @param _methodName is the method name. + * @return Data types of request parameters and responses. + */ + function getMethodReqAndRes(string memory _methodName) external view returns(Types.Type[] memory ,Types.Type[] 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 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 returns(bytes memory); + + /** + * Supported request method types. + * @return Method types. + */ + function options()external returns(MethodTypes[] memory); +} + +``` + +### Library + +The library [`Types.sol`](../assets/erc-xxxx/Types.sol) contains an enumeration of Solidity types used in the above interfaces. + +## Rationale + +### How to customize response status code? + +The return value type of each request method is bytes, so you can customize the response status code and other response information according to your needs. + +### How to disable a request method? + +You can add a mapping(string=>bool) to save the request method name and available state. After the application is upgraded, manually set the available state of the disabled request method to false. And check the state every time a request is processed. + +### Why is there no delete method type? + +The data in smart contracts is public, and deleting data is an inefficient operation. In order to facilitate data management and retrieval, you can add a mapping to save the valid state of the data, and add a put request method to set valid and invalid data. And only return valid data in get method. + +### How to dynamically add new request methods? + +Before deploying the contract, you can add a mapping (address=>bool) to record the external contract permissions. When the application adds new functions, give the v2 contract write permissions and add new request methods to the v2 contract. + +### How to encode request parameters and decode response? + +Use ```getMethodReqAndRes``` to get the data type of the method request parameters and the data type of the response value. Then use some js library to encode and decode it. +For example: + +```javascript +var reqDataEncode = web3.eth.abi.encodeParameters( + ["string", "uint256"], + ["alice", "1"] +); + +var resDataDecode = web3.eth.abi.decodeParameters( + ["string", "uint256"], + reqDataEncode +); +``` + +## Reference Implementation + +See [Request Method Types Example](../assets/erc-xxxx/RequestMethodTypes.sol) + +## Security Considerations + +Contract request methods are divided into safe methods and unsafe methods. If the method request is a read-only operation and will not change the state of the contract, then the method is safe. + +**Safe Methods:** GET, OPTIONS +**Unsafe Methods:** POST, PUT + +## Copyright + +Copyright and related rights waived via [CC0](../LICENSE.md). + diff --git a/assets/erc-xxxx/RequestMethodTypes.sol b/assets/erc-xxxx/RequestMethodTypes.sol new file mode 100644 index 0000000000..638412654a --- /dev/null +++ b/assets/erc-xxxx/RequestMethodTypes.sol @@ -0,0 +1,96 @@ +// SPDX-License-Identifier: GPL-3.0 +pragma solidity >=0.8.0; +import "./Types.sol"; +import "./IRequestMethodTypes.sol"; +contract RequestMethodTypes is IRequestMethodTypes{ + + //@dev define the data type of this component + struct Profiles{ + string name; + uint256 age; + } + + mapping (address=>Profiles) users; + + //@dev Types contains all data types in solidity + mapping (string=>Types.Type[]) methodRequests; + mapping (string=>Types.Type[]) methodResponses; + mapping (MethodTypes=>string[]) methods; + + constructor(){ + Types.Type[] memory getReqArray = new Types.Type[](1); + getReqArray[0] = Types.Type.ADDRESS; + Types.Type[] memory dataTypeArray = new Types.Type[](2); + dataTypeArray[0] = Types.Type.STRING; + dataTypeArray[1] = Types.Type.UINT256; + Types.Type[] memory putReqArray = new Types.Type[](3); + putReqArray[0] = Types.Type.ADDRESS; + putReqArray[1] = Types.Type.STRING; + putReqArray[2] = Types.Type.UINT256; + // @dev initialize get, post, put request parameter data types and response data types + setMethod("getUser",MethodTypes.GET,getReqArray,dataTypeArray); + setMethod("createUser",MethodTypes.POST,dataTypeArray,new Types.Type[](0)); + setMethod("updateUserName",MethodTypes.PUT,putReqArray,new Types.Type[](0)); + } + + 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; + } + + function getMethodReqAndRes(string memory _methodName)public view returns(Types.Type[] memory ,Types.Type[] memory ){ + return( + methodRequests[_methodName], + methodResponses[_methodName] + ); + } + + function getMethods(MethodTypes _methodTypes)public view returns (string[] memory){ + return methods[_methodTypes]; + } + + function get(string memory _methodName,bytes memory _methodReq)public view returns(bytes memory){ + if(compareStrings(_methodName,"getUser")){ + address user=abi.decode(_methodReq, (address)); + bytes memory userData=abi.encode(users[user].name,users[user].age); + return userData; + }else{ + return abi.encode(""); + } + } + + function post(string memory _methodName,bytes memory _methodReq)public returns(bytes memory){ + if(compareStrings(_methodName,"createUser")){ + (string memory name,uint256 age)=abi.decode(_methodReq, (string,uint256)); + users[msg.sender]=Profiles(name,age); + + } + return abi.encode(""); + } + + function put(string memory _methodName,bytes memory _methodReq)public returns(bytes memory){ + if(compareStrings(_methodName,"updateUserName")){ + (string memory name)=abi.decode(_methodReq, (string)); + users[msg.sender].name=name; + return abi.encode(""); + } + return abi.encode(""); + } + + 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; + } + + //@dev compares two strings for equality + function compareStrings(string memory _a, string memory _b) private pure returns (bool) { + return keccak256(abi.encodePacked(_a)) == keccak256(abi.encodePacked(_b)); + } + + +} \ No newline at end of file diff --git a/assets/erc-xxxx/Types.sol b/assets/erc-xxxx/Types.sol new file mode 100644 index 0000000000..3c7b339899 --- /dev/null +++ b/assets/erc-xxxx/Types.sol @@ -0,0 +1,205 @@ +// 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 + } +} \ No newline at end of file From e9edeaaf41ebfe70ac994889d2941ff14d3ec060 Mon Sep 17 00:00:00 2001 From: rickey <12867336+HelloRickey@users.noreply.github.com> Date: Wed, 13 Mar 2024 10:41:16 +0800 Subject: [PATCH 2/7] add discussions-to --- ERCS/erc-xxxx.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ERCS/erc-xxxx.md b/ERCS/erc-xxxx.md index 727eb84411..c0b6a087b0 100644 --- a/ERCS/erc-xxxx.md +++ b/ERCS/erc-xxxx.md @@ -3,7 +3,7 @@ eip: xxxx title: Request Method Types description: Use a set of request methods to indicate the type of action to take on the contract. author: Rickey (@HelloRickey) -discussions-to: +discussions-to: https://ethereum-magicians.org/t/add-erc-request-method-types/19183 status: Draft type: Standards Track category: ERC From c52ba2dd59a3e01a2a5bf448caf665288616cfb5 Mon Sep 17 00:00:00 2001 From: rickey <12867336+HelloRickey@users.noreply.github.com> Date: Thu, 14 Mar 2024 11:25:59 +0800 Subject: [PATCH 3/7] change eip number --- ERCS/{erc-xxxx.md => erc-7654.md} | 8 ++++---- assets/{erc-xxxx => erc-7654}/RequestMethodTypes.sol | 0 assets/{erc-xxxx => erc-7654}/Types.sol | 0 3 files changed, 4 insertions(+), 4 deletions(-) rename ERCS/{erc-xxxx.md => erc-7654.md} (96%) rename assets/{erc-xxxx => erc-7654}/RequestMethodTypes.sol (100%) rename assets/{erc-xxxx => erc-7654}/Types.sol (100%) diff --git a/ERCS/erc-xxxx.md b/ERCS/erc-7654.md similarity index 96% rename from ERCS/erc-xxxx.md rename to ERCS/erc-7654.md index c0b6a087b0..896ada882b 100644 --- a/ERCS/erc-xxxx.md +++ b/ERCS/erc-7654.md @@ -1,9 +1,9 @@ --- -eip: xxxx +eip: 7654 title: Request Method Types description: Use a set of request methods to indicate the type of action to take on the contract. author: Rickey (@HelloRickey) -discussions-to: https://ethereum-magicians.org/t/add-erc-request-method-types/19183 +discussions-to: https://ethereum-magicians.org/t/erc-7654-request-method-types/19183 status: Draft type: Standards Track category: ERC @@ -122,7 +122,7 @@ interface IRequestMethodTypes{ ### Library -The library [`Types.sol`](../assets/erc-xxxx/Types.sol) contains an enumeration of Solidity types used in the above interfaces. +The library [`Types.sol`](../assets/erc-7654/Types.sol) contains an enumeration of Solidity types used in the above interfaces. ## Rationale @@ -161,7 +161,7 @@ var resDataDecode = web3.eth.abi.decodeParameters( ## Reference Implementation -See [Request Method Types Example](../assets/erc-xxxx/RequestMethodTypes.sol) +See [Request Method Types Example](../assets/erc-7654/RequestMethodTypes.sol) ## Security Considerations diff --git a/assets/erc-xxxx/RequestMethodTypes.sol b/assets/erc-7654/RequestMethodTypes.sol similarity index 100% rename from assets/erc-xxxx/RequestMethodTypes.sol rename to assets/erc-7654/RequestMethodTypes.sol diff --git a/assets/erc-xxxx/Types.sol b/assets/erc-7654/Types.sol similarity index 100% rename from assets/erc-xxxx/Types.sol rename to assets/erc-7654/Types.sol From 0adc994293854a310d7a3210ee0a4cab59fc9e07 Mon Sep 17 00:00:00 2001 From: rickey <12867336+HelloRickey@users.noreply.github.com> Date: Thu, 14 Mar 2024 11:42:06 +0800 Subject: [PATCH 4/7] fix assets error --- ERCS/erc-7654.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ERCS/erc-7654.md b/ERCS/erc-7654.md index 896ada882b..6e34df0129 100644 --- a/ERCS/erc-7654.md +++ b/ERCS/erc-7654.md @@ -122,7 +122,7 @@ interface IRequestMethodTypes{ ### Library -The library [`Types.sol`](../assets/erc-7654/Types.sol) contains an enumeration of Solidity types used in the above interfaces. +The library [`Types.sol`](../assets/eip-7654/Types.sol) contains an enumeration of Solidity types used in the above interfaces. ## Rationale @@ -161,7 +161,7 @@ var resDataDecode = web3.eth.abi.decodeParameters( ## Reference Implementation -See [Request Method Types Example](../assets/erc-7654/RequestMethodTypes.sol) +See [Request Method Types Example](../assets/eip-7654/RequestMethodTypes.sol) ## Security Considerations From 0abfcf6db285009ea792ec3b444e20374e295924 Mon Sep 17 00:00:00 2001 From: rickey <12867336+HelloRickey@users.noreply.github.com> Date: Wed, 27 Mar 2024 18:40:16 +0800 Subject: [PATCH 5/7] fix some issues --- ERCS/erc-7654.md | 6 ++++++ assets/erc-7654/RequestMethodTypes.sol | 11 +++++------ 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/ERCS/erc-7654.md b/ERCS/erc-7654.md index 6e34df0129..f22455ab0a 100644 --- a/ERCS/erc-7654.md +++ b/ERCS/erc-7654.md @@ -73,6 +73,12 @@ interface IRequestMethodTypes{ 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. diff --git a/assets/erc-7654/RequestMethodTypes.sol b/assets/erc-7654/RequestMethodTypes.sol index 638412654a..ec6939dc6b 100644 --- a/assets/erc-7654/RequestMethodTypes.sol +++ b/assets/erc-7654/RequestMethodTypes.sol @@ -23,10 +23,9 @@ contract RequestMethodTypes is IRequestMethodTypes{ Types.Type[] memory dataTypeArray = new Types.Type[](2); dataTypeArray[0] = Types.Type.STRING; dataTypeArray[1] = Types.Type.UINT256; - Types.Type[] memory putReqArray = new Types.Type[](3); + Types.Type[] memory putReqArray = new Types.Type[](2); putReqArray[0] = Types.Type.ADDRESS; putReqArray[1] = Types.Type.STRING; - putReqArray[2] = Types.Type.UINT256; // @dev initialize get, post, put request parameter data types and response data types setMethod("getUser",MethodTypes.GET,getReqArray,dataTypeArray); setMethod("createUser",MethodTypes.POST,dataTypeArray,new Types.Type[](0)); @@ -71,10 +70,10 @@ contract RequestMethodTypes is IRequestMethodTypes{ function put(string memory _methodName,bytes memory _methodReq)public returns(bytes memory){ if(compareStrings(_methodName,"updateUserName")){ - (string memory name)=abi.decode(_methodReq, (string)); - users[msg.sender].name=name; - return abi.encode(""); - } + (address userAddress,string memory name)=abi.decode(_methodReq, (address,string)); + require(userAddress==msg.sender); + users[userAddress].name=name; + } return abi.encode(""); } From 9af9a2c0ecf0a13852f0d1e2eb1ae6c9f98400f5 Mon Sep 17 00:00:00 2001 From: rickey <12867336+HelloRickey@users.noreply.github.com> Date: Sat, 8 Jun 2024 15:13:00 +0800 Subject: [PATCH 6/7] fix requested changes --- ERCS/erc-7654.md | 50 ++++---------------------- assets/erc-7654/RequestMethodTypes.sol | 2 +- 2 files changed, 8 insertions(+), 44 deletions(-) diff --git a/ERCS/erc-7654.md b/ERCS/erc-7654.md index f22455ab0a..4bb244ceae 100644 --- a/ERCS/erc-7654.md +++ b/ERCS/erc-7654.md @@ -10,25 +10,13 @@ category: ERC created: 2024-03-13 --- -## Abstract +## Abstract -This proposal standardizes a set of request and response communication standards between clients and smart contracts, using POST, GET, and PUT requests to create, read, and update the states of smart contracts. You can customize different request method names, request parameters and response values, and each request method will be mapped to a specific operation. +Usually each abi file corresponds to a specific contract, and the client cannot use the same abi to call different functions of different contracts. Contract Request Methods redefines the request method of the contract, so that multiple contracts can be called without using multiple different abi files. ## Motivation -In Web2.0, we often use HTTP methods to define how the client interacts with the server. We use the POST, GET, and PUT methods to perform different operations on the resources in the server. This standard effectively simplifies the interaction between the client and the server. Similar standards can also be used to wrap functions in smart contracts. When the client makes a request to the contract, it indicates the type and method of operation to be taken, and the corresponding method in the contract performs the operation and returns a response. - -Advantages of using this standard include: - -- Standardization: Provide a unified and standardized communication method for clients and contracts, so that all parties can conduct more standardized interactions. - -- Composability: It can customize request methods, request parameters and response values, and easily build complex applications with multiple contract request methods components. - -- Interoperability: Easily extend plugins or middleware in request methods to enable interoperability with onchain applications from all parties. - -- State Management: Create, retrieve, and modify smart contract states according to the application use cases of each party. - -- RESTful: It can adopt RESTful design principles to make it more predictable and make calls to all parties clearer and easier to understand. +Usually each abi file corresponds to a specific contract, and the client cannot use the same abi to call different functions of different contracts. Contract Request Methods redefines the request method of the contract. It implements the ability to call different functions of multiple different contracts using a consistent set of rules and a standard. ## Specification @@ -132,38 +120,14 @@ The library [`Types.sol`](../assets/eip-7654/Types.sol) contains an enumeration ## Rationale -### How to customize response status code? - -The return value type of each request method is bytes, so you can customize the response status code and other response information according to your needs. +### Type of request method ### -### How to disable a 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. -You can add a mapping(string=>bool) to save the request method name and available state. After the application is upgraded, manually set the available state of the disabled request method to false. And check the state every time a request is processed. +### Request method parameter type ### -### Why is there no delete method 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. -The data in smart contracts is public, and deleting data is an inefficient operation. In order to facilitate data management and retrieval, you can add a mapping to save the valid state of the data, and add a put request method to set valid and invalid data. And only return valid data in get method. - -### How to dynamically add new request methods? - -Before deploying the contract, you can add a mapping (address=>bool) to record the external contract permissions. When the application adds new functions, give the v2 contract write permissions and add new request methods to the v2 contract. - -### How to encode request parameters and decode response? - -Use ```getMethodReqAndRes``` to get the data type of the method request parameters and the data type of the response value. Then use some js library to encode and decode it. -For example: - -```javascript -var reqDataEncode = web3.eth.abi.encodeParameters( - ["string", "uint256"], - ["alice", "1"] -); - -var resDataDecode = web3.eth.abi.decodeParameters( - ["string", "uint256"], - reqDataEncode -); -``` ## Reference Implementation diff --git a/assets/erc-7654/RequestMethodTypes.sol b/assets/erc-7654/RequestMethodTypes.sol index ec6939dc6b..b3c3221402 100644 --- a/assets/erc-7654/RequestMethodTypes.sol +++ b/assets/erc-7654/RequestMethodTypes.sol @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: GPL-3.0 +// SPDX-License-Identifier: CC0-1.0 pragma solidity >=0.8.0; import "./Types.sol"; import "./IRequestMethodTypes.sol"; From accb2bb942180df9bf89fc6c8438f25151f5a5b8 Mon Sep 17 00:00:00 2001 From: rickey <12867336+HelloRickey@users.noreply.github.com> Date: Tue, 11 Jun 2024 19:58:32 +0800 Subject: [PATCH 7/7] fix requested changes --- ERCS/erc-7654.md | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/ERCS/erc-7654.md b/ERCS/erc-7654.md index 4bb244ceae..edfd53de14 100644 --- a/ERCS/erc-7654.md +++ b/ERCS/erc-7654.md @@ -12,11 +12,13 @@ created: 2024-03-13 ## Abstract -Usually each abi file corresponds to a specific contract, and the client cannot use the same abi to call different functions of different contracts. Contract Request Methods redefines the request method of the contract, so that multiple contracts can be called without using multiple different abi files. +This proposal standardizes a set of request and response communication standards between clients and smart contracts, using POST, GET, and PUT requests to create, read, and update the states of smart contracts. You can customize different request method names, request parameters and response values, and each request method will be mapped to a specific operation. ## Motivation -Usually each abi file corresponds to a specific contract, and the client cannot use the same abi to call different functions of different contracts. Contract Request Methods redefines the request method of the contract. It implements the ability to call different functions of multiple different contracts using a consistent set of rules and a standard. +Since each contract has different functions, the client cannot use a standard to call different functions of different contracts. Contract Request Methods redefines the request method of the contract, so that different functions of multiple different contracts can be called using a consistent set of rules and protocols. + +By dividing the function types into POST, GET, and PUT, different operations can be performed on the contract. This clear operation type can not only help all parties limit the access and operation of contract data, but also effectively simplify the interaction between the client and the contract, making it easier for all parties to understand the functions and hierarchical structure of the contract. The request and response parameter data types of each function of this standard can express the expected operation of the contract and have the ability to describe its own structure, which is conducive to the parties and contracts to create a unified and predictable way of exchanging data. ## Specification @@ -37,7 +39,7 @@ Workflow: 1. Call ```options``` to obtain supported request method types. 2. Call ```getMethods``` to obtain the request method name. 3. Call ```getMethodReqAndRes``` to obtain the request parameter data type and response value data type. -4. Encode request parameters and call ```get```, ```pot```, and ```put```. +4. Encode request parameters and call ```get```, ```post```, and ```put```. 5. Decode response value. ### Interfaces @@ -120,11 +122,11 @@ The library [`Types.sol`](../assets/eip-7654/Types.sol) contains an enumeration ## Rationale -### Type of request method ### +### 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 ### +### 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.