Skip to content
This repository was archived by the owner on Mar 3, 2021. It is now read-only.

Commit 49e4a55

Browse files
committed
remix-simulator put default balance
1 parent a4642d5 commit 49e4a55

File tree

5 files changed

+51
-15
lines changed

5 files changed

+51
-15
lines changed

remix-simulator/src/methods/accounts.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@ var Accounts = function () {
55
// TODO: make it random and/or use remix-libs
66
this.accounts = [this.web3.eth.accounts.create(['abcd']), this.web3.eth.accounts.create(['ef12']), this.web3.eth.accounts.create(['ef34'])]
77

8-
this.accounts[this.accounts[0].address.toLowerCase()] = this.accounts[0]
9-
this.accounts[this.accounts[1].address.toLowerCase()] = this.accounts[1]
10-
this.accounts[this.accounts[2].address.toLowerCase()] = this.accounts[2]
8+
let setAccounts = (i) => {
9+
const account = this.accounts[i]
10+
this.accounts[account.address.toLowerCase()] = account
11+
this.accounts[account.address.toLowerCase()].privateKey = Buffer.from(this.accounts[account.address.toLowerCase()].privateKey.slice(2), 'hex')
12+
}
1113

12-
this.accounts[this.accounts[0].address.toLowerCase()].privateKey = Buffer.from(this.accounts[this.accounts[0].address.toLowerCase()].privateKey.slice(2), 'hex')
13-
this.accounts[this.accounts[1].address.toLowerCase()].privateKey = Buffer.from(this.accounts[this.accounts[1].address.toLowerCase()].privateKey.slice(2), 'hex')
14-
this.accounts[this.accounts[2].address.toLowerCase()].privateKey = Buffer.from(this.accounts[this.accounts[2].address.toLowerCase()].privateKey.slice(2), 'hex')
14+
setAccounts(0)
15+
setAccounts(1)
16+
setAccounts(2)
1517
}
1618

1719
Accounts.prototype.methods = function () {

remix-simulator/src/provider.js

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
const log = require('./utils/logs.js')
22
const merge = require('merge')
3+
var remixLib = require('remix-lib')
34

5+
var executionContext = remixLib.execution.executionContext
46
const Accounts = require('./methods/accounts.js')
57
const Blocks = require('./methods/blocks.js')
68
const Misc = require('./methods/misc.js')
@@ -9,8 +11,9 @@ const Transactions = require('./methods/transactions.js')
911
const Whisper = require('./methods/whisper.js')
1012

1113
var Provider = function () {
12-
this.Accounts = new Accounts()
14+
executionContext.setContext('vm', null, () => {}, (msg) => { console.log(msg) })
1315

16+
this.Accounts = new Accounts()
1417
this.methods = {}
1518
this.methods = merge(this.methods, this.Accounts.methods())
1619
this.methods = merge(this.methods, (new Blocks()).methods())
@@ -44,4 +47,29 @@ Provider.prototype.isConnected = function () {
4447
return true
4548
}
4649

50+
Provider.prototype.init = async function () {
51+
const accounts = this.Accounts.accounts
52+
let setAccounts = (i) => {
53+
return new Promise((resolve, reject) => {
54+
const account = accounts[i]
55+
56+
let stateManager = executionContext.vm().stateManager
57+
const address = account.address
58+
stateManager.getAccount(account.address, (error, account) => {
59+
if (error) return reject(error)
60+
account.balance = '0xf00000000000000001'
61+
console.log('available address : ', address)
62+
stateManager.putAccount(address, account, function cb (error) {
63+
console.log('setBalance', address)
64+
if (error) return reject(error)
65+
resolve()
66+
})
67+
})
68+
})
69+
}
70+
await setAccounts(0)
71+
await setAccounts(1)
72+
await setAccounts(2)
73+
}
74+
4775
module.exports = Provider

remix-tests/src/run.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ commander.command('help').description('output usage information').action(functio
3535
// get current version
3636
commander
3737
.option('-v, --verbose <level>', 'run with verbosity', mapVerbosity)
38-
.action(function (filename) {
38+
.action(async function (filename) {
3939
// Console message
4040
console.log(('\n\t👁 :: Running remix-tests - Unit testing for solidity :: 👁\t\n').white)
4141
// set logger verbosity
@@ -45,7 +45,9 @@ commander
4545
}
4646
let web3 = new Web3()
4747
// web3.setProvider(new web3.providers.HttpProvider('http://localhost:8545'))
48-
web3.setProvider(new Provider())
48+
const provider = new Provider()
49+
await provider.init()
50+
web3.setProvider(provider)
4951
// web3.setProvider(new web3.providers.WebsocketProvider('ws://localhost:8546'))
5052

5153
if (!fs.existsSync(filename)) {

remix-tests/src/runTestSources.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,17 @@ let TestRunner = require('./testRunner.js')
88
const Web3 = require('web3')
99
const Provider = require('remix-simulator').Provider
1010

11-
var createWeb3Provider = function () {
11+
var createWeb3Provider = async function () {
1212
let web3 = new Web3()
13-
web3.setProvider(new Provider())
13+
const provider = new Provider()
14+
await provider.init()
15+
web3.setProvider(provider)
1416
return web3
1517
}
1618

17-
const runTestSources = function (contractSources, testCallback, resultCallback, finalCallback, importFileCb, opts) {
19+
const runTestSources = async function (contractSources, testCallback, resultCallback, finalCallback, importFileCb, opts) {
1820
opts = opts || {}
19-
let web3 = opts.web3 || createWeb3Provider()
21+
let web3 = opts.web3 || await createWeb3Provider()
2022
let accounts = opts.accounts || null
2123
async.waterfall([
2224
function getAccountList (next) {

remix-tests/tests/testRunner.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@ let Deployer = require('../src/deployer.js')
77
let TestRunner = require('../src/testRunner.js')
88
const Provider = require('remix-simulator').Provider
99

10-
function compileAndDeploy (filename, callback) {
10+
async function compileAndDeploy (filename, callback) {
1111
let web3 = new Web3()
12-
web3.setProvider(new Provider())
12+
const provider = new Provider()
13+
await provider.init()
14+
web3.setProvider(provider)
1315
let compilationData
1416
let accounts
1517
async.waterfall([

0 commit comments

Comments
 (0)