script.jsx 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. //EXAMPLE
  2. //helper function to get URL parameters.
  3. var urlParams;
  4. (window.onpopstate = function () {
  5. var match,
  6. pl = /\+/g, // Regex for replacing addition symbol with a space
  7. search = /([^&=]+)=?([^&]*)/g,
  8. decode = function (s) { return decodeURIComponent(s.replace(pl, " ")); },
  9. query = window.location.search.substring(1);
  10. urlParams = {};
  11. while (match = search.exec(query))
  12. urlParams[decode(match[1])] = decode(match[2]);
  13. })();
  14. /*------------------*/
  15. var config;
  16. if("config" in urlParams) {
  17. config = urlParams['config'];
  18. } else {
  19. var config = 'reactor_config.json'; //random default
  20. }
  21. $.ajax({
  22. url: config,
  23. dataType: 'json',
  24. cache: false,
  25. error: function(data) {
  26. console.log(data);
  27. },
  28. success: function(data) {
  29. //map through multiple contracts (this includes multiple ones in 1 file + different files).
  30. //console.log(data);
  31. var total_compiled = {};
  32. var addresses = {};
  33. var templates = {};
  34. var options = {};
  35. Object.keys(data["contracts"]).map(function(contract_name) { //iterate through multiple contracts based on keys
  36. $.ajax({
  37. //fetch .sol and compile it, adding compiled result & its specified address to separate dictionaries
  38. //3 parts: the compiled code from .sols. The address mapping. The templates.
  39. url: data["contracts"][contract_name].path,
  40. dataType: 'text',
  41. cache: false,
  42. async: false,
  43. success: function(contract) {
  44. /*
  45. This is slightly "hacky". If one file has multiple contracts, it returns one dictionary.
  46. This concatenates them in the scenario where there are multiple files as well.
  47. */
  48. compiled = web3.eth.compile.solidity(contract);
  49. Object.keys(compiled).map(function(compiled_contract_name) {
  50. if(total_compiled.hasOwnProperty(compiled_contract_name) == false) { //not yet inserted
  51. addresses[compiled_contract_name] = data["contracts"][compiled_contract_name].address; //not sure why I've been doing [] & . notation here.
  52. templates[compiled_contract_name] = data["contracts"][compiled_contract_name].template;
  53. options[compiled_contract_name] = {"template_overlay": data["contracts"][compiled_contract_name].template_overlay};
  54. }
  55. });
  56. $.extend(total_compiled, compiled);
  57. }
  58. });
  59. });
  60. console.log(total_compiled);
  61. console.log(addresses);
  62. console.log(templates);
  63. console.log(options);
  64. $.ajax({
  65. url: "../config.json",
  66. dataType: 'json',
  67. cache: false,
  68. })
  69. .done(function(data) {
  70. React.render(<Header data={data} />, document.getElementById('top'));
  71. React.render(<Reactor templates={templates} compiled={total_compiled} addresses={addresses} options={options}/>, document.getElementById('contracts'));
  72. });
  73. }
  74. });