Skip to content

Commit

Permalink
add prettier config
Browse files Browse the repository at this point in the history
  • Loading branch information
0x-stan committed Aug 25, 2021
1 parent 2893cfe commit 8f54128
Show file tree
Hide file tree
Showing 6 changed files with 171 additions and 138 deletions.
24 changes: 11 additions & 13 deletions basic/01-web3js-deploy/index.js
Original file line number Diff line number Diff line change
@@ -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);
Expand Down
29 changes: 15 additions & 14 deletions basic/02-web3js-transaction/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
44 changes: 22 additions & 22 deletions basic/02-web3js-transaction/index.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
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;

/*
-- Define Provider --
*/
// 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

Expand All @@ -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
Expand Down Expand Up @@ -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);

Expand All @@ -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);
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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++) {
Expand All @@ -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 !!!!');
}
}

Expand All @@ -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) => {
Expand All @@ -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(
{
Expand All @@ -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()
Expand Down
28 changes: 13 additions & 15 deletions basic/03-web3js-erc20/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Loading

0 comments on commit 8f54128

Please sign in to comment.