This script can be used to deploy smart contracts on any networks (ex. testnets, private net) using the most primitive way written in pure NodeJS. All you need is your contract code, wallet address and your corresponding private key to sign the transactions for the contract creation. Here we use the rinkeby testnet, but you can always change it as you wish just by modifying Web3.providers.HttpProvider
(line 8).
- run
node index
to start the repl - As defined in the code, there is a set of functions available under the
helper
class. For example, usehelper.contractName()
to call the contractName function
contractName(source)
: get the contract name from contract source codeloadContract(path)
: return the source code in contract file (ex. myContract.sol)sendRawTnx(source, address, pkey)
: given the contract source code, address to sign transaction, and private key, create the contract object and deploy it onto the blockchain. After the contract is mined, it will return the contract address.
Note: To simplify the problem, the waiting time of contract mining is hardcoded to 30 seconds since the average block mining time in rinkeby testnet is around 25 seconds.contractObject(source, contractAddress)
: create a contrat object to interact with the contract once it is deployed onto the blockchainetherBalance(contract)
: get the ether amount in the smart contracttest()
: just a testing function for testing things conveniently
Just paste the following code line by line.
You can visit the block explorer etherscan to see all the transactions and get a clearer understanding of what is happening.
node index
source = "contract test { function hi() public returns (uint256) { return 123; }}"
address = <your_public_key>
pkey = <your_private_key>
helper.sendRawTnx(source, address, pkey) // will return the contract address after mined
// after you see 'contract mined!', press 'Ctrl+C' to return to the console
c = helper.contractObject(source, 'contract_address') // paste the contract address you just get
c.hi.call().toNumber()
helper.etherBalance(c) // should return 0 if no ether is sent to the contract address
// press 'Ctrl+D' to exit
- ethereumjs-tx: ^1.3.3
- fs: 0.0.1-security
- repl: ^0.1.3
- solc: ^0.4.15
- web3: 0.20.1 (the newest version is too unstable)