solc.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. const PouchDB = require('pouchdb')
  2. // const moment = require('moment')
  3. // const superagent = require('superagent')
  4. const compiler = require('solc')
  5. /* Set Slack bot token. */
  6. const dbAuth = process.env.DB_AUTH
  7. /* Initialize database. */
  8. const db = new PouchDB(`http://apecs:${dbAuth}@localhost:5984/projects`)
  9. /**
  10. * Solidity Compiler
  11. */
  12. const solc = async function (req, res) {
  13. console.log('\nSTARTED SOLC');
  14. /* Set body. */
  15. const body = req.body
  16. console.log('\nBODY', body)
  17. /* Validate body. */
  18. if (!body) {
  19. /* Set status code. */
  20. res.status(400)
  21. return res.json({
  22. error: 'A request BODY is required with this request.'
  23. })
  24. }
  25. // const path = require('path')
  26. // const fs = require('fs')
  27. // const solc = require('solc')
  28. //
  29. // const sample = path.resolve(__dirname, 'contracts', 'hello.sol')
  30. // const source = fs.readFileSync(sample, 'UTF-8')
  31. // console.log('\nSOURCE', source)
  32. //
  33. // // const compiled = solc.compile(source, 1)
  34. //
  35. // var input = {
  36. // language: 'Solidity',
  37. // sources: {
  38. // 'hello.sol' : {
  39. // content: source
  40. // }
  41. // },
  42. // settings: {
  43. // outputSelection: {
  44. // '*': {
  45. // '*': [ '*' ]
  46. // }
  47. // }
  48. // }
  49. // };
  50. //
  51. // const output = JSON.parse(solc.compile(JSON.stringify(input)))
  52. // console.log('\nOUTPUT', output)
  53. //
  54. // const details = output.contracts['hello.sol']['Hello']
  55. // console.log('\nDETAILS', details)
  56. //
  57. // const bytecode = details.evm.bytecode.object
  58. // console.log('\nBYTECODE\n', bytecode)
  59. //
  60. // const deployedBytecode = details.evm.deployedBytecode.object
  61. // console.log('\nBYTECODE MATCH', bytecode == deployedBytecode)
  62. //
  63. // if (bytecode != deployedBytecode) {
  64. // console.log('\nDEPLOYED BYTECODE\n', deployedBytecode)
  65. //
  66. // }
  67. const input = {
  68. language: 'Solidity',
  69. sources: {
  70. 'hi-there.sol': {
  71. content: body.source
  72. }
  73. },
  74. settings: {
  75. outputSelection: {
  76. '*': {
  77. '*': ['*']
  78. }
  79. }
  80. }
  81. }
  82. let output
  83. try {
  84. output = JSON.parse(compiler.compile(JSON.stringify(input)))
  85. // `output` here contains the JSON output as specified in the documentation
  86. for (var contractName in output.contracts['test.sol']) {
  87. console.log(
  88. contractName +
  89. ': ' +
  90. output.contracts['test.sol'][contractName].evm.bytecode.object
  91. );
  92. }
  93. } catch (err) {
  94. console.error(err)
  95. }
  96. // solc.loadRemoteVersion('0.4.21+commit.dfe3193c', (_err, _solc) => {
  97. // if (_err) {
  98. // console.error('SOMETHING WENT WRONG', _err)
  99. // } else {
  100. // // NOTE: Use `solcSnapshot` here with the same interface `solc` has
  101. // const compiled = JSON.parse(_solc.compile(JSON.stringify(input)))
  102. // console.log('\nCOMPILED', compiled)
  103. // }
  104. // })
  105. //
  106. console.log('OUTPUT', output)
  107. /* Return all output. */
  108. res.json(output)
  109. }
  110. module.exports = solc