Solidity
購買代幣功能在眾售智能合約中不起作用
我是乙太坊區塊鏈的新手。我創建了一個眾籌和 ERC20 智能合約並部署在 rinkeby 測試網路上。ERC20 代幣轉移功能執行良好。
但是當我檢查購買令牌功能的功能時。所以它執行成功。但是當我在https://rinkeby.etherscan.io/檢查它的輸出時。所以它顯示 0 代幣轉移。而且它不會在錢包上顯示代幣價格。
眾籌智能合約程式碼如下:-
pragma solidity ^0.4.2; import "./DappToken.sol"; contract DappTokenSale { address admin; DappToken public tokenContract; uint256 public tokenPrice; uint256 public tokensSold; uint256 public decimals; event Sell(address _buyer, uint256 _amount); function DappTokenSale(DappToken _tokenContract, uint256 _tokenPrice) public { admin = msg.sender; tokenContract = _tokenContract; tokenPrice = _tokenPrice; } function multiply(uint x, uint y) internal pure returns (uint z) { require(y == 0 || (z = x * y) / y == x); } function buyTokens(address _receiver, uint256 _amount) public payable { _amount = msg.value; require(_receiver != address(0)); require(_amount > 0); uint256 tokensToBuy = multiply(_amount, (10 ** decimals)) / 1 ether * tokenPrice; require(tokenContract.transfer(msg.sender, tokensToBuy)); tokensSold += _amount; emit Sell(msg.sender, tokensToBuy); } // Ending Token DappTokenSale function endSale() public { // Require admin require (msg.sender == admin); // Transfer remaing DappToken to admin require(tokenContract.transfer(admin,tokenContract.balanceOf(this))); // Destroy Contract selfdestruct(admin); } }
DappToken Solidity 程式碼:-
pragma solidity ^0.4.24; /** * The contractName contract does this and that... */ contract DappToken { // Name string public name = "DappToken"; // Symbol string public symbol = 'DAPP'; //standard string public standard = 'DApp Token v1.0'; //Decimals uint256 public decimals = 18; //token price uint256 public tokenPrice = 2000; //1 eth = 2000 tokens uint256 public totalSupply; event Transfer( address indexed _form, address indexed _to, uint256 _value ); event Approval( address indexed _owner, address indexed _spender, uint256 _value ); mapping(address => uint256) public balanceOf; mapping(address => mapping (address => uint256)) public allowance; // function DappToken (uint256 _intialSupply) public { // balanceOf[msg.sender] = _intialSupply; // totalSupply = _intialSupply; // //allocate the intial supply // // } constructor(uint256 _intialSupply) public { balanceOf[msg.sender] = _intialSupply; totalSupply = _intialSupply; } //Transfar function transfer(address _to,uint256 _value) public returns (bool success){ // Exception if account does not enough require(balanceOf[msg.sender] >= _value); // Transfar the balance balanceOf[msg.sender] -= _value; balanceOf[_to] += _value; // Transfar Event emit Transfer(msg.sender,_to,_value); // Return a boolean return true; } // approve function approve(address _spender,uint256 _value) public returns (bool success){ //allowence allowance[msg.sender][_spender] = _value; // Approve event emit Approval(msg.sender,_spender,_value); return true; } // transfer form function transferFrom(address _from, address _to, uint256 _value) public returns (bool success) { require(_value <= balanceOf[_from]); require(_value <= allowance[_from][msg.sender]); // change the balance balanceOf[_from] -= _value; //update the balance balanceOf[_to] += _value; allowance[_from][msg.sender] -= _value; emit Transfer(_from,_to,_value); return true; } }
實際上我想購買這樣的代幣功能:-
- 使用者輸入代幣數量(3 個乙太幣)。
- 我設置代幣價格 1 ether= 2000 代幣。
- 因此,當使用者輸入 3 個乙太幣時,他的帳戶中就會轉移 6000 個代幣。
所以我的查詢是:-
Query-1 :-為什麼購買代幣功能不會在接收者賬戶中發送 6000 代幣?
Query-1 :-我在智能合約程式碼中做錯了什麼?
我認為這應該是你的功能:
function buyTokens(address _receiver) public payable { uint256 _amount = msg.value; require(_receiver != address(0)); require(_amount > 0); uint256 tokensToBuy = multiply(_amount, (10 * decimals)) / 1 ether tokenPrice; require(tokenContract.transfer(_receiver, tokensToBuy)); tokensSold += _amount; emit Sell(msg.sender, tokensToBuy); }
編輯:您可以通過Metamask檢查最終使用者收到的代幣數量。