Solidity

如何插入發送方和接收方均可更新的數據?

  • August 19, 2022

———堅固———

struct Requests
{ 
   string status; // accepted | pending | rejected
   string amount; // 1,000,000 PINE
   string description; //
}

mapping (address => Requests[]) public Rqst;

這是一個創建請求:

function createRequest(Requests memory rqst) external
{
       Rqst[msg.sender].push(Requests({
           status:rqst.status,
           amount:rqst.amount,
           description:rqst.description
       }));
}

這是獲取請求:

function getRequest(uint requestIndex) external view returns (Requests memory)
{
       Requests storage rqst = Rqst[msg.sender][requestIndex];
       return rqst;
}

更新請求

function updateRequest(uint256 requestIndex, Requests memory bar) external 
{
   Rqst[msg.sender][requestIndex].status = bar.status;
   Rqst[msg.sender][requestIndex].amount = bar.amount;
   Rqst[msg.sender][requestIndex].description = bar.description;
}

———Solidity 結束———

———React.js———

 const createRequest =  async () => {
   await contract.methods.createRequest(['pending',amount, description]).send({ from: defaultAccount });
 }

注意:在 createRequest 中,defaultAccount 是目前的元遮罩登錄,也就是員工。

如果元遮罩登錄是經理,則呼叫此方法 getRequests。

 const getRequests =  async (contract, defaultAccount) => {
   const f = async () => {
     const len = await contract.methods.getRequestount().call({ from: defaultAccount });
     console.log("len",len)
     let iterator = 0
     let contain = []
     while ( iterator <= len) {
       try {
         console.log("iterator",iterator)
         let o = await contract.methods.getRequest(String(iterator)).call({ from: defaultAccount });
         if (o[0] !== '') {
           contain.push({
             status: o['status'],
             amount: o['amount'],
             description: o['description']
           })
         }
         else {
           console.log('The index ' + iterator + ' is empty');
         }
       } catch {
         console.log('Failed to get requests ' + iterator);
       }
       iterator++;
     }
     return contain
   }
   let c = await f()
   dispatch(setRequests(c))
 }

———-React.js 結束——–

我正在向我的經理提出請求。經理有一個顯示請求列表的管理員。

當我getRequest()web3 返回時MetaMask - RPC Error: execution reverted

我嘗試使用管理器創建請求,然後顯示請求,它執行良好。

您的目標似乎是允許經理獲取和更新員工的請求。

您應該為員工的地址添加一個參數

function getRequest(address employee, uint requestIndex) external view returns (Requests memory) {
       Requests storage rqst = Rqst[employee][requestIndex];
       return rqst;
} 

這將返回給定索引處的請求。

要返回員工的所有請求,您可以這樣做:

function getRequests(address employee) external view returns (Requests[] memory) {
       return Rqst[employee];
} 

同樣,要更新員工的請求,您應該為員工的地址添加一個參數

隨意為經理添加一個變數,address public manager;在建構子中設置經理

// This will set the deployer of the contract as manager
constructor() {manager = msg.sender}

和權限更新請求

   function updateRequest(
     address employee, 
     uint256 requestIndex, 
     Requests memory bar
   ) external {
     require(msg.sender == employee || msg.sender == manager, "NOT_ALLOWED")

     Rqst[employee][requestIndex].status = bar.status;
     Rqst[employee][requestIndex].amount = bar.amount;
     Rqst[employee][requestIndex].description = bar.description;

   }

 

引用自:https://ethereum.stackexchange.com/questions/133904