运行脚本
Remix IDE supports execution of JS & TS scripts.
编写并运行脚本
Create a file with .js
or .ts
extension and put your logic inside it. To run a script either:
Click the green play button in the upper left of the Editor.
Right click on the script name in the
File Explorers
and click on the Run option.Ctrl+Shift+S
when the script is displayed in the editor.在编辑器中将脚本设置为选中文件,并在 Remix 终端运行
remix.exeCurrent()
。
Here is a sample .js script:
function test() {
var num=12;
if(num<10)
console.log(num + " is less than 10");
else
console.log(num + " is not less than 10");
}
test();
使用上述选项之一运行将在 Remix 终端中显示结果
Why run scripts in Remix?
模拟您的dapp前端将如何使用web3.js或ethers.js
快速部署和与多个合约实例交互,而无需通过Remix GUI。
对以前部署的合约运行一些测试。
部署合约的脚本
设置
这些脚本需要访问合约的ABI。 ABI位于合约的元数据文件中。请确保通过转到设置模块并检查“生成合约元数据”选项是否已选中来创建此元数据文件。
编译Solidity文件-以生成合约元数据。
在Deploy & Run插件中,选择环境。
Async/await脚本适用于所有环境:Remix VM、Injected Provider(通常是MetaMask)和外部的HTTP Provider.。
文件浏览器中的JS脚本
在工作区的脚本文件夹中,有两个示例文件:一个使用web3.js,另一个使用ethers.js。
Compile a contract and run a script in one click
When drafting a contract, it can be helpful to run a script just after the compilation succeeds.
With this technique, one can write some code then quickly deploy and set the state of the contracts.
The script can contains Mocha tests to be run.
In order to connect a contract with a script, add the NatSpec tag @custom:dev-run-script
to the contract followed by the absolute file path, like:
/**
* @title ContractName
* @dev ContractDescription
* @custom:dev-run-script file_path
*/
contract ContractName {}
When you are ready to deploy the code for real, remove the NatSpec comment @custom:dev-run-script
.
快捷键:在编辑Solidity文件时,按Ctrl+Shift+S将编译该文件并运行脚本。相比之下,按Ctrl+S仅会开始编译。
示例脚本
下面的示例部署了一个名为CustomERC20.sol
的Solidity合约。该示例使用web3.js库,也可以使用Ethers.js。
有关此示例的更多信息,请参见:运行async/await脚本
(async () => {
try {
console.log('deploy...')
// Note that the script needs the ABI which is generated from the compilation artifact.
const metadata = JSON.parse(await remix.call('fileManager', 'getFile', 'browser/artifacts/CustomERC20.json'))
const accounts = await web3.eth.getAccounts()
let contract = new web3.eth.Contract(metadata.abi)
contract = contract.deploy({
data: metadata.data.bytecode.object,
arguments: ["Mask", "N95"]
})
newContractInstance = await contract.send({
from: accounts[0],
gas: 1500000,
gasPrice: '30000000000'
})
console.log(newContractInstance.options.address)
} catch (e) {
console.log(e.message)
}
})()
欲了解更多脚本示例,请查看常见问题的脚本。
Remix脚本中的require
require
or import
statement is supported in a limited manner for Remix supported modules with Remix Scripts.
For now, modules supported by Remix are:
ethers
web3
swarmgw
chai
starknet
multihashes
zokrates-js
snarkjs
circomlibjs
@zk-kit/incremental-merkle-tree
@semaphore-protocol/proof
@semaphore-protocol/group
@semaphore-protocol/identity
@semaphore-protocol/data
@chainlink/functions-toolkit
@personaelabs/spartan-ecdsa
@ethereumjs/util
ffjavascript
sindri
remix
hardhat (only for hardhat.ethers object)
对于不支持的模块,将显示此错误信息:<module_name>
模块 require 不受 Remix IDE 支持。