-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathoneclick.js
99 lines (86 loc) · 2.44 KB
/
oneclick.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
var fs = require('fs'); // include file system module
const axios = require('axios');
var path = require('path');
var cl = require('colors-cli');
const ONE_CLICK_DAPP_URL = 'https://oneclickdapp.com';
const STARTUP_MESSAGE =
'\nNeat contracts! (^̮^) You are doing some cool stuff!\n>>> Generating interface(s)....';
const contract = cl.cyan;
const welcome = cl.cyan;
const url = cl.yellow;
const getContractText = (contractName, network, abi, mnemonic) => {
const abiLength = abi.length;
return `\n${contract(contractName)} (${
abi.length
} functions) on network ${network} can be used at:\n\n${url(
`${ONE_CLICK_DAPP_URL}/${mnemonic}`
)}\n`;
};
const getNetworkMSG = contractName => {
return `\nNo network listed for ${contract(
contractName
)}, Please run: truffle migrate\nIf this doesn't fix the issue, check that the contract is being deployed properly.`;
};
module.exports = config => {
console.log(welcome(STARTUP_MESSAGE));
const contractBuildDir = path.join(
config._values.working_directory,
'build',
'contracts'
);
const filenames = fs.readdirSync(contractBuildDir, (err, data) => {
if (err) {
throw err;
}
return data;
});
filenames.forEach(filename => {
const filePath = path.join(contractBuildDir, filename);
createInterface(filePath);
});
};
function createInterface(dataSource) {
let fileContents = null;
let jsonParsed = null;
try {
fileContents = fs.readFileSync(
dataSource, // "/path/to/file.json"
'utf-8'
);
} catch (err) {
console.error(err);
return;
}
try {
jsonParsed = JSON.parse(fileContents);
} catch (err) {
console.error(err);
return;
}
const abi = jsonParsed.abi;
const contractName = jsonParsed.contractName;
const networkIds = Object.keys(jsonParsed.networks);
if (networkIds.length < 1) {
console.log(getNetworkMSG(contractName));
}
networkIds.forEach(networkId => {
const deployment = jsonParsed.networks[networkId];
const address = deployment.address;
axios
.post(`${ONE_CLICK_DAPP_URL}/contracts`, {
contractName: contractName,
contractAddress: address,
abi: abi,
network: networkId,
creatorAddress: 'truffle-plugin'
})
.then(res => {
console.log(
getContractText(contractName, networkId, abi, res.data.mnemonic)
);
})
.catch(err => {
console.log(err.message);
});
});
}