This repository contains source code for Abax Protocol smart contracts as well as set of test suites and utilities scripts.
src- contains sources of Abax Protocol's core contractstests- contains e2e tests as well as utilities used for writing/running testsscripts- contains code for benchmarking contracts and initial deployment scripts
-
To build Abax Protocol you need to have prepared rust environment, with cargo-contract compatible with ink! 5.x . Follow official guides on how to set up/migrate to the environment that supports ink! 5.x:
-
https://use.ink/faq/migrating-from-ink-3-to-4/#compatibility
-
To run tests and use convenience scripts you have to run
pnpm/pnpm installcommand to install required npm packages.
Note: Most of the node scripts will display much more information if you set an environment variable DEBUG (eg. env DEBUG=1 pnpm test).
We've prepared some convenience scripts that somehow simplify the build process and streamline it to be usable from the root repo's directory.
pnpm build- allows you to build all contracts contained in thesrcfolder, have the artifacts copied over to theartifactsfolder, generate typechain types and measure the amount of time the build took. In additionbuild.log/build.log.htmlare created with the details of the build process - useful since the console output ofcargo contract buildfor all contracts is lenghty. For preview we recommend to use either browser (build.log.html) orANSI Colors(build.log)pnpm cs <contract_name>- allows you to build single contract and have its artifacts copied over to theartifactsfolder.
Currently, only test deployment, onto local chain is supported by scripts - currently version v0.24.0 of the substrate-contracts-node is being used for that purpose.
Assuming you've build all contracts here are the steps to have contracts deployed and available for interactions:
- Run
pnpm localNode- this will spin upsubstrate-contracts-nodewith some opinionated, predefined by us settings including logging, max connection limit to the web socket etc. Running node directly from CLI might cause tests to fail partially/randomly. Warning/Notepnpm testdoes kill the process of the local node to make test execution avaiable. - Run
pnpm deployTestto deploy contracts. The command will output adeployedContracts.jsonfile that contains addresses of each deployed contract. The mentioned file is being used later on for test purposes but might serve simply as a lookup file.
Initially tests were using Redspot however due to compatibility issues, terrible performance and dependencies mismatch (including polkadot.js core packages actively developed or foreshadowed ink! 4) we've abandoned the library.
Instead we've used raw mocha library alongside some scripts to abstract logic required for e2e tests to work properly and that turned out to be a fast and reliable solution.
Tests consist of regular test suites (created via describe/makeSuite) and a scenario-based tests. Scenarios are stored in the folder tests/scenarios/stories and each contains a set of lists of interactions with Abax Protocol with expected outcomes.T
TL;DR; To run tests it is sufficient to run pnpm build and pnpm test.
Note Contract event testing is unstable at the moment and some tests may fail because of that. In order to rerun failed tests apply mocha's only to selected test suites (eg describe.only instead of describe/ makeSuite.only instead of makeSuite, it.only instead of it).
Note2 In order to narrow down test execution of scenario tests use hardcoded settings from scenariosRunner.test.ts.
Example 1: Apply only to scenario file deposit.json
const selectedFiles: string[] = ['deposit'];
const selectedScenarios: string[] = [];
const skipScenarios = false;
Example 2: Apply only to scenario titled Account 1 deposits 200 DAI on behalf of account 2, account 2 tries to borrow 0.1 WETH
const selectedFiles: string[] = [];
const selectedScenarios: string[] = ['Account 1 deposits 200 DAI on behalf of account 2, account 2 tries to borrow 0.1 WETH'];
const skipScenarios = false;
Example 3: Skip scenario execution
const selectedFiles: string[] = [];
const selectedScenarios: string[] = [];
const skipScenarios = true;
Test execution consists of a set of simple steps:
- Clear db backup/test artifacts from previous run
- Deploy, set up contracts (using
pnpm deployTest) and store/persist node's db - run
mocha - For every set of tests that require a "clean setup", instead of reruning deployment, use the database state saved previously. The caveat of doing that is that we have to restart the node (details in
nodePersistence.ts). That being said thanks to doing that tests perform couple of times better than usingredspot. This operation happens inbeforehooks, notably most used in themakeSuitetest suite wrapper frommake-suite.ts.
pnpm testis what that will perform all of the steps mentioned previously and on top of that store logs of node in thesubstrate-contracts-node.testrun.logfile (note that with the given approach the node is being restarted so keeping track of logs in the console is rather annoying). Thesubstrate-contracts-node.testrun.retouched.loglog file contains the same data with the exception of UInt8 arrays being swapped with proper addresses. In the future, we plan to add removal of duplicate logs (happening because of the node itself ).
- Allow to allocate more memory for node:
echo 'export NODE_OPTIONS="--max-old-space-size=8192"' >> ~/.bashrc - Run test scenario file in debug (targetting persistent local node):
pnpm test:debug <test_scenario_file_name>