This is a boilerplate code to make your smart contract as an npm library using truffle-plugin-modularizer
. It exports the json files in build/contracts/
and creates src/index.js
. Then you will be able to use your contracts as an instance of truffle-contract
. Finally, publish them onto the npm repository, and import into your applications.
See this to get more detail how truffle-plugin-modularize
works.
-
Create a directory to start your project
mkdir YourProject cd YourProject
-
Unbox
npm-truffle-box
npx truffle unbox wanseob/npm-truffle-box
-
Develop and test
npm run test
-
Configure your truffle-config.json
// truffle-config.js module.exports = { ..., plugins: [ 'truffle-plugin-modularizer' ], modularizer: { // output: 'src/index.js', // target: 'build/contracts' // includeOnly: [], // networks: [] }, }
-
Configure your package.json
//package.json { "name": "Your project name", ..., "main": "src/index.js", ... }
-
Deploy to npm
npm publish
-
install your module
$ cd /your/react/app/path $ npm install --save "your-project-name"
-
Import contracts into your front-end application and init with web3 provider
import { SampleContract } from 'your-project-name' web3 = window.web3 SampleContract(web3).deployed().then( async instance => { await instance.add(10) await instance.add(20) await instance.add(30) let values = await instance.getValues() console.log("Values: ", values) } )
npm run testContracts
: It runs test cases intest/
directory for your smart contracts. This usestruffle test
command.npm run testModule
: It runs test cases inmoduleTest/
to check the modularized output. It modularizes your contracts and create a temporal output filebuild/index.tmp.js
. You should write Mocha test cases importing your smart contract from the temporal output file.npm run test
: It tests both the contracts and the modularized output.npm run standard
: It follows JS standard style.npm run ethlint
: Lint your smart contracts.
If you don't prefer standard style and ethlint, remove "pre-commit" property from the package.json
.
It only supports linux & macOS