Skip to content

Commit 02543fb

Browse files
committed
Final updates to run-transaction example
1 parent cfd71d4 commit 02543fb

File tree

1 file changed

+18
-33
lines changed
  • examples/run-transactions-complete

1 file changed

+18
-33
lines changed

examples/run-transactions-complete/index.ts

Lines changed: 18 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,47 @@
11
import VM from '../..'
22
import Account from 'ethereumjs-account'
33
import * as utils from 'ethereumjs-util'
4-
import { promisify } from 'util'
4+
import PStateManager from '../../lib/state/promisified'
55

66
const Transaction = require('ethereumjs-tx') // Change when https://github.com/ethereumjs/ethereumjs-vm/pull/541 gets merged
77

88
async function main() {
99
const vm = new VM()
10+
const psm = new PStateManager(vm.stateManager)
1011

1112
// import the key pair
12-
// pre-generated (saves time)
1313
// used to sign transactions and generate addresses
1414
const keyPair = require('./key-pair')
15+
const privateKey = utils.toBuffer(keyPair.secretKey)
1516

16-
// Transaction to initalize the name register, in this case
17-
// it will register the sending address as 'null_radix'
18-
// Notes:
19-
// - A transaction has the fiels:
20-
// - nonce
21-
// - gasPrice
22-
// - gasLimit
23-
// - data
24-
const rawTx1 = require('./raw-tx1')
25-
26-
// 2nd Transaction
27-
const rawTx2 = require('./raw-tx2')
28-
29-
// the address we are sending from
3017
const publicKeyBuf = utils.toBuffer(keyPair.publicKey)
3118
const address = utils.pubToAddress(publicKeyBuf, true)
3219

3320
console.log('---------------------')
3421
console.log('Sender address: ', utils.bufferToHex(address))
3522

3623
// create a new account
37-
const account = new Account()
38-
39-
// give the account some wei.
40-
account.balance = utils.toBuffer('0xf00000000000000001')
24+
const account = new Account({
25+
balance: 100e18,
26+
})
4127

4228
// Save the account
43-
await promisify(vm.stateManager.putAccount.bind(vm.stateManager))(address, account)
29+
await psm.putAccount(address, account)
30+
31+
const rawTx1 = require('./raw-tx1')
32+
const rawTx2 = require('./raw-tx2')
4433

4534
// The first transaction deploys a contract
46-
const createdAddress = (await runTx(vm, rawTx1, keyPair))!
35+
const createdAddress = (await runTx(vm, rawTx1, privateKey))!
4736

4837
// The second transaction calls that contract
49-
await runTx(vm, rawTx2, keyPair)
38+
await runTx(vm, rawTx2, privateKey)
5039

5140
// Now lets look at what we created. The transaction
5241
// should have created a new account for the contract
5342
// in the state. Lets test to see if it did.
5443

55-
const createdAccount = (await promisify(vm.stateManager.getAccount.bind(vm.stateManager))(
56-
createdAddress,
57-
)) as Account
44+
const createdAccount = await psm.getAccount(createdAddress)
5845

5946
console.log('-------results-------')
6047
console.log('nonce: ' + createdAccount.nonce.toString('hex'))
@@ -64,27 +51,25 @@ async function main() {
6451
console.log('---------------------')
6552
}
6653

67-
async function runTx(vm: VM, rawTx: any, keyPair: { secretKey: string }) {
68-
// create a new transaction out of the json
54+
async function runTx(vm: VM, rawTx: any, privateKey: Buffer) {
6955
const tx = new Transaction(rawTx)
7056

71-
tx.sign(utils.toBuffer(keyPair.secretKey))
57+
tx.sign(privateKey)
7258

7359
console.log('----running tx-------')
7460
const results = await vm.runTx({
7561
tx: tx,
7662
})
77-
const createdAddress = results.createdAddress
7863

79-
// log some results
8064
console.log('gas used: ' + results.gasUsed.toString())
8165
console.log('returned: ' + results.vm.return.toString('hex'))
8266

67+
const createdAddress = results.createdAddress
68+
8369
if (createdAddress) {
8470
console.log('address created: ' + createdAddress.toString('hex'))
71+
return createdAddress
8572
}
86-
87-
return createdAddress
8873
}
8974

9075
main()

0 commit comments

Comments
 (0)