Solidity

Etherscan 說沒有足夠的乙太幣,為什麼?

  • February 10, 2020

Etherscan 說沒有足夠的乙太幣,為什麼?參數,我輸入 => requestLoan(3,31,3) 並在更改帳戶後,provideGuarantee(0,3)

我已經創建了 dApp 以及它在我的程式碼中的外觀,你可以在這裡看到

this.etherAmount = 3;
await this.bankContract.methods.requestLoan(this.etherAmount, 31, 2).send({from: this.state.borrower}).then(res => console.log("Request loan " + res.status));
await this.bankContract.methods.provideGuarantee(0, 4).send({from: this.state.guarantor, gas: 40000, value: this.etherAmount}).then(res => console.log("Provide guarantee " + res.status));

交易連結 => https://rinkeby.etherscan.io/tx/0xab31946f54cb86187dd517ff7c8ebc34730714a9717ee33a11ea22d54a31ed11

function requestLoan(uint etherBorrow, uint8 payBackDate, uint8 
etherInterest) public
{
   Loan memory loan = Loan({loanee: msg.sender, index: loansCount, etherBorrow: etherBorrow,
                                   payBackDate: now + (payBackDate * 1 days), etherInterest: etherInterest,
                                   _isGuaranteeProvided: false, _isLoanProvided: false, _isLoanExist: true});
   loans[loansCount] = loan;
   loansCount++;
}

function provideGuarantee(uint index, uint8 guaranteeInterest) public payable // payable means that value should have ether
{
   require(
       index < loansCount,
       "This index does not exist");

   require(loans[index].loanee != msg.sender,
           "The borrower can't provide a guarantee to himself");

   require(lenders[index] != msg.sender,
           "The lender can't provide guarantee for the loan");

   require(!loans[index]._isGuaranteeProvided, 
       "This loan already has a guarantee");

   require(guaranteesCount < loansCount || !guarantees[index]._isWaitingForHandling, 
       "This guarantee already waiting for handling of borrower");

   require(guaranteeInterest > 0, 
           "Too low interest");

   require(
       msg.value == loans[index].etherBorrow,
       "You don't have enough eather to provide guarantee");

   require(
       loans[index]._isLoanExist,
       "This loan does not exist");

   require(
       !guarantees[index]._isGuaranteeExist,
       "This guarantee does exist");

   Guarantee memory guarantee = Guarantee({guarantor: msg.sender, etherInterest: guaranteeInterest,
                                               loanIndex: index, _isWaitingForHandling: true, _isGuaranteeExist: true});
   guarantees[index] = guarantee;
   guaranteesCount++;
}

wei您正在為value您的功能提供 3 provideGuarantee- 您正在為其創建貸款的相同金額。在您的程式碼中,您檢查金額是否相同 ( msg.value == loans[index].etherBorrow) 則合約會拋出該錯誤:Fail with error 'You don't have enough eather to provide guarantee

我有點不確定你為什麼會有這樣的情況以及你到底想要做什麼,但可能你只需要提供更多的乙太幣(weis)作為value

正如 LauriPeltonen 所說,失敗的檢查是msg.value == loans[index].etherBorrow因為您發送 3wei,然後loans[index].etherBorrow在 index = 0 時不是 3。

查看事務歷史記錄,第一次成功呼叫requestLoan是 at https://rinkeby.etherscan.io/tx/0x879a238d031e81bac5cc2092a8897b6feaa8f269c0be737ad313c812e4aaef22。使用以下參數。

  • etherBorrow = 300000000000
  • 還款日期 = 31
  • 乙太利息 = 5

因此,除非您有其他函式來覆蓋貸款數據呼叫provideGuarantee(0, 4)將失敗,除非您發送 300000000000 wei = 300 gwei。

我建議在創建負載時始終生成事件,以便查看事件,您可以確定執行期間分配的索引等內容。

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