1

In geth console, I know I can pass arguments via new.

However if I just wanna use evm, how should it be done?

For example,

contract SetNumber { 
    int public number;
    function SetNumber(int _number) {
        number = _number;
    } 
}

How should I pass _number in evm?

I thought it might be by using --input

But I've checked the bytecode, there's no keccak256 of SetNumber(int256) in it.

1

I suspect the confusion is your SetNumber() function is a constructor. It's a constructor due to it having the same name as the contract.

Constructors run once only during the deployment process and can never run again. That means there's no way to send it a transaction.

Here I just renamed a few things and introduced another function that's ready to receive a message. This contract does what I think you wanted the original to do.

contract Number { 

    int public number;

    // this is the constructor. optionally remove init and get 0 to start.
    function Number(int initialize) {
        number = initialize;
    }

    // this function recieves messages and updates the number.
    function setNumber(int value) {
        number = value;
    }   
}

Hope it helps.

Edit:

Ajoy pointed out that I might have misunderstood the question.

If you're having difficulty passing value into the constructor itself, it happens during the transaction that deploys the contract, as explained here:

https://github.com/ethereum/go-ethereum/wiki/Contract-Tutorial#compile-and-deploy

They have an example of deploying a contract with "supply" passed to the constructor.

  • 1
    I think OP intends SetNumber(int) to be the constructor. The question is how to pass arguments to contract constructor. Or in other words, with your code, his question would be how to pass the initialize argument value to the contract constructor function Number(int initialize) – Ajoy Bhatia Feb 10 '17 at 22:37
  • You might be right. I may have misinterpreted "I know I can ... with new" sounded like "I know that part" in my head. – Rob Hitchens - B9lab Feb 10 '17 at 22:42
  • @AjoyBhatia was right. Sorry I did not make myself clear. – 張以白 Feb 13 '17 at 14:03
  • And I found that append arguments to the bytecode works! – 張以白 Feb 13 '17 at 14:04
  • 1
    Some more nitpicking, sorry. @RobHitchens - OP did know how to do that at the time of posting the question - but only with geth console, which is what the example of passing supply argument to the constructor shows. But he/she wanted to know how to do that in EVM. I may be wrong but I thought OP was asking about constructing the bytecode for the contract by hand. Anyway, he/she found how to do what was needed, so doesn't matter. :-) – Ajoy Bhatia Feb 13 '17 at 19:04

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.