Guest User

Untitled

a guest
Oct 9th, 2018
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // A $( document ).ready() block.
  2. $(document).ready(function() {
  3.     console.log('ready!');
  4. });
  5. // Initialize Web3
  6. if (typeof web3 !== 'undefined') {
  7.     web3 = new Web3(web3.currentProvider);
  8. } else {
  9.     web3 = new Web3(new Web3.providers.HttpProvider('http://localhost:8545'));
  10. }
  11.  
  12. // Set Account
  13. web3.eth.defaultAccount = web3.eth.accounts[0];
  14. // Set Contract Abi
  15. var contractAbi = [
  16.     {
  17.         constant: false,
  18.         inputs: [
  19.             {
  20.                 name: '_ipfsAddress',
  21.                 type: 'string'
  22.             },
  23.             {
  24.                 name: '_author',
  25.                 type: 'string'
  26.             }
  27.         ],
  28.         name: 'createKidney',
  29.         outputs: [],
  30.         payable: false,
  31.         stateMutability: 'nonpayable',
  32.         type: 'function'
  33.     },
  34.     {
  35.         anonymous: false,
  36.         inputs: [
  37.             {
  38.                 indexed: false,
  39.                 name: 'id',
  40.                 type: 'uint256'
  41.             },
  42.             {
  43.                 indexed: false,
  44.                 name: 'date',
  45.                 type: 'uint256'
  46.             },
  47.             {
  48.                 indexed: false,
  49.                 name: 'ipfsAddress',
  50.                 type: 'string'
  51.             },
  52.             {
  53.                 indexed: false,
  54.                 name: 'author',
  55.                 type: 'string'
  56.             }
  57.         ],
  58.         name: 'KidneyCreated',
  59.         type: 'event'
  60.     },
  61.     {
  62.         constant: false,
  63.         inputs: [
  64.             {
  65.                 name: 'id',
  66.                 type: 'uint256'
  67.             }
  68.         ],
  69.         name: 'toggleActive',
  70.         outputs: [],
  71.         payable: false,
  72.         stateMutability: 'nonpayable',
  73.         type: 'function'
  74.     },
  75.     {
  76.         inputs: [],
  77.         payable: false,
  78.         stateMutability: 'nonpayable',
  79.         type: 'constructor'
  80.     },
  81.     {
  82.         constant: true,
  83.         inputs: [
  84.             {
  85.                 name: 'id',
  86.                 type: 'uint256'
  87.             }
  88.         ],
  89.         name: 'getKidney',
  90.         outputs: [
  91.             {
  92.                 name: '',
  93.                 type: 'uint256'
  94.             },
  95.             {
  96.                 name: '',
  97.                 type: 'uint256'
  98.             },
  99.             {
  100.                 name: '',
  101.                 type: 'string'
  102.             },
  103.             {
  104.                 name: '',
  105.                 type: 'string'
  106.             },
  107.             {
  108.                 name: '',
  109.                 type: 'bool'
  110.             }
  111.         ],
  112.         payable: false,
  113.         stateMutability: 'view',
  114.         type: 'function'
  115.     },
  116.     {
  117.         constant: true,
  118.         inputs: [],
  119.         name: 'getKidneyIds',
  120.         outputs: [
  121.             {
  122.                 name: '',
  123.                 type: 'uint256[]'
  124.             }
  125.         ],
  126.         payable: false,
  127.         stateMutability: 'view',
  128.         type: 'function'
  129.     }
  130. ]; // Add Your Contract ABI here!!!
  131.  
  132. // Set Contract Address
  133. var contractAddress = '0xd24be3716b795be94a9a1c7e85c2f14135a4ee81'; // Add Your Contract address here!!!
  134.  
  135. // Set the Contract
  136. var contract = web3.eth.contract(contractAbi).at(contractAddress);
  137.  
  138. $('#form1').on('submit', event => {
  139.     event.preventDefault();
  140.     var organ = $('#organ').val();
  141.     var author = $('#author').val();
  142.     var obj = {
  143.         organ,
  144.         author
  145.     };
  146.     console.log(obj);
  147.     contract.createKidney(
  148.         organ,
  149.         author,
  150.         { from: web3.eth.defaultAccount, gas: 1000000 },
  151.         err => {
  152.             console.log(err);
  153.         }
  154.     );
  155. });
  156.  
  157. $('#form2').on('submit', event => {
  158.     event.preventDefault();
  159.     var id = $('#kidneyId').val();
  160.     contract.getKidney(id, err => {
  161.         console.log(err);
  162.     });
  163. });
  164. (async () => {
  165.     const accounts = await web3.eth.getAccounts();
  166.     console.log(accounts);
  167.  
  168.     const balance = await web3.eth.getBalance(accounts[0]);
  169.     console.log('balance', web3.utils.fromWei(balance, 'ether'));
  170. })();
  171.  
  172. $('#getKidneyIds').click(() => {
  173.     contract.getKidneyIds().call(err => console.log(err));
  174. });
Add Comment
Please, Sign In to add comment