Ame Network has been deployed on multiple testnets. This tutorial will guide you to quickly build a social component.

Component Structure

You can copy the Component template directly, or implement a component based on IComponent.

Component introduction:

The methodRequests and methodResponses are used to define the request parameter type and response data type of Method. Type 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;

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]
  );
}

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 pure returns(bytes memory){
  return abi.encode(_methodName,_methodReq);
}

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

Component Data

Component data is designed based on the functions provided by your component. Our example will implement user creation, query and modification.Each user has two data fields: name and age.

struct Profiles{
  string name;
  uint256 age;
}
mapping (address=>Profiles) users;

Initialization Method Data Type

Initialize the methods you provide in the constructor. We initialize three request methods:

GET request, getUser, obtains the user's name and age.POST request, createUser, creates a user.PUT request, updateUserName, updates the user's name.

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[](2);
  putReqArray[0] = Types.Type.ADDRESS;
  putReqArray[1] = Types.Type.STRING;
  setMethod("getUser",MethodTypes.GET,getReqArray,dataTypeArray);
  setMethod("createUser",MethodTypes.POST,dataTypeArray,new Types.Type[](0));
  setMethod("updateUserName",MethodTypes.PUT,putReqArray,new Types.Type[](0));
}

Implement Request Methods

Implement each request method in GET, POST, and PUT.

    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);
            bytes memory resBytes=abi.encode(name,age);
            emit  Response(resBytes);
            return resBytes;
        }
        return abi.encode("");
    }

    function put(string memory _methodName,bytes memory _methodReq)public returns(bytes memory){
        if(compareStrings(_methodName,"updateUserName")){
            (address userAddress,string memory name)=abi.decode(_methodReq, (address,string));
            require(userAddress==msg.sender);
            users[userAddress].name=name;
        }
        return abi.encode("");
    }

Deploy Contract

All code can be viewed at ComponentExample.

This component example has been deployed on multiple testnets.

How to Use Ame Components Scan

Ame Components Scan is a manager for viewing social components of various networks. You can use it directly to interact with components.

1. Please select a network and connect your wallet.

2. Register as a new user

3. Enter component address to search.

4. Call methods in GET, POST, and PUT

Finale

If you want to contribute your components, you can refer to the component standard, I am still actively building Ame Network. If you are also interested, you can contact me on Twitter at any time.

Mirror文章信息

Mirror原文:查看原文

作者地址:0xBFDc0906313Dec9DD0f38D5867060627e3ba5C9E

内容类型:application/json

应用名称:MirrorXYZ

内容摘要:Vh4OH8k3Pd3_CERrr-wT_7bayB1p2opmYSUHJZQ5iAc

原始内容摘要:9bxItBA2muUtOzH8PLdRCIjcBw1lpvmm-im1HqtF2GY

区块高度:1403287

发布时间:2024-04-13 10:34:11