Solidity

如果合約中有錢,無法提現到多個錢包?

  • September 6, 2022

如果合約中有 0 個乙太坊,這個提現功能可以正常工作;但是,一有乙太坊(我一直在測試發送 0.04 個乙太坊),提款功能就會中斷。我認為這是solidity整數乘法的問題,但我不確定。

這裡的目標是能夠為提款設定一個“稅率”,其中 % 的提款流向了“納稅人”。現在我把它設置為1%。

我感謝任何幫助或建議,謝謝!


pragma solidity ^0.8.10;

import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
import "@openzeppelin/contracts/access/Ownable.sol";


contract TestWithdraw is ERC721, Ownable {
   uint256 private _tax_rate = 1;
   address private _tax_man = 0xf9DEB97CcA539576CD582A785465eB9088f36696;


   constructor() ERC721('assetName', 'asset') 
   {
   }
   function recieve_money() public payable {

   }

   function withdraw_to_me() public view returns (uint256){
       uint256 balance = address(this).balance;
       uint256 local_tax = 100 - _tax_rate;
       uint256 new_balance = balance * local_tax;
       return (new_balance);
   }
   function withdraw_to_taxman() public view returns (uint256){
       uint256 balance = address(this).balance;
       uint256 withdraw_amount = balance * _tax_rate;
       return (withdraw_amount);
   }

   function withdraw() external onlyOwner {
       payable(msg.sender).transfer(withdraw_to_me());
       payable(_tax_man).transfer(withdraw_to_taxman());
   }

}

現在,您local_tax以百分比表示,範圍為 0-100。您將 乘以balance這個數字,但忘記調整它是 a percentage,因此您應該在之後除以 100。

你試圖轉出比你的合約更多的乙太幣。這將解決它:


pragma solidity ^0.8.10;

import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
import "@openzeppelin/contracts/access/Ownable.sol";


contract TestWithdraw is ERC721, Ownable {
   uint256 private _tax_rate = 1;
   address private _tax_man = 0xf9DEB97CcA539576CD582A785465eB9088f36696;


   constructor() ERC721('assetName', 'asset') 
   {
   }
   function recieve_money() public payable {

   }

   function withdraw_to_me() public view returns (uint256){
       uint256 balance = address(this).balance;
       uint256 local_tax = 100 - _tax_rate;
       uint256 new_balance = (balance * local_tax) / 100;
       return (new_balance);
   }
   function withdraw_to_taxman() public view returns (uint256){
       uint256 balance = address(this).balance;
       uint256 withdraw_amount = (balance * _tax_rate) / 100;
       return (withdraw_amount);
   }

   function withdraw() external onlyOwner {
       payable(msg.sender).transfer(withdraw_to_me());
       payable(_tax_man).transfer(withdraw_to_taxman());
   }

}

此外,你有點過於復雜了。就像是 :


pragma solidity ^0.8.10;

import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
import "@openzeppelin/contracts/access/Ownable.sol";

contract TestWithdraw is ERC721, Ownable {
   uint256 private _tax_rate = 1;
   address private _tax_man = 0xf9DEB97CcA539576CD582A785465eB9088f36696;


   constructor() ERC721('assetName', 'asset') 
   {
   }
   function recieve_money() public payable {

   }

   function withdraw() external onlyOwner {
       uint tax = (address(this).balance * tax_rate) / 100;
       payable(_tax_man).transfer(tax);
       payable(msg.sender).transfer(address(this).balance);    
   }

}

也可以,更緊湊和優雅。其他方面說明,solidity 風格指南建議使用蛇皮套而不是駱駝皮套,但這並不重要哈哈

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