parent
5c9f5dfe1e
commit
aca95d02ef
@ -0,0 +1,82 @@ |
|||||||
|
import React from 'react' // eslint-disable-line
|
||||||
|
import { format } from 'util' |
||||||
|
import { Plugin } from '@remixproject/engine' |
||||||
|
import { compile } from '@remix-project/remix-solidity' |
||||||
|
import { TransactionConfig } from 'web3-core' |
||||||
|
const _paq = window._paq = window._paq || [] //eslint-disable-line
|
||||||
|
|
||||||
|
const profile = { |
||||||
|
name: 'solidity-script', |
||||||
|
displayName: 'solidity-script', |
||||||
|
description: 'solidity-script', |
||||||
|
methods: ['execute'] |
||||||
|
} |
||||||
|
|
||||||
|
export class SolidityScript extends Plugin { |
||||||
|
constructor () { |
||||||
|
super(profile) |
||||||
|
} |
||||||
|
|
||||||
|
async execute (path: string) { |
||||||
|
_paq.push(['trackEvent', 'SolidityScript', 'execute', 'script']) |
||||||
|
let content = await this.call('fileManager', 'readFile', path) |
||||||
|
const params = await this.call('solidity', 'getCompilerParameters') |
||||||
|
content = ` |
||||||
|
import "hardhat/console.sol"; |
||||||
|
contract SolidityScript { |
||||||
|
${content} |
||||||
|
}` |
||||||
|
const targets = { 'script.sol': { content } } |
||||||
|
|
||||||
|
// compile
|
||||||
|
const compiler = await this.call('solidity', 'getCompiler') |
||||||
|
const compilation = await compile(targets, params, async (url, cb) => { |
||||||
|
await this.call('contentImport', 'resolveAndSave', url).then((result) => cb(null, result)).catch((error) => cb(error.message)) |
||||||
|
}, compiler.state.worker) |
||||||
|
|
||||||
|
// get the vm provider
|
||||||
|
const contract = compilation.getContract('SolidityScript') |
||||||
|
const bytecode = '0x' + contract.object.evm.bytecode.object |
||||||
|
const web3 = await this.call('blockchain', 'web3VM') |
||||||
|
const accounts = await this.call('blockchain', 'getAccounts') |
||||||
|
if (!accounts || accounts.length === 0) { |
||||||
|
throw new Error('no account available') |
||||||
|
} |
||||||
|
|
||||||
|
// deploy the contract
|
||||||
|
let tx: TransactionConfig = { |
||||||
|
from: accounts[0], |
||||||
|
data: bytecode |
||||||
|
} |
||||||
|
const receipt = await web3.eth.sendTransaction(tx) |
||||||
|
tx = { |
||||||
|
from: accounts[0], |
||||||
|
to: receipt.contractAddress, |
||||||
|
data: '0x504917d1' |
||||||
|
} |
||||||
|
const receiptCall = await web3.eth.sendTransaction(tx) |
||||||
|
|
||||||
|
const hhlogs = await web3.eth.getHHLogsForTx(receiptCall.transactionHash) |
||||||
|
|
||||||
|
if (hhlogs && hhlogs.length) { |
||||||
|
let finalLogs = <div><div><b>console.log:</b></div> |
||||||
|
{ |
||||||
|
hhlogs.map((log) => { |
||||||
|
let formattedLog |
||||||
|
// Hardhat implements the same formatting options that can be found in Node.js' console.log,
|
||||||
|
// which in turn uses util.format: https://nodejs.org/dist/latest-v12.x/docs/api/util.html#util_util_format_format_args
|
||||||
|
// For example: console.log("Name: %s, Age: %d", remix, 6) will log 'Name: remix, Age: 6'
|
||||||
|
// We check first arg to determine if 'util.format' is needed
|
||||||
|
if (typeof log[0] === 'string' && (log[0].includes('%s') || log[0].includes('%d'))) { |
||||||
|
formattedLog = format(log[0], ...log.slice(1)) |
||||||
|
} else { |
||||||
|
formattedLog = log.join(' ') |
||||||
|
} |
||||||
|
return <div>{formattedLog}</div> |
||||||
|
})} |
||||||
|
</div>
|
||||||
|
_paq.push(['trackEvent', 'udapp', 'hardhat', 'console.log']) |
||||||
|
this.call('terminal', 'logHtml', finalLogs) |
||||||
|
} |
||||||
|
}
|
||||||
|
} |
@ -0,0 +1,73 @@ |
|||||||
|
'use strict' |
||||||
|
|
||||||
|
import * as solc from 'solc/wrapper' |
||||||
|
import { CompilerInput, MessageToWorker } from './types' |
||||||
|
let compileJSON: ((input: CompilerInput) => string) | null = (input) => { return '' } |
||||||
|
const missingInputs: string[] = [] |
||||||
|
|
||||||
|
// 'DedicatedWorkerGlobalScope' object (the Worker global scope) is accessible through the self keyword
|
||||||
|
// 'dom' and 'webworker' library files can't be included together https://github.com/microsoft/TypeScript/issues/20595
|
||||||
|
export default function (self) { // eslint-disable-line @typescript-eslint/explicit-module-boundary-types
|
||||||
|
self.addEventListener('message', (e) => { |
||||||
|
const data: MessageToWorker = e.data |
||||||
|
switch (data.cmd) { |
||||||
|
case 'loadVersion': |
||||||
|
{ |
||||||
|
delete self.Module |
||||||
|
// NOTE: workaround some browsers?
|
||||||
|
self.Module = undefined |
||||||
|
compileJSON = null |
||||||
|
// importScripts() method of synchronously imports one or more scripts into the worker's scope
|
||||||
|
self.importScripts(data.data) |
||||||
|
const compiler: solc = solc(self.Module) |
||||||
|
compileJSON = (input) => { |
||||||
|
try { |
||||||
|
const missingInputsCallback = (path) => { |
||||||
|
missingInputs.push(path) |
||||||
|
return { error: 'Deferred import' } |
||||||
|
} |
||||||
|
return compiler.compile(input, { import: missingInputsCallback }) |
||||||
|
} catch (exception) { |
||||||
|
return JSON.stringify({ error: 'Uncaught JavaScript exception:\n' + exception }) |
||||||
|
} |
||||||
|
} |
||||||
|
self.postMessage({ |
||||||
|
cmd: 'versionLoaded', |
||||||
|
data: compiler.version(), |
||||||
|
license: compiler.license() |
||||||
|
}) |
||||||
|
break |
||||||
|
} |
||||||
|
|
||||||
|
case 'compile': { |
||||||
|
missingInputs.length = 0 |
||||||
|
if (data.input && compileJSON) { |
||||||
|
self.postMessage({ |
||||||
|
cmd: 'compiled', |
||||||
|
job: data.job, |
||||||
|
timestamp: data.timestamp, |
||||||
|
data: compileJSON(data.input), |
||||||
|
input: data.input, |
||||||
|
missingInputs: missingInputs |
||||||
|
}) |
||||||
|
} |
||||||
|
break |
||||||
|
} |
||||||
|
|
||||||
|
case 'standalone-compile': |
||||||
|
missingInputs.length = 0 |
||||||
|
if (data.input && compileJSON) { |
||||||
|
self.postMessage({ |
||||||
|
cmd: 'standalone-compiled', |
||||||
|
job: data.job, |
||||||
|
timestamp: data.timestamp, |
||||||
|
data: compileJSON(data.input), |
||||||
|
input: data.input, |
||||||
|
missingInputs: missingInputs |
||||||
|
}) |
||||||
|
} |
||||||
|
break |
||||||
|
} |
||||||
|
|
||||||
|
}, false) |
||||||
|
} |
Loading…
Reference in new issue