forked from ethereumjs/ethereumjs-monorepo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
73 lines (56 loc) · 2.27 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
import { Address, Account, BN, toBuffer, pubToAddress } from 'ethereumjs-util'
import { Transaction, TxData } from '@ethereumjs/tx'
import VM from '../..'
async function main() {
const vm = new VM()
// Import the key pair,
// used to sign transactions and generate addresses
const keyPair = require('./key-pair')
const privateKey = toBuffer(keyPair.secretKey)
const publicKeyBuf = toBuffer(keyPair.publicKey)
const address = new Address(pubToAddress(publicKeyBuf, true))
console.log('---------------------')
console.log('Sender address: ', address.toString())
// Create a new account
const acctData = {
nonce: 0,
balance: new BN(10).pow(new BN(19)), // 10 eth
}
const account = Account.fromAccountData(acctData)
// Save the account
await vm.stateManager.putAccount(address, account)
const txData1 = require('./raw-tx1')
const txData2 = require('./raw-tx2')
// The first transaction deploys a contract
const createdAddress = (await runTx(vm, txData1, privateKey))!
// The second transaction calls that contract
await runTx(vm, txData2, privateKey)
// Now let's look at what we created. The transaction
// should have created a new account for the contract
// in the state. Let's test to see if it did.
const createdAccount = await vm.stateManager.getAccount(createdAddress)
console.log('-------results-------')
console.log('nonce: ' + createdAccount.nonce.toString())
console.log('balance in wei: ', createdAccount.balance.toString())
console.log('stateRoot: 0x' + createdAccount.stateRoot.toString('hex'))
console.log('codeHash: 0x' + createdAccount.codeHash.toString('hex'))
console.log('---------------------')
}
async function runTx(vm: VM, txData: TxData, privateKey: Buffer) {
const tx = Transaction.fromTxData(txData).sign(privateKey)
console.log('----running tx-------')
const results = await vm.runTx({ 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())
return createdAddress
}
}
main()
.then(() => process.exit(0))
.catch((error) => {
console.error(error)
process.exit(1)
})