|
| 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 | + }) |
0 commit comments