Ethereum web3.js module for Webpack. Deploys and returns instantiated contract.
MIT License
Deploys Ethereum VM bytecode and returns ready-to-use JavaScript instance of the deployed smart contract(s). Also returns initialized web3 object from Web3 object for easy usage.
Ideally to be used with solc-loader for solidity compilation and bytecode generation.
A web3 provider is required when running web3-loader
. testrpc is handy during development, while geth would be useful to test in actual Ethereum environment where blocks are not mined immediately after every actions.
Sample dapp or starter kit can be found at uzyn/ethereum-webpack-example-dapp.
npm install web3-loader --save-dev
This loader is ideally to be used after solc-loader
on Solidity smart contract code (.sol
).
At your project's webpack.config.js
:
module.exports = {
module: {
loaders: [
{
test: /\.sol$/,
loaders: ['web3', 'solc']
]
}
}
Using Ethereum's The Coin as an example:
import { MyToken } from './contract/MyToken.sol';
// MyToken is now available as a ready-to-use contract instance
const symbol = MyToken.symbol();
MyToken.transfer('0x............', 1);
You can also interact with the ininitialized web3 instance without having to import and initialize web3 in your code.
import { MyToken, web3 } from './contract/MyToken.sol';
let currentBlock = web3.eth.blockNumber;
This loader is able to automatically inject address of deployed contract if your contracts depends on it. To use dependency injection you will need:
require
in your jsinject_
constructor variablesConsider such example:
//Manager.sol
contract Manager {
//Some state + complex stuff that is accessed by other contracts
}
//SomeContract.sol
import 'Manager.sol';
contract SomeContract1 {
address manager;
function SomeContract(inject_Manager) {
manager = inject_Manager;
}
function doSmth() {
Manager(manager).someMethod();
}
}
//Contracts.sol will contain just 'import' statements
import 'SomeContract1';
In JS code:
var contracts = require('Contracts.sol');
var SomeContract1 = contracts.SomeContract;
var Manager = contracts.Manager;
var web3 = contracts.web3;
In Contracts.sol
only root contracts can be specified. Loader automatically builds dependency
graph based on import
statements and inject_
constructor variables.
Run webpack -d
to see debug information on the order of deployment.
If your construct must accept other variables they should be placed before because loader just appends injected contract addresses to the end of constructorParams
config variable.
Configuration is not needed for most common use cases.
provider
http://localhost:8545
from
web3.eth.accounts[0]
.gasLimit
web3.eth.getBlock(web3.eth.defaultBlock).gasLimit
constructorParams
{}
(empty object)
deployedContracts
{}
(empty object)
Recommended especially for configuring of contract addresses.
// webpack.config.js
module.exports = {
web3Loader: {
// Web3
provider: 'http://localhost:8545',
// For deployment
from: '0xFfA57D3e88A24311565C9929F180739E43FBD0aA',
gasLimit: 100000,
// Specify contract constructor parameters, if any.
// constructorParams: {
// ContractOne: [ 'param1_value', 'param2_value' ]
// }
constructorParams: {},
// To use deployed contracts instead of redeploying, include contract addresses in config
// deployedContracts: {
// ContractOne: '0x...........',
// ContractTwo: '0x...........',
// }
deployedContracts: {}
}
}
Query style is also supported but can be tricky for constructorParams
and deployedContracts
.
loaders: ['web3?gasLimit=50000&provider=http://example.org:8545']
MIT · U-Zyn Chua (@uzyn)
Tips: 0xFfA57D3e88A24311565C9929F180739E43FBD0aA