diff --git a/basic/01-web3js-deploy/index.js b/basic/01-web3js-deploy/index.js index 91e267fb2..90c253a74 100644 --- a/basic/01-web3js-deploy/index.js +++ b/basic/01-web3js-deploy/index.js @@ -1,42 +1,40 @@ -let Web3 = require("web3"); -let solc = require("solc"); -let fs = require("fs"); +let Web3 = require('web3'); +let solc = require('solc'); +let fs = require('fs'); // Get privatekey from sk.txt -require("dotenv").config(); +require('dotenv').config(); const privatekey = process.env.PRIVATE_KEY; // Load contract -const source = fs.readFileSync("Incrementer.sol", "utf8"); +const source = fs.readFileSync('Incrementer.sol', 'utf8'); // compile solidity const input = { - language: "Solidity", + language: 'Solidity', sources: { - "Incrementer.sol": { + 'Incrementer.sol': { content: source, }, }, settings: { outputSelection: { - "*": { - "*": ["*"], + '*': { + '*': ['*'], }, }, }, }; const tempFile = JSON.parse(solc.compile(JSON.stringify(input))); -const contractFile = tempFile.contracts["Incrementer.sol"]["Incrementer"]; +const contractFile = tempFile.contracts['Incrementer.sol']['Incrementer']; // Get bin & abi const bytecode = contractFile.evm.bytecode.object; const abi = contractFile.abi; // Create web3 with kovan provider,you can fix kovan to other testnet -const web3 = new Web3( - "https://kovan.infura.io/v3/" + process.env.INFURA_ID -); +const web3 = new Web3('https://kovan.infura.io/v3/' + process.env.INFURA_ID); // Create account from privatekey const account = web3.eth.accounts.privateKeyToAccount(privatekey); diff --git a/basic/02-web3js-transaction/compile.js b/basic/02-web3js-transaction/compile.js index fc0037984..992b16c9e 100644 --- a/basic/02-web3js-transaction/compile.js +++ b/basic/02-web3js-transaction/compile.js @@ -2,27 +2,28 @@ const fs = require('fs'); const solc = require('solc'); // 以 utf8 方式加载合约 -const source = fs.readFileSync('Incrementer.sol','utf8'); +const source = fs.readFileSync('Incrementer.sol', 'utf8'); // 编译合约 const input = { - language: 'Solidity', - sources: { - 'Incrementer.sol': { - content: source, + language: 'Solidity', + sources: { + 'Incrementer.sol': { + content: source, + }, + }, + settings: { + outputSelection: { + '*': { + '*': ['*'], }, - }, - settings: { - outputSelection: { - '*': { - '*': ['*'], - }, - }, - }, + }, + }, }; const tempFile = JSON.parse(solc.compile(JSON.stringify(input))); -const contractOfIncrementer = tempFile.contracts['Incrementer.sol']['Incrementer']; +const contractOfIncrementer = + tempFile.contracts['Incrementer.sol']['Incrementer']; // 导出合约数据,可以使用 console 打印 contractFile 中的具体内容信息 module.exports = contractOfIncrementer; diff --git a/basic/02-web3js-transaction/index.js b/basic/02-web3js-transaction/index.js index 8d043e769..f2202bcbe 100644 --- a/basic/02-web3js-transaction/index.js +++ b/basic/02-web3js-transaction/index.js @@ -1,8 +1,8 @@ -const Web3 = require("web3"); -const fs = require("fs"); -const contractOfIncrementer = require("./compile"); +const Web3 = require('web3'); +const fs = require('fs'); +const contractOfIncrementer = require('./compile'); -require("dotenv").config(); +require('dotenv').config(); const privatekey = process.env.PRIVATE_KEY; /* @@ -10,8 +10,8 @@ const privatekey = process.env.PRIVATE_KEY; */ // Provider const providerRPC = { - development: "https://kovan.infura.io/v3/" + process.env.INFURA_ID, - moonbase: "https://rpc.testnet.moonbeam.network", + development: 'https://kovan.infura.io/v3/' + process.env.INFURA_ID, + moonbase: 'https://rpc.testnet.moonbeam.network', }; const web3 = new Web3(providerRPC.development); //Change to correct network @@ -34,7 +34,7 @@ const abi = contractOfIncrementer.abi; */ const Trans = async () => { - console.log("============================ 1. Deploy Contract"); + console.log('============================ 1. Deploy Contract'); console.log(`Attempting to deploy from account ${account.address}`); // Create Contract Instance @@ -74,7 +74,7 @@ const Trans = async () => { // Create the contract with contract address console.log(); console.log( - "============================ 2. Call Contract Interface getNumber" + '============================ 2. Call Contract Interface getNumber' ); let incrementer = new web3.eth.Contract(abi, createReceipt.contractAddress); @@ -88,7 +88,7 @@ const Trans = async () => { // Add 3 to Contract Public Variable console.log(); console.log( - "============================ 3. Call Contract Interface increment" + '============================ 3. Call Contract Interface increment' ); const _value = 3; let incrementTx = incrementer.methods.increment(_value); @@ -121,7 +121,7 @@ const Trans = async () => { * */ console.log(); - console.log("============================ 4. Call Contract Interface reset"); + console.log('============================ 4. Call Contract Interface reset'); const resetTx = incrementer.methods.reset(); const resetTransaction = await web3.eth.accounts.signTransaction( @@ -149,26 +149,26 @@ const Trans = async () => { * */ console.log(); - console.log("============================ 5. Listen to Events"); - console.log(" Listen to Increment Event only once && continuouslly"); + console.log('============================ 5. Listen to Events'); + console.log(' Listen to Increment Event only once && continuouslly'); // kovan don't support http protocol to event listen, need to use websocket // more details , please refer to https://medium.com/blockcentric/listening-for-smart-contract-events-on-public-blockchains-fdb5a8ac8b9a const web3Socket = new Web3( new Web3.providers.WebsocketProvider( - "wss://kovan.infura.io/ws/v3/" + process.env.INFURA_ID + 'wss://kovan.infura.io/ws/v3/' + process.env.INFURA_ID ) ); incrementer = new web3Socket.eth.Contract(abi, createReceipt.contractAddress); // listen to Increment event only once - incrementer.once("Increment", (error, event) => { - console.log("I am a onetime event listner, I am going to die now"); + incrementer.once('Increment', (error, event) => { + console.log('I am a onetime event listner, I am going to die now'); }); // listen to Increment event continuouslly incrementer.events.Increment(() => { - console.log("I am a longlive event listner, I get a event now"); + console.log('I am a longlive event listner, I get a event now'); }); for (let step = 0; step < 3; step++) { @@ -186,7 +186,7 @@ const Trans = async () => { if (step == 2) { // clear all the listeners web3Socket.eth.clearSubscriptions(); - console.log("Clearing all the events listeners !!!!"); + console.log('Clearing all the events listeners !!!!'); } } @@ -199,10 +199,10 @@ const Trans = async () => { * */ console.log(); - console.log("============================ 6. Going to get past events"); - const pastEvents = await incrementer.getPastEvents("Increment", { + console.log('============================ 6. Going to get past events'); + const pastEvents = await incrementer.getPastEvents('Increment', { fromBlock: deployedBlockNumber, - toBlock: "latest", + toBlock: 'latest', }); pastEvents.map((event) => { @@ -218,7 +218,7 @@ const Trans = async () => { * */ console.log(); - console.log("============================ 7. Check the transaction error"); + console.log('============================ 7. Check the transaction error'); incrementTx = incrementer.methods.increment(0); incrementTransaction = await web3.eth.accounts.signTransaction( { @@ -232,7 +232,7 @@ const Trans = async () => { const receipt = null; receipt = await web3.eth .sendSignedTransaction(incrementTransaction.rawTransaction) - .on("error", console.error); + .on('error', console.error); }; Trans() diff --git a/basic/03-web3js-erc20/compile.js b/basic/03-web3js-erc20/compile.js index 23d2ca2db..ec3323109 100644 --- a/basic/03-web3js-erc20/compile.js +++ b/basic/03-web3js-erc20/compile.js @@ -2,29 +2,27 @@ const fs = require('fs'); const solc = require('solc'); // Get Path and Load Contract -const source = fs.readFileSync('SimpleToken.sol','utf8'); +const source = fs.readFileSync('SimpleToken.sol', 'utf8'); // Compile Contract const input = { - language: 'Solidity', - sources: { - 'SimpleToken.sol': { - content: source, + language: 'Solidity', + sources: { + 'SimpleToken.sol': { + content: source, + }, + }, + settings: { + outputSelection: { + '*': { + '*': ['*'], }, - }, - settings: { - outputSelection: { - '*': { - '*': ['*'], - }, - }, - }, + }, + }, }; - const tempFile = JSON.parse(solc.compile(JSON.stringify(input))); const contractFile = tempFile.contracts['SimpleToken.sol']['SimpleToken']; - // Export Contract Data module.exports = contractFile; diff --git a/basic/03-web3js-erc20/index.js b/basic/03-web3js-erc20/index.js index 80bcf88e8..40bc52093 100644 --- a/basic/03-web3js-erc20/index.js +++ b/basic/03-web3js-erc20/index.js @@ -1,23 +1,27 @@ const Web3 = require('web3'); -const fs = require("fs"); +const fs = require('fs'); const contractFile = require('./compile'); -require('dotenv').config() +require('dotenv').config(); const privatekey = process.env.PRIVATE_KEY; /* -- Define Provider & Variables -- */ -const receiver = "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266" +const receiver = '0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266'; // Provider -const web3 = new Web3(new Web3.providers.HttpProvider('https://kovan.infura.io/v3/' + process.env.INFURA_ID)); +const web3 = new Web3( + new Web3.providers.HttpProvider( + 'https://kovan.infura.io/v3/' + process.env.INFURA_ID + ) +); //account -const account = web3.eth.accounts.privateKeyToAccount(privatekey); +const account = web3.eth.accounts.privateKeyToAccount(privatekey); const account_from = { - privateKey: account.privateKey, - accountaddress: account.address, + privateKey: account.privateKey, + accountaddress: account.address, }; // sol ---> abi + bin @@ -28,73 +32,80 @@ const abi = contractFile.abi; -- Deploy Contract -- */ const Trans = async () => { - console.log(`Attempting to deploy from account ${account_from.accountaddress}`); - web3.eth.getBlockNumber(function (error, result) { - console.log(result) - }) - // Create deploy Contract Instance - const deployContract = new web3.eth.Contract(abi); - -// method 1 - // Create Constructor Tx - const deployTx = deployContract.deploy({ - data: bytecode, - arguments: ["DAPPLEARNING","DAPP",0,10000000], - }); - - // Sign Transacation and Send - const deployTransaction = await web3.eth.accounts.signTransaction( - { - data: deployTx.encodeABI(), - gas: "8000000", - }, - account_from.privateKey - ); - - // Send Tx and Wait for Receipt - const deployReceipt = await web3.eth.sendSignedTransaction( - deployTransaction.rawTransaction - ); - console.log( - `Contract deployed at address: ${deployReceipt.contractAddress}` - ); - - // method 2 infura not support -// const deployTx2 = await deployContract.deploy({ -// data: bytecode, -// arguments: ["hello","Dapp",1,100000000], -// }).send({ -// from: '0x54A65DB20D7653CE509d3ee42656a8F138037d51', -// gas: 1500000, -// gasPrice: '30000000000000'}). -// then(function(newContractInstance){ -// console.log(newContractInstance.options.address) // instance with the new contract address -// }); - - const erc20Contract = new web3.eth.Contract(abi, deployReceipt.contractAddress); - - - //build the Tx - const transferTx = erc20Contract.methods.transfer(receiver,100000).encodeABI(); - - // Sign Tx with PK - const transferTransaction = await web3.eth.accounts.signTransaction( - { - to: deployReceipt.contractAddress, - data: transferTx, - gas: 8000000, - }, - account_from.privateKey - ); - - // Send Tx and Wait for Receipt - const transferReceipt = await web3.eth.sendSignedTransaction( - transferTransaction.rawTransaction - ); - - erc20Contract.methods.balanceOf(receiver).call().then((result)=>{ + console.log( + `Attempting to deploy from account ${account_from.accountaddress}` + ); + web3.eth.getBlockNumber(function (error, result) { + console.log(result); + }); + // Create deploy Contract Instance + const deployContract = new web3.eth.Contract(abi); + + // method 1 + // Create Constructor Tx + const deployTx = deployContract.deploy({ + data: bytecode, + arguments: ['DAPPLEARNING', 'DAPP', 0, 10000000], + }); + + // Sign Transacation and Send + const deployTransaction = await web3.eth.accounts.signTransaction( + { + data: deployTx.encodeABI(), + gas: '8000000', + }, + account_from.privateKey + ); + + // Send Tx and Wait for Receipt + const deployReceipt = await web3.eth.sendSignedTransaction( + deployTransaction.rawTransaction + ); + console.log(`Contract deployed at address: ${deployReceipt.contractAddress}`); + + // method 2 infura not support + // const deployTx2 = await deployContract.deploy({ + // data: bytecode, + // arguments: ["hello","Dapp",1,100000000], + // }).send({ + // from: '0x54A65DB20D7653CE509d3ee42656a8F138037d51', + // gas: 1500000, + // gasPrice: '30000000000000'}). + // then(function(newContractInstance){ + // console.log(newContractInstance.options.address) // instance with the new contract address + // }); + + const erc20Contract = new web3.eth.Contract( + abi, + deployReceipt.contractAddress + ); + + //build the Tx + const transferTx = erc20Contract.methods + .transfer(receiver, 100000) + .encodeABI(); + + // Sign Tx with PK + const transferTransaction = await web3.eth.accounts.signTransaction( + { + to: deployReceipt.contractAddress, + data: transferTx, + gas: 8000000, + }, + account_from.privateKey + ); + + // Send Tx and Wait for Receipt + const transferReceipt = await web3.eth.sendSignedTransaction( + transferTransaction.rawTransaction + ); + + erc20Contract.methods + .balanceOf(receiver) + .call() + .then((result) => { console.log(`The balance of receiver is ${result}`); - }) + }); }; Trans() @@ -102,4 +113,4 @@ Trans() .catch((error) => { console.error(error); process.exit(1); - }); \ No newline at end of file + }); diff --git a/prettier.config.js b/prettier.config.js new file mode 100644 index 000000000..5a00e77dc --- /dev/null +++ b/prettier.config.js @@ -0,0 +1,25 @@ +// for more details https://prettier.io/docs/en/configuration.html +// prettier.config.js or .prettierrc.js +module.exports = { + trailingComma: 'es5', + tabWidth: 2, + Tabs: false, + overrides: [ + { + files: ['*.js', '*.ts'], + options: { + semi: true, + singleQuote: true, + }, + }, + // solidity + { + files: '*.sol', + options: { + semi: false, + tabWidth: 4, + printWidth: 120, + }, + }, + ], +};