Solidity

如何讓 require() 在 Solidity 的應付函式中接受乙太的十進制值?

  • March 12, 2019

使用時,

  1. require(msg.value == registerFeeMap[msg.sender] /_currentRate) (計算的小數 ETH 值)
  2. require(msg.value == registerFeeMap[msg.sender]*10**18/ _currentRate)

(ETH 轉換為 Wei) 3. require((msg.value*10**18) == registerFeeMap[msg.sender]*10**18/ _currentRate)

在, function feePayment(string _sign, uint _currentRate) external payable 在使用我得到的所有 3 個時,VM Exception while processing transaction: revert

  1. _currentRate這裡是函式的參數feePayment(string _sign, uint _currentRate) external payable,_sign 是 MetaMask 簽名的消息字元串,_currentRate 是 CryptoCompare API 中的 INR 與 ETH 值。
  2. registerFeeMap``function newHome(string _add, string _type, uint _startEpoch, uint _endEpoch, uint _months, uint _rent, uint _security) external用取決於_rent變數的值初始化。

儲存在 registerFeeMap 中的範例值 >= 100

API 中的 _currentRate 範例值 = 8380

如果 Ether 的小數值用於支付,我應該如何將 Eth 轉換為 Wei for require()?

我的契約程式碼如下

pragma solidity ^0.4.19;

contract Owned {

address owner;

function Owned() public {
   owner = msg.sender;
}

modifier onlyOwner() {
   require(msg.sender == owner);
   _;
}
}


