1
pragma solidity ^0.4.18;

import 'zeppelin-solidity/contracts/math/SafeMath.sol';
import 'zeppelin-solidity/contracts/token/ERC20/ERC20.sol';

import '../util/BytesLib.sol';

contract Crowdsale {
    using SafeMath for uint256;
    using BytesLib for bytes;

    // Address where funds are collected
    address public wallet;

    ERC20 public token;

    // Amount of wei raised
    uint256 public weiRaised;

    function Crowdsale(address _wallet, ERC20 _token) public {
        require(_wallet != address(0));

        wallet = _wallet;
        token = _token;
    }

    function () external payable {
        buyTokens(msg.sender);
    }

    function buyTokens(address _beneficiary) public payable {

        bytes memory msgData = msg.data;
        bytes memory dataValue = msgData.slice(36, 32);
        uint256 weiAmount = dataValue.toUint(0);

        weiRaised = weiRaised.add(weiAmount);
      }   
}

when I transfer my ERC20 tokens(not Ether) to the contract, the msg.data.length is always zero. However, in dev environment, when I assign a value to the msg.data below, the parameter weiAmount get the correct number as expected.

bytes msgData = '0xa9059cbb0000000000000000000000007d052a29e0cf3b046e0618df7bed3423725619df0000000000000000000000000000000000000000000000000de0b6b3a7640000'.

ps: BytesLib.sol is used to slice the bytes, see this link https://github.com/GNSPS/solidity-bytes-utils/blob/master/contracts/BytesLib.sol

  • 1
    plz add your code to have a better view of your question – shahabvshahabi Mar 8 '18 at 10:08
  • Could you deploy to ropsten test net and share the transaction you made at ropsten.etherscan.io? – Muhammad Altabba Mar 8 '18 at 11:52
  • Also it is not clear what is your problem as you said "it works" then you said "the result is always zero". Do you mean that the transaction succeed? and you said "the result is always zero". Do you mean the msg.data.length is zero or you are referring to something else? I suggest you rewrite your question to be more understandable. – Muhammad Altabba Mar 8 '18 at 11:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.