-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathserver.js
127 lines (103 loc) · 3.26 KB
/
server.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
const fs = require('fs')
const ganache = require('ganache-cli')
const path = require('path')
const { exec } = require('child_process')
const Api = require('./api')
const Cheshire = require('./lib/cheshire.js')
const config = require('./config.json')
const Contract = require('./lib/contract.js')
const User = require('./lib/user.js')
const { log } = console
const Server = {
startTestnet() {
log('> Starting testnet...')
ganache.server({
accounts: config.accounts,
// debug: true,
// logger: console,
// verbose: true,
})
.listen(config.portTestnet, (err) => {
if (err) {
log('Error starting Ganache:', err)
process.exit(1)
}
})
},
async compileContracts() {
log('> Compiling contracts...')
return new Promise((resolve, reject) => {
const child = exec('truffle compile')
child.stdout.pipe(process.stdout)
child.stderr.pipe(process.stderr)
child.on('error', log)
child.on('close', (code) => {
if (code === 0) {
resolve()
} else {
log('Exited with code', code)
reject()
}
})
})
},
async deployContracts() {
log('> Deploying CryptoKitties contracts to testnet...')
const ownerCut = 375
const kittyCore = await Contract.deploy('KittyCore')
const saleClockAuction = await Contract.deploy('SaleClockAuction', kittyCore.address, ownerCut)
const siringClockAuction = await Contract.deploy('SiringClockAuction', kittyCore.address, ownerCut)
const geneScience = await Contract.deploy('GeneScience')
await kittyCore.setSaleAuctionAddress(saleClockAuction.address)
await kittyCore.setSiringAuctionAddress(siringClockAuction.address)
await kittyCore.setGeneScienceAddress(geneScience.address)
await kittyCore.unpause()
return {
kittyCore: kittyCore.address,
saleClockAuction: saleClockAuction.address,
siringClockAuction: siringClockAuction.address,
geneScience: geneScience.address,
}
},
async startApiServer() {
log('> Starting local CryptoKitties API server...')
Api.listen(config.portApi)
},
async loadAccounts() {
log('> Loading accounts')
config.accounts.forEach(async (account, index) => {
const apiObject = {
address: account.address,
nickname: `User #${index}`,
image: '1',
}
await User.createUser(account.address, account.address, apiObject)
})
},
async runSetupScript() {
log('> Running setup script...')
const scriptPath = path.join(process.cwd(), process.argv[2] || './scripts/setup.js')
if (!fs.existsSync(scriptPath)) {
log('Exiting, script not found:', scriptPath)
process.exit(1)
}
const cheshire = new Cheshire(config)
console.group()
// eslint-disable-next-line global-require, import/no-dynamic-require
await require(scriptPath)(cheshire)
console.groupEnd()
},
async start() {
await this.startTestnet()
await this.compileContracts()
await this.deployContracts()
await this.startApiServer()
await this.loadAccounts()
await this.runSetupScript()
log('')
log('Cheshire is live 😺 Here\'s what\'s inside:')
log('')
await new Cheshire(config).printHelp()
},
}
Server.start()