Skip to content

Commit b0787a8

Browse files
committed
Add web3 adapter for server
1 parent da87f20 commit b0787a8

17 files changed

+304
-81
lines changed

.circleci/circle_dapploy.conf

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ contractOutput=./samples/sample-datavault-react/src/ABI
99
# [AWS]
1010
# bucketName=layerone.smart-contracts/ABI
1111

12-
# Where to place generated web3 interface adaptor on web3 client,
12+
# Where to place generated web3 interface adapter on web3 client,
1313
# add any exclusions to keep from config
1414
[Web3]
15-
web3ModuleOutput=./.circle/reports/web3Adaptor.js
15+
web3ClientPath=./.circle/reports/web3Adapter-client.js
1616
excludeContracts=Migrations,QuadkeyLib,DutchAuctionLib
1717

1818
# Include Portis section if you want Portis support in your web3 client

.dapploy

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ contractOutput=./samples/sample-datavault-react/src/ABI
1010
#bucketName=layerone.smart-contracts
1111
#remotePath=ABI
1212

13-
# Where to place generated web3 interface adaptor on web3 client,
13+
# Where to place generated web3 interface adapter on web3 client,
1414
# add any exclusions to keep from config
1515
[Web3]
16-
web3ModuleOutput=./samples/sample-datavault-react/src/web3Adaptor.js
16+
web3ClientPath=./samples/sample-datavault-react/src/web3Adapter-client.js
1717
excludeContracts=Migrations,QuadkeyLib,DutchAuctionLib
1818

1919
# Include Portis section if you want Portis support in your web3 client

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ cd my-first-coin
122122

123123
Congrats, you just built your first ERC-20 Token! Head on over to [Dapper](https://github.com/XYOracleNetwork/tool-dapper-react) to play with it in a web-ui
124124

125-
4. Checkout some dapploy samples to get another Dapp built with dapploy that uses IPFS and Dapploy's web3 adaptor.
125+
4. Checkout some dapploy samples to get another Dapp built with dapploy that uses IPFS and Dapploy's web3 adapter.
126126

127127
```
128128
cd samples/sample-datavault-solidity && dapploy

dapploy

+1-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const { parseConfig, initConfig } = require(`./src/configParser`)
1212
const { buildAndDeployTemplate, supported } = require(`./src/deployTemplate`)
1313
const defaultConfig = `.dapploy`
1414
program
15-
.version(`0.1.0`)
15+
.version(`0.0.3`)
1616
.option(`-t, --projectDir <dir>`, `Truffle Project Directory`)
1717
.option(`-n, --network <network>`, `Deploy to network`)
1818
.option(`-c, --config <config>`, `Config file`, defaultConfig)
@@ -42,7 +42,6 @@ program.command(`init [dir]`)
4242
}
4343
})
4444

45-
4645
program.parse(process.argv)
4746

4847
async function asyncForEach (array, callback) {

package.json

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "tool-dapploy-nodejs",
3-
"version": "0.0.2",
3+
"version": "0.0.3",
44
"description": "Dapploy is a handy CLI tool for dApps. Create, build and dapploy your Smart Contracts!",
55
"repository": {
66
"type": "git",
@@ -20,7 +20,7 @@
2020
"web3": "^1.0.0-beta.35"
2121
},
2222
"devDependencies": {
23-
"ganache-cli": "^6.1.7",
23+
"babel-eslint": "^10.0.1",
2424
"eslint": "^5.4.0",
2525
"eslint-config-airbnb": "^17.1.0",
2626
"eslint-config-standard": "^12.0.0",
@@ -30,7 +30,8 @@
3030
"eslint-plugin-node": "^7.0.1",
3131
"eslint-plugin-promise": "^4.0.0",
3232
"eslint-plugin-react": "^7.11.1",
33-
"eslint-plugin-standard": "^4.0.0"
33+
"eslint-plugin-standard": "^4.0.0",
34+
"ganache-cli": "^6.1.7"
3435
},
3536
"publishConfig": {
3637
"registry": "https://registry.npmjs.org/"

samples/sample-datavault-react/.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
src/ABI/*.json
2-
src/web3Adaptor.js
2+
src/web3Adapter-client.js
33

44
# Logs
55
logs

samples/sample-datavault-react/src/App.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React, { Component } from 'react'
22
import './App.css'
3-
import { web3, DataVault, injectWeb3, validContract } from './web3Adaptor'
3+
import { web3, DataVault, injectWeb3, validContract } from './web3Adapter-client'
44
import ipfs from './ipfs'
55
import { Button, Grid, Form } from 'react-bootstrap'
66
import { Div, Input } from 'glamorous'

samples/sample-datavault-solidity/.dapploy

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ contractOutput=../sample-datavault-react/src/ABI
1010
bucketName=layerone.smart-contracts
1111
remotePath=ABI
1212

13-
# Where to place generated web3 interface adaptor on web3 client,
13+
# Where to place generated web3 interface adapter on web3 client,
1414
# add any exclusions to keep from config
1515
[Web3]
16-
web3ModuleOutput=../sample-datavault-react/src/web3Adaptor.js
16+
web3ClientPath=../sample-datavault-react/src/web3Adapter-client.js
1717
excludeContracts=Migrations,QuadkeyLib,DutchAuctionLib
1818

1919
# Include Portis section if you want Portis support in your web3 client

src/configParser.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ const configParsing = (program) => {
2727
}
2828

2929
if (config.sections().includes(`Web3`)) {
30-
parseParams(program, `Web3`, [`web3ModuleOutput`])
30+
parseParams(program, `Web3`, [`web3ClientPath`, `web3ServerPath`])
3131
const excludeStr = config.get(`Web3`, `excludeContracts`)
3232
if (excludeStr) {
3333
program.excludeContracts = excludeStr.split(`,`)
@@ -78,7 +78,8 @@ const initConfig = (program) => {
7878

7979
// With String Interpolation, %(key_name)s
8080
config.addSection(`Web3`)
81-
config.set(`Web3`, `web3ModuleOutput`, `./src`)
81+
config.set(`Web3`, `web3ClientPath`, `./src`)
82+
config.set(`Web3`, `web3ServerPath`, `./src`)
8283
config.set(`Web3`, `excludeContracts`, `Migrations`)
8384

8485
config.write(configFile)

src/deployDapp.js

+13-5
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ const path = require(`path`) // from node.js
22
const { uploadRemote } = require(`./awsFolderUploader`)
33
const { migrateTruffle } = require(`./truffleMigrator`)
44
const { execPromise } = require(`./execWrapper`)
5-
const { exportConfig } = require(`./web3ConfigExporter`)
5+
const {
6+
exportClientConfig,
7+
exportServerConfig
8+
} = require(`./web3ConfigExporter`)
69
const { uploadIPFS } = require(`./ipfsUploader`)
710
const tempContractsOutput = `/tmp/tempContractsOutputFolder`
811
/* Copies the contracts in the specified project to a local project (react client, etc)
@@ -40,15 +43,20 @@ const cleanIfNeeded = (program) => {
4043
console.log(` $ Cleaning build folder at ${program.projectDir}`)
4144
return execPromise(clean, { cwd: program.projectDir })
4245
}
43-
console.log(` $ Skipping cleaning (use -clean for clean migration)`)
46+
console.log(` $ Skipping cleaning (use -l for clean migration)`)
4447

4548
return Promise.resolve()
4649
}
4750

4851
const createWeb3Module = (program, contracts) => {
49-
if (program.web3ModuleOutput) {
50-
console.log(` $ Exporting Web3 module to ${program.web3ModuleOutput}`)
51-
exportConfig(program, contracts)
52+
console.log(`HERE`, program.web3ClientPath, program.web3ServerPath)
53+
if (program.web3ClientPath) {
54+
console.log(` $ Exporting Web3 Client module to ${program.web3ClientPath}`)
55+
exportClientConfig(program, contracts)
56+
}
57+
if (program.web3ServerPath) {
58+
console.log(` $ Exporting Web3 Server module to ${program.web3ServerPath}`)
59+
exportServerConfig(program, contracts)
5260
}
5361
}
5462

src/truffleMigrator.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const migrateTruffle = ({
1616

1717
if (!projectDir || !network) {
1818
throw new Error(
19-
`Missing projectDir or network, add to configuration file or pass in param`
19+
`Missing projectDir or network, are you missing a .dapploy file?`
2020
)
2121
}
2222
const command = `${__dirname}/../node_modules/.bin/truffle migrate --network ${network} --reset`

src/web3ConfigExporter.js

+51-17
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
const shell = require(`shelljs`)
22
const path = require(`path`)
33

4-
const templateFile = `${__dirname}/../templates/template-web3.js`
5-
6-
const tempFile = `/tmp/temp999` // Copy the template to working file for us to modify
4+
const getTempFile = type => `./temp999-${type}` // Copy the template to working file for us to modify
75
const fs = require(`fs`)
86

97
const portisConfigString = ({ portisApiKey, network, infuraApiKey }) => {
@@ -45,16 +43,20 @@ const contractDeclarationString = (contracts) => {
4543
return returnString
4644
}
4745

48-
const contractInstantiationString = (params, contracts) => {
49-
let { contractOutput } = params
50-
const { web3ModuleOutput } = params
46+
const contractInstantiationString = (program, contracts, type) => {
47+
let { contractOutput } = program
48+
const { projectDir } = program
49+
const web3AdapterPath =
50+
type === `client` ? program.web3ClientPath : program.web3ServerPath
51+
5152
if (!contractOutput) {
52-
contractOutput = `/add/abi/path/to/config`
53+
contractOutput = `${projectDir}/build/contracts`
5354
}
5455
const relativePath = path.relative(
55-
path.dirname(web3ModuleOutput),
56+
path.dirname(web3AdapterPath),
5657
contractOutput
5758
)
59+
5860
let returnString = `\n`
5961

6062
contracts.forEach((contract) => {
@@ -76,10 +78,9 @@ const contractInstantiationString = (params, contracts) => {
7678
return returnString
7779
}
7880

79-
const exportConfig = (program, contracts) => {
80-
console.log(`Copying`, templateFile, `to`, tempFile)
81-
shell.cp(templateFile, tempFile)
82-
81+
const exportClientConfig = (program, contracts) => {
82+
const tempFile = getTempFile(`client`)
83+
copyTemplate(`client`)
8384
shell.sed(
8485
`-i`,
8586
`PORTIS_DECLARATION`,
@@ -99,14 +100,47 @@ const exportConfig = (program, contracts) => {
99100
contractInstantiationString(program, contracts),
100101
tempFile
101102
)
102-
const outPath = path.dirname(program.web3ModuleOutput)
103+
const outPath = path.dirname(program.web3ClientPath)
104+
shell.mkdir(`-p`, outPath)
105+
if (!fs.existsSync(outPath)) {
106+
return Promise.reject(new Error(`Cannot create web3 adapter at ${outPath}`))
107+
}
108+
console.log(` $ Moving`, tempFile, `to`, program.web3ClientPath)
109+
shell.mv(tempFile, program.web3ClientPath)
110+
return Promise.resolve(true)
111+
}
112+
113+
const copyTemplate = (type) => {
114+
const templateFile = `${__dirname}/../templates/template-web3-${type}.js`
115+
const tempFile = getTempFile(type)
116+
console.log(`Copying`, templateFile, `to`, tempFile)
117+
shell.cp(templateFile, tempFile)
118+
}
119+
120+
const exportServerConfig = (program, contracts) => {
121+
const tempFile = getTempFile(`server`)
122+
copyTemplate(`server`)
123+
124+
shell.sed(
125+
`-i`,
126+
`CONTRACT_DECLARATIONS`,
127+
contractDeclarationString(contracts),
128+
tempFile
129+
)
130+
shell.sed(
131+
`-i`,
132+
`CONTRACT_INSTANTIATION`,
133+
contractInstantiationString(program, contracts),
134+
tempFile
135+
)
136+
const outPath = path.dirname(program.web3ServerPath)
103137
shell.mkdir(`-p`, outPath)
104138
if (!fs.existsSync(outPath)) {
105-
return Promise.reject(new Error(`Cannot create web3 adaptor at ${outPath}`))
139+
return Promise.reject(new Error(`Cannot create web3 adapter at ${outPath}`))
106140
}
107-
console.log(` $ Moving`, tempFile, `to`, program.web3ModuleOutput)
108-
shell.mv(tempFile, program.web3ModuleOutput)
141+
console.log(` $ Moving`, tempFile, `to`, program.web3ServerPath)
142+
shell.mv(tempFile, program.web3ServerPath)
109143
return Promise.resolve(true)
110144
}
111145

112-
module.exports = { exportConfig }
146+
module.exports = { exportClientConfig, exportServerConfig }

templates/template-solidity/.dapploy

+3-2
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@ contractOutput=./src/ABI
1010
# bucketName=layerone.smart-contracts
1111
# remotePath=ABI
1212

13-
# Where to place generated web3 interface adaptor on web3 client,
13+
# Where to place generated web3 interface adapter on web3 client,
1414
# add any exclusions to keep from config
1515
[Web3]
16-
web3ModuleOutput=./src/web3Adaptor.js
16+
web3ClientPath=./src/web3Adapter-client.js
17+
web3ServerPath=./src/web3Adapter-server.js
1718
excludeContracts=Migrations
1819

1920
# Include Portis section if you want Portis support in your web3 client
+21-24
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
require(`babel-register`)({
2-
ignore: /node_modules\/(?!openzeppelin-solidity)/,
2+
ignore: /node_modules\/(?!openzeppelin-solidity)/
33
})
44
require(`babel-polyfill`)
55
const HDWalletProvider = require(`truffle-hdwallet-provider`)
@@ -17,44 +17,41 @@ module.exports = {
1717
development: {
1818
network_id: `*`, // Match any network id
1919
host: `localhost`,
20-
port: 8545,
20+
port: 8545
2121
},
2222
kovan: {
2323
network_id: 42,
2424
from: wallet,
25-
provider: () =>
26-
new HDWalletProvider(
27-
mnemonic,
28-
`https://kovan.infura.io/v3/${infuraKey}`,
29-
),
25+
provider: () => new HDWalletProvider(
26+
mnemonic,
27+
`https://kovan.infura.io/v3/${infuraKey}`
28+
),
3029
gas: 6986331,
31-
gasPrice: 2000000000,
30+
gasPrice: 2500000000
3231
},
3332
ropsten: {
3433
network_id: 3,
35-
provider: () =>
36-
new HDWalletProvider(
37-
mnemonic,
38-
`https://ropsten.infura.io/${infuraKey}`,
39-
),
34+
provider: () => new HDWalletProvider(
35+
mnemonic,
36+
`https://ropsten.infura.io/${infuraKey}`
37+
),
4038
gas: 6986331,
41-
gasPrice: 2000000000,
39+
gasPrice: 2500000000
4240
},
4341
mainnet: {
4442
network_id: `1`,
45-
provider: () =>
46-
new HDWalletProvider(
47-
mnemonic,
48-
`https://mainnet.infura.io/${infuraKey}`,
49-
),
43+
provider: () => new HDWalletProvider(
44+
mnemonic,
45+
`https://mainnet.infura.io/${infuraKey}`
46+
),
5047
gas: 4500000,
51-
gasPrice: 2000000000,
52-
},
48+
gasPrice: 2500000000
49+
}
5350
},
5451
solc: {
5552
optimizer: {
5653
enabled: true,
57-
runs: 500,
58-
},
59-
},
54+
runs: 500
55+
}
56+
}
6057
}

0 commit comments

Comments
 (0)