Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Request] Simple deploy/call contract example #128

Closed
SilentCicero opened this issue Jun 24, 2017 · 2 comments
Closed

[Request] Simple deploy/call contract example #128

SilentCicero opened this issue Jun 24, 2017 · 2 comments

Comments

@SilentCicero
Copy link

SilentCicero commented Jun 24, 2017

Could we provide a simple example of deploying and calling a simple method with ethereumjs-vm?

I would prefer if I don't need to setup accounts/balances/nonces/txs etc..

pragma solidity ^0.4.4;

contract SimpleGet { uint256 public val = 4555; }

I just want to deploy a simple contract ^^, then call the val() public constant method.

Any thoughts on this (can i use runCode/runCall/runTx.. what is recommended) @axic @wanderer ?


SimpleGet Contract Bytecode:

0x60606040526101c76000553415601157fe5b5b608f806100206000396000f30060606040526000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff1680633c6bb43614603a575bfe5b3415604157fe5b6047605d565b6040518082815260200191505060405180910390f35b600054815600a165627a7a7230582020e95b37b367992e3e2cedd2faa7d85b517bab7f4a0108a81ade0eafe1ac62930029

val Method Call Code:

0x3c6bb436
@SilentCicero SilentCicero changed the title [Request] Simple deploy/run contract example [Request] Simple deploy/call contract example Jun 24, 2017
@SilentCicero
Copy link
Author

[update]

Here is what I came up with:

API:

interface:
runMethod({ bytecode: '0x..', interface: [ ..] } , 'someMethod', [ param1, param2 ] );

Usage:

runMethod({
  bytecode: '0x60606040526101c76000553415601157fe5b5b608f806100206000396000f30060606040526000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff1680633c6bb43614603a575bfe5b3415604157fe5b6047605d565b6040518082815260200191505060405180910390f35b600054815600a165627a7a7230582020e95b37b367992e3e2cedd2faa7d85b517bab7f4a0108a81ade0eafe1ac62930029',
  interface: [{"constant":true,"inputs":[],"name":"val","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"}],
}, 'val', []).then(console.log).catch(console.log);

Code:

const VM = require('ethereumjs-vm');
const Tx = require('ethereumjs-tx');
const sign = require('ethjs-signer').sign;
const BN = require('ethjs').BN;
const abi = require('ethjs-abi');
const util = require('ethjs-util');

function runMethod(contract, methodName, inputs) {
  return new Promise((resolve, reject) => {
    const vm = new VM({
      enableHomestead: true,
      activatePrecompiles: true
    });

    const contractInterface = typeof contract.interface === 'string' ? JSON.parse(contract.interface) : contract.interface;
    const methodObject = contractInterface.filter(item => item.name === methodName ? 1 : 0)[0];

    const address = '0x0F6af8F8D7AAD198a7607C96fb74Ffa02C5eD86B';
    const privateKey = '0xecbcd9838f7f2afa6e809df8d7cdae69aa5dfc14d563ee98e97effd3f6a652f2';
    const genesisData = { "0F6af8F8D7AAD198a7607C96fb74Ffa02C5eD86B": "1606938044258990275541962092341162602522202993782792835301376" };

    vm.stateManager.generateGenesis(genesisData, function(){
      try {
        const tx = new Tx(sign({
            from: address,
            value: 0,
            gas: new BN('99999999999'),
            gasPrice: new BN('1'),
            nonce: new BN(0),
            data: contract.bytecode,
          }, privateKey));

        vm.runTx({ tx, skipBalance: true, skipNonce: true }, (contractError, contractTx) => {

          if (contractError) { reject(contractError); return; }

          try {
            const tx2 = new Tx(sign({
                to: `0x${contractTx.createdAddress.toString('hex')}`,
                value: 0,
                gas: new BN('9999999999'),
                gasPrice: new BN('1'),
                nonce: new BN(1),
                data: abi.encodeMethod(methodObject, inputs),
              }, privateKey));

            vm.runTx({ tx: tx2, skipBalance: true, skipNonce: true }, (callError, callTx) => {

              if (callError) { reject(callError); return; }

              try {
                const outputBytecode = `0x${callTx.vm.return.toString('hex')}`;

                const result = abi.decodeMethod(methodObject, outputBytecode);

                resolve(result);
              } catch (bytecodeError) { reject(bytecodeError); return; }
            });
          } catch (error) { reject(error); return; }
        });
      } catch (error) { reject(error); return; }
    });
  });
};

@holgerd77
Copy link
Member

holgerd77 added a commit that referenced this issue Dec 1, 2020
holgerd77 pushed a commit that referenced this issue Dec 14, 2020
Add simple profiling / Basic benchmark & README update
holgerd77 pushed a commit that referenced this issue Mar 11, 2021
Added keccak256 alias for keccak(a, 256)
holgerd77 added a commit that referenced this issue May 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants