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

Commit

Permalink
remix-simulator put default balance
Browse files Browse the repository at this point in the history
  • Loading branch information
yann300 committed Mar 25, 2019
1 parent a4642d5 commit 49e4a55
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 15 deletions.
14 changes: 8 additions & 6 deletions remix-simulator/src/methods/accounts.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ 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]
let setAccounts = (i) => {
const account = this.accounts[i]
this.accounts[account.address.toLowerCase()] = account
this.accounts[account.address.toLowerCase()].privateKey = Buffer.from(this.accounts[account.address.toLowerCase()].privateKey.slice(2), 'hex')
}

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')
setAccounts(0)
setAccounts(1)
setAccounts(2)
}

Accounts.prototype.methods = function () {
Expand Down
30 changes: 29 additions & 1 deletion remix-simulator/src/provider.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
const log = require('./utils/logs.js')
const merge = require('merge')
var remixLib = require('remix-lib')

var executionContext = remixLib.execution.executionContext
const Accounts = require('./methods/accounts.js')
const Blocks = require('./methods/blocks.js')
const Misc = require('./methods/misc.js')
Expand All @@ -9,8 +11,9 @@ const Transactions = require('./methods/transactions.js')
const Whisper = require('./methods/whisper.js')

var Provider = function () {
this.Accounts = new Accounts()
executionContext.setContext('vm', null, () => {}, (msg) => { console.log(msg) })

this.Accounts = new Accounts()
this.methods = {}
this.methods = merge(this.methods, this.Accounts.methods())
this.methods = merge(this.methods, (new Blocks()).methods())
Expand Down Expand Up @@ -44,4 +47,29 @@ Provider.prototype.isConnected = function () {
return true
}

Provider.prototype.init = async function () {
const accounts = this.Accounts.accounts
let setAccounts = (i) => {
return new Promise((resolve, reject) => {
const account = accounts[i]

let stateManager = executionContext.vm().stateManager
const address = account.address
stateManager.getAccount(account.address, (error, account) => {
if (error) return reject(error)
account.balance = '0xf00000000000000001'
console.log('available address : ', address)
stateManager.putAccount(address, account, function cb (error) {
console.log('setBalance', address)
if (error) return reject(error)
resolve()
})
})
})
}
await setAccounts(0)
await setAccounts(1)
await setAccounts(2)
}

module.exports = Provider
6 changes: 4 additions & 2 deletions remix-tests/src/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ commander.command('help').description('output usage information').action(functio
// get current version
commander
.option('-v, --verbose <level>', 'run with verbosity', mapVerbosity)
.action(function (filename) {
.action(async function (filename) {
// Console message
console.log(('\n\t👁 :: Running remix-tests - Unit testing for solidity :: 👁\t\n').white)
// set logger verbosity
Expand All @@ -45,7 +45,9 @@ commander
}
let web3 = new Web3()
// web3.setProvider(new web3.providers.HttpProvider('http://localhost:8545'))
web3.setProvider(new Provider())
const provider = new Provider()
await provider.init()
web3.setProvider(provider)
// web3.setProvider(new web3.providers.WebsocketProvider('ws://localhost:8546'))

if (!fs.existsSync(filename)) {
Expand Down
10 changes: 6 additions & 4 deletions remix-tests/src/runTestSources.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,17 @@ let TestRunner = require('./testRunner.js')
const Web3 = require('web3')
const Provider = require('remix-simulator').Provider

var createWeb3Provider = function () {
var createWeb3Provider = async function () {
let web3 = new Web3()
web3.setProvider(new Provider())
const provider = new Provider()
await provider.init()
web3.setProvider(provider)
return web3
}

const runTestSources = function (contractSources, testCallback, resultCallback, finalCallback, importFileCb, opts) {
const runTestSources = async function (contractSources, testCallback, resultCallback, finalCallback, importFileCb, opts) {
opts = opts || {}
let web3 = opts.web3 || createWeb3Provider()
let web3 = opts.web3 || await createWeb3Provider()
let accounts = opts.accounts || null
async.waterfall([
function getAccountList (next) {
Expand Down
6 changes: 4 additions & 2 deletions remix-tests/tests/testRunner.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ let Deployer = require('../src/deployer.js')
let TestRunner = require('../src/testRunner.js')
const Provider = require('remix-simulator').Provider

function compileAndDeploy (filename, callback) {
async function compileAndDeploy (filename, callback) {
let web3 = new Web3()
web3.setProvider(new Provider())
const provider = new Provider()
await provider.init()
web3.setProvider(provider)
let compilationData
let accounts
async.waterfall([
Expand Down

0 comments on commit 49e4a55

Please sign in to comment.