forked from ethereumjs/ethereumjs-monorepo
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.ts
79 lines (60 loc) · 2.29 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import VM from '../..'
import Account from 'ethereumjs-account'
import * as utils from 'ethereumjs-util'
import PStateManager from '../../lib/state/promisified'
import { Transaction } from 'ethereumjs-tx'
async function main() {
const vm = new VM()
const psm = new PStateManager(vm.stateManager)
// import the key pair
// used to sign transactions and generate addresses
const keyPair = require('./key-pair')
const privateKey = utils.toBuffer(keyPair.secretKey)
const publicKeyBuf = utils.toBuffer(keyPair.publicKey)
const address = utils.pubToAddress(publicKeyBuf, true)
console.log('---------------------')
console.log('Sender address: ', utils.bufferToHex(address))
// create a new account
const account = new Account({
balance: 100e18,
})
// Save the account
await psm.putAccount(address, account)
const rawTx1 = require('./raw-tx1')
const rawTx2 = require('./raw-tx2')
// The first transaction deploys a contract
const createdAddress = (await runTx(vm, rawTx1, privateKey))!
// The second transaction calls that contract
await runTx(vm, rawTx2, privateKey)
// Now lets look at what we created. The transaction
// should have created a new account for the contract
// in the state. Lets test to see if it did.
const createdAccount = await psm.getAccount(createdAddress)
console.log('-------results-------')
console.log('nonce: ' + createdAccount.nonce.toString('hex'))
console.log('balance in wei: ', createdAccount.balance.toString('hex') || 0)
console.log('stateRoot: ' + createdAccount.stateRoot.toString('hex'))
console.log('codeHash: ' + createdAccount.codeHash.toString('hex'))
console.log('---------------------')
}
async function runTx(vm: VM, rawTx: any, privateKey: Buffer) {
const tx = new Transaction(rawTx)
tx.sign(privateKey)
console.log('----running tx-------')
const results = await vm.runTx({
tx: tx,
})
console.log('gas used: ' + results.gasUsed.toString())
console.log('returned: ' + results.execResult.returnValue.toString('hex'))
const createdAddress = results.createdAddress
if (createdAddress) {
console.log('address created: ' + createdAddress.toString('hex'))
return createdAddress
}
}
main()
.then(() => process.exit(0))
.catch(error => {
console.error(error)
process.exit(1)
})