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.
73 lines
1.7 KiB
73 lines
1.7 KiB
7 years ago
|
'use strict'
|
||
|
var utileth = require('ethereumjs-util')
|
||
5 years ago
|
var Tx = require('ethereumjs-tx').Transaction
|
||
7 years ago
|
var Block = require('ethereumjs-block')
|
||
|
var BN = require('ethereumjs-util').BN
|
||
|
var remixLib = require('remix-lib')
|
||
|
|
||
|
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),
|
||
6 years ago
|
data: Buffer.from(data, 'hex')
|
||
7 years ago
|
})
|
||
|
tx.sign(from.privateKey)
|
||
|
var block = new Block({
|
||
|
header: {
|
||
|
timestamp: new Date().getTime() / 1000 | 0,
|
||
|
number: 0
|
||
|
},
|
||
|
transactions: [],
|
||
|
uncleHeaders: []
|
||
|
})
|
||
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) {
|
||
5 years ago
|
var VM = require('ethereumjs-vm').default
|
||
7 years ago
|
var Web3Providers = remixLib.vm.Web3Providers
|
||
|
var address = utileth.privateToAddress(privateKey)
|
||
|
var vm = new VM({
|
||
|
enableHomestead: true,
|
||
|
activatePrecompiles: true
|
||
|
})
|
||
6 years ago
|
|
||
|
vm.stateManager.getAccount(address, (error, account) => {
|
||
|
if (error) return console.log(error)
|
||
|
account.balance = '0xf00000000000000001'
|
||
|
vm.stateManager.putAccount(address, account, function cb (error) {
|
||
|
if (error) console.log(error)
|
||
|
})
|
||
|
})
|
||
|
|
||
7 years ago
|
var web3Providers = new Web3Providers()
|
||
|
web3Providers.addVM('VM', vm)
|
||
|
web3Providers.get('VM', function (error, obj) {
|
||
|
if (error) {
|
||
|
var mes = 'provider TEST not defined'
|
||
|
console.log(mes)
|
||
|
} else {
|
||
6 years ago
|
vm.web3 = obj
|
||
7 years ago
|
}
|
||
|
})
|
||
|
return vm
|
||
|
}
|
||
|
|
||
|
module.exports = {
|
||
|
sendTx: sendTx,
|
||
|
initVM: initVM
|
||
|
}
|