Advertisement
Guest User

Untitled

a guest
Jun 13th, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const fs = require('fs');
  2. const path = require('path');
  3. const Web3 = require('web3');
  4. const dotenv = require('dotenv');
  5. const Sample = artifacts.require('Sample.sol');
  6. const {Enigma, utils, eeConstants} = require('enigma-js/node');
  7.  
  8. dotenv.config({path:path.resolve(process.cwd(), '..', '.env')});
  9.  
  10. const migrationsFolder = process.cwd();   // save it because it changes later on...
  11.  
  12. var EnigmaContract;
  13. if(typeof process.env.SGX_MODE === 'undefined' || (process.env.SGX_MODE != 'SW' && process.env.SGX_MODE != 'HW' )) {
  14.     console.log(`Error reading ".env" file, aborting....`);
  15.     process.exit();
  16. } else if (process.env.SGX_MODE == 'SW') {
  17.   EnigmaContract = require('../build/enigma_contracts/EnigmaSimulation.json');
  18. } else {
  19.   EnigmaContract = require('../build/enigma_contracts/Enigma.json');
  20. }
  21. const EnigmaTokenContract = require('../build/enigma_contracts/EnigmaToken.json');
  22. const provider = new Web3.providers.HttpProvider('http://localhost:9545');
  23. const web3 = new Web3(provider);
  24. var enigma = null;
  25.  
  26. function sleep(ms) {
  27.   return new Promise(resolve => setTimeout(resolve, ms));
  28. }
  29.  
  30. async function deploySecretContract(config){
  31.   console.log(`Deploying Secret Contract "${config.filename}"...`);
  32.   var scTask;
  33.   var preCode;
  34.  
  35.   try {
  36.     preCode = fs.readFileSync(path.resolve(migrationsFolder, '../build/secret_contracts', config.filename));
  37.     preCode = preCode.toString('hex');
  38.   } catch(e) {
  39.     console.log('Error:', e.stack);
  40.   }
  41.  
  42.   scTask = await new Promise((resolve, reject) => {
  43.     enigma.deploySecretContract(config.fn, config.args, config.gasLimit, config.gasPrice, config.from, preCode)
  44.       .on(eeConstants.DEPLOY_SECRET_CONTRACT_RESULT, (receipt) => resolve(receipt))
  45.       .on(eeConstants.ERROR, (error) => reject(error));
  46.   });
  47.  
  48.   // Wait for the confirmed deploy contract task
  49.   do {
  50.     await sleep(1000);
  51.     scTask = await enigma.getTaskRecordStatus(scTask);
  52.     process.stdout.write('Waiting. Current Task Status is '+scTask.ethStatus+'\r');
  53.   } while (scTask.ethStatus != 2);
  54.   process.stdout.write('Completed. Final Task Status is '+scTask.ethStatus+'\n');
  55.  
  56.   // Verify deployed contract
  57.   var result = await enigma.admin.isDeployed(scTask.scAddr);
  58.   if(result) {
  59.  
  60.     fs.writeFile(path.join('../test/',config.filename.replace(/\.wasm$/, '.txt')), scTask.scAddr, 'utf8', function(err) {
  61.       if(err) {
  62.         return console.log(err);
  63.       }
  64.     });
  65.  
  66.     return scTask.scAddr;
  67.   } else {
  68.     console.log('Something went wrong deploying Secret Contract "${contract}", aborting');
  69.     process.exit();
  70.   }
  71. }
  72.  
  73. module.exports = async function(deployer, network, accounts) {
  74.  
  75.   enigma = new Enigma(
  76.     web3,
  77.     EnigmaContract.networks['4447'].address,
  78.     EnigmaTokenContract.networks['4447'].address,
  79.     'http://localhost:3346',
  80.     {
  81.       gas: 4712388,
  82.       gasPrice: 100000000000,
  83.       from: accounts[0],
  84.     },
  85.   );
  86.   enigma.admin();
  87.  
  88.   // Deploy your Smart and Secret contracts below this point:
  89.  
  90.   deployer.deploy(Sample).then(function(){
  91.     console.log(`Smart Contract "Sample.Sol" has been deployed at ETH address: ${Sample.address}`);
  92.     return;
  93.   });
  94.  
  95.   const config = {
  96.     filename: 'simple_addition.wasm',
  97.     fn: 'construct()',
  98.     args: '',
  99.     gasLimit: 100000,
  100.     gasPrice: utils.toGrains(1),
  101.     from: accounts[0]
  102.   };
  103.   const address = await deploySecretContract(config);
  104.   console.log(`Secret Contract "${config.filename}" deployed at Enigma address: ${address}`);
  105.  
  106.   // Deploy custom smart contract
  107.   const configMillionairesProblem = {
  108.     filename: 'millionaires_problem.wasm',
  109.     fn: 'construct()',
  110.     args: [],
  111.     gasLimit: 1000000,
  112.     gasPrice: utils.toGrains(1),
  113.     from: accounts[0]
  114.   };
  115.   const addressMillionairesProblem = await deploySecretContract(configMillionairesProblem);
  116.   console.log(`Secret Contract "${configMillionairesProblem.filename}" deployed at Enigma address: ${addressMillionairesProblem}`);
  117. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement