Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/raineorshine/eth-new-contract
Compile and deploy Solidity contracts straight from source.
https://github.com/raineorshine/eth-new-contract
ethereum solidity
Last synced: about 1 month ago
JSON representation
Compile and deploy Solidity contracts straight from source.
- Host: GitHub
- URL: https://github.com/raineorshine/eth-new-contract
- Owner: raineorshine
- License: isc
- Created: 2016-06-23T22:18:52.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2017-08-16T16:43:02.000Z (over 7 years ago)
- Last Synced: 2024-09-13T23:28:00.922Z (2 months ago)
- Topics: ethereum, solidity
- Language: JavaScript
- Homepage:
- Size: 36.1 KB
- Stars: 12
- Watchers: 3
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# eth-new-contract
[![npm version](https://img.shields.io/npm/v/eth-new-contract.svg)](https://npmjs.org/package/eth-new-contract)`eth-new-contract` is an npm module that allows you to deploy a Solidity contract and create a web3 contract instance straight from source. Subsequent calls use cached bytecode for performance.
The default `new` method of web3 has the somewhat quirky behavior of invoking its callback twice—once to return the transaction hash and once when the contract is deployed. Usually you don't care about the transaction hash initially, so in this library, the promise resolves when it is deployed. `contract.transactionHash` can then be accessed like on any web3 contract.
## Install
```sh
$ npm install --save eth-new-contract
```## Usage
```js
const Web3 = require('web3')
const provider = new Web3.providers.HttpProvider('http://localhost:8545')
const web3 = new Web3(provider)
const newContract = require('eth-new-contract').default(provider)// instantiate from source
const source = 'contract MyContract { function GetAnswer() constant returns(uint) { return 42; } }'
newContract(source, { from: web3.eth.accounts[0] })
.then(contract => {
console.log('Contract deployed at ' + contract.address)
})
```You can also compile and generate the web3 constructor yourself and pass it to `eth-new-contract`:
```js
const solc = require('solc')
const Web3 = require('web3')
const web3 = new Web3(new Web3.providers.HttpProvider('http://localhost:8545'))
const newContract = require('eth-new-contract').default() // no provider needed// compile contract
const compilation = solc.compile(contractSource)
const bytecode = compilation.contracts[contractName].bytecode
const abi = JSON.parse(compilation.contracts[contractName].interface)
const MyContract = web3.eth.contract(abi)// deploy
newContract(MyContract, { from: web3.eth.accounts[0], data: bytecode })
.then(contract => {
console.log('Contract deployed at ' + contract.address)
})
```## Developer Notes
- `bip39` incompatibility with testrpc requires explicit dependency version: https://github.com/ethereumjs/testrpc/issues/313#issuecomment-304790839
## License
ISC © [Raine Revere](https://github.com/raineorshine)