GitXplorerGitXplorer
u

web3-loader

public
28 stars
12 forks
5 issues

Commits

List of commits on branch master.
Unverified
54055e95fd37bc23b0aed4a6b7f182890b7f3cf4

Merge pull request #21 from moklick/patch-1

uuzyn committed 8 years ago
Unverified
6019ae3f57b8455e58dfd0d4ff83c539a29e64ee

use js syntax highlighting

mmoklick committed 8 years ago
Verified
4b1a025e3f21f7514101ebfc3469631abf56c526

Patch bump

uuzyn committed 8 years ago
Unverified
09b4916d183321d271c5620d2c9127ee928a1e52

Merge pull request #19 from uzyn/bugfix/parity-compatibility

uuzyn committed 8 years ago
Verified
5705c7e07659cb80aba5374e3e730bf66138cca1

Padded bytecode with 0x for parity compatibility

uuzyn committed 8 years ago
Verified
e1fe7f7213b78bcd50329b01b1b348512da1d7cb

Ver increment.

uuzyn committed 8 years ago

README

The README file for this repository.

web3 loader for webpack

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 code

Sample dapp or starter kit can be found at uzyn/ethereum-webpack-example-dapp.

Installation

npm install web3-loader --save-dev

Usage

This loader is ideally to be used after solc-loader on Solidity smart contract code (.sol).

Example webpack config

At your project's webpack.config.js:

module.exports = {
  module: {
    loaders: [
      {
        test: /\.sol$/,
        loaders: ['web3', 'solc']
    ]
  }
}

Usage in your project

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;

Solidity Contract Dependency Injection

This loader is able to automatically inject address of deployed contract if your contracts depends on it. To use dependency injection you will need:

  • Contracts.sol - a central file which you will require in your js
  • inject_ constructor variables

Consider 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

Configuration is not needed for most common use cases.

Options

  1. provider
    • Web3 provider
    • Default: http://localhost:8545
  2. from
    • Account to deploy contract from.
    • Default: first account at your Web3 provider, ie. web3.eth.accounts[0].
  3. gasLimit
    • Maximum gas allowed for deploying of each contracts.
    • Default: latest gasLimit of Ethereum, ie. web3.eth.getBlock(web3.eth.defaultBlock).gasLimit
  4. constructorParams
    • Specify contract constructor parameters, if any.
    • Parameters for a contract must be listed in an array form.
    • Default: {} (empty object)
    • See next section for example.
  5. deployedContracts
    • If you would like to use existing deployed contracts, specify contract addresses for each contracts.
    • If a contract is not found, it would be deployed at build.
    • Default: {} (empty object)
    • See next section for example.

Config style

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

Query style is also supported but can be tricky for constructorParams and deployedContracts.

loaders: ['web3?gasLimit=50000&provider=http://example.org:8545']

License

MIT · U-Zyn Chua (@uzyn)

Tips: 0xFfA57D3e88A24311565C9929F180739E43FBD0aA