In a crowdsale contract, the buyer calls the buyTokens() function, and sends ether along to receive tokens in exchange. In the openzeppelin contracts, the buyToken takes only parameter - address _beneficiary.
I am faced with a problem & a challenge
1] Problem- How will a user send ether to this function? For eg: CrowdSale.address.call{value: _weiAmount}, or just call the buyTokens function.
2] How can i test this function in a local environment[truffle, or ganache]?
——
CODE BLOCK
——
function buyTokens(address _beneficiary) public payable {
_beneficiary = msg.sender;
uint256 _weiAmount = msg.value;
uint256 _numberOfTokenUnits = _weiAmount.div(_rate);
_prePurchaseCheck(_beneficiary, _weiAmount);
_processPurchase(_beneficiary, _numberOfTokenUnits);
tokenSold = tokenSold.add(_numberOfTokenUnits);
_weiRaised = _weiRaised.add(_weiAmount);
_forwardFunds(_weiAmount);
emit TokensPurchased(_beneficiary, _beneficiary, _numberOfTokenUnits, block.timestamp);
}
function _prePurchaseCheck(address _buyer, uint256 _amount) internal view {
require(tokenSold <= 6000000000, "Sold out. Wait for phase 2 sale.");
require(_amount >= 500000000000000000 && _amount<= 10000000000000000000, "You can only purchase with amounts between 0.5 ether to 10 ether. Alternatively, the tokens available for sale maybe less than 0.5 ether. ");
require(_buyer != address(0), "Buyer can not be zero address");
}
function _processPurchase(address _beneficiary, uint256 _value) internal {
token.transfer(_beneficiary, _value);
this;
}
function _forwardFunds(uint256 _amount) internal /* returns(bool success) */ {
_wallet.transfer(_amount);
}
@Lauri_P Tagging you for better visibility.
Dec 15, 2020, 1:55 PM
Crowdsale contract usually implements fallback. So you don't need to call any function, just send eth to the contract and it will automatically calculate the token amount and send it to msg.sender
Dec 15, 2020, 1:57 PM
On that note, I do have Implemented both fallback & receive functions. Now, the testing part.
How can one know whether this function works properly, without having to write unit tests?
Dec 15, 2020, 1:58 PM
Without unit tests.... You can deploy on testnet and check the internal transactions
Dec 15, 2020, 1:59 PM
Can you please provide additional context. I am able to test internal functions within the buyTokens(), by changing their visibility to public. Have trouble writing a test case.
Can you please provide some pointer in that direction?
Much appreciated.
Can you please provide some pointer in that direction?
Much appreciated.
Dec 15, 2020, 2:02 PM
There isn't much of test case to be applied right... The only values that are being manipulated are sent ether and token amount. Additionally, you can check that emitted event which contains all the data required
Dec 15, 2020, 2:07 PM
Yeah but the way the network is the interaction doesn’t pass.
Just tried that Robô broski
Dec 15, 2020, 5:23 PM
Umm... What??
Dec 15, 2020, 5:45 PM
Like fallback/receive function
At least not right now with the network at full
Maybe on weekends who knows
Dec 15, 2020, 5:46 PM
Ooh you mean transaction is not being confirmed?
Dec 15, 2020, 5:47 PM
Erc 721 is just another style of ERC
There are many Ercs
They just follow certain parameters.
Erc 20 was created to help create tokens that could interact better with the Ethereum blockchain
Erc 721 is protocol for a non fungible token
Dec 15, 2020, 5:52 PM
Ooh that makes sense. Thanks🙂
So is there any specific erc which fulfils my need?
Which is Interacting with other contract
Dec 15, 2020, 5:54 PM
I haven’t read what you want :/
You can create a minter contract that interacts with your token
In etherscan you can see all transactions made by each specific token
Dec 15, 2020, 5:55 PM