1
1
import VM from '../..'
2
2
import Account from 'ethereumjs-account'
3
3
import * as utils from 'ethereumjs-util'
4
- import { promisify } from 'util '
4
+ import PStateManager from '../../lib/state/promisified '
5
5
6
6
const Transaction = require ( 'ethereumjs-tx' ) // Change when https://github.com/ethereumjs/ethereumjs-vm/pull/541 gets merged
7
7
8
8
async function main ( ) {
9
9
const vm = new VM ( )
10
+ const psm = new PStateManager ( vm . stateManager )
10
11
11
12
// import the key pair
12
- // pre-generated (saves time)
13
13
// used to sign transactions and generate addresses
14
14
const keyPair = require ( './key-pair' )
15
+ const privateKey = utils . toBuffer ( keyPair . secretKey )
15
16
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
30
17
const publicKeyBuf = utils . toBuffer ( keyPair . publicKey )
31
18
const address = utils . pubToAddress ( publicKeyBuf , true )
32
19
33
20
console . log ( '---------------------' )
34
21
console . log ( 'Sender address: ' , utils . bufferToHex ( address ) )
35
22
36
23
// 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
+ } )
41
27
42
28
// 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' )
44
33
45
34
// The first transaction deploys a contract
46
- const createdAddress = ( await runTx ( vm , rawTx1 , keyPair ) ) !
35
+ const createdAddress = ( await runTx ( vm , rawTx1 , privateKey ) ) !
47
36
48
37
// The second transaction calls that contract
49
- await runTx ( vm , rawTx2 , keyPair )
38
+ await runTx ( vm , rawTx2 , privateKey )
50
39
51
40
// Now lets look at what we created. The transaction
52
41
// should have created a new account for the contract
53
42
// in the state. Lets test to see if it did.
54
43
55
- const createdAccount = ( await promisify ( vm . stateManager . getAccount . bind ( vm . stateManager ) ) (
56
- createdAddress ,
57
- ) ) as Account
44
+ const createdAccount = await psm . getAccount ( createdAddress )
58
45
59
46
console . log ( '-------results-------' )
60
47
console . log ( 'nonce: ' + createdAccount . nonce . toString ( 'hex' ) )
@@ -64,27 +51,25 @@ async function main() {
64
51
console . log ( '---------------------' )
65
52
}
66
53
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 ) {
69
55
const tx = new Transaction ( rawTx )
70
56
71
- tx . sign ( utils . toBuffer ( keyPair . secretKey ) )
57
+ tx . sign ( privateKey )
72
58
73
59
console . log ( '----running tx-------' )
74
60
const results = await vm . runTx ( {
75
61
tx : tx ,
76
62
} )
77
- const createdAddress = results . createdAddress
78
63
79
- // log some results
80
64
console . log ( 'gas used: ' + results . gasUsed . toString ( ) )
81
65
console . log ( 'returned: ' + results . vm . return . toString ( 'hex' ) )
82
66
67
+ const createdAddress = results . createdAddress
68
+
83
69
if ( createdAddress ) {
84
70
console . log ( 'address created: ' + createdAddress . toString ( 'hex' ) )
71
+ return createdAddress
85
72
}
86
-
87
- return createdAddress
88
73
}
89
74
90
75
main ( )
0 commit comments