2

I came across an article: https://wanderer.github.io/ethereum/nodejs/code/2014/08/12/running-contracts-with-vm/

At the bottom of the page is shown how to write a contract execution route. But unfortunately it does not work for me, I do not know too much javascript. Can anyone say what I need to improve?

var VM = require('ethereumjs-vm')

var vm = new VM()
var code = '7f4e616d65526567000000000000000000000000000000000000000000000000003055307f4e616d6552656700000000000000000000000000000000000000000000000000557f436f6e666967000000000000000000000000000000000000000000000000000073661005d2720d855f1d9976f88bb10c1a3398c77f5573661005d2720d855f1d9976f88bb10c1a3398c77f7f436f6e6669670000000000000000000000000000000000000000000000000000553360455560df806100c56000396000f3007f726567697374657200000000000000000000000000000000000000000000000060003514156053576020355415603257005b335415603e5760003354555b6020353360006000a233602035556020353355005b60007f756e72656769737465720000000000000000000000000000000000000000000060003514156082575033545b1560995733335460006000a2600033545560003355005b60007f6b696c6c00000000000000000000000000000000000000000000000000000000600035141560cb575060455433145b1560d25733ff5b6000355460005260206000f3'

vm.runCode({
    code: Buffer.from(code, 'hex'), // code needs to be a Buffer
    gasLimit: Buffer.from('ffffffff', 'hex')
  }, function(err, results){
    console.log('returned: ' + results.return.toString('hex'));
  })

 vm.onStep = function (info, done) {
    //prints the program counter, the current opcode and the amount of gas left 
    console.log('[vm] ' + info.pc + ' Opcode: ' + info.opcode + ' Gas: ' + info.gasLeft.toString());

    //prints out the current stack
    info.stack.forEach(function (item) {
        console.log('[vm]    ' + item.toString('hex'));
    });
    //important! call `done` when your done messing around
    done();
};

vm.onStep()
  • What happens when you run the code? Is there an error? – sfmiller940 Jun 19 '18 at 19:48
1

I'm not sure where you found that format for vm.onStep.

But from this example it seems like you should try this format:

vm.on('step', function (data) {
    console.log('[vm] ' + data.pc + ' Opcode: ' + data.opcode + ' Gas: ' + data.gasLeft.toString());
    data.stack.forEach(function (item) {
        console.log('[vm]    ' + item.toString('hex'));
    });
};

You might want to put this before vm.runCode like in the example.

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.