Reputation: 121
I am a beginner on smart contract development. I am using openZeppelin, truffle and Ganache to develop some really basic token and crowdsale contract. I came across an error when I tried to call the buytoken()
method from the crowdsale contract in Truffle console . Can someone help me solve the problem? There is no problem when migrating and deploying the contract.
contract Crowdsale {
using SafeMath for uint256;
// The token being sold
ERC20 public token;
// Address where funds are collected
address public wallet;
// How many token units a buyer gets per wei.
// The rate is the conversion between wei and the smallest and indivisible token unit.
// So, if you are using a rate of 1 with a DetailedERC20 token with 3 decimals called TOK
// 1 wei will give you 1 unit, or 0.001 TOK.
uint256 public rate;
// Amount of wei raised
uint256 public weiRaised;
/**
* Event for token purchase logging
* @param purchaser who paid for the tokens
* @param beneficiary who got the tokens
* @param value weis paid for purchase
* @param amount amount of tokens purchased
*/
event TokenPurchase(
address indexed purchaser,
address indexed beneficiary,
uint256 value,
uint256 amount
);
/**
* @param _rate Number of token units a buyer gets per wei
* @param _wallet Address where collected funds will be forwarded to
* @param _token Address of the token being sold
*/
constructor(uint256 _rate, address _wallet, ERC20 _token) public {
require(_rate > 0);
require(_wallet != address(0));
require(_token != address(0));
rate = _rate;
wallet = _wallet;
token = _token;
}
// -----------------------------------------
// Crowdsale external interface
// -----------------------------------------
/**
* @dev fallback function ***DO NOT OVERRIDE***
*/
function () external payable {
}
/**
* @dev low level token purchase ***DO NOT OVERRIDE***
* @param _beneficiary Address performing the token purchase
*/
function buyTokens(address _beneficiary, uint256 amount) public payable {
uint256 weiAmount = amount.mul(rate);
token.transfer(_beneficiary, weiAmount);
}
The truffle console command is listed below:
myToken.deployed().then(function(i){BT = i})
myCrowdsale.deployed().then(function(i){BTC = i})
BT.transferOwnership(BTC.address)
purchaser = web3.eth.accounts[2]
BTC.buyTokens(purchaser,web3.toWei(5, "ether") )
Upvotes: 1
Views: 1330
Reputation: 40404
When implementing a payable
the amount paid must not be an argument, but instead it will be available at msg.value
. Otherwise you're not sending any ether, and/or can be exploited, if I call the method with: 5 ether
as amount
but I only send 1 wei
.
function buyTokens(address _beneficiary) public payable {
uint256 weiAmount = msg.value.mul(rate);
token.transfer(_beneficiary, weiAmount);
}
Furthermore, if the beneficiary address is the same that buys the token, you can use: msg.sender
And the method must be called like this:
BTC.buyTokens(purchaser, { value: web3.toWei(5, "ether"), from: purchaser });
or using msg.sender
BTC.buyTokens({ value: web3.toWei(5, "ether"), from: purchaser });
If you don't use: from
the ethers will be sent by the default account, which is not purchaser
in your case.
Upvotes: 1