├── .gitignore ├── compile.js ├── contracts └── hello.sol ├── deploy.js ├── package-lock.json ├── package.json ├── readme.md └── test └── hello.test.js /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | yarn-debug.log* 6 | yarn-error.log* 7 | 8 | # Runtime data 9 | pids 10 | *.pid 11 | *.seed 12 | *.pid.lock 13 | 14 | # Directory for instrumented libs generated by jscoverage/JSCover 15 | lib-cov 16 | 17 | # Coverage directory used by tools like istanbul 18 | coverage 19 | 20 | # nyc test coverage 21 | .nyc_output 22 | 23 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 24 | .grunt 25 | 26 | # Bower dependency directory (https://bower.io/) 27 | bower_components 28 | 29 | # node-waf configuration 30 | .lock-wscript 31 | 32 | # Compiled binary addons (https://nodejs.org/api/addons.html) 33 | build/Release 34 | 35 | # Dependency directories 36 | node_modules/ 37 | jspm_packages/ 38 | 39 | # Typescript v1 declaration files 40 | typings/ 41 | 42 | # Optional npm cache directory 43 | .npm 44 | 45 | # Optional eslint cache 46 | .eslintcache 47 | 48 | # Optional REPL history 49 | .node_repl_history 50 | 51 | # Output of 'npm pack' 52 | *.tgz 53 | 54 | # Yarn Integrity file 55 | .yarn-integrity 56 | 57 | # dotenv environment variables file 58 | .env 59 | 60 | # next.js build output 61 | .next -------------------------------------------------------------------------------- /compile.js: -------------------------------------------------------------------------------- 1 | const path = require('path'); 2 | const fs = require('fs'); 3 | const solc = require('solc'); 4 | 5 | const helloPath = path.resolve(__dirname, 'contracts', 'hello.sol'); 6 | const source = fs.readFileSync(helloPath, 'utf8'); 7 | const compiledSource = solc.compile(source, 1).contracts[':Hello']; 8 | 9 | module.exports = compiledSource; 10 | 11 | // console.log(compiledSource); 12 | -------------------------------------------------------------------------------- /contracts/hello.sol: -------------------------------------------------------------------------------- 1 | pragma solidity ^0.4.17; 2 | contract Hello { 3 | string private message; 4 | 5 | function Hello(string mes) public { 6 | message = mes; 7 | } 8 | 9 | function setMessage(string mes) public { 10 | message = mes; 11 | } 12 | 13 | function getMessage() public view returns(string) { 14 | return message; 15 | } 16 | } -------------------------------------------------------------------------------- /deploy.js: -------------------------------------------------------------------------------- 1 | const HDWalletProvider = require('truffle-hdwallet-provider'); 2 | const Web3 = require('web3'); 3 | const { interface, bytecode } = require('./compile'); 4 | 5 | const provider = new HDWalletProvider( 6 | process.env.MNEMONIC, 7 | process.env.INFURA_URL 8 | ); 9 | 10 | const web3 = new Web3(provider); 11 | 12 | const deploy = async () => { 13 | const accounts = await web3.eth.getAccounts(); 14 | console.log('Attemping to deploy from account', accounts[0]); 15 | 16 | const result = await new web3.eth.Contract(JSON.parse(interface)) 17 | .deploy({ data: bytecode, arguments: ['Hi there!'] }) 18 | .send({ gas: '1000000', from: accounts[0] }); 19 | 20 | console.log(interface); 21 | console.log('Contract deployed to ', result.options.address); 22 | }; 23 | 24 | deploy(); -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "hello", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "mocha" 8 | }, 9 | "author": "", 10 | "license": "ISC", 11 | "dependencies": { 12 | "ganache-cli": "^6.1.0", 13 | "mocha": "^5.0.4", 14 | "solc": "^0.4.21", 15 | "truffle-hdwallet-provider": "0.0.3", 16 | "web3": "^1.0.0-beta.26" 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # Install packages 2 | 3 | npm install 4 | 5 | # Config Mnemonic env variable: 6 | 7 | Copy Mnemonic on Metamask extension. If you forgot it, you can reset it on **Settings/Reveal Seed Words**. 8 | ![enter image description here](https://i.imgur.com/EQg6gl2.png) 9 | 10 | On Windows 10, open **PowerShell** (not cmd) and use bellow command: 11 | 12 | $env:MNEMONIC='12 words of your mnemory' 13 | 14 | # Config Infura URL env variable: 15 | 16 | Register Infura URL on [https://infura.io/](https://infura.io/) 17 | 18 | Check email to receive Infura URL. 19 | 20 | On Windows, open PowerShell and use bellow command: 21 | 22 | $env:INFURA_URL="https://rinkeby.infura.io/yourgeneratedcode" 23 | 24 | # Compile contract: 25 | 26 | node ./compile.js 27 | 28 | # Run unit test: 29 | 30 | npm run test 31 | 32 | # Deploy to Rineby Test Net: 33 | 34 | node ./deploy.js 35 | 36 | Make sure you that: You switched to Rineby Test Net on Metamask and you must have a account with Ethers. You can use [https://faucet.rinkeby.io/](https://faucet.rinkeby.io/) to request some Ethers. 37 | 38 | -------------------------------------------------------------------------------- /test/hello.test.js: -------------------------------------------------------------------------------- 1 | const assert = require('assert'); 2 | const ganache = require('ganache-cli'); 3 | const Web3 = require('web3'); 4 | 5 | const provider = ganache.provider(); 6 | const web3 = new Web3(provider); 7 | 8 | const { interface, bytecode } = require('../compile'); 9 | 10 | let accounts; 11 | let hello; 12 | const INITIAL_THINGS = 'Hi there!'; 13 | 14 | beforeEach(async () => { 15 | // get a list of all accounts 16 | accounts = await web3.eth.getAccounts(); 17 | 18 | hello = await new web3.eth.Contract(JSON.parse(interface)) 19 | .deploy({ data: bytecode, arguments: [INITIAL_THINGS] }) 20 | .send({ from: accounts[0], gas: '1000000' }) 21 | 22 | hello.setProvider(provider); 23 | }) 24 | 25 | describe('Hello', () => { 26 | it('deploys a contract', () => { 27 | assert.ok(hello.options.address); 28 | }); 29 | 30 | it ('has a default message', async () => { 31 | const message = await hello.methods.getMessage().call(); 32 | assert.equal(message, INITIAL_THINGS); 33 | }); 34 | 35 | it('can change the message' , async()=>{ 36 | await hello.methods.setMessage('bye').send({from: accounts[0]}); 37 | const message = await hello.methods.getMessage().call(); 38 | assert.equal(message, 'bye'); 39 | }); 40 | }) 41 | 42 | 43 | 44 | 45 | --------------------------------------------------------------------------------