contract Rent is Owned {    
struct Person {

   address eth;
   string legalName;
   string email;
   uint aadhaar;

   string signTerms; //sign on the Terms and Conditions

   uint[] myOwned;
   uint[] myRented;
}

struct Parties {

   address landlord;
   address tenant;

   string signLandlord;
   string signTenant;

   bool completed;
}


struct House {

   string addressHouse;
   string type_of_property;

   uint startEpoch;
   uint endEpoch; 

   uint monthDuration; // difference in Months of these Dates

   uint rentAmount;
   uint securityFee;
   uint registerFee;

   bool completed;
}

struct OtherDetails {

   string latitude;
   string longitude;

   string ipfs_url; // for Future Versions of, when we support IPFS URLs

   uint squareFootage;
   uint numberBedrooms;
   string others;

   bool completed;
}

struct Checks {

   bool isValid;

   bool tenantApprove;
   bool govApprove;

   bool tenantCheck;

   bool paidRegisterFee;
   bool paidSecurityFee;
}

Parties[] public allParties; 
House[] public allHouses;
OtherDetails[] public allOtherDetails;
Checks[] private allChecks;

mapping(address => Person) public addressToPerson;
mapping(address => bool) private checkUser;
mapping(uint => bool) private checkAadhaar;

mapping(address => uint) private registerFeeMap;
mapping(address => uint) private securityFeeMap;


function Rent() public {

   checkUser[owner] = true;
   var govt = Person(owner, 'Owner', 'Owner', 0, 'No Sign', new uint[](0), new uint[](0));

   addressToPerson[owner] = govt;
}


event registerParty(string message);

function registerParties(address _tenant) external  {

   require(msg.sender != _tenant);

   if(checkUser[msg.sender] == true)
   {
       if(checkUser[_tenant] == true)
       {
           var newParty = Parties(msg.sender, _tenant,'N/A', 'N/A', true);
           var index = allParties.push(newParty) - 1;

           var newHouse = House('No Address Added', 'No Property Type', 0, 0, 0, 0, 0, 0, false);
           allHouses.push(newHouse);

           var newDetails = OtherDetails('28.7041', '77.1025', 'Currently IPFS Not Supported', 0, 0, 'N/A', false);
           allOtherDetails.push(newDetails);

           var newChecks = Checks(false, false, false, false, false, false);
           allChecks.push(newChecks);

           var user = addressToPerson[msg.sender];
           user.myOwned.push(index);

           var tenant = addressToPerson[_tenant];
           tenant.myRented.push(index);

           registerParty('Tenant was Successfully added to Contract, Proceed to Step 2');
       }

       else
       {
           registerParty('Tenant is not registered on Charter, registration of both parties is required');
       }
   }

   else
   {
       registerParty('Kindly register on Charter, before drafting a Contract');
   }
}


event registerHome(string message, uint FeePayable);

function newHome(string _add, string _type, uint _startEpoch, uint _endEpoch, uint _months, uint _rent, uint _security) external {

   if(checkUser[msg.sender] == true)
   {
       var user = addressToPerson[msg.sender];

       uint num = user.myOwned.length - 1;

       if(num < 0)
       {
           registerHome('Kindly fill Tenant Info in Step 1 before proceeding to Step 2', 0);
       }

       else
       {
           uint index = user.myOwned[num];
           var houseOwner = allParties[index];

           if(houseOwner.landlord != msg.sender)
           {
               registerHome('Kindly register on Charter, before drafting a Contract', 0);
           }

           else
           {
               var home = allHouses[index];

               if(home.completed == false)
               {
                   home.addressHouse = _add;
                   home.type_of_property = _type;

                   home.startEpoch = _startEpoch;
                   home.endEpoch = _endEpoch;

                   home.monthDuration = _months;

                   home.rentAmount = _rent;
                   home.securityFee = _security;

                   var _tenant = allParties[index].tenant; 

                   if((home.monthDuration < 12)&&(home.monthDuration > 0))
                   {
                       home.registerFee = 100;
                       home.completed = true;

                       registerHome("Property's Information successfully entered, Proceed to Step-3", home.registerFee);
                   }

                   else if(home.monthDuration <= 60)
                   {
                       if(_security > 0)
                       home.registerFee = 100 + ((2 * 12 * _rent) / 100) + 1100;

                       else
                       home.registerFee = ((2 * 12 * _rent) / 100) + 1100;

                       home.completed = true;

                       registerHome("Property's Information successfully entered, Proceed to Step-3", home.registerFee);
                   }

                   else if(home.monthDuration <= 120)
                   {
                       if(_security > 0)
                       home.registerFee = 100 + ((3 * 12 * _rent) / 100) + 1100;

                       else
                       home.registerFee = ((3 * 12 * _rent) / 100) + 1100;     

                       home.completed = true;

                       registerHome("Property's Information successfully entered, Proceed to Step-3", home.registerFee);       
                   }

                   else if(home.monthDuration <= 240)
                   {
                       if(_security > 0)
                       home.registerFee = 100 + ((6 * 12 * _rent) / 100) + 1100;

                       else
                       home.registerFee = ((6 * 12 * _rent) / 100) + 1100;

                       home.completed = true;
                       registerHome("Property's Information successfully entered, Proceed to Step-3", home.registerFee);
                   }

                   else
                   {
                       home.completed = false;
                       registerHome('Kindly Enter Duration of Contract Correctly.. (Min - 1 month, Max - 240 Months' , 0);
                   }
               }

               else
               {
                   registerHome('Home Registration is already Completed ', 0);
               }

               registerFeeMap[msg.sender] = home.registerFee;
               securityFeeMap[_tenant] = home.securityFee;
           }

       }
   }

   else
   {
       registerHome('Kindly register on Charter, before drafting a Contract', 0);
   }
}



event feePay(string message);

function feePayment(string _sign, uint _currentRate) external payable {

   require(msg.value == (registerFeeMap[msg.sender] * 10**18 / _currentRate));

   if(checkUser[msg.sender] == true)
   {
       var user = addressToPerson[msg.sender];

       uint num = user.myOwned.length - 1;

       if(num < 0)
       {
           feePay('Complete all the Steps, before paying Registration Fee Payment');
       }

       else
       {

           uint index = user.myOwned[num];
           var party = allParties[index];
           var house = allHouses[index];
           var details = allOtherDetails[index];
           var checks = allChecks[index];

           if((details.completed == true)&&(house.completed == true)&&(party.completed == true))
           {
               user = addressToPerson[msg.sender];
               checks.paidRegisterFee = true;
               party.signLandlord = _sign;

               feePay('Government Registration Fee Payment Successful, Tenant Verification pending..');
           }

           else 
           {
               feePay('Complete all the Steps given above before Fee Payment');
           }
       }
   }

   else
   {
       feePay('Kindly register on Charter, before drafting a Contract');
   }
}
}

> > 儲存在 registerFeeMap 中
> > 的範例值 - 100 API 中 _currentRate 的範例值 - 8380 > > >

在 uint 數學中

100/8380 = 0

相同(即總是)。

因此,如果您的 msg.value 不為零,則您的第一個 require 參數不正確並恢復。

第二個是錯誤的,’^’ 高程運算符是錯誤的。

第三個應該是:

(A * 10**18 / B) / 10**18

或者您可以在之前乘以 msg.value * 10**18 進行比較。

這是關於 Solidity 劃分的一般性討論。

但是您確定要調整 msg.value 屬性嗎?

Msg.value 以 wei 表示。

你擔心 0.01193317422 wei 嗎?

我不這麼認為。讀取您的數據並將它們列印到控制台。之後你會更有信心。

(1 wei = 1 ether / 10**18,即 0.000000000000000001 ether)

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