Truffle typechain target
Custom target plugin for TypeChain or other origin at gitlab.
Details
Provides generation of typescript interfaces for Ethereum smart contracts. Basic types are mostly standing on @machinomy/types-web3 and @truffle-types/web3
packages for Truffle 4 or @types/web3 and @truffle-types/typescript-types
packages for Truffle 5.
Configuration
To use this target etheir from CLI or configuration file you should at first create a script file with content
Truffle 4
const { Truffle } = require("typechain-truffle-target");
module.exports = ctx => {
return new Truffle(ctx);
};
Truffle 5
const { TruffleV5 } = require("typechain-truffle-target");
module.exports = ctx => {
return new TruffleV5(ctx);
};
There you could perform any presetup or preconfiguration as you like.
CLI
Then using:
typechain --target=[path to previous script] [glob]
For example:
typechain --target=./load-custom-plugin --outDir types typescript-contracts './build/contracts/*.json'
Config file
Then create or append to file ts-generator.json
the next object:
[
{
"generator": "typechain",
"target": "./load-custom-plugin",
"files": "./build/contracts/*.json",
"outDir": "./types",
"declareModule": "truffle-contracts",
"bigNumberType": "BigNumber"
}
]
where
- "generator" - name of typechain generator;
- "target - created custom script file with target instantiation;
- "files" - glob pattern where to look for input files (contract artifacts in our case);
-
"declareModule" - name of module declaration for created interfaces. By default uses
truffle-contracts
module name; it is also influences out directory name for contracts interfaces; -
"outDir" - place where results will be saved. By default it will save into
./types
with two subdirectories:truffle-contracts
andtruffle
; -
"bigNumberType" - (only for Truffle 5) type of bignumber
"BigNumber"
or"BN"
;
Then run
ts-generator ./ts-generator.json
# or
npx ts-generator ./ts-generator.json
Module name renaming works **ONLY** with configuration file.
And do not forget to add created directory either to include or typeRoots in tsconfig.json
.
More info
For more detail you could refer to original repo or gitlab version.