commit
185c280709
@ -0,0 +1,18 @@ |
|||||||
|
import { WebsocketPlugin } from '@remixproject/engine-web' |
||||||
|
import * as packageJson from '../../../../../package.json' |
||||||
|
|
||||||
|
const profile = { |
||||||
|
name: 'hardhat', |
||||||
|
displayName: 'Hardhat', |
||||||
|
url: 'ws://127.0.0.1:65522', |
||||||
|
methods: ['compile'], |
||||||
|
description: 'Using Remixd daemon, allow to access hardhat API', |
||||||
|
kind: 'other', |
||||||
|
version: packageJson.version |
||||||
|
} |
||||||
|
|
||||||
|
export class HardhatHandle extends WebsocketPlugin { |
||||||
|
constructor () { |
||||||
|
super(profile) |
||||||
|
} |
||||||
|
} |
@ -1,302 +0,0 @@ |
|||||||
'use strict' |
|
||||||
var StaticAnalysisRunner = require('@remix-project/remix-analyzer').CodeAnalysis |
|
||||||
var yo = require('yo-yo') |
|
||||||
var $ = require('jquery') |
|
||||||
var remixLib = require('@remix-project/remix-lib') |
|
||||||
var utils = remixLib.util |
|
||||||
var css = require('./styles/staticAnalysisView-styles') |
|
||||||
var Renderer = require('../../ui/renderer') |
|
||||||
const SourceHighlighter = require('../../editor/sourceHighlighter') |
|
||||||
|
|
||||||
var EventManager = require('../../../lib/events') |
|
||||||
|
|
||||||
function staticAnalysisView (localRegistry, analysisModule) { |
|
||||||
var self = this |
|
||||||
this.event = new EventManager() |
|
||||||
this.view = null |
|
||||||
this.runner = new StaticAnalysisRunner() |
|
||||||
this.modulesView = this.renderModules() |
|
||||||
this.lastCompilationResult = null |
|
||||||
this.lastCompilationSource = null |
|
||||||
this.currentFile = 'No file compiled' |
|
||||||
this.sourceHighlighter = new SourceHighlighter() |
|
||||||
this.analysisModule = analysisModule |
|
||||||
self._components = { |
|
||||||
renderer: new Renderer(analysisModule) |
|
||||||
} |
|
||||||
self._components.registry = localRegistry |
|
||||||
// dependencies
|
|
||||||
self._deps = { |
|
||||||
offsetToLineColumnConverter: self._components.registry.get('offsettolinecolumnconverter').api |
|
||||||
} |
|
||||||
|
|
||||||
analysisModule.on('solidity', 'compilationFinished', (file, source, languageVersion, data) => { |
|
||||||
self.lastCompilationResult = null |
|
||||||
self.lastCompilationSource = null |
|
||||||
if (languageVersion.indexOf('soljson') !== 0) return |
|
||||||
self.lastCompilationResult = data |
|
||||||
self.lastCompilationSource = source |
|
||||||
self.currentFile = file |
|
||||||
self.correctRunBtnDisabled() |
|
||||||
if (self.view && self.view.querySelector('#autorunstaticanalysis').checked) { |
|
||||||
self.run() |
|
||||||
} |
|
||||||
}) |
|
||||||
} |
|
||||||
|
|
||||||
staticAnalysisView.prototype.render = function () { |
|
||||||
this.runBtn = yo`<button class="btn btn-sm w-25 btn-primary" onclick="${() => { this.run() }}" >Run</button>` |
|
||||||
const view = yo` |
|
||||||
<div class="${css.analysis}"> |
|
||||||
<div class="my-2 d-flex flex-column align-items-left"> |
|
||||||
<div class="${css.top} d-flex justify-content-between"> |
|
||||||
<div class="pl-2 ${css.label}" for="checkAllEntries"> |
|
||||||
<input id="checkAllEntries" |
|
||||||
type="checkbox" |
|
||||||
onclick="${(event) => { this.checkAll(event) }}" |
|
||||||
style="vertical-align:bottom" |
|
||||||
checked="true" |
|
||||||
> |
|
||||||
<label class="text-nowrap pl-2 mb-0" for="checkAllEntries"> |
|
||||||
Select all |
|
||||||
</label> |
|
||||||
</div> |
|
||||||
<div class="${css.label}" for="autorunstaticanalysis"> |
|
||||||
<input id="autorunstaticanalysis" |
|
||||||
type="checkbox" |
|
||||||
style="vertical-align:bottom" |
|
||||||
checked="true" |
|
||||||
> |
|
||||||
<label class="text-nowrap pl-2 mb-0" for="autorunstaticanalysis"> |
|
||||||
Autorun |
|
||||||
</label> |
|
||||||
</div> |
|
||||||
${this.runBtn} |
|
||||||
</div> |
|
||||||
</div> |
|
||||||
<div id="staticanalysismodules" class="list-group list-group-flush"> |
|
||||||
${this.modulesView} |
|
||||||
</div> |
|
||||||
<div class="mt-2 p-2 d-flex border-top flex-column"> |
|
||||||
<span>last results for:</span> |
|
||||||
<span class="text-break break-word word-break font-weight-bold" id="staticAnalysisCurrentFile">${this.currentFile}</span> |
|
||||||
</div> |
|
||||||
<div class="${css.result} my-1" id='staticanalysisresult'></div> |
|
||||||
</div> |
|
||||||
` |
|
||||||
|
|
||||||
if (!this.view) { |
|
||||||
this.view = view |
|
||||||
} |
|
||||||
this.correctRunBtnDisabled() |
|
||||||
return view |
|
||||||
} |
|
||||||
|
|
||||||
staticAnalysisView.prototype.selectedModules = function () { |
|
||||||
if (!this.view) { |
|
||||||
return [] |
|
||||||
} |
|
||||||
const selected = this.view.querySelectorAll('[name="staticanalysismodule"]:checked') |
|
||||||
var toRun = [] |
|
||||||
for (var i = 0; i < selected.length; i++) { |
|
||||||
toRun.push(selected[i].attributes.index.value) |
|
||||||
} |
|
||||||
return toRun |
|
||||||
} |
|
||||||
|
|
||||||
staticAnalysisView.prototype.run = function () { |
|
||||||
if (!this.view) { |
|
||||||
return |
|
||||||
} |
|
||||||
const highlightLocation = async (location, fileName) => { |
|
||||||
await this.analysisModule.call('editor', 'discardHighlight') |
|
||||||
await this.analysisModule.call('editor', 'highlight', location, fileName) |
|
||||||
} |
|
||||||
const selected = this.selectedModules() |
|
||||||
const warningContainer = $('#staticanalysisresult') |
|
||||||
warningContainer.empty() |
|
||||||
this.view.querySelector('#staticAnalysisCurrentFile').innerText = this.currentFile |
|
||||||
var self = this |
|
||||||
if (this.lastCompilationResult && selected.length) { |
|
||||||
this.runBtn.removeAttribute('disabled') |
|
||||||
let warningCount = 0 |
|
||||||
this.runner.run(this.lastCompilationResult, selected, (results) => { |
|
||||||
const groupedModules = utils.groupBy(preProcessModules(this.runner.modules()), 'categoryId') |
|
||||||
results.map((result, j) => { |
|
||||||
let moduleName |
|
||||||
Object.keys(groupedModules).map((key) => { |
|
||||||
groupedModules[key].forEach((el) => { |
|
||||||
if (el.name === result.name) { |
|
||||||
moduleName = groupedModules[key][0].categoryDisplayName |
|
||||||
} |
|
||||||
}) |
|
||||||
}) |
|
||||||
const alreadyExistedEl = this.view.querySelector(`[id="staticAnalysisModule${moduleName}"]`) |
|
||||||
if (!alreadyExistedEl) { |
|
||||||
warningContainer.append(` |
|
||||||
<div class="mb-4" name="staticAnalysisModules" id="staticAnalysisModule${moduleName}"> |
|
||||||
<span class="text-dark h6">${moduleName}</span> |
|
||||||
</div> |
|
||||||
`)
|
|
||||||
} |
|
||||||
|
|
||||||
result.report.map((item, i) => { |
|
||||||
let location = '' |
|
||||||
let locationString = 'not available' |
|
||||||
let column = 0 |
|
||||||
let row = 0 |
|
||||||
let fileName = this.currentFile |
|
||||||
if (item.location) { |
|
||||||
var split = item.location.split(':') |
|
||||||
var file = split[2] |
|
||||||
location = { |
|
||||||
start: parseInt(split[0]), |
|
||||||
length: parseInt(split[1]) |
|
||||||
} |
|
||||||
location = self._deps.offsetToLineColumnConverter.offsetToLineColumn( |
|
||||||
location, |
|
||||||
parseInt(file), |
|
||||||
self.lastCompilationSource.sources, |
|
||||||
self.lastCompilationResult.sources |
|
||||||
) |
|
||||||
row = location.start.line |
|
||||||
column = location.start.column |
|
||||||
locationString = (row + 1) + ':' + column + ':' |
|
||||||
fileName = Object.keys(self.lastCompilationResult.contracts)[file] |
|
||||||
} |
|
||||||
warningCount++ |
|
||||||
const msg = yo` |
|
||||||
<span class="d-flex flex-column"> |
|
||||||
<span class="h6 font-weight-bold">${result.name}</span> |
|
||||||
${item.warning} |
|
||||||
${item.more ? yo`<span><a href="${item.more}" target="_blank">more</a></span>` : yo`<span></span>`} |
|
||||||
<span class="" title="Position in ${fileName}">Pos: ${locationString}</span> |
|
||||||
</span>` |
|
||||||
self._components.renderer.error( |
|
||||||
msg, |
|
||||||
this.view.querySelector(`[id="staticAnalysisModule${moduleName}"]`), |
|
||||||
{ |
|
||||||
click: () => highlightLocation(location, fileName), |
|
||||||
type: 'warning', |
|
||||||
useSpan: true, |
|
||||||
errFile: fileName, |
|
||||||
errLine: row, |
|
||||||
errCol: column |
|
||||||
} |
|
||||||
) |
|
||||||
}) |
|
||||||
}) |
|
||||||
// hide empty staticAnalysisModules sections
|
|
||||||
this.view.querySelectorAll('[name="staticAnalysisModules"]').forEach((section) => { |
|
||||||
if (!section.getElementsByClassName('alert-warning').length) section.hidden = true |
|
||||||
}) |
|
||||||
self.event.trigger('staticAnaysisWarning', [warningCount]) |
|
||||||
}) |
|
||||||
} else { |
|
||||||
this.runBtn.setAttribute('disabled', 'disabled') |
|
||||||
if (selected.length) { |
|
||||||
warningContainer.html('No compiled AST available') |
|
||||||
} |
|
||||||
self.event.trigger('staticAnaysisWarning', [-1]) |
|
||||||
} |
|
||||||
} |
|
||||||
staticAnalysisView.prototype.checkModule = function (event) { |
|
||||||
const selected = this.view.querySelectorAll('[name="staticanalysismodule"]:checked') |
|
||||||
const checkAll = this.view.querySelector('[id="checkAllEntries"]') |
|
||||||
this.correctRunBtnDisabled() |
|
||||||
if (event.target.checked) { |
|
||||||
checkAll.checked = true |
|
||||||
} else if (!selected.length) { |
|
||||||
checkAll.checked = false |
|
||||||
} |
|
||||||
} |
|
||||||
staticAnalysisView.prototype.correctRunBtnDisabled = function () { |
|
||||||
if (!this.view) { |
|
||||||
return |
|
||||||
} |
|
||||||
const selected = this.view.querySelectorAll('[name="staticanalysismodule"]:checked') |
|
||||||
if (this.lastCompilationResult && selected.length !== 0) { |
|
||||||
this.runBtn.removeAttribute('disabled') |
|
||||||
} else { |
|
||||||
this.runBtn.setAttribute('disabled', 'disabled') |
|
||||||
} |
|
||||||
} |
|
||||||
staticAnalysisView.prototype.checkAll = function (event) { |
|
||||||
if (!this.view) { |
|
||||||
return |
|
||||||
} |
|
||||||
// checks/unchecks all
|
|
||||||
const checkBoxes = this.view.querySelectorAll('[name="staticanalysismodule"]') |
|
||||||
checkBoxes.forEach((checkbox) => { checkbox.checked = event.target.checked }) |
|
||||||
this.correctRunBtnDisabled() |
|
||||||
} |
|
||||||
|
|
||||||
staticAnalysisView.prototype.handleCollapse = function (e) { |
|
||||||
const downs = e.toElement.parentElement.getElementsByClassName('fas fa-angle-double-right') |
|
||||||
const iEls = document.getElementsByTagName('i') |
|
||||||
for (var i = 0; i < iEls.length; i++) { iEls[i].hidden = false } |
|
||||||
downs[0].hidden = true |
|
||||||
} |
|
||||||
|
|
||||||
staticAnalysisView.prototype.renderModules = function () { |
|
||||||
const groupedModules = utils.groupBy(preProcessModules(this.runner.modules()), 'categoryId') |
|
||||||
const moduleEntries = Object.keys(groupedModules).map((categoryId, i) => { |
|
||||||
const category = groupedModules[categoryId] |
|
||||||
const entriesDom = category.map((item, i) => { |
|
||||||
return yo` |
|
||||||
<div class="form-check"> |
|
||||||
<input id="staticanalysismodule_${categoryId}_${i}" |
|
||||||
type="checkbox" |
|
||||||
class="form-check-input staticAnalysisItem" |
|
||||||
name="staticanalysismodule" |
|
||||||
index=${item._index} |
|
||||||
checked="true" |
|
||||||
style="vertical-align:bottom" |
|
||||||
onclick="${(event) => this.checkModule(event)}" |
|
||||||
> |
|
||||||
<label for="staticanalysismodule_${categoryId}_${i}" class="form-check-label mb-1"> |
|
||||||
<p class="mb-0 font-weight-bold text-capitalize">${item.name}</p> |
|
||||||
${item.description} |
|
||||||
</label> |
|
||||||
</div> |
|
||||||
` |
|
||||||
}) |
|
||||||
return yo` |
|
||||||
<div class="${css.block}"> |
|
||||||
<input type="radio" name="accordion" class="w-100 d-none card" id="heading${categoryId}" onclick=${(e) => this.handleCollapse(e)}"/> |
|
||||||
<label for="heading${categoryId}" style="cursor: pointer;" class="pl-3 card-header h6 d-flex justify-content-between font-weight-bold border-left px-1 py-2 w-100"> |
|
||||||
${category[0].categoryDisplayName} |
|
||||||
<div> |
|
||||||
<i class="fas fa-angle-double-right"></i> |
|
||||||
</div> |
|
||||||
</label> |
|
||||||
<div class="w-100 d-block px-2 my-1 ${css.entries}"> |
|
||||||
${entriesDom} |
|
||||||
</div> |
|
||||||
</div> |
|
||||||
` |
|
||||||
}) |
|
||||||
// collaps first module
|
|
||||||
moduleEntries[0].getElementsByTagName('input')[0].checked = true |
|
||||||
moduleEntries[0].getElementsByTagName('i')[0].hidden = true |
|
||||||
return yo` |
|
||||||
<div class="accordion" id="accordionModules"> |
|
||||||
${moduleEntries} |
|
||||||
</div>` |
|
||||||
} |
|
||||||
|
|
||||||
module.exports = staticAnalysisView |
|
||||||
|
|
||||||
/** |
|
||||||
* @dev Process & categorize static analysis modules to show them on UI |
|
||||||
* @param arr list of static analysis modules received from remix-analyzer module |
|
||||||
*/ |
|
||||||
function preProcessModules (arr) { |
|
||||||
return arr.map((Item, i) => { |
|
||||||
const itemObj = new Item() |
|
||||||
itemObj._index = i |
|
||||||
itemObj.categoryDisplayName = itemObj.category.displayName |
|
||||||
itemObj.categoryId = itemObj.category.id |
|
||||||
return itemObj |
|
||||||
}) |
|
||||||
} |
|
@ -1,36 +0,0 @@ |
|||||||
var csjs = require('csjs-inject') |
|
||||||
|
|
||||||
var css = csjs` |
|
||||||
.analysis { |
|
||||||
display: flex; |
|
||||||
flex-direction: column; |
|
||||||
} |
|
||||||
.result { |
|
||||||
margin-top: 1%; |
|
||||||
max-height: 300px; |
|
||||||
word-break: break-word; |
|
||||||
} |
|
||||||
.buttons { |
|
||||||
margin: 1rem 0; |
|
||||||
} |
|
||||||
.label { |
|
||||||
display: flex; |
|
||||||
align-items: center; |
|
||||||
} |
|
||||||
.label { |
|
||||||
display: flex; |
|
||||||
align-items: center; |
|
||||||
user-select: none; |
|
||||||
} |
|
||||||
.block input[type='radio']:checked ~ .entries{ |
|
||||||
height: auto; |
|
||||||
transition: .5s ease-in; |
|
||||||
} |
|
||||||
.entries{ |
|
||||||
height: 0; |
|
||||||
overflow: hidden; |
|
||||||
transition: .5s ease-out; |
|
||||||
} |
|
||||||
` |
|
||||||
|
|
||||||
module.exports = css |
|
@ -1,46 +0,0 @@ |
|||||||
'use strict' |
|
||||||
const { bufferToHex, isHexString } = require('ethereumjs-util') |
|
||||||
|
|
||||||
function convertToPrefixedHex (input) { |
|
||||||
if (input === undefined || input === null || isHexString(input)) { |
|
||||||
return input |
|
||||||
} else if (Buffer.isBuffer(input)) { |
|
||||||
return bufferToHex(input) |
|
||||||
} |
|
||||||
return '0x' + input.toString(16) |
|
||||||
} |
|
||||||
|
|
||||||
/* |
|
||||||
txResult.result can be 3 different things: |
|
||||||
- VM call or tx: ethereumjs-vm result object |
|
||||||
- Node transaction: object returned from eth.getTransactionReceipt() |
|
||||||
- Node call: return value from function call (not an object) |
|
||||||
|
|
||||||
Also, VM results use BN and Buffers, Node results use hex strings/ints, |
|
||||||
So we need to normalize the values to prefixed hex strings |
|
||||||
*/ |
|
||||||
function resultToRemixTx (txResult) { |
|
||||||
const { result, transactionHash } = txResult |
|
||||||
const { status, execResult, gasUsed, createdAddress, contractAddress } = result |
|
||||||
let returnValue, errorMessage |
|
||||||
|
|
||||||
if (isHexString(result)) { |
|
||||||
returnValue = result |
|
||||||
} else if (execResult !== undefined) { |
|
||||||
returnValue = execResult.returnValue |
|
||||||
errorMessage = execResult.exceptionError |
|
||||||
} |
|
||||||
|
|
||||||
return { |
|
||||||
transactionHash, |
|
||||||
status, |
|
||||||
gasUsed: convertToPrefixedHex(gasUsed), |
|
||||||
error: errorMessage, |
|
||||||
return: convertToPrefixedHex(returnValue), |
|
||||||
createdAddress: convertToPrefixedHex(createdAddress || contractAddress) |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
module.exports = { |
|
||||||
resultToRemixTx |
|
||||||
} |
|
@ -0,0 +1,121 @@ |
|||||||
|
'use strict' |
||||||
|
import { Transaction } from '@ethereumjs/tx' |
||||||
|
import { Block } from '@ethereumjs/block' |
||||||
|
import { BN, bufferToHex, Address } from 'ethereumjs-util' |
||||||
|
import { EventManager } from '../eventManager' |
||||||
|
import { LogsManager } from './logsManager' |
||||||
|
|
||||||
|
export class TxRunnerVM { |
||||||
|
event |
||||||
|
blockNumber |
||||||
|
runAsync |
||||||
|
pendingTxs |
||||||
|
vmaccounts |
||||||
|
queusTxs |
||||||
|
blocks |
||||||
|
txs |
||||||
|
logsManager |
||||||
|
commonContext |
||||||
|
getVMObject: () => any |
||||||
|
|
||||||
|
constructor (vmaccounts, api, getVMObject) { |
||||||
|
this.event = new EventManager() |
||||||
|
this.logsManager = new LogsManager() |
||||||
|
// has a default for now for backwards compatability
|
||||||
|
this.getVMObject = getVMObject |
||||||
|
this.commonContext = this.getVMObject().common |
||||||
|
this.blockNumber = 0 |
||||||
|
this.runAsync = true |
||||||
|
this.blockNumber = 0 // The VM is running in Homestead mode, which started at this block.
|
||||||
|
this.runAsync = false // We have to run like this cause the VM Event Manager does not support running multiple txs at the same time.
|
||||||
|
this.pendingTxs = {} |
||||||
|
this.vmaccounts = vmaccounts |
||||||
|
this.queusTxs = [] |
||||||
|
this.blocks = [] |
||||||
|
} |
||||||
|
|
||||||
|
execute (args, confirmationCb, gasEstimationForceSend, promptCb, callback) { |
||||||
|
let data = args.data |
||||||
|
if (data.slice(0, 2) !== '0x') { |
||||||
|
data = '0x' + data |
||||||
|
} |
||||||
|
|
||||||
|
try { |
||||||
|
this.runInVm(args.from, args.to, data, args.value, args.gasLimit, args.useCall, args.timestamp, callback) |
||||||
|
} catch (e) { |
||||||
|
callback(e, null) |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
runInVm (from, to, data, value, gasLimit, useCall, timestamp, callback) { |
||||||
|
const self = this |
||||||
|
const account = self.vmaccounts[from] |
||||||
|
if (!account) { |
||||||
|
return callback('Invalid account selected') |
||||||
|
} |
||||||
|
if (Number.isInteger(gasLimit)) { |
||||||
|
gasLimit = '0x' + gasLimit.toString(16) |
||||||
|
} |
||||||
|
|
||||||
|
this.getVMObject().stateManager.getAccount(Address.fromString(from)).then((res) => { |
||||||
|
// See https://github.com/ethereumjs/ethereumjs-tx/blob/master/docs/classes/transaction.md#constructor
|
||||||
|
// for initialization fields and their types
|
||||||
|
value = value ? parseInt(value) : 0 |
||||||
|
const tx = Transaction.fromTxData({ |
||||||
|
nonce: new BN(res.nonce), |
||||||
|
gasPrice: '0x1', |
||||||
|
gasLimit: gasLimit, |
||||||
|
to: to, |
||||||
|
value: value, |
||||||
|
data: Buffer.from(data.slice(2), 'hex') |
||||||
|
}, { common: this.commonContext }).sign(account.privateKey) |
||||||
|
|
||||||
|
const coinbases = ['0x0e9281e9c6a0808672eaba6bd1220e144c9bb07a', '0x8945a1288dc78a6d8952a92c77aee6730b414778', '0x94d76e24f818426ae84aa404140e8d5f60e10e7e'] |
||||||
|
const difficulties = [new BN('69762765929000', 10), new BN('70762765929000', 10), new BN('71762765929000', 10)] |
||||||
|
|
||||||
|
var block = Block.fromBlockData({ |
||||||
|
header: { |
||||||
|
timestamp: timestamp || (new Date().getTime() / 1000 | 0), |
||||||
|
number: self.blockNumber, |
||||||
|
coinbase: coinbases[self.blockNumber % coinbases.length], |
||||||
|
difficulty: difficulties[self.blockNumber % difficulties.length], |
||||||
|
gasLimit: new BN(gasLimit.replace('0x', ''), 16).imuln(2) |
||||||
|
}, |
||||||
|
transactions: [tx] |
||||||
|
}, { common: this.commonContext }) |
||||||
|
|
||||||
|
if (!useCall) { |
||||||
|
++self.blockNumber |
||||||
|
this.runBlockInVm(tx, block, callback) |
||||||
|
} else { |
||||||
|
this.getVMObject().stateManager.checkpoint().then(() => { |
||||||
|
this.runBlockInVm(tx, block, (err, result) => { |
||||||
|
this.getVMObject().stateManager.revert().then(() => { |
||||||
|
callback(err, result) |
||||||
|
}) |
||||||
|
}) |
||||||
|
}) |
||||||
|
} |
||||||
|
}).catch((e) => { |
||||||
|
callback(e) |
||||||
|
}) |
||||||
|
} |
||||||
|
|
||||||
|
runBlockInVm (tx, block, callback) { |
||||||
|
this.getVMObject().vm.runBlock({ block: block, generate: true, skipBlockValidation: true, skipBalance: false }).then((results) => { |
||||||
|
const result = results.results[0] |
||||||
|
if (result) { |
||||||
|
const status = result.execResult.exceptionError ? 0 : 1 |
||||||
|
result.status = `0x${status}` |
||||||
|
} |
||||||
|
callback(null, { |
||||||
|
result: result, |
||||||
|
transactionHash: bufferToHex(Buffer.from(tx.hash())), |
||||||
|
block, |
||||||
|
tx |
||||||
|
}) |
||||||
|
}).catch(function (err) { |
||||||
|
callback(err) |
||||||
|
}) |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,147 @@ |
|||||||
|
'use strict' |
||||||
|
import { EventManager } from '../eventManager' |
||||||
|
import Web3 from 'web3' |
||||||
|
|
||||||
|
export class TxRunnerWeb3 { |
||||||
|
event |
||||||
|
_api |
||||||
|
getWeb3: () => Web3 |
||||||
|
currentblockGasLimit: () => number |
||||||
|
|
||||||
|
constructor (api, getWeb3, currentblockGasLimit) { |
||||||
|
this.event = new EventManager() |
||||||
|
this.getWeb3 = getWeb3 |
||||||
|
this.currentblockGasLimit = currentblockGasLimit |
||||||
|
this._api = api |
||||||
|
} |
||||||
|
|
||||||
|
_executeTx (tx, gasPrice, api, promptCb, callback) { |
||||||
|
if (gasPrice) tx.gasPrice = this.getWeb3().utils.toHex(gasPrice) |
||||||
|
if (api.personalMode()) { |
||||||
|
promptCb( |
||||||
|
(value) => { |
||||||
|
this._sendTransaction((this.getWeb3() as any).personal.sendTransaction, tx, value, callback) |
||||||
|
}, |
||||||
|
() => { |
||||||
|
return callback('Canceled by user.') |
||||||
|
} |
||||||
|
) |
||||||
|
} else { |
||||||
|
this._sendTransaction(this.getWeb3().eth.sendTransaction, tx, null, callback) |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
_sendTransaction (sendTx, tx, pass, callback) { |
||||||
|
const cb = (err, resp) => { |
||||||
|
if (err) { |
||||||
|
return callback(err, resp) |
||||||
|
} |
||||||
|
this.event.trigger('transactionBroadcasted', [resp]) |
||||||
|
var listenOnResponse = () => { |
||||||
|
// eslint-disable-next-line no-async-promise-executor
|
||||||
|
return new Promise(async (resolve, reject) => { |
||||||
|
const receipt = await tryTillReceiptAvailable(resp, this.getWeb3()) |
||||||
|
tx = await tryTillTxAvailable(resp, this.getWeb3()) |
||||||
|
resolve({ |
||||||
|
receipt, |
||||||
|
tx, |
||||||
|
transactionHash: receipt ? receipt['transactionHash'] : null |
||||||
|
}) |
||||||
|
}) |
||||||
|
} |
||||||
|
listenOnResponse().then((txData) => { callback(null, txData) }).catch((error) => { callback(error) }) |
||||||
|
} |
||||||
|
const args = pass !== null ? [tx, pass, cb] : [tx, cb] |
||||||
|
try { |
||||||
|
sendTx.apply({}, args) |
||||||
|
} catch (e) { |
||||||
|
return callback(`Send transaction failed: ${e.message} . if you use an injected provider, please check it is properly unlocked. `) |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
execute (args, confirmationCb, gasEstimationForceSend, promptCb, callback) { |
||||||
|
let data = args.data |
||||||
|
if (data.slice(0, 2) !== '0x') { |
||||||
|
data = '0x' + data |
||||||
|
} |
||||||
|
|
||||||
|
return this.runInNode(args.from, args.to, data, args.value, args.gasLimit, args.useCall, args.timestamp, confirmationCb, gasEstimationForceSend, promptCb, callback) |
||||||
|
} |
||||||
|
|
||||||
|
runInNode (from, to, data, value, gasLimit, useCall, timestamp, confirmCb, gasEstimationForceSend, promptCb, callback) { |
||||||
|
const tx = { from: from, to: to, data: data, value: value } |
||||||
|
|
||||||
|
if (useCall) { |
||||||
|
const tag = Date.now() // for e2e reference
|
||||||
|
tx['gas'] = gasLimit |
||||||
|
tx['timestamp'] = timestamp |
||||||
|
return this.getWeb3().eth.call(tx, function (error, result: any) { |
||||||
|
if (error) return callback(error) |
||||||
|
callback(null, { |
||||||
|
result: result |
||||||
|
}) |
||||||
|
}) |
||||||
|
} |
||||||
|
this.getWeb3().eth.estimateGas(tx, (err, gasEstimation) => { |
||||||
|
if (err && err.message.indexOf('Invalid JSON RPC response') !== -1) { |
||||||
|
// // @todo(#378) this should be removed when https://github.com/WalletConnect/walletconnect-monorepo/issues/334 is fixed
|
||||||
|
callback(new Error('Gas estimation failed because of an unknown internal error. This may indicated that the transaction will fail.')) |
||||||
|
} |
||||||
|
gasEstimationForceSend(err, () => { |
||||||
|
// callback is called whenever no error
|
||||||
|
tx['gas'] = !gasEstimation ? gasLimit : gasEstimation |
||||||
|
|
||||||
|
if (this._api.config.getUnpersistedProperty('doNotShowTransactionConfirmationAgain')) { |
||||||
|
return this._executeTx(tx, null, this._api, promptCb, callback) |
||||||
|
} |
||||||
|
|
||||||
|
this._api.detectNetwork((err, network) => { |
||||||
|
if (err) { |
||||||
|
console.log(err) |
||||||
|
return |
||||||
|
} |
||||||
|
|
||||||
|
confirmCb(network, tx, tx['gas'], (gasPrice) => { |
||||||
|
return this._executeTx(tx, gasPrice, this._api, promptCb, callback) |
||||||
|
}, (error) => { |
||||||
|
callback(error) |
||||||
|
}) |
||||||
|
}) |
||||||
|
}, () => { |
||||||
|
const blockGasLimit = this.currentblockGasLimit() |
||||||
|
// NOTE: estimateGas very likely will return a large limit if execution of the code failed
|
||||||
|
// we want to be able to run the code in order to debug and find the cause for the failure
|
||||||
|
if (err) return callback(err) |
||||||
|
|
||||||
|
let warnEstimation = ' An important gas estimation might also be the sign of a problem in the contract code. Please check loops and be sure you did not sent value to a non payable function (that\'s also the reason of strong gas estimation). ' |
||||||
|
warnEstimation += ' ' + err |
||||||
|
|
||||||
|
if (gasEstimation > gasLimit) { |
||||||
|
return callback('Gas required exceeds limit: ' + gasLimit + '. ' + warnEstimation) |
||||||
|
} |
||||||
|
if (gasEstimation > blockGasLimit) { |
||||||
|
return callback('Gas required exceeds block gas limit: ' + gasLimit + '. ' + warnEstimation) |
||||||
|
} |
||||||
|
}) |
||||||
|
}) |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
async function tryTillReceiptAvailable (txhash, web3) { |
||||||
|
try { |
||||||
|
const receipt = await web3.eth.getTransactionReceipt(txhash) |
||||||
|
if (receipt) return receipt |
||||||
|
} catch (e) {} |
||||||
|
await pause() |
||||||
|
return await tryTillReceiptAvailable(txhash, web3) |
||||||
|
} |
||||||
|
|
||||||
|
async function tryTillTxAvailable (txhash, web3) { |
||||||
|
try { |
||||||
|
const tx = await web3.eth.getTransaction(txhash) |
||||||
|
if (tx) return tx |
||||||
|
} catch (e) {} |
||||||
|
return await tryTillTxAvailable(txhash, web3) |
||||||
|
} |
||||||
|
|
||||||
|
async function pause () { return new Promise((resolve, reject) => { setTimeout(resolve, 500) }) } |
@ -1,379 +0,0 @@ |
|||||||
import { waterfall } from 'async' |
|
||||||
import { BN, privateToAddress, isValidPrivate, toChecksumAddress, Address } from 'ethereumjs-util' |
|
||||||
import { randomBytes } from 'crypto' |
|
||||||
import { EventEmitter } from 'events' |
|
||||||
import { TxRunner } from './execution/txRunner' |
|
||||||
import { sortAbiFunction, getFallbackInterface, getReceiveInterface, inputParametersDeclarationToString } from './execution/txHelper' |
|
||||||
import { EventManager } from './eventManager' |
|
||||||
import { ExecutionContext } from './execution/execution-context' |
|
||||||
import { resultToRemixTx } from './helpers/txResultHelper' |
|
||||||
|
|
||||||
export class UniversalDApp { |
|
||||||
events |
|
||||||
event |
|
||||||
executionContext |
|
||||||
config |
|
||||||
txRunner |
|
||||||
accounts |
|
||||||
transactionContextAPI |
|
||||||
|
|
||||||
constructor (config, executionContext) { |
|
||||||
this.events = new EventEmitter() |
|
||||||
this.event = new EventManager() |
|
||||||
// has a default for now for backwards compatability
|
|
||||||
this.executionContext = executionContext || new ExecutionContext() |
|
||||||
this.config = config |
|
||||||
|
|
||||||
this.txRunner = new TxRunner({}, { |
|
||||||
config: config, |
|
||||||
detectNetwork: (cb) => { |
|
||||||
this.executionContext.detectNetwork(cb) |
|
||||||
}, |
|
||||||
personalMode: () => { |
|
||||||
return this.executionContext.getProvider() === 'web3' ? this.config.get('settings/personal-mode') : false |
|
||||||
} |
|
||||||
}, this.executionContext) |
|
||||||
this.accounts = {} |
|
||||||
this.executionContext.event.register('contextChanged', this.resetEnvironment.bind(this)) |
|
||||||
} |
|
||||||
|
|
||||||
// TODO : event should be triggered by Udapp instead of TxListener
|
|
||||||
/** Listen on New Transaction. (Cannot be done inside constructor because txlistener doesn't exist yet) */ |
|
||||||
startListening (txlistener) { |
|
||||||
txlistener.event.register('newTransaction', (tx) => { |
|
||||||
this.events.emit('newTransaction', tx) |
|
||||||
}) |
|
||||||
} |
|
||||||
|
|
||||||
resetEnvironment () { |
|
||||||
this.accounts = {} |
|
||||||
if (this.executionContext.isVM()) { |
|
||||||
this._addAccount('3cd7232cd6f3fc66a57a6bedc1a8ed6c228fff0a327e169c2bcc5e869ed49511', '0x56BC75E2D63100000') |
|
||||||
this._addAccount('2ac6c190b09897cd8987869cc7b918cfea07ee82038d492abce033c75c1b1d0c', '0x56BC75E2D63100000') |
|
||||||
this._addAccount('dae9801649ba2d95a21e688b56f77905e5667c44ce868ec83f82e838712a2c7a', '0x56BC75E2D63100000') |
|
||||||
this._addAccount('d74aa6d18aa79a05f3473dd030a97d3305737cbc8337d940344345c1f6b72eea', '0x56BC75E2D63100000') |
|
||||||
this._addAccount('71975fbf7fe448e004ac7ae54cad0a383c3906055a65468714156a07385e96ce', '0x56BC75E2D63100000') |
|
||||||
} |
|
||||||
// TODO: most params here can be refactored away in txRunner
|
|
||||||
this.txRunner = new TxRunner(this.accounts, { |
|
||||||
// TODO: only used to check value of doNotShowTransactionConfirmationAgain property
|
|
||||||
config: this.config, |
|
||||||
// TODO: to refactor, TxRunner already has access to executionContext
|
|
||||||
detectNetwork: (cb) => { |
|
||||||
this.executionContext.detectNetwork(cb) |
|
||||||
}, |
|
||||||
personalMode: () => { |
|
||||||
return this.executionContext.getProvider() === 'web3' ? this.config.get('settings/personal-mode') : false |
|
||||||
} |
|
||||||
}, this.executionContext) |
|
||||||
this.txRunner.event.register('transactionBroadcasted', (txhash) => { |
|
||||||
this.executionContext.detectNetwork((error, network) => { |
|
||||||
if (error || !network) return |
|
||||||
this.event.trigger('transactionBroadcasted', [txhash, network.name]) |
|
||||||
}) |
|
||||||
}) |
|
||||||
} |
|
||||||
|
|
||||||
resetAPI (transactionContextAPI) { |
|
||||||
this.transactionContextAPI = transactionContextAPI |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* Create a VM Account |
|
||||||
* @param {{privateKey: string, balance: string}} newAccount The new account to create |
|
||||||
*/ |
|
||||||
createVMAccount (newAccount) { |
|
||||||
const { privateKey, balance } = newAccount |
|
||||||
if (this.executionContext.getProvider() !== 'vm') { |
|
||||||
throw new Error('plugin API does not allow creating a new account through web3 connection. Only vm mode is allowed') |
|
||||||
} |
|
||||||
this._addAccount(privateKey, balance) |
|
||||||
const privKey = Buffer.from(privateKey, 'hex') |
|
||||||
return '0x' + privateToAddress(privKey).toString('hex') |
|
||||||
} |
|
||||||
|
|
||||||
newAccount (password, passwordPromptCb, cb) { |
|
||||||
if (!this.executionContext.isVM()) { |
|
||||||
if (!this.config.get('settings/personal-mode')) { |
|
||||||
return cb('Not running in personal mode') |
|
||||||
} |
|
||||||
return passwordPromptCb((passphrase) => { |
|
||||||
this.executionContext.web3().personal.newAccount(passphrase, cb) |
|
||||||
}) |
|
||||||
} |
|
||||||
let privateKey |
|
||||||
do { |
|
||||||
privateKey = randomBytes(32) |
|
||||||
} while (!isValidPrivate(privateKey)) |
|
||||||
this._addAccount(privateKey, '0x56BC75E2D63100000') |
|
||||||
cb(null, '0x' + privateToAddress(privateKey).toString('hex')) |
|
||||||
} |
|
||||||
|
|
||||||
/** Add an account to the list of account (only for Javascript VM) */ |
|
||||||
_addAccount (privateKey, balance) { |
|
||||||
if (!this.executionContext.isVM()) { |
|
||||||
throw new Error('_addAccount() cannot be called in non-VM mode') |
|
||||||
} |
|
||||||
|
|
||||||
if (!this.accounts) { |
|
||||||
return |
|
||||||
} |
|
||||||
privateKey = Buffer.from(privateKey, 'hex') |
|
||||||
const address = privateToAddress(privateKey) |
|
||||||
|
|
||||||
// FIXME: we don't care about the callback, but we should still make this proper
|
|
||||||
const stateManager = this.executionContext.vm().stateManager |
|
||||||
stateManager.getAccount(address).then((account) => { |
|
||||||
account.balance = new BN(balance.replace('0x', '') || 'f00000000000000001', 16) |
|
||||||
stateManager.putAccount(address, account).catch((error) => { |
|
||||||
console.log(error) |
|
||||||
}) |
|
||||||
}).catch((error) => { |
|
||||||
console.log(error) |
|
||||||
}) |
|
||||||
|
|
||||||
this.accounts[toChecksumAddress('0x' + address.toString('hex'))] = { privateKey, nonce: 0 } |
|
||||||
} |
|
||||||
|
|
||||||
/** Return the list of accounts */ |
|
||||||
getAccounts (cb) { |
|
||||||
return new Promise((resolve, reject) => { |
|
||||||
const provider = this.executionContext.getProvider() |
|
||||||
switch (provider) { |
|
||||||
case 'vm': |
|
||||||
if (!this.accounts) { |
|
||||||
if (cb) cb('No accounts?') |
|
||||||
reject(new Error('No accounts?')) |
|
||||||
return |
|
||||||
} |
|
||||||
if (cb) cb(null, Object.keys(this.accounts)) |
|
||||||
resolve(Object.keys(this.accounts)) |
|
||||||
break |
|
||||||
case 'web3': |
|
||||||
if (this.config.get('settings/personal-mode')) { |
|
||||||
return this.executionContext.web3().personal.getListAccounts((error, accounts) => { |
|
||||||
if (cb) cb(error, accounts) |
|
||||||
if (error) return reject(error) |
|
||||||
resolve(accounts) |
|
||||||
}) |
|
||||||
} else { |
|
||||||
this.executionContext.web3().eth.getAccounts((error, accounts) => { |
|
||||||
if (cb) cb(error, accounts) |
|
||||||
if (error) return reject(error) |
|
||||||
resolve(accounts) |
|
||||||
}) |
|
||||||
} |
|
||||||
break |
|
||||||
case 'injected': { |
|
||||||
this.executionContext.web3().eth.getAccounts((error, accounts) => { |
|
||||||
if (cb) cb(error, accounts) |
|
||||||
if (error) return reject(error) |
|
||||||
resolve(accounts) |
|
||||||
}) |
|
||||||
} |
|
||||||
} |
|
||||||
}) |
|
||||||
} |
|
||||||
|
|
||||||
/** Get the balance of an address */ |
|
||||||
getBalance (address, cb) { |
|
||||||
if (!this.executionContext.isVM()) { |
|
||||||
return this.executionContext.web3().eth.getBalance(address, (err, res) => { |
|
||||||
if (err) { |
|
||||||
return cb(err) |
|
||||||
} |
|
||||||
cb(null, res.toString(10)) |
|
||||||
}) |
|
||||||
} |
|
||||||
if (!this.accounts) { |
|
||||||
return cb('No accounts?') |
|
||||||
} |
|
||||||
|
|
||||||
this.executionContext.vm().stateManager.getAccount(Address.fromString(address)).then((res) => { |
|
||||||
cb(null, new BN(res.balance).toString(10)) |
|
||||||
}).catch(() => { |
|
||||||
cb('Account not found') |
|
||||||
}) |
|
||||||
} |
|
||||||
|
|
||||||
/** Get the balance of an address, and convert wei to ether */ |
|
||||||
getBalanceInEther (address, callback) { |
|
||||||
this.getBalance(address, (error, balance) => { |
|
||||||
if (error) { |
|
||||||
return callback(error) |
|
||||||
} |
|
||||||
callback(null, this.executionContext.web3().utils.fromWei(balance, 'ether')) |
|
||||||
}) |
|
||||||
} |
|
||||||
|
|
||||||
pendingTransactionsCount () { |
|
||||||
return Object.keys(this.txRunner.pendingTxs).length |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* deploy the given contract |
|
||||||
* |
|
||||||
* @param {String} data - data to send with the transaction ( return of txFormat.buildData(...) ). |
|
||||||
* @param {Function} callback - callback. |
|
||||||
*/ |
|
||||||
createContract (data, confirmationCb, continueCb, promptCb, callback) { |
|
||||||
this.runTx({ data: data, useCall: false }, confirmationCb, continueCb, promptCb, callback) |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* call the current given contract |
|
||||||
* |
|
||||||
* @param {String} to - address of the contract to call. |
|
||||||
* @param {String} data - data to send with the transaction ( return of txFormat.buildData(...) ). |
|
||||||
* @param {Object} funAbi - abi definition of the function to call. |
|
||||||
* @param {Function} callback - callback. |
|
||||||
*/ |
|
||||||
callFunction (to, data, funAbi, confirmationCb, continueCb, promptCb, callback) { |
|
||||||
const useCall = funAbi.stateMutability === 'view' || funAbi.stateMutability === 'pure' |
|
||||||
this.runTx({ to, data, useCall }, confirmationCb, continueCb, promptCb, callback) |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* call the current given contract |
|
||||||
* |
|
||||||
* @param {String} to - address of the contract to call. |
|
||||||
* @param {String} data - data to send with the transaction ( return of txFormat.buildData(...) ). |
|
||||||
* @param {Function} callback - callback. |
|
||||||
*/ |
|
||||||
sendRawTransaction (to, data, confirmationCb, continueCb, promptCb, callback) { |
|
||||||
this.runTx({ to, data, useCall: false }, confirmationCb, continueCb, promptCb, callback) |
|
||||||
} |
|
||||||
|
|
||||||
context () { |
|
||||||
return (this.executionContext.isVM() ? 'memory' : 'blockchain') |
|
||||||
} |
|
||||||
|
|
||||||
getABI (contract) { |
|
||||||
return sortAbiFunction(contract.abi) |
|
||||||
} |
|
||||||
|
|
||||||
getFallbackInterface (contractABI) { |
|
||||||
return getFallbackInterface(contractABI) |
|
||||||
} |
|
||||||
|
|
||||||
getReceiveInterface (contractABI) { |
|
||||||
return getReceiveInterface(contractABI) |
|
||||||
} |
|
||||||
|
|
||||||
getInputs (funABI) { |
|
||||||
if (!funABI.inputs) { |
|
||||||
return '' |
|
||||||
} |
|
||||||
return inputParametersDeclarationToString(funABI.inputs) |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* This function send a tx only to javascript VM or testnet, will return an error for the mainnet |
|
||||||
* SHOULD BE TAKEN CAREFULLY! |
|
||||||
* |
|
||||||
* @param {Object} tx - transaction. |
|
||||||
*/ |
|
||||||
sendTransaction (tx) { |
|
||||||
return new Promise((resolve, reject) => { |
|
||||||
this.executionContext.detectNetwork((error, network) => { |
|
||||||
if (error) return reject(error) |
|
||||||
if (network.name === 'Main' && network.id === '1') { |
|
||||||
return reject(new Error('It is not allowed to make this action against mainnet')) |
|
||||||
} |
|
||||||
this.silentRunTx(tx, (error, result) => { |
|
||||||
if (error) return reject(error) |
|
||||||
try { |
|
||||||
resolve(resultToRemixTx(result)) |
|
||||||
} catch (e) { |
|
||||||
reject(e) |
|
||||||
} |
|
||||||
}) |
|
||||||
}) |
|
||||||
}) |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* This function send a tx without alerting the user (if mainnet or if gas estimation too high). |
|
||||||
* SHOULD BE TAKEN CAREFULLY! |
|
||||||
* |
|
||||||
* @param {Object} tx - transaction. |
|
||||||
* @param {Function} callback - callback. |
|
||||||
*/ |
|
||||||
silentRunTx (tx, cb) { |
|
||||||
this.txRunner.rawRun( |
|
||||||
tx, |
|
||||||
(network, tx, gasEstimation, continueTxExecution, cancelCb) => { continueTxExecution() }, |
|
||||||
(error, continueTxExecution, cancelCb) => { if (error) { cb(error) } else { continueTxExecution() } }, |
|
||||||
(okCb, cancelCb) => { okCb() }, |
|
||||||
cb |
|
||||||
) |
|
||||||
} |
|
||||||
|
|
||||||
runTx (args, confirmationCb, continueCb, promptCb, cb) { |
|
||||||
const self = this |
|
||||||
waterfall([ |
|
||||||
function getGasLimit (next) { |
|
||||||
if (self.transactionContextAPI.getGasLimit) { |
|
||||||
return self.transactionContextAPI.getGasLimit(next) |
|
||||||
} |
|
||||||
next(null, 3000000) |
|
||||||
}, |
|
||||||
function queryValue (gasLimit, next) { |
|
||||||
if (args.value) { |
|
||||||
return next(null, args.value, gasLimit) |
|
||||||
} |
|
||||||
if (args.useCall || !self.transactionContextAPI.getValue) { |
|
||||||
return next(null, 0, gasLimit) |
|
||||||
} |
|
||||||
self.transactionContextAPI.getValue(function (err, value) { |
|
||||||
next(err, value, gasLimit) |
|
||||||
}) |
|
||||||
}, |
|
||||||
function getAccount (value, gasLimit, next) { |
|
||||||
if (args.from) { |
|
||||||
return next(null, args.from, value, gasLimit) |
|
||||||
} |
|
||||||
if (self.transactionContextAPI.getAddress) { |
|
||||||
return self.transactionContextAPI.getAddress(function (err, address) { |
|
||||||
next(err, address, value, gasLimit) |
|
||||||
}) |
|
||||||
} |
|
||||||
self.getAccounts(function (err, accounts) { |
|
||||||
const address = accounts[0] |
|
||||||
|
|
||||||
if (err) return next(err) |
|
||||||
if (!address) return next('No accounts available') |
|
||||||
if (self.executionContext.isVM() && !self.accounts[address]) { |
|
||||||
return next('Invalid account selected') |
|
||||||
} |
|
||||||
next(null, address, value, gasLimit) |
|
||||||
}) |
|
||||||
}, |
|
||||||
function runTransaction (fromAddress, value, gasLimit, next) { |
|
||||||
const tx = { to: args.to, data: args.data.dataHex, useCall: args.useCall, from: fromAddress, value: value, gasLimit: gasLimit, timestamp: args.data.timestamp } |
|
||||||
const payLoad = { funAbi: args.data.funAbi, funArgs: args.data.funArgs, contractBytecode: args.data.contractBytecode, contractName: args.data.contractName, contractABI: args.data.contractABI, linkReferences: args.data.linkReferences } |
|
||||||
let timestamp = Date.now() |
|
||||||
if (tx.timestamp) { |
|
||||||
timestamp = tx.timestamp |
|
||||||
} |
|
||||||
|
|
||||||
self.event.trigger('initiatingTransaction', [timestamp, tx, payLoad]) |
|
||||||
self.txRunner.rawRun(tx, confirmationCb, continueCb, promptCb, |
|
||||||
function (error, result) { |
|
||||||
const eventName = (tx.useCall ? 'callExecuted' : 'transactionExecuted') |
|
||||||
self.event.trigger(eventName, [error, tx.from, tx.to, tx.data, tx.useCall, result, timestamp, payLoad]) |
|
||||||
|
|
||||||
if (error && (typeof (error) !== 'string')) { |
|
||||||
if (error.message) error = error.message |
|
||||||
else { |
|
||||||
// eslint-disable-next-line no-empty
|
|
||||||
try { error = 'error: ' + JSON.stringify(error) } catch (e) {} |
|
||||||
} |
|
||||||
} |
|
||||||
next(error, result) |
|
||||||
} |
|
||||||
) |
|
||||||
} |
|
||||||
], cb) |
|
||||||
} |
|
||||||
} |
|
@ -1 +1 @@ |
|||||||
export { Provider } from './provider' |
export { Provider, extend } from './provider' |
||||||
|
@ -0,0 +1,169 @@ |
|||||||
|
/* global ethereum */ |
||||||
|
'use strict' |
||||||
|
import Web3 from 'web3' |
||||||
|
import { rlp, keccak, bufferToHex } from 'ethereumjs-util' |
||||||
|
import { vm as remixLibVm, execution } from '@remix-project/remix-lib' |
||||||
|
import VM from '@ethereumjs/vm' |
||||||
|
import Common from '@ethereumjs/common' |
||||||
|
import StateManager from '@ethereumjs/vm/dist/state/stateManager' |
||||||
|
import { StorageDump } from '@ethereumjs/vm/dist/state/interface' |
||||||
|
|
||||||
|
/* |
||||||
|
extend vm state manager and instanciate VM |
||||||
|
*/ |
||||||
|
|
||||||
|
class StateManagerCommonStorageDump extends StateManager { |
||||||
|
keyHashes: { [key: string]: string } |
||||||
|
constructor () { |
||||||
|
super() |
||||||
|
this.keyHashes = {} |
||||||
|
} |
||||||
|
|
||||||
|
putContractStorage (address, key, value) { |
||||||
|
this.keyHashes[keccak(key).toString('hex')] = bufferToHex(key) |
||||||
|
return super.putContractStorage(address, key, value) |
||||||
|
} |
||||||
|
|
||||||
|
async dumpStorage (address) { |
||||||
|
let trie |
||||||
|
try { |
||||||
|
trie = await this._getStorageTrie(address) |
||||||
|
} catch (e) { |
||||||
|
console.log(e) |
||||||
|
throw e |
||||||
|
} |
||||||
|
return new Promise<StorageDump>((resolve, reject) => { |
||||||
|
try { |
||||||
|
const storage = {} |
||||||
|
const stream = trie.createReadStream() |
||||||
|
stream.on('data', (val) => { |
||||||
|
const value = rlp.decode(val.value) |
||||||
|
storage['0x' + val.key.toString('hex')] = { |
||||||
|
key: this.keyHashes[val.key.toString('hex')], |
||||||
|
value: '0x' + value.toString('hex') |
||||||
|
} |
||||||
|
}) |
||||||
|
stream.on('end', function () { |
||||||
|
resolve(storage) |
||||||
|
}) |
||||||
|
} catch (e) { |
||||||
|
reject(e) |
||||||
|
} |
||||||
|
}) |
||||||
|
} |
||||||
|
|
||||||
|
async getStateRoot (force = false) { |
||||||
|
await this._cache.flush() |
||||||
|
|
||||||
|
const stateRoot = this._trie.root |
||||||
|
return stateRoot |
||||||
|
} |
||||||
|
|
||||||
|
async setStateRoot (stateRoot) { |
||||||
|
await this._cache.flush() |
||||||
|
|
||||||
|
if (stateRoot === this._trie.EMPTY_TRIE_ROOT) { |
||||||
|
this._trie.root = stateRoot |
||||||
|
this._cache.clear() |
||||||
|
this._storageTries = {} |
||||||
|
return |
||||||
|
} |
||||||
|
|
||||||
|
const hasRoot = await this._trie.checkRoot(stateRoot) |
||||||
|
if (!hasRoot) { |
||||||
|
throw new Error('State trie does not contain state root') |
||||||
|
} |
||||||
|
|
||||||
|
this._trie.root = stateRoot |
||||||
|
this._cache.clear() |
||||||
|
this._storageTries = {} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/* |
||||||
|
trigger contextChanged, web3EndpointChanged |
||||||
|
*/ |
||||||
|
export class VMContext { |
||||||
|
currentFork: string |
||||||
|
blockGasLimitDefault: number |
||||||
|
blockGasLimit: number |
||||||
|
customNetWorks |
||||||
|
blocks |
||||||
|
latestBlockNumber |
||||||
|
txs |
||||||
|
vms |
||||||
|
web3vm |
||||||
|
logsManager |
||||||
|
exeResults |
||||||
|
|
||||||
|
constructor () { |
||||||
|
this.blockGasLimitDefault = 4300000 |
||||||
|
this.blockGasLimit = this.blockGasLimitDefault |
||||||
|
this.currentFork = 'berlin' |
||||||
|
this.vms = { |
||||||
|
/* |
||||||
|
byzantium: createVm('byzantium'), |
||||||
|
constantinople: createVm('constantinople'), |
||||||
|
petersburg: createVm('petersburg'), |
||||||
|
istanbul: createVm('istanbul'), |
||||||
|
*/ |
||||||
|
berlin: this.createVm('berlin') |
||||||
|
} |
||||||
|
this.blocks = {} |
||||||
|
this.latestBlockNumber = 0 |
||||||
|
this.txs = {} |
||||||
|
this.exeResults = {} |
||||||
|
this.logsManager = new execution.LogsManager() |
||||||
|
} |
||||||
|
|
||||||
|
createVm (hardfork) { |
||||||
|
const stateManager = new StateManagerCommonStorageDump() |
||||||
|
const common = new Common({ chain: 'mainnet', hardfork }) |
||||||
|
const vm = new VM({ |
||||||
|
common, |
||||||
|
activatePrecompiles: true, |
||||||
|
stateManager: stateManager |
||||||
|
}) |
||||||
|
|
||||||
|
const web3vm = new remixLibVm.Web3VMProvider() |
||||||
|
web3vm.setVM(vm) |
||||||
|
return { vm, web3vm, stateManager, common } |
||||||
|
} |
||||||
|
|
||||||
|
web3 () { |
||||||
|
return this.vms[this.currentFork].web3vm |
||||||
|
} |
||||||
|
|
||||||
|
blankWeb3 () { |
||||||
|
return new Web3() |
||||||
|
} |
||||||
|
|
||||||
|
vm () { |
||||||
|
return this.vms[this.currentFork].vm |
||||||
|
} |
||||||
|
|
||||||
|
vmObject () { |
||||||
|
return this.vms[this.currentFork] |
||||||
|
} |
||||||
|
|
||||||
|
addBlock (block) { |
||||||
|
let blockNumber = '0x' + block.header.number.toString('hex') |
||||||
|
if (blockNumber === '0x') { |
||||||
|
blockNumber = '0x0' |
||||||
|
} |
||||||
|
|
||||||
|
this.blocks['0x' + block.hash().toString('hex')] = block |
||||||
|
this.blocks[blockNumber] = block |
||||||
|
this.latestBlockNumber = blockNumber |
||||||
|
|
||||||
|
this.logsManager.checkBlock(blockNumber, block, this.web3()) |
||||||
|
} |
||||||
|
|
||||||
|
trackTx (tx, block) { |
||||||
|
this.txs[tx] = block |
||||||
|
} |
||||||
|
|
||||||
|
trackExecResult (tx, execReult) { |
||||||
|
this.exeResults[tx] = execReult |
||||||
|
} |
||||||
|
} |
@ -1,16 +1,15 @@ |
|||||||
{ |
{ |
||||||
"extends": "./tsconfig.json", |
"extends": "./tsconfig.json", |
||||||
"compilerOptions": { |
"compilerOptions": { |
||||||
"module": "commonjs", |
"module": "commonjs", |
||||||
"outDir": "../../dist/out-tsc", |
"outDir": "../../dist/out-tsc", |
||||||
"declaration": true, |
"declaration": true, |
||||||
"rootDir": "./", |
"rootDir": "./", |
||||||
"types": ["node"] |
"types": ["node"] |
||||||
}, |
}, |
||||||
"exclude": [ |
"exclude": [ |
||||||
"**/*.spec.ts", |
"**/*.spec.ts", |
||||||
"tests/" |
"tests/" |
||||||
], |
], |
||||||
"include": ["**/*.ts"] |
"include": ["**/*.ts"] |
||||||
} |
} |
||||||
|
|
@ -1,16 +1,15 @@ |
|||||||
{ |
{ |
||||||
"extends": "./tsconfig.json", |
"extends": "./tsconfig.json", |
||||||
"compilerOptions": { |
"compilerOptions": { |
||||||
"outDir": "../../dist/out-tsc", |
"outDir": "../../dist/out-tsc", |
||||||
"module": "commonjs", |
"module": "commonjs", |
||||||
"types": ["jest", "node"] |
"types": ["jest", "node"] |
||||||
}, |
}, |
||||||
"include": [ |
"include": [ |
||||||
"**/*.spec.ts", |
"**/*.spec.ts", |
||||||
"**/*.spec.tsx", |
"**/*.spec.tsx", |
||||||
"**/*.spec.js", |
"**/*.spec.js", |
||||||
"**/*.spec.jsx", |
"**/*.spec.jsx", |
||||||
"**/*.d.ts" |
"**/*.d.ts" |
||||||
] |
] |
||||||
} |
} |
||||||
|
|
||||||
|
@ -0,0 +1,4 @@ |
|||||||
|
{ |
||||||
|
"presets": ["@nrwl/react/babel"], |
||||||
|
"plugins": [] |
||||||
|
} |
@ -0,0 +1,19 @@ |
|||||||
|
{ |
||||||
|
"env": { |
||||||
|
"browser": true, |
||||||
|
"es6": true |
||||||
|
}, |
||||||
|
"extends": "../../../.eslintrc", |
||||||
|
"globals": { |
||||||
|
"Atomics": "readonly", |
||||||
|
"SharedArrayBuffer": "readonly" |
||||||
|
}, |
||||||
|
"parserOptions": { |
||||||
|
"ecmaVersion": 11, |
||||||
|
"sourceType": "module" |
||||||
|
}, |
||||||
|
"rules": { |
||||||
|
"no-unused-vars": "off", |
||||||
|
"@typescript-eslint/no-unused-vars": "error" |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,7 @@ |
|||||||
|
# remix-ui-checkbox |
||||||
|
|
||||||
|
This library was generated with [Nx](https://nx.dev). |
||||||
|
|
||||||
|
## Running unit tests |
||||||
|
|
||||||
|
Run `nx test remix-ui-checkbox` to execute the unit tests via [Jest](https://jestjs.io). |
@ -0,0 +1 @@ |
|||||||
|
export * from './lib/remix-ui-checkbox' |
@ -0,0 +1,47 @@ |
|||||||
|
import React from 'react' //eslint-disable-line
|
||||||
|
import './remix-ui-checkbox.css' |
||||||
|
|
||||||
|
/* eslint-disable-next-line */ |
||||||
|
export interface RemixUiCheckboxProps { |
||||||
|
onClick?: (event) => void |
||||||
|
onChange?: (event) => void |
||||||
|
label?: string |
||||||
|
inputType?: string |
||||||
|
name?: string |
||||||
|
checked?: boolean |
||||||
|
id?: string |
||||||
|
itemName?: string |
||||||
|
categoryId?: string |
||||||
|
} |
||||||
|
|
||||||
|
export const RemixUiCheckbox = ({ |
||||||
|
id, |
||||||
|
label, |
||||||
|
onClick, |
||||||
|
inputType, |
||||||
|
name, |
||||||
|
checked, |
||||||
|
onChange, |
||||||
|
itemName, |
||||||
|
categoryId |
||||||
|
}: RemixUiCheckboxProps) => { |
||||||
|
return ( |
||||||
|
<div className="listenOnNetwork_2A0YE0 custom-control custom-checkbox" style={{ display: 'flex', alignItems: 'center' }} onClick={onClick}> |
||||||
|
<input |
||||||
|
id={id} |
||||||
|
type={inputType} |
||||||
|
onChange={onChange} |
||||||
|
style={{ verticalAlign: 'bottom' }} |
||||||
|
name={name} |
||||||
|
className="custom-control-input" |
||||||
|
checked={checked} |
||||||
|
/> |
||||||
|
<label className="form-check-label custom-control-label" id={`heading${categoryId}`} style={{ paddingTop: '0.15rem' }}> |
||||||
|
{name ? <div className="font-weight-bold">{itemName}</div> : ''} |
||||||
|
{label} |
||||||
|
</label> |
||||||
|
</div> |
||||||
|
) |
||||||
|
} |
||||||
|
|
||||||
|
export default RemixUiCheckbox |
@ -0,0 +1,16 @@ |
|||||||
|
{ |
||||||
|
"extends": "../../../tsconfig.json", |
||||||
|
"compilerOptions": { |
||||||
|
"jsx": "react", |
||||||
|
"allowJs": true, |
||||||
|
"esModuleInterop": true, |
||||||
|
"allowSyntheticDefaultImports": true |
||||||
|
}, |
||||||
|
"files": [], |
||||||
|
"include": [], |
||||||
|
"references": [ |
||||||
|
{ |
||||||
|
"path": "./tsconfig.lib.json" |
||||||
|
} |
||||||
|
] |
||||||
|
} |
@ -0,0 +1,13 @@ |
|||||||
|
{ |
||||||
|
"extends": "./tsconfig.json", |
||||||
|
"compilerOptions": { |
||||||
|
"outDir": "../../../dist/out-tsc", |
||||||
|
"types": ["node"] |
||||||
|
}, |
||||||
|
"files": [ |
||||||
|
"../../../node_modules/@nrwl/react/typings/cssmodule.d.ts", |
||||||
|
"../../../node_modules/@nrwl/react/typings/image.d.ts" |
||||||
|
], |
||||||
|
"exclude": ["**/*.spec.ts", "**/*.spec.tsx"], |
||||||
|
"include": ["**/*.js", "**/*.jsx", "**/*.ts", "**/*.tsx"] |
||||||
|
} |
@ -0,0 +1,293 @@ |
|||||||
|
import React from 'react' |
||||||
|
import { File } from '../types' |
||||||
|
import { extractNameFromKey, extractParentFromKey } from '../utils' |
||||||
|
|
||||||
|
export const fetchDirectoryError = (error: any) => { |
||||||
|
return { |
||||||
|
type: 'FETCH_DIRECTORY_ERROR', |
||||||
|
payload: error |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
export const fetchDirectoryRequest = (promise: Promise<any>) => { |
||||||
|
return { |
||||||
|
type: 'FETCH_DIRECTORY_REQUEST', |
||||||
|
payload: promise |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
export const fetchDirectorySuccess = (path: string, files: File[]) => { |
||||||
|
return { |
||||||
|
type: 'FETCH_DIRECTORY_SUCCESS', |
||||||
|
payload: { path, files } |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
export const fileSystemReset = () => { |
||||||
|
return { |
||||||
|
type: 'FILESYSTEM_RESET' |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
const normalize = (parent, filesList, newInputType?: string): any => { |
||||||
|
const folders = {} |
||||||
|
const files = {} |
||||||
|
|
||||||
|
Object.keys(filesList || {}).forEach(key => { |
||||||
|
key = key.replace(/^\/|\/$/g, '') // remove first and last slash
|
||||||
|
let path = key |
||||||
|
path = path.replace(/^\/|\/$/g, '') // remove first and last slash
|
||||||
|
|
||||||
|
if (filesList[key].isDirectory) { |
||||||
|
folders[extractNameFromKey(key)] = { |
||||||
|
path, |
||||||
|
name: extractNameFromKey(path), |
||||||
|
isDirectory: filesList[key].isDirectory |
||||||
|
} |
||||||
|
} else { |
||||||
|
files[extractNameFromKey(key)] = { |
||||||
|
path, |
||||||
|
name: extractNameFromKey(path), |
||||||
|
isDirectory: filesList[key].isDirectory |
||||||
|
} |
||||||
|
} |
||||||
|
}) |
||||||
|
|
||||||
|
if (newInputType === 'folder') { |
||||||
|
const path = parent + '/blank' |
||||||
|
|
||||||
|
folders[path] = { |
||||||
|
path: path, |
||||||
|
name: '', |
||||||
|
isDirectory: true |
||||||
|
} |
||||||
|
} else if (newInputType === 'file') { |
||||||
|
const path = parent + '/blank' |
||||||
|
|
||||||
|
files[path] = { |
||||||
|
path: path, |
||||||
|
name: '', |
||||||
|
isDirectory: false |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
return Object.assign({}, folders, files) |
||||||
|
} |
||||||
|
|
||||||
|
const fetchDirectoryContent = async (provider, folderPath: string, newInputType?: string): Promise<any> => { |
||||||
|
return new Promise((resolve) => { |
||||||
|
provider.resolveDirectory(folderPath, (error, fileTree) => { |
||||||
|
if (error) console.error(error) |
||||||
|
const files = normalize(folderPath, fileTree, newInputType) |
||||||
|
|
||||||
|
resolve({ [extractNameFromKey(folderPath)]: files }) |
||||||
|
}) |
||||||
|
}) |
||||||
|
} |
||||||
|
|
||||||
|
export const fetchDirectory = (provider, path: string) => (dispatch: React.Dispatch<any>) => { |
||||||
|
const promise = fetchDirectoryContent(provider, path) |
||||||
|
|
||||||
|
dispatch(fetchDirectoryRequest(promise)) |
||||||
|
promise.then((files) => { |
||||||
|
dispatch(fetchDirectorySuccess(path, files)) |
||||||
|
}).catch((error) => { |
||||||
|
dispatch(fetchDirectoryError({ error })) |
||||||
|
}) |
||||||
|
return promise |
||||||
|
} |
||||||
|
|
||||||
|
export const resolveDirectoryError = (error: any) => { |
||||||
|
return { |
||||||
|
type: 'RESOLVE_DIRECTORY_ERROR', |
||||||
|
payload: error |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
export const resolveDirectoryRequest = (promise: Promise<any>) => { |
||||||
|
return { |
||||||
|
type: 'RESOLVE_DIRECTORY_REQUEST', |
||||||
|
payload: promise |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
export const resolveDirectorySuccess = (path: string, files: File[]) => { |
||||||
|
return { |
||||||
|
type: 'RESOLVE_DIRECTORY_SUCCESS', |
||||||
|
payload: { path, files } |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
export const resolveDirectory = (provider, path: string) => (dispatch: React.Dispatch<any>) => { |
||||||
|
const promise = fetchDirectoryContent(provider, path) |
||||||
|
|
||||||
|
dispatch(resolveDirectoryRequest(promise)) |
||||||
|
promise.then((files) => { |
||||||
|
dispatch(resolveDirectorySuccess(path, files)) |
||||||
|
}).catch((error) => { |
||||||
|
dispatch(resolveDirectoryError({ error })) |
||||||
|
}) |
||||||
|
return promise |
||||||
|
} |
||||||
|
|
||||||
|
export const fetchProviderError = (error: any) => { |
||||||
|
return { |
||||||
|
type: 'FETCH_PROVIDER_ERROR', |
||||||
|
payload: error |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
export const fetchProviderRequest = (promise: Promise<any>) => { |
||||||
|
return { |
||||||
|
type: 'FETCH_PROVIDER_REQUEST', |
||||||
|
payload: promise |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
export const fetchProviderSuccess = (provider: any) => { |
||||||
|
return { |
||||||
|
type: 'FETCH_PROVIDER_SUCCESS', |
||||||
|
payload: provider |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
export const fileAddedSuccess = (path: string, files) => { |
||||||
|
return { |
||||||
|
type: 'FILE_ADDED', |
||||||
|
payload: { path, files } |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
export const folderAddedSuccess = (path: string, files) => { |
||||||
|
return { |
||||||
|
type: 'FOLDER_ADDED', |
||||||
|
payload: { path, files } |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
export const fileRemovedSuccess = (path: string, removePath: string) => { |
||||||
|
return { |
||||||
|
type: 'FILE_REMOVED', |
||||||
|
payload: { path, removePath } |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
export const fileRenamedSuccess = (path: string, removePath: string, files) => { |
||||||
|
return { |
||||||
|
type: 'FILE_RENAMED', |
||||||
|
payload: { path, removePath, files } |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
export const init = (provider, workspaceName: string, plugin, registry) => (dispatch: React.Dispatch<any>) => { |
||||||
|
if (provider) { |
||||||
|
provider.event.on('fileAdded', async (filePath) => { |
||||||
|
if (extractParentFromKey(filePath) === '/.workspaces') return |
||||||
|
const path = extractParentFromKey(filePath) || provider.workspace || provider.type || '' |
||||||
|
const data = await fetchDirectoryContent(provider, path) |
||||||
|
|
||||||
|
dispatch(fileAddedSuccess(path, data)) |
||||||
|
if (filePath.includes('_test.sol')) { |
||||||
|
plugin.emit('newTestFileCreated', filePath) |
||||||
|
} |
||||||
|
}) |
||||||
|
provider.event.on('folderAdded', async (folderPath) => { |
||||||
|
if (extractParentFromKey(folderPath) === '/.workspaces') return |
||||||
|
const path = extractParentFromKey(folderPath) || provider.workspace || provider.type || '' |
||||||
|
const data = await fetchDirectoryContent(provider, path) |
||||||
|
|
||||||
|
dispatch(folderAddedSuccess(path, data)) |
||||||
|
}) |
||||||
|
provider.event.on('fileRemoved', async (removePath) => { |
||||||
|
const path = extractParentFromKey(removePath) || provider.workspace || provider.type || '' |
||||||
|
|
||||||
|
dispatch(fileRemovedSuccess(path, removePath)) |
||||||
|
}) |
||||||
|
provider.event.on('fileRenamed', async (oldPath) => { |
||||||
|
const path = extractParentFromKey(oldPath) || provider.workspace || provider.type || '' |
||||||
|
const data = await fetchDirectoryContent(provider, path) |
||||||
|
|
||||||
|
dispatch(fileRenamedSuccess(path, oldPath, data)) |
||||||
|
}) |
||||||
|
provider.event.on('fileExternallyChanged', async (path: string, file: { content: string }) => { |
||||||
|
const config = registry.get('config').api |
||||||
|
const editor = registry.get('editor').api |
||||||
|
|
||||||
|
if (config.get('currentFile') === path && editor.currentContent() !== file.content) { |
||||||
|
if (provider.isReadOnly(path)) return editor.setText(file.content) |
||||||
|
dispatch(displayNotification( |
||||||
|
path + ' changed', |
||||||
|
'This file has been changed outside of Remix IDE.', |
||||||
|
'Replace by the new content', 'Keep the content displayed in Remix', |
||||||
|
() => { |
||||||
|
editor.setText(file.content) |
||||||
|
} |
||||||
|
)) |
||||||
|
} |
||||||
|
}) |
||||||
|
provider.event.on('fileRenamedError', async () => { |
||||||
|
dispatch(displayNotification('File Renamed Failed', '', 'Ok', 'Cancel')) |
||||||
|
}) |
||||||
|
provider.event.on('rootFolderChanged', async () => { |
||||||
|
workspaceName = provider.workspace || provider.type || '' |
||||||
|
fetchDirectory(provider, workspaceName)(dispatch) |
||||||
|
}) |
||||||
|
dispatch(fetchProviderSuccess(provider)) |
||||||
|
dispatch(setCurrentWorkspace(workspaceName)) |
||||||
|
} else { |
||||||
|
dispatch(fetchProviderError('No provider available')) |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
export const setCurrentWorkspace = (name: string) => { |
||||||
|
return { |
||||||
|
type: 'SET_CURRENT_WORKSPACE', |
||||||
|
payload: name |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
export const addInputFieldSuccess = (path: string, files: File[]) => { |
||||||
|
return { |
||||||
|
type: 'ADD_INPUT_FIELD', |
||||||
|
payload: { path, files } |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
export const addInputField = (provider, type: string, path: string) => (dispatch: React.Dispatch<any>) => { |
||||||
|
const promise = fetchDirectoryContent(provider, path, type) |
||||||
|
|
||||||
|
promise.then((files) => { |
||||||
|
dispatch(addInputFieldSuccess(path, files)) |
||||||
|
}).catch((error) => { |
||||||
|
console.error(error) |
||||||
|
}) |
||||||
|
return promise |
||||||
|
} |
||||||
|
|
||||||
|
export const removeInputFieldSuccess = (path: string) => { |
||||||
|
return { |
||||||
|
type: 'REMOVE_INPUT_FIELD', |
||||||
|
payload: { path } |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
export const removeInputField = (path: string) => (dispatch: React.Dispatch<any>) => { |
||||||
|
return dispatch(removeInputFieldSuccess(path)) |
||||||
|
} |
||||||
|
|
||||||
|
export const displayNotification = (title: string, message: string, labelOk: string, labelCancel: string, actionOk?: (...args) => void, actionCancel?: (...args) => void) => { |
||||||
|
return { |
||||||
|
type: 'DISPLAY_NOTIFICATION', |
||||||
|
payload: { title, message, labelOk, labelCancel, actionOk, actionCancel } |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
export const hideNotification = () => { |
||||||
|
return { |
||||||
|
type: 'DISPLAY_NOTIFICATION' |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
export const closeNotificationModal = () => (dispatch: React.Dispatch<any>) => { |
||||||
|
dispatch(hideNotification()) |
||||||
|
} |
@ -0,0 +1,344 @@ |
|||||||
|
import * as _ from 'lodash' |
||||||
|
import { extractNameFromKey } from '../utils' |
||||||
|
interface Action { |
||||||
|
type: string; |
||||||
|
payload: Record<string, any>; |
||||||
|
} |
||||||
|
|
||||||
|
export const fileSystemInitialState = { |
||||||
|
files: { |
||||||
|
files: [], |
||||||
|
expandPath: [], |
||||||
|
workspaceName: null, |
||||||
|
blankPath: null, |
||||||
|
isRequesting: false, |
||||||
|
isSuccessful: false, |
||||||
|
error: null |
||||||
|
}, |
||||||
|
provider: { |
||||||
|
provider: null, |
||||||
|
isRequesting: false, |
||||||
|
isSuccessful: false, |
||||||
|
error: null |
||||||
|
}, |
||||||
|
notification: { |
||||||
|
title: null, |
||||||
|
message: null, |
||||||
|
actionOk: () => {}, |
||||||
|
actionCancel: () => {}, |
||||||
|
labelOk: null, |
||||||
|
labelCancel: null |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
export const fileSystemReducer = (state = fileSystemInitialState, action: Action) => { |
||||||
|
switch (action.type) { |
||||||
|
case 'FETCH_DIRECTORY_REQUEST': { |
||||||
|
return { |
||||||
|
...state, |
||||||
|
files: { |
||||||
|
...state.files, |
||||||
|
isRequesting: true, |
||||||
|
isSuccessful: false, |
||||||
|
error: null |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
case 'FETCH_DIRECTORY_SUCCESS': { |
||||||
|
return { |
||||||
|
...state, |
||||||
|
files: { |
||||||
|
...state.files, |
||||||
|
files: action.payload.files, |
||||||
|
isRequesting: false, |
||||||
|
isSuccessful: true, |
||||||
|
error: null |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
case 'FETCH_DIRECTORY_ERROR': { |
||||||
|
return { |
||||||
|
...state, |
||||||
|
files: { |
||||||
|
...state.files, |
||||||
|
isRequesting: false, |
||||||
|
isSuccessful: false, |
||||||
|
error: action.payload |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
case 'RESOLVE_DIRECTORY_REQUEST': { |
||||||
|
return { |
||||||
|
...state, |
||||||
|
files: { |
||||||
|
...state.files, |
||||||
|
isRequesting: true, |
||||||
|
isSuccessful: false, |
||||||
|
error: null |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
case 'RESOLVE_DIRECTORY_SUCCESS': { |
||||||
|
return { |
||||||
|
...state, |
||||||
|
files: { |
||||||
|
...state.files, |
||||||
|
files: resolveDirectory(state.files.workspaceName, action.payload.path, state.files.files, action.payload.files), |
||||||
|
isRequesting: false, |
||||||
|
isSuccessful: true, |
||||||
|
error: null |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
case 'RESOLVE_DIRECTORY_ERROR': { |
||||||
|
return { |
||||||
|
...state, |
||||||
|
files: { |
||||||
|
...state.files, |
||||||
|
isRequesting: false, |
||||||
|
isSuccessful: false, |
||||||
|
error: action.payload |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
case 'FETCH_PROVIDER_REQUEST': { |
||||||
|
return { |
||||||
|
...state, |
||||||
|
provider: { |
||||||
|
...state.provider, |
||||||
|
isRequesting: true, |
||||||
|
isSuccessful: false, |
||||||
|
error: null |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
case 'FETCH_PROVIDER_SUCCESS': { |
||||||
|
return { |
||||||
|
...state, |
||||||
|
provider: { |
||||||
|
...state.provider, |
||||||
|
provider: action.payload, |
||||||
|
isRequesting: false, |
||||||
|
isSuccessful: true, |
||||||
|
error: null |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
case 'FETCH_PROVIDER_ERROR': { |
||||||
|
return { |
||||||
|
...state, |
||||||
|
provider: { |
||||||
|
...state.provider, |
||||||
|
isRequesting: false, |
||||||
|
isSuccessful: false, |
||||||
|
error: action.payload |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
case 'SET_CURRENT_WORKSPACE': { |
||||||
|
return { |
||||||
|
...state, |
||||||
|
files: { |
||||||
|
...state.files, |
||||||
|
workspaceName: action.payload |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
case 'ADD_INPUT_FIELD': { |
||||||
|
return { |
||||||
|
...state, |
||||||
|
files: { |
||||||
|
...state.files, |
||||||
|
files: addInputField(state.files.workspaceName, action.payload.path, state.files.files, action.payload.files), |
||||||
|
blankPath: action.payload.path, |
||||||
|
isRequesting: false, |
||||||
|
isSuccessful: true, |
||||||
|
error: null |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
case 'REMOVE_INPUT_FIELD': { |
||||||
|
return { |
||||||
|
...state, |
||||||
|
files: { |
||||||
|
...state.files, |
||||||
|
files: removeInputField(state.files.workspaceName, state.files.blankPath, state.files.files), |
||||||
|
blankPath: null, |
||||||
|
isRequesting: false, |
||||||
|
isSuccessful: true, |
||||||
|
error: null |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
case 'FILE_ADDED': { |
||||||
|
return { |
||||||
|
...state, |
||||||
|
files: { |
||||||
|
...state.files, |
||||||
|
files: fileAdded(state.files.workspaceName, action.payload.path, state.files.files, action.payload.files), |
||||||
|
expandPath: [...new Set([...state.files.expandPath, action.payload.path])], |
||||||
|
isRequesting: false, |
||||||
|
isSuccessful: true, |
||||||
|
error: null |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
case 'FOLDER_ADDED': { |
||||||
|
return { |
||||||
|
...state, |
||||||
|
files: { |
||||||
|
...state.files, |
||||||
|
files: folderAdded(state.files.workspaceName, action.payload.path, state.files.files, action.payload.files), |
||||||
|
expandPath: [...new Set([...state.files.expandPath, action.payload.path])], |
||||||
|
isRequesting: false, |
||||||
|
isSuccessful: true, |
||||||
|
error: null |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
case 'FILE_REMOVED': { |
||||||
|
return { |
||||||
|
...state, |
||||||
|
files: { |
||||||
|
...state.files, |
||||||
|
files: fileRemoved(state.files.workspaceName, action.payload.path, action.payload.removePath, state.files.files), |
||||||
|
isRequesting: false, |
||||||
|
isSuccessful: true, |
||||||
|
error: null |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
case 'FILE_RENAMED': { |
||||||
|
return { |
||||||
|
...state, |
||||||
|
files: { |
||||||
|
...state.files, |
||||||
|
files: fileRenamed(state.files.workspaceName, action.payload.path, action.payload.removePath, state.files.files, action.payload.files), |
||||||
|
isRequesting: false, |
||||||
|
isSuccessful: true, |
||||||
|
error: null |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
case 'DISPLAY_NOTIFICATION': { |
||||||
|
return { |
||||||
|
...state, |
||||||
|
notification: { |
||||||
|
title: action.payload.title, |
||||||
|
message: action.payload.message, |
||||||
|
actionOk: action.payload.actionOk || fileSystemInitialState.notification.actionOk, |
||||||
|
actionCancel: action.payload.actionCancel || fileSystemInitialState.notification.actionCancel, |
||||||
|
labelOk: action.payload.labelOk, |
||||||
|
labelCancel: action.payload.labelCancel |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
case 'HIDE_NOTIFICATION': { |
||||||
|
return { |
||||||
|
...state, |
||||||
|
notification: fileSystemInitialState.notification |
||||||
|
} |
||||||
|
} |
||||||
|
default: |
||||||
|
throw new Error() |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
const resolveDirectory = (root, path: string, files, content) => { |
||||||
|
if (path === root) return { [root]: { ...content[root], ...files[root] } } |
||||||
|
const pathArr: string[] = path.split('/').filter(value => value) |
||||||
|
|
||||||
|
if (pathArr[0] !== root) pathArr.unshift(root) |
||||||
|
const _path = pathArr.map((key, index) => index > 1 ? ['child', key] : key).reduce((acc: string[], cur) => { |
||||||
|
return Array.isArray(cur) ? [...acc, ...cur] : [...acc, cur] |
||||||
|
}, []) |
||||||
|
|
||||||
|
const prevFiles = _.get(files, _path) |
||||||
|
|
||||||
|
files = _.set(files, _path, { |
||||||
|
isDirectory: true, |
||||||
|
path, |
||||||
|
name: extractNameFromKey(path), |
||||||
|
child: { ...content[pathArr[pathArr.length - 1]], ...(prevFiles ? prevFiles.child : {}) } |
||||||
|
}) |
||||||
|
|
||||||
|
return files |
||||||
|
} |
||||||
|
|
||||||
|
const removePath = (root, path: string, pathName, files) => { |
||||||
|
const pathArr: string[] = path.split('/').filter(value => value) |
||||||
|
|
||||||
|
if (pathArr[0] !== root) pathArr.unshift(root) |
||||||
|
const _path = pathArr.map((key, index) => index > 1 ? ['child', key] : key).reduce((acc: string[], cur) => { |
||||||
|
return Array.isArray(cur) ? [...acc, ...cur] : [...acc, cur] |
||||||
|
}, []) |
||||||
|
const prevFiles = _.get(files, _path) |
||||||
|
|
||||||
|
prevFiles && prevFiles.child && prevFiles.child[pathName] && delete prevFiles.child[pathName] |
||||||
|
files = _.set(files, _path, { |
||||||
|
isDirectory: true, |
||||||
|
path, |
||||||
|
name: extractNameFromKey(path), |
||||||
|
child: prevFiles ? prevFiles.child : {} |
||||||
|
}) |
||||||
|
|
||||||
|
return files |
||||||
|
} |
||||||
|
|
||||||
|
const addInputField = (root, path: string, files, content) => { |
||||||
|
if (path === root) return { [root]: { ...content[root], ...files[root] } } |
||||||
|
const result = resolveDirectory(root, path, files, content) |
||||||
|
|
||||||
|
return result |
||||||
|
} |
||||||
|
|
||||||
|
const removeInputField = (root, path: string, files) => { |
||||||
|
if (path === root) { |
||||||
|
delete files[root][path + '/' + 'blank'] |
||||||
|
return files |
||||||
|
} |
||||||
|
return removePath(root, path, path + '/' + 'blank', files) |
||||||
|
} |
||||||
|
|
||||||
|
const fileAdded = (root, path: string, files, content) => { |
||||||
|
return resolveDirectory(root, path, files, content) |
||||||
|
} |
||||||
|
|
||||||
|
const folderAdded = (root, path: string, files, content) => { |
||||||
|
return resolveDirectory(root, path, files, content) |
||||||
|
} |
||||||
|
|
||||||
|
const fileRemoved = (root, path: string, removedPath: string, files) => { |
||||||
|
if (path === root) { |
||||||
|
delete files[root][removedPath] |
||||||
|
|
||||||
|
return files |
||||||
|
} |
||||||
|
return removePath(root, path, extractNameFromKey(removedPath), files) |
||||||
|
} |
||||||
|
|
||||||
|
const fileRenamed = (root, path: string, removePath: string, files, content) => { |
||||||
|
if (path === root) { |
||||||
|
const allFiles = { [root]: { ...content[root], ...files[root] } } |
||||||
|
|
||||||
|
delete allFiles[root][extractNameFromKey(removePath) || removePath] |
||||||
|
return allFiles |
||||||
|
} |
||||||
|
const pathArr: string[] = path.split('/').filter(value => value) |
||||||
|
|
||||||
|
if (pathArr[0] !== root) pathArr.unshift(root) |
||||||
|
const _path = pathArr.map((key, index) => index > 1 ? ['child', key] : key).reduce((acc: string[], cur) => { |
||||||
|
return Array.isArray(cur) ? [...acc, ...cur] : [...acc, cur] |
||||||
|
}, []) |
||||||
|
const prevFiles = _.get(files, _path) |
||||||
|
|
||||||
|
delete prevFiles.child[extractNameFromKey(removePath)] |
||||||
|
files = _.set(files, _path, { |
||||||
|
isDirectory: true, |
||||||
|
path, |
||||||
|
name: extractNameFromKey(path), |
||||||
|
child: { ...content[pathArr[pathArr.length - 1]], ...prevFiles.child } |
||||||
|
}) |
||||||
|
|
||||||
|
return files |
||||||
|
} |
@ -0,0 +1,13 @@ |
|||||||
|
export const extractNameFromKey = (key: string): string => { |
||||||
|
const keyPath = key.split('/') |
||||||
|
|
||||||
|
return keyPath[keyPath.length - 1] |
||||||
|
} |
||||||
|
|
||||||
|
export const extractParentFromKey = (key: string):string => { |
||||||
|
if (!key) return |
||||||
|
const keyPath = key.split('/') |
||||||
|
keyPath.pop() |
||||||
|
|
||||||
|
return keyPath.join('/') |
||||||
|
} |
@ -0,0 +1,4 @@ |
|||||||
|
{ |
||||||
|
"presets": ["@nrwl/react/babel"], |
||||||
|
"plugins": [] |
||||||
|
} |
@ -0,0 +1,19 @@ |
|||||||
|
{ |
||||||
|
"env": { |
||||||
|
"browser": true, |
||||||
|
"es6": true |
||||||
|
}, |
||||||
|
"extends": "../../../.eslintrc", |
||||||
|
"globals": { |
||||||
|
"Atomics": "readonly", |
||||||
|
"SharedArrayBuffer": "readonly" |
||||||
|
}, |
||||||
|
"parserOptions": { |
||||||
|
"ecmaVersion": 11, |
||||||
|
"sourceType": "module" |
||||||
|
}, |
||||||
|
"rules": { |
||||||
|
"no-unused-vars": "off", |
||||||
|
"@typescript-eslint/no-unused-vars": "error" |
||||||
|
} |
||||||
|
} |
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in new issue