Tasks
#
What are tasksEvery time you run npx Redspot xxx
, you are running tasks. For example, npx redspot compile
runs the compilation task.
To view the tasks currently available by redspot
npx redspot
Run the npx redspot --help
command to view the help information.
Redspot version 0.11.4Usage: redspot [GLOBAL OPTIONS] <TASK> [TASK OPTIONS]GLOBAL OPTIONS: --config A Redspot config file. --help Shows this message, or a task's help if its name is provided --log-level Set log levels 1-5 (default: "2") --max-memory The maximum amount of memory that Redspot can use. --network The network to connect to. --show-stack-traces Show stack traces. --tsconfig Reserved redspot argument -- Has no effect. --verbose Enables Redspot verbose logging --version Shows redspot's version.
AVAILABLE TASKS: clean Clears the cache and deletes all artifacts compile Compiles the entire project, building all artifacts console Opens a redspot console explorer Start redspot explorer help Prints this message run Runs a user-defined script after compiling the project test Runs mocha tests testnet Running the test networkTo get help for a specific task run: npx redspot help [task]
#
Global OPTIONSDefines the global configuration of Redspot runtime. It can be attached to any task.
To specify the connection to the Substrate network (substrate network needs to be configured in config).
npx redspot test --network substrate
To specify the level of the log to be printed, and the log level is 2 by default
npx redspot test --log-level 3
You can also set GLOBAL OPTIONS through environment variables.
Set the log level:
REDSPOT_LOG_LEVEL=5 npx redspot test
Set up the network:
REDSPOT_NETWORK=substrate npx redspot test.
#
Task OPTIONSUsually each task will provide its own parameter configuration. For example
$ npx redspot compile --helpRedspot version 0.11.4
Usage: redspot [GLOBAL OPTIONS] compile --docker <BOOLEAN> --quiet <BOOLEAN> [...sourcePattern]
OPTIONS:
--docker Compiling with docker --quiet Check for document changes
POSITIONAL ARGUMENTS:
sourcePattern A glob string that is matched against (default: [])
compile: Compiles the entire project, building all artifacts
For global options help run: redspot help
For compiling commands, you can pass in the path of the contract to specify the contract that needs to be compiled, for example,npx redspot compile examples/erc20
.
clean
#
Clears the cache and deletes all artifacts generated by Redspot
compile
#
To compile your contracts in your Redspot project, use the built-in compile task:
redspot [GLOBAL OPTIONS] compile --docker <BOOLEAN> --quiet <BOOLEAN> [...sourcePattern]
Currently supports the compilation of ink contracts and Solang contracts. When compiling the ink contract, please make sure you have installed cargo-contract. When compiling the Solang contract, please make sure you have installed Solang. In redspot.config.ts, you can configure compilation options.
{ ... contract: { ink: { toolchain: 'nightly', // set the cargo-contract compile-time toolchain sources: ['contracts/**/*'] // Configure the directory to find the contract files }, solang: { sources: ['contracts/**/*.sol'] // Configure the directory to find contract files } }, paths: { ... artifacts: 'artifacts' // specify the directory where the contract compilation products are stored ... } ...}
The compile command receives the sourcePattern parameter, which can override the sources in the configuration file, for example:
npx redspot compile examples/erc20
:It will only find contracts in the examples/erc20 directory.
Once the compilation is complete, the files generated after compilation can be found in the artifacts
directory. There are usually two kinds of format,[ContractName].contract
and[ContractName].json
. The only difference between them is that Wasm is not included in .json
, and the size is smaller. You can also use your own tools to compile, and then copy the [ContractName].contract
file to the artifacts directory. This will not affect the use of other functions.
#
compile task options--docker <BOOLEAN>
: boolean to indicate compiling with docker. For details, please refer Docker
--quiet <BOOLEAN>
: if there is no change to contract code or redspot config, the compilation will be skipped
sourcePattern
: A glob string that is matched against, so you can specify which contract to compile. For example,
npx redspot compile erc20
will only compile the contract code under erc20
directory.
With no sourcePattern
sepecified, it will compile the code under contracts
directory
console
#
Opens a redspot console. For details, please refer Redspot Console
$ npx redspot console
explorer
#
Start redspot explorer. For details, please refer Redspot Explorer
npx redspot explorer
help
#
npx redspot --helpRedspot version 0.11.4
Usage: redspot [GLOBAL OPTIONS] <TASK> [TASK OPTIONS]
GLOBAL OPTIONS:
--config A Redspot config file. --help Shows this message, or a task's help if its name is provided --log-level Set log levels 1-5 (default: "2") --max-memory The maximum amount of memory that Redspot can use. --network The network to connect to. --show-stack-traces Show stack traces. --tsconfig Reserved redspot argument -- Has no effect. --verbose Enables Redspot verbose logging --version Shows redspot's version.
AVAILABLE TASKS:
check Check whatever you need clean Clears the cache and deletes all artifacts compile Compiles the entire project, building all artifacts console Opens a redspot console explorer Start redspot explorer help Prints this message run Runs a user-defined script after compiling the project test Runs mocha tests testnet Running the test network
To get help for a specific task run: npx redspot help [task]
testnet
#
Testnet
task will execute command in docker env. Please refer Docker for details.
test
#
Automated testing helps developers avoid repeating tasks. Test requires the blockchain node to be setup properly. Please refer to Installation to learn how to setup the blockchain test node.
The test can be configured through redspot.config.ts
{ defaultNetwork: "development", // the default network to connect to ... networks: { development: { endpoint: "ws://127.0.0.1:9944", // url of the network to connect to (websocket) types: {}, // Type definitions to be passed to polkadotjs for use accounts: ["//Alice", "tomato mad peasant blush poem obtain inspire distance attitude mercy return marriage"] // The account to use for the signature, default is ['// Alice', '//Bob', '//Charlie', '//Dave', '//Eve', '//Ferdie'] gasLimit: "400000000000", // default gaslimit }, } path: { tests: 'tests', // directory to find test files }, mocha: { timeout: 60000, // timeout for mocha tests ... } ...}
You can also configure the test blockchain network through Environmental variable in command, for example:
REDSPOT_NETWORK=development npx redpost test
#
test task options--no-compile
: indicate running compile
task before running this task.For example
npx redspot test --no-compile
--source-pattern
: You can also specify which test file to be executed, For example,
npx redspot test ./tests/[filename].ts
#
writing your test fileTest uses mocha as the test framework by default. You can configure mocha options in the redspot.config.ts file. For all supported options, see mocha.
Here is sample test file
import BN from 'bn.js';import { expect } from 'chai';import { patract, network, artifacts } from 'redspot';const { getContractFactory, getRandomSigner } = patract;const { api, getSigners } = network;describe('ERC20', () => { after(() => { return api.disconnect(); }); async function setup() { const one = new BN(10).pow(new BN(api.registry.chainDecimals[0])); const signers = await getSigners(); const Alice = signers[0]; const sender = Alice; const contractFactory = await getContractFactory('erc20', sender); const contract = await contractFactory.deploy('new', '1000'); const abi = artifacts.readArtifact('erc20'); const receiver = await getRandomSigner(); return { sender, contractFactory, contract, abi, receiver, Alice, one }; } it('Assigns initial balance', async () => { const { contract, sender } = await setup(); const result = await contract.query.balanceOf(sender.address); expect(result.output).to.equal(1000); }); it('Transfer emits event', async () => { const { contract, sender, receiver } = await setup(); await expect(contract.tx.transfer(receiver.address, 7)) .to.emit(contract, 'Transfer') .withArgs(sender.address, receiver.address, 7); }); it('Can not transfer above the amount', async () => { const { contract, receiver } = await setup(); await expect(contract.tx.transfer(receiver.address, 1007)).to.not.emit( contract, 'Transfer' ); }); it('Can not transfer from empty account', async () => { const { contract, Alice, one, sender } = await setup(); const emptyAccount = await getRandomSigner(Alice, one.muln(10000)); await expect( contract.tx.transfer(sender.address, 7, { signer: emptyAccount }) ).to.not.emit(contract, 'Transfer'); });});
async function setup() { }
- The
setup
function creates random account calledAlice
and assigned fixed balance to this account. This account will then be used to perform various tests to ensure that the test results are the same every time. - In this test file, two Redspot plugins are used,
@redspot/patract
and@redspot/chai
. For details, please refer Plugins npx redspot test
wraps tomocha
to perform the test. You can directlymocha
command to run. For example, you can run test:
TS_NODE_TRANSPILE_ONLY=true mocha -r ts-node/register tests/erc20.test.ts --timeout 60000
** TO Use ts-node to compile and run typescript code, you need to add -r ts-node/register
.TS_NODE_TRANSPILE_ONLY=true
Sets ts-node to ignore typescript type errors when running.
run
#
The Run command can be used to run any typescript file. Setting --no-compile
can avoid automatically running the compilation command npx redspot run --no-compile
.
Like Test, it is just a command wrap around TS_NODE_TRANSPILE_ONLY=true node -r ts-node/register [filepath]
.
We used npx redspot run
for deployment in [Quick Start](../intro/Quick Start)
You can use Run to run the deployment script, for example:
import { network, patract } from 'redspot';const { getContractFactory } = patract;const { getSigners, api } = network;async function run() { console.log('deploy erc20'); await api.isReady; console.log('deploy erc201'); const signers = await getSigners(); const signer = signers[0]; const contractFactory = await getContractFactory('erc20', signer); const contract = await contractFactory.deploy('new', '1000000', { gasLimit: '200000000000', value: '10000000000000000' }); console.log( 'Deploy successfully. The contract address: ', contract.address.toString() ); api.disconnect();}run().catch(err => { console.log(err);});
This deployment script deploys the contract to targetted network and returns the contract address that holds the erc20
contract.
The @redspot/patract plug-in is used here as getContractFactory
.
console
#
You can start a Node REPL console by running the npx redspot console
command. It has a built-in Redspot Runtime Environment, where you can access plug-ins, configurations, and tasks provided by Redspot. For details, please refer Console
(./console)