Skip to content

Commit 266db02

Browse files
committed
Update run transaction example
1 parent ada837e commit 266db02

File tree

6 files changed

+124
-151
lines changed

6 files changed

+124
-151
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Running transactions
2+
3+
This directory contains an example on how to run transactions
4+
5+
The example does these things:
6+
7+
1. Sends a transaction that deploys a contract
8+
1. Sends a transaction that calls the deployed contract
9+
1. Prints the results
10+
11+
## Installation
12+
13+
1. Run `npm install` in the root of this project
14+
15+
## Running the example
16+
17+
1. Run `npm run build:dist` in the root of this project
18+
1. Run `npm run example` in this directory

examples/run-transactions-complete/index.js

Lines changed: 0 additions & 148 deletions
This file was deleted.
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
import VM from '../..'
2+
import Account from 'ethereumjs-account'
3+
import * as utils from 'ethereumjs-util'
4+
import { promisify } from 'util'
5+
import stateManager from '../../lib/state/stateManager'
6+
7+
const Transaction = require('ethereumjs-tx') // Change when https://github.com/ethereumjs/ethereumjs-vm/pull/541 gets merged
8+
9+
async function main() {
10+
const vm = new VM()
11+
12+
// import the key pair
13+
// pre-generated (saves time)
14+
// used to sign transactions and generate addresses
15+
const keyPair = require('./key-pair')
16+
17+
// Transaction to initalize the name register, in this case
18+
// it will register the sending address as 'null_radix'
19+
// Notes:
20+
// - A transaction has the fiels:
21+
// - nonce
22+
// - gasPrice
23+
// - gasLimit
24+
// - data
25+
const rawTx1 = require('./raw-tx1')
26+
27+
// 2nd Transaction
28+
const rawTx2 = require('./raw-tx2')
29+
30+
// the address we are sending from
31+
const publicKeyBuf = utils.toBuffer(keyPair.publicKey)
32+
const address = utils.pubToAddress(publicKeyBuf, true)
33+
34+
console.log('---------------------')
35+
console.log('Sender address: ', utils.bufferToHex(address))
36+
37+
// create a new account
38+
const account = new Account()
39+
40+
// give the account some wei.
41+
account.balance = utils.toBuffer('0xf00000000000000001')
42+
43+
// Save the account
44+
await promisify(vm.stateManager.putAccount.bind(vm.stateManager))(address, account)
45+
46+
// The first transaction deploys a contract
47+
const createdAddress = (await runTx(vm, rawTx1, keyPair))!
48+
49+
// The second transaction calls that contract
50+
await runTx(vm, rawTx2, keyPair)
51+
52+
// Now lets look at what we created. The transaction
53+
// should have created a new account for the contract
54+
// in the state. Lets test to see if it did.
55+
56+
const createdAccount = (await promisify(vm.stateManager.getAccount.bind(vm.stateManager))(
57+
createdAddress,
58+
)) as Account
59+
60+
console.log('-------results-------')
61+
console.log('nonce: ' + createdAccount.nonce.toString('hex'))
62+
console.log('balance in wei: ' + createdAccount.balance.toString('hex'))
63+
console.log('stateRoot: ' + createdAccount.stateRoot.toString('hex'))
64+
console.log('codeHash: ' + createdAccount.codeHash.toString('hex'))
65+
console.log('---------------------')
66+
}
67+
68+
async function runTx(vm: VM, rawTx: any, keyPair: { secretKey: string }) {
69+
// create a new transaction out of the json
70+
const tx = new Transaction(rawTx)
71+
72+
tx.sign(utils.toBuffer(keyPair.secretKey))
73+
74+
console.log('----running tx-------')
75+
const results = await vm.runTx({
76+
tx: tx,
77+
})
78+
const createdAddress = results.createdAddress
79+
80+
// log some results
81+
console.log('gas used: ' + results.gasUsed.toString())
82+
console.log('returned: ' + results.vm.return.toString('hex'))
83+
84+
if (createdAddress) {
85+
console.log('address created: ' + createdAddress.toString('hex'))
86+
}
87+
88+
return createdAddress
89+
}
90+
91+
main()
92+
.then(() => process.exit(0))
93+
.catch(error => {
94+
console.error(error)
95+
process.exit(1)
96+
})
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
{
2-
"secretKey": "3cd7232cd6f3fc66a57a6bedc1a8ed6c228fff0a327e169c2bcc5e869ed49511",
3-
"publicKey": "0406cc661590d48ee972944b35ad13ff03c7876eae3fd191e8a2f77311b0a3c6613407b5005e63d7d8d76b89d5f900cde691497688bb281e07a5052ff61edebdc0"
2+
"secretKey": "0x3cd7232cd6f3fc66a57a6bedc1a8ed6c228fff0a327e169c2bcc5e869ed49511",
3+
"publicKey": "0x0406cc661590d48ee972944b35ad13ff03c7876eae3fd191e8a2f77311b0a3c6613407b5005e63d7d8d76b89d5f900cde691497688bb281e07a5052ff61edebdc0"
44
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"name": "run-transactions-complete",
3+
"private": true,
4+
"scripts": {
5+
"example": "ts-node index.ts"
6+
}
7+
}

examples/run-transactions-complete/raw-tx2.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ var rawTx2 = {
77
gasLimit: '0x20710',
88
value: '0x10',
99
to: '0x692a70d2e424a56d2c6c27aa97d1a86395877b3a',
10-
data: bidSig + time
10+
data: bidSig + time,
1111
}
1212

1313
module.exports = rawTx2

0 commit comments

Comments
 (0)