Last active
November 25, 2015 01:08
-
-
Save wanderer/70626eec29f92a4046d2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// imports | |
var VM = require('ethereumjs-vm') | |
var Trie = require('merkle-patricia-tree') | |
var async = require('async') | |
var ethUtils = require('ethereumjs-util') | |
var Account = require('ethereumjs-account') | |
var Tx = require('ethereumjs-tx') | |
var crypto = require('crypto') | |
// read in the solidity code and compile it | |
var code = "60606040523615600d57600d565b604b5b60003411156048577fa163a6249e860c278ef4049759a7f7c7e8c141d30fd634fda9b5a6a95d111a3060405180905060405180910390a15b5b565b00" | |
code = new Buffer(code, 'hex') | |
// This stores the state | |
var stateTrie = new Trie() | |
// Create vm | |
var vm = new VM(stateTrie) | |
// accounts; will be populated later | |
// use different accounts to simulate mutilple parties bidding on an auction | |
var accounts = [] | |
// the address of the auction contract that we will create | |
var createdAddress | |
async.series([ | |
setup, | |
function checkContractCreation(done) { | |
var account = accounts[0] | |
runTx({ | |
nonce: account.nonce++, | |
data: code, | |
to: '', | |
pk: account.privateKey, | |
value: 0 | |
}, function (results) { | |
console.log(results); | |
}, done) | |
} | |
]) | |
// setup function. It populates the state with a few accounts. | |
function setup(cb) { | |
for (var i = 0; i < 5; i++) { | |
var rand = crypto.randomBytes(32) | |
accounts.push({ | |
privateKey: rand, | |
address: ethUtils.privateToAddress(rand), | |
nonce: 0 | |
}) | |
} | |
// create a new account | |
var account = new Account() | |
// give the account some wei. | |
// This needs to be a `Buffer` or a string. all strings need to be in hex. | |
account.balance = '0xf00000000000000001' | |
// store in the trie | |
async.each(accounts, function (a, done) { | |
stateTrie.put(a.address, account.serialize(), done) | |
}, cb) | |
} | |
// a simple helper function; it forms a transaction and runs it in the vm | |
function runTx(opts, test, cb) { | |
var rawTx = { | |
to: opts.to, | |
nonce: opts.nonce, | |
value: opts.value, | |
gasPrice: '0x01', | |
gasLimit: '0xffff90710', // set an high gas limit so we don't run out of gas | |
data: opts.data, | |
block: opts.block | |
} | |
var tx = new Tx(rawTx) | |
tx.sign(opts.pk) | |
vm.runTx({ | |
tx: tx | |
}, function (err, results) { | |
test(results) | |
cb() | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment