commit
592bcc649b
@ -0,0 +1,399 @@ |
||||
'use strict' |
||||
|
||||
import { |
||||
Plugin |
||||
} from '@remixproject/engine' |
||||
import git from 'isomorphic-git' |
||||
import IpfsHttpClient from 'ipfs-http-client' |
||||
import { |
||||
saveAs |
||||
} from 'file-saver' |
||||
|
||||
const JSZip = require('jszip') |
||||
const path = require('path') |
||||
const FormData = require('form-data') |
||||
const axios = require('axios') |
||||
|
||||
const profile = { |
||||
name: 'dGitProvider', |
||||
displayName: 'Decentralized git', |
||||
description: '', |
||||
icon: 'assets/img/fileManager.webp', |
||||
version: '0.0.1', |
||||
methods: ['init', 'status', 'log', 'commit', 'add', 'remove', 'rm', 'lsfiles', 'readblob', 'resolveref', 'branches', 'branch', 'checkout', 'currentbranch', 'push', 'pin', 'pull', 'pinList', 'unPin', 'setIpfsConfig', 'zip', 'setItem', 'getItem'], |
||||
kind: 'file-system' |
||||
} |
||||
class DGitProvider extends Plugin { |
||||
constructor () { |
||||
super(profile) |
||||
this.ipfsconfig = { |
||||
host: 'ipfs.komputing.org', |
||||
port: 443, |
||||
protocol: 'https', |
||||
ipfsurl: 'https://ipfsgw.komputing.org/ipfs/' |
||||
} |
||||
this.globalIPFSConfig = { |
||||
host: 'ipfs.io', |
||||
port: 443, |
||||
protocol: 'https', |
||||
ipfsurl: 'https://ipfs.io/ipfs/' |
||||
} |
||||
} |
||||
|
||||
async getGitConfig () { |
||||
const workspace = await this.call('filePanel', 'getCurrentWorkspace') |
||||
return { |
||||
fs: window.remixFileSystem, |
||||
dir: workspace.absolutePath |
||||
} |
||||
} |
||||
|
||||
async init () { |
||||
await git.init({ |
||||
...await this.getGitConfig(), |
||||
defaultBranch: 'main' |
||||
}) |
||||
} |
||||
|
||||
async status (cmd) { |
||||
const status = await git.statusMatrix({ |
||||
...await this.getGitConfig(), |
||||
...cmd |
||||
}) |
||||
return status |
||||
} |
||||
|
||||
async add (cmd) { |
||||
await git.add({ |
||||
...await this.getGitConfig(), |
||||
...cmd |
||||
}) |
||||
this.call('fileManager', 'refresh') |
||||
} |
||||
|
||||
async rm (cmd) { |
||||
await git.remove({ |
||||
...await this.getGitConfig(), |
||||
...cmd |
||||
}) |
||||
this.call('fileManager', 'refresh') |
||||
} |
||||
|
||||
async checkout (cmd) { |
||||
await git.checkout({ |
||||
...await this.getGitConfig(), |
||||
...cmd |
||||
}) |
||||
this.call('fileManager', 'refresh') |
||||
} |
||||
|
||||
async log (cmd) { |
||||
const status = await git.log({ |
||||
...await this.getGitConfig(), |
||||
...cmd |
||||
}) |
||||
return status |
||||
} |
||||
|
||||
async branch (cmd) { |
||||
const status = await git.branch({ |
||||
...await this.getGitConfig(), |
||||
...cmd |
||||
}) |
||||
this.call('fileManager', 'refresh') |
||||
return status |
||||
} |
||||
|
||||
async currentbranch () { |
||||
const name = await git.currentBranch({ |
||||
...await this.getGitConfig() |
||||
}) |
||||
return name |
||||
} |
||||
|
||||
async branches () { |
||||
const branches = await git.listBranches({ |
||||
...await this.getGitConfig() |
||||
}) |
||||
return branches |
||||
} |
||||
|
||||
async commit (cmd) { |
||||
await this.init() |
||||
try { |
||||
const sha = await git.commit({ |
||||
...await this.getGitConfig(), |
||||
...cmd |
||||
}) |
||||
return sha |
||||
} catch (e) {} |
||||
} |
||||
|
||||
async lsfiles (cmd) { |
||||
const filesInStaging = await git.listFiles({ |
||||
...await this.getGitConfig(), |
||||
...cmd |
||||
}) |
||||
return filesInStaging |
||||
} |
||||
|
||||
async resolveref (cmd) { |
||||
const oid = await git.resolveRef({ |
||||
...await this.getGitConfig(), |
||||
...cmd |
||||
}) |
||||
return oid |
||||
} |
||||
|
||||
async readblob (cmd) { |
||||
const readBlobResult = await git.readBlob({ |
||||
...await this.getGitConfig(), |
||||
...cmd |
||||
}) |
||||
return readBlobResult |
||||
} |
||||
|
||||
async setIpfsConfig (config) { |
||||
this.ipfsconfig = config |
||||
return new Promise((resolve, reject) => { |
||||
resolve(this.checkIpfsConfig()) |
||||
}) |
||||
} |
||||
|
||||
async checkIpfsConfig (config) { |
||||
this.ipfs = IpfsHttpClient(config || this.ipfsconfig) |
||||
try { |
||||
await this.ipfs.config.getAll() |
||||
return true |
||||
} catch (e) { |
||||
return false |
||||
} |
||||
} |
||||
|
||||
async push () { |
||||
if (!this.checkIpfsConfig()) return false |
||||
const workspace = await this.call('filePanel', 'getCurrentWorkspace') |
||||
const files = await this.getDirectory('/') |
||||
this.filesToSend = [] |
||||
for (const file of files) { |
||||
const c = window.remixFileSystem.readFileSync(`${workspace.absolutePath}/${file}`) |
||||
const ob = { |
||||
path: file, |
||||
content: c |
||||
} |
||||
this.filesToSend.push(ob) |
||||
} |
||||
const addOptions = { |
||||
wrapWithDirectory: true |
||||
} |
||||
const r = await this.ipfs.add(this.filesToSend, addOptions) |
||||
return r.cid.string |
||||
} |
||||
|
||||
async pin (pinataApiKey, pinataSecretApiKey) { |
||||
const workspace = await this.call('filePanel', 'getCurrentWorkspace') |
||||
const files = await this.getDirectory('/') |
||||
this.filesToSend = [] |
||||
|
||||
const data = new FormData() |
||||
files.forEach(async (file) => { |
||||
const c = window.remixFileSystem.readFileSync(`${workspace.absolutePath}/${file}`) |
||||
data.append('file', new Blob([c]), `base/${file}`) |
||||
}) |
||||
// get last commit data
|
||||
let ob |
||||
try { |
||||
const commits = await this.log({ ref: 'HEAD' }) |
||||
ob = { |
||||
ref: commits[0].oid, |
||||
message: commits[0].commit.message |
||||
} |
||||
} catch (e) { |
||||
ob = { |
||||
ref: 'no commits', |
||||
message: 'no commits' |
||||
} |
||||
} |
||||
const today = new Date() |
||||
const metadata = JSON.stringify({ |
||||
name: `remix - ${workspace.name} - ${today.toLocaleString()}`, |
||||
keyvalues: ob |
||||
}) |
||||
const pinataOptions = JSON.stringify({ |
||||
wrapWithDirectory: false |
||||
}) |
||||
data.append('pinataOptions', pinataOptions) |
||||
data.append('pinataMetadata', metadata) |
||||
const url = 'https://api.pinata.cloud/pinning/pinFileToIPFS' |
||||
try { |
||||
const result = await axios |
||||
.post(url, data, { |
||||
maxBodyLength: 'Infinity', |
||||
headers: { |
||||
'Content-Type': `multipart/form-data; boundary=${data._boundary}`, |
||||
pinata_api_key: pinataApiKey, |
||||
pinata_secret_api_key: pinataSecretApiKey |
||||
} |
||||
}) |
||||
return result.data.IpfsHash |
||||
} catch (error) { |
||||
throw new Error(error) |
||||
} |
||||
} |
||||
|
||||
async pinList (pinataApiKey, pinataSecretApiKey) { |
||||
const url = 'https://api.pinata.cloud/data/pinList?status=pinned' |
||||
try { |
||||
const result = await axios |
||||
.get(url, { |
||||
maxBodyLength: 'Infinity', |
||||
headers: { |
||||
pinata_api_key: pinataApiKey, |
||||
pinata_secret_api_key: pinataSecretApiKey |
||||
} |
||||
}) |
||||
return result.data |
||||
} catch (error) { |
||||
throw new Error(error) |
||||
} |
||||
} |
||||
|
||||
async unPin (pinataApiKey, pinataSecretApiKey, hashToUnpin) { |
||||
const url = `https://api.pinata.cloud/pinning/unpin/${hashToUnpin}` |
||||
try { |
||||
await axios |
||||
.delete(url, { |
||||
headers: { |
||||
pinata_api_key: pinataApiKey, |
||||
pinata_secret_api_key: pinataSecretApiKey |
||||
} |
||||
}) |
||||
return true |
||||
} catch (error) { |
||||
throw new Error(error) |
||||
} |
||||
}; |
||||
|
||||
async pull (cmd) { |
||||
const permission = await this.askUserPermission('pull', 'Import multiple files into your workspaces.') |
||||
console.log(this.ipfsconfig) |
||||
if (!permission) return false |
||||
const cid = cmd.cid |
||||
if (!cmd.local) { |
||||
this.ipfs = IpfsHttpClient(this.globalIPFSConfig) |
||||
} else { |
||||
if (!this.checkIpfsConfig()) return false |
||||
} |
||||
await this.call('filePanel', 'createWorkspace', `workspace_${Date.now()}`, false) |
||||
const workspace = await this.call('filePanel', 'getCurrentWorkspace') |
||||
for await (const file of this.ipfs.get(cid)) { |
||||
file.path = file.path.replace(cid, '') |
||||
if (!file.content) { |
||||
continue |
||||
} |
||||
const content = [] |
||||
for await (const chunk of file.content) { |
||||
content.push(chunk) |
||||
} |
||||
const dir = path.dirname(file.path) |
||||
try { |
||||
this.createDirectories(`${workspace.absolutePath}/${dir}`) |
||||
} catch (e) {} |
||||
try { |
||||
window.remixFileSystem.writeFileSync(`${workspace.absolutePath}/${file.path}`, Buffer.concat(content) || new Uint8Array()) |
||||
} catch (e) {} |
||||
} |
||||
this.call('fileManager', 'refresh') |
||||
} |
||||
|
||||
async getItem (name) { |
||||
if (typeof window !== 'undefined') { |
||||
return window.localStorage.getItem(name) |
||||
} |
||||
} |
||||
|
||||
async setItem (name, content) { |
||||
try { |
||||
if (typeof window !== 'undefined') { |
||||
window.localStorage.setItem(name, content) |
||||
} |
||||
} catch (exception) { |
||||
return false |
||||
} |
||||
return true |
||||
} |
||||
|
||||
async zip () { |
||||
const zip = new JSZip() |
||||
const workspace = await this.call('filePanel', 'getCurrentWorkspace') |
||||
const files = await this.getDirectory('/') |
||||
this.filesToSend = [] |
||||
for (const file of files) { |
||||
const c = window.remixFileSystem.readFileSync(`${workspace.absolutePath}/${file}`) |
||||
zip.file(file, c) |
||||
} |
||||
await zip.generateAsync({ |
||||
type: 'blob' |
||||
}) |
||||
.then(function (content) { |
||||
saveAs(content, `${workspace.name}.zip`) |
||||
}) |
||||
} |
||||
|
||||
async createDirectories (strdirectories) { |
||||
const ignore = ['.', '/.', ''] |
||||
if (ignore.indexOf(strdirectories) > -1) return false |
||||
const directories = strdirectories.split('/') |
||||
for (let i = 0; i < directories.length; i++) { |
||||
let previouspath = '' |
||||
if (i > 0) previouspath = '/' + directories.slice(0, i).join('/') |
||||
const finalPath = previouspath + '/' + directories[i] |
||||
try { |
||||
window.remixFileSystem.mkdirSync(finalPath) |
||||
} catch (e) {} |
||||
} |
||||
} |
||||
|
||||
async getDirectory (dir) { |
||||
let result = [] |
||||
const files = await this.call('fileManager', 'readdir', dir) |
||||
const fileArray = normalize(files) |
||||
for (const fi of fileArray) { |
||||
if (fi) { |
||||
const type = fi.data.isDirectory |
||||
if (type === true) { |
||||
result = [ |
||||
...result, |
||||
...(await this.getDirectory( |
||||
`${fi.filename}` |
||||
)) |
||||
] |
||||
} else { |
||||
result = [...result, fi.filename] |
||||
} |
||||
} |
||||
} |
||||
return result |
||||
} |
||||
} |
||||
|
||||
const normalize = (filesList) => { |
||||
const folders = [] |
||||
const files = [] |
||||
Object.keys(filesList || {}).forEach(key => { |
||||
if (filesList[key].isDirectory) { |
||||
folders.push({ |
||||
filename: key, |
||||
data: filesList[key] |
||||
}) |
||||
} else { |
||||
files.push({ |
||||
filename: key, |
||||
data: filesList[key] |
||||
}) |
||||
} |
||||
}) |
||||
return [...folders, ...files] |
||||
} |
||||
|
||||
module.exports = DGitProvider |
@ -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) |
||||
} |
||||
} |
@ -0,0 +1,71 @@ |
||||
import * as packageJson from '../../../../../package.json' |
||||
import { Plugin } from '@remixproject/engine' |
||||
import Web3 from 'web3' |
||||
const yo = require('yo-yo') |
||||
const modalDialogCustom = require('../ui/modal-dialog-custom') |
||||
|
||||
const profile = { |
||||
name: 'hardhat-provider', |
||||
displayName: 'Hardhat Provider', |
||||
kind: 'provider', |
||||
description: 'Hardhat provider', |
||||
methods: ['sendAsync'], |
||||
version: packageJson.version |
||||
} |
||||
|
||||
export default class HardhatProvider extends Plugin { |
||||
constructor (blockchain) { |
||||
super(profile) |
||||
this.provider = null |
||||
this.blockchain = blockchain |
||||
} |
||||
|
||||
onDeactivation () { |
||||
this.provider = null |
||||
} |
||||
|
||||
hardhatProviderDialogBody () { |
||||
return yo` |
||||
<div class=""> |
||||
Note: To run Hardhat network node on your system, go to hardhat project folder and run command: |
||||
<div class="border p-1">npx hardhat node</div> |
||||
<br> |
||||
For more info, visit: <a href="https://hardhat.org/getting-started/#connecting-a-wallet-or-dapp-to-hardhat-network" target="_blank">Hardhat Documentation</a> |
||||
<br><br> |
||||
Hardhat JSON-RPC Endpoint |
||||
</div> |
||||
` |
||||
} |
||||
|
||||
sendAsync (data) { |
||||
return new Promise((resolve, reject) => { |
||||
if (!this.provider) { |
||||
modalDialogCustom.prompt('Hardhat node request', this.hardhatProviderDialogBody(), 'http://127.0.0.1:8545', (target) => { |
||||
this.provider = new Web3.providers.HttpProvider(target) |
||||
this.sendAsyncInternal(data, resolve, reject) |
||||
}, () => { |
||||
this.sendAsyncInternal(data, resolve, reject) |
||||
}) |
||||
} else { |
||||
this.sendAsyncInternal(data, resolve, reject) |
||||
} |
||||
}) |
||||
} |
||||
|
||||
sendAsyncInternal (data, resolve, reject) { |
||||
if (this.provider) { |
||||
this.provider[this.provider.sendAsync ? 'sendAsync' : 'send'](data, (error, message) => { |
||||
if (error) { |
||||
this.provider = null |
||||
return reject(error) |
||||
} |
||||
resolve(message) |
||||
}) |
||||
} else { |
||||
const result = data.method === 'net_listening' ? 'canceled' : [] |
||||
resolve({ jsonrpc: '2.0', result: result, id: data.id }) |
||||
} |
||||
} |
||||
} |
||||
|
||||
module.exports = HardhatProvider |
@ -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,14 @@ |
||||
'use strict' |
||||
|
||||
module.exports = { |
||||
contract: ` |
||||
pragma experimental ABIEncoderV2; |
||||
contract calldataLocal { |
||||
constructor () public {
|
||||
} |
||||
|
||||
function level11(uint8[1] calldata foo, string[2][1] calldata boo) public { |
||||
uint p = 45; |
||||
} |
||||
} |
||||
`}
|
@ -0,0 +1,61 @@ |
||||
'use strict' |
||||
|
||||
import deepequal from 'deep-equal' |
||||
import { sendTx } from '../vmCall' |
||||
import { TraceManager } from '../../../src/trace/traceManager' |
||||
import { CodeManager } from '../../../src/code/codeManager' |
||||
import { SolidityProxy } from '../../../src/solidity-decoder/solidityProxy' |
||||
import { InternalCallTree } from '../../../src/solidity-decoder/internalCallTree' |
||||
import { EventManager } from '../../../src/eventManager' |
||||
import * as helper from './helper' |
||||
|
||||
module.exports = async function (st, vm, privateKey, contractBytecode, compilationResult) { |
||||
let txHash |
||||
try { |
||||
let data = await sendTx(vm, { nonce: 0, privateKey: privateKey }, null, 0, contractBytecode) |
||||
const to = (data as any).result.createdAddress.toString() |
||||
// call to level11
|
||||
data = await sendTx(vm, { nonce: 1, privateKey: privateKey }, to, 0, 'a372a595000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000001520000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000015400000000000000000000000000000000000000000000000000000000000000') |
||||
txHash = (data as any).hash |
||||
} catch (e) { |
||||
return st.fail(e) |
||||
} |
||||
return new Promise((resolve) => {
|
||||
vm.web3.eth.getTransaction(txHash, function (error, tx) { |
||||
if (error) { |
||||
return st.fail(error) |
||||
} |
||||
var traceManager = new TraceManager({ web3: vm.web3 }) |
||||
var codeManager = new CodeManager(traceManager) |
||||
codeManager.clear() |
||||
var solidityProxy = new SolidityProxy({ getCurrentCalledAddressAt: traceManager.getCurrentCalledAddressAt.bind(traceManager), getCode: codeManager.getCode.bind(codeManager) }) |
||||
solidityProxy.reset(compilationResult) |
||||
var debuggerEvent = new EventManager() |
||||
var callTree = new InternalCallTree(debuggerEvent, traceManager, solidityProxy, codeManager, { includeLocalVariables: true }) |
||||
callTree.event.register('callTreeBuildFailed', (error) => { |
||||
st.fail(error) |
||||
}) |
||||
callTree.event.register('callTreeNotReady', (reason) => { |
||||
st.fail(reason) |
||||
}) |
||||
callTree.event.register('callTreeReady', (scopes, scopeStarts) => { |
||||
helper.decodeLocals(st, 140, traceManager, callTree, function (locals) { |
||||
try { |
||||
const expected = {"p":{"value":"45","type":"uint256"},"foo":{"length":"1","value":[{"value":"3","type":"uint8"}],"type":"uint8[1]"},"boo":{"length":"1","value":[{"length":"2","value":[{"value":"R","type":"string"},{"value":"T","type":"string"}],"type":"string[2]"}],"type":"string[2][1]"}} |
||||
st.deepEqual(locals, expected) |
||||
} catch (e) { |
||||
st.fail(e.message) |
||||
} |
||||
resolve({}) |
||||
}) |
||||
}) |
||||
|
||||
|
||||
traceManager.resolveTrace(tx).then(() => { |
||||
debuggerEvent.trigger('newTraceLoaded', [traceManager.trace]) |
||||
}).catch((error) => { |
||||
st.fail(error) |
||||
}) |
||||
}) |
||||
}) |
||||
} |
@ -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", |
||||
"compilerOptions": { |
||||
"module": "commonjs", |
||||
"outDir": "../../dist/out-tsc", |
||||
"declaration": true, |
||||
"rootDir": "./", |
||||
"types": ["node"] |
||||
}, |
||||
"exclude": [ |
||||
"**/*.spec.ts", |
||||
"tests/" |
||||
], |
||||
"include": ["**/*.ts"] |
||||
"extends": "./tsconfig.json", |
||||
"compilerOptions": { |
||||
"module": "commonjs", |
||||
"outDir": "../../dist/out-tsc", |
||||
"declaration": true, |
||||
"rootDir": "./", |
||||
"types": ["node"] |
||||
}, |
||||
"exclude": [ |
||||
"**/*.spec.ts", |
||||
"tests/" |
||||
], |
||||
"include": ["**/*.ts"] |
||||
} |
||||
|
@ -1,16 +1,15 @@ |
||||
{ |
||||
"extends": "./tsconfig.json", |
||||
"compilerOptions": { |
||||
"outDir": "../../dist/out-tsc", |
||||
"module": "commonjs", |
||||
"types": ["jest", "node"] |
||||
}, |
||||
"include": [ |
||||
"**/*.spec.ts", |
||||
"**/*.spec.tsx", |
||||
"**/*.spec.js", |
||||
"**/*.spec.jsx", |
||||
"**/*.d.ts" |
||||
] |
||||
} |
||||
|
||||
"extends": "./tsconfig.json", |
||||
"compilerOptions": { |
||||
"outDir": "../../dist/out-tsc", |
||||
"module": "commonjs", |
||||
"types": ["jest", "node"] |
||||
}, |
||||
"include": [ |
||||
"**/*.spec.ts", |
||||
"**/*.spec.tsx", |
||||
"**/*.spec.js", |
||||
"**/*.spec.jsx", |
||||
"**/*.d.ts" |
||||
] |
||||
} |
||||
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in new issue