From f604436155d05ba9453f5904d2dffa496d306146 Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Tue, 9 Apr 2019 14:05:24 -0400 Subject: [PATCH] add eth_getBalance; use accounts in executionContext vm --- remix-simulator/README.md | 2 +- remix-simulator/package.json | 2 ++ remix-simulator/src/methods/accounts.js | 32 +++++++++++++++++++------ 3 files changed, 28 insertions(+), 8 deletions(-) diff --git a/remix-simulator/README.md b/remix-simulator/README.md index 3e81dc02d2..f29de37cd9 100644 --- a/remix-simulator/README.md +++ b/remix-simulator/README.md @@ -15,7 +15,7 @@ Implemented: * [~] eth_gasPrice * [~] eth_accounts * [X] eth_blockNumber -* [_] eth_getBalance +* [X] eth_getBalance * [_] eth_getStorageAt * [_] eth_getTransactionCount * [_] eth_getBlockTransactionCountByHash diff --git a/remix-simulator/package.json b/remix-simulator/package.json index e65688c34a..36f60c686b 100644 --- a/remix-simulator/package.json +++ b/remix-simulator/package.json @@ -23,6 +23,8 @@ "fast-async": "^6.3.7", "merge": "^1.2.0", "remix-lib": "0.4.5", + "ethereumjs-vm": "3.0.0", + "ethereumjs-util": "^5.1.2", "standard": "^10.0.3", "time-stamp": "^2.0.0", "web3": "1.0.0-beta.27" diff --git a/remix-simulator/src/methods/accounts.js b/remix-simulator/src/methods/accounts.js index d23f572229..b3e0335d77 100644 --- a/remix-simulator/src/methods/accounts.js +++ b/remix-simulator/src/methods/accounts.js @@ -1,3 +1,7 @@ +var RemixLib = require('remix-lib') +var executionContext = RemixLib.execution.executionContext +var ethJSUtil = require('ethereumjs-util') +var BN = ethJSUtil.BN var Web3 = require('web3') var Accounts = function () { @@ -5,18 +9,20 @@ var Accounts = function () { // TODO: make it random and/or use remix-libs this.accounts = [this.web3.eth.accounts.create(['abcd']), this.web3.eth.accounts.create(['ef12']), this.web3.eth.accounts.create(['ef34'])] - this.accounts[this.accounts[0].address.toLowerCase()] = this.accounts[0] - this.accounts[this.accounts[1].address.toLowerCase()] = this.accounts[1] - this.accounts[this.accounts[2].address.toLowerCase()] = this.accounts[2] + executionContext.init({get: () => { return true }}) - this.accounts[this.accounts[0].address.toLowerCase()].privateKey = Buffer.from(this.accounts[this.accounts[0].address.toLowerCase()].privateKey.slice(2), 'hex') - this.accounts[this.accounts[1].address.toLowerCase()].privateKey = Buffer.from(this.accounts[this.accounts[1].address.toLowerCase()].privateKey.slice(2), 'hex') - this.accounts[this.accounts[2].address.toLowerCase()].privateKey = Buffer.from(this.accounts[this.accounts[2].address.toLowerCase()].privateKey.slice(2), 'hex') + for (let _account of this.accounts) { + executionContext.vm().stateManager.getAccount(Buffer.from(_account.address.toLowerCase().replace("0x", ""), 'hex'), (err, account) => { + var balance = '0x56BC75E2D63100000' + account.balance = balance || '0xf00000000000000001' + }) + } } Accounts.prototype.methods = function () { return { - eth_accounts: this.eth_accounts.bind(this) + eth_accounts: this.eth_accounts.bind(this), + eth_getBalance: this.eth_getBalance.bind(this) } } @@ -24,4 +30,16 @@ Accounts.prototype.eth_accounts = function (payload, cb) { return cb(null, this.accounts.map((x) => x.address)) } +Accounts.prototype.eth_getBalance = function (payload, cb) { + let address = payload.params[0] + address = ethJSUtil.stripHexPrefix(address) + + executionContext.vm().stateManager.getAccount(Buffer.from(address, 'hex'), function (err, account) { + if (err) { + return cb('Account not found') + } + cb(null, new BN(account.balance).toString(10)) + }) +} + module.exports = Accounts