remix-project mirror
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
remix-project/libs/remix-debug/test/vmCall.ts

64 lines
1.5 KiB

7 years ago
'use strict'
import { Block, BlockHeader } from '@ethereumjs/block'
import VM from '@ethereumjs/vm'
7 years ago
var utileth = require('ethereumjs-util')
var Tx = require('@ethereumjs/tx').Transaction
7 years ago
var BN = require('ethereumjs-util').BN
var remixLib = require('@remix-project/remix-lib')
7 years ago
function sendTx (vm, from, to, value, data, cb) {
var tx = new Tx({
nonce: new BN(from.nonce++),
gasPrice: new BN(1),
gasLimit: new BN(3000000, 10),
to: to,
value: new BN(value, 10),
data: Buffer.from(data, 'hex')
7 years ago
})
tx.sign(from.privateKey)
var header = BlockHeader.fromHeaderData({
timestamp: new Date().getTime() / 1000 | 0,
number: 0
7 years ago
})
var block = new Block(header, [], [])
5 years ago
vm.runTx({block: block, tx: tx, skipBalance: true, skipNonce: true}).then(function (result) {
7 years ago
setTimeout(() => {
5 years ago
cb(null, utileth.bufferToHex(tx.hash()))
7 years ago
}, 500)
5 years ago
}).catch((error) => {
console.error(error)
cb(error)
7 years ago
})
}
/*
Init VM / Send Transaction
*/
5 years ago
function initVM (privateKey) {
7 years ago
var address = utileth.privateToAddress(privateKey)
var vm = new VM({
activatePrecompiles: true
})
vm.stateManager.getAccount(address).then((account) => {
account.balance = new BN('f00000000000000001', 16)
vm.stateManager.putAccount(address, account).catch((error) => {
console.log(error)
})
}).catch((error) => {
console.log(error)
})
var web3Provider = new remixLib.vm.Web3VMProvider()
web3Provider.setVM(vm)
vm.web3 = web3Provider
7 years ago
return vm
}
module.exports = {
sendTx: sendTx,
initVM: initVM
}