-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
103 lines (90 loc) · 2.75 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
const _ = require('lodash')
const fs = require('fs')
const path = require('path')
const deploy = require('./src/js/exercise/index')
const blockchain = require('./src/js/exercise/blockchain')
const solc = require('solc')
const WEBSITE_TPL = _.template(fs.readFileSync(path.resolve(__dirname, './assets/website.html')))
const EBOOK_TPL = _.template(fs.readFileSync(path.resolve(__dirname, './assets/ebook.html')))
const assertLibrary = fs.readFileSync(path.resolve(__dirname, './src/sol/Assert.sol'), 'utf8')
const isWriteMode = () => {
return process.env.WRITE_MODE && JSON.parse(process.env.WRITE_MODE)
}
async function deployAssertLibrary () {
if (isWriteMode()) {
return
}
const input = {
'Assert.sol': assertLibrary
}
const codes = solc.compile({sources: input}, 1)
this.config.values.variables.assertLibrary = await blockchain.deploy(codes.contracts['Assert.sol:Assert'])
}
/**
* Manage all pre-operations necessary for the exercise to work
* @param {{blocks: Array<{name: string, body: string}>}} blk - Information about the block being parsed
* @returns {string} - HTML code to insert into the webpage
*/
async function processDeployement (blk) {
const log = this.book.log
const codes = {}
_.each(blk.blocks, function (_blk) {
codes[_blk.name] = _blk.body.trim()
})
// To have a quick update on local machine deployment can be disabled
if (!isWriteMode()) {
// Compile and deploy test contracts to our blockchain
codes.deployed = await deploy(codes, { address: this.config.values.variables.assertLibrary, source: assertLibrary })
} else {
codes.exerciseId = -1
codes.deployed = []
}
codes.deployed = JSON.stringify(codes.deployed)
if (codes.hints === undefined) {
// TODO: when no hints, what shall we do?
// Rewrite client side verification to include only the abi
} else {
codes.hints = await this.book.renderBlock('markdown', codes.hints)
}
// Select appropriate template
const tpl = (this.generator === 'website' ? WEBSITE_TPL : EBOOK_TPL)
let wording = await this.book.renderBlock('markdown', blk.body)
wording = wording.replace('<p>', '').replace('</p>', '')
return tpl({
message: wording,
codes: codes
})
}
module.exports = {
website: {
assets: './assets',
js: [
'ace/ace.js',
'ace/theme-tomorrow.js',
'ace/ext-language_tools.js',
'ace/mode-solidity.js',
'dist/bundle.js',
'dist/0.bundle.js'
],
css: [
'exercises.css',
'hint.css'
]
},
ebook: {
assets: './assets',
css: [
'ebook.css'
]
},
hooks: {
init: deployAssertLibrary
},
blocks: {
exercise: {
parse: false,
blocks: ['hints', 'initial', 'solution', 'validation'],
process: processDeployement
}
}
}