abrhsn1998

web3.js

Nov 23rd, 2020
253
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import Web3 from 'web3';
  2.  
  3. var Tx = require('ethereumjs-tx').Transaction;
  4.  
  5. var accountAddress = '0x65C2f74a732b2825fF9B244361E2C6dd3aD40dC0';
  6.  
  7. var accountPrivateKey = ''// My Private Key
  8.  
  9. var privateKey = Buffer.from(accountPrivateKey, 'hex');
  10.  
  11. var contractAddress = ''// My contract Code
  12.  
  13. var abi = [
  14. {
  15. "inputs": [
  16. {
  17. "internalType": "uint256",
  18. "name": "_sensorId",
  19. "type": "uint256"
  20. },
  21. {
  22. "internalType": "string",
  23. "name": "_date",
  24. "type": "string"
  25. },
  26. {
  27. "internalType": "string",
  28. "name": "_hour",
  29. "type": "string"
  30. },
  31. {
  32. "internalType": "string",
  33. "name": "_minute",
  34. "type": "string"
  35. },
  36. {
  37. "internalType": "uint256[]",
  38. "name": "_datas",
  39. "type": "uint256[]"
  40. }
  41. ],
  42. "name": "createData",
  43. "outputs": [
  44. {
  45. "internalType": "uint256",
  46. "name": "",
  47. "type": "uint256"
  48. }
  49. ],
  50. "stateMutability": "payable",
  51. "type": "function"
  52. },
  53. {
  54. "inputs": [
  55. {
  56. "internalType": "string",
  57. "name": "_sensorString",
  58. "type": "string"
  59. }
  60. ],
  61. "name": "createSensor",
  62. "outputs": [
  63. {
  64. "internalType": "uint256",
  65. "name": "",
  66. "type": "uint256"
  67. }
  68. ],
  69. "stateMutability": "payable",
  70. "type": "function"
  71. },
  72. {
  73. "inputs": [],
  74. "stateMutability": "payable",
  75. "type": "constructor"
  76. },
  77. {
  78. "inputs": [],
  79. "name": "dataCounter",
  80. "outputs": [
  81. {
  82. "internalType": "uint256",
  83. "name": "",
  84. "type": "uint256"
  85. }
  86. ],
  87. "stateMutability": "view",
  88. "type": "function"
  89. },
  90. {
  91. "inputs": [
  92. {
  93. "internalType": "uint256",
  94. "name": "",
  95. "type": "uint256"
  96. }
  97. ],
  98. "name": "dataList",
  99. "outputs": [
  100. {
  101. "internalType": "uint256",
  102. "name": "dataId",
  103. "type": "uint256"
  104. },
  105. {
  106. "internalType": "uint256",
  107. "name": "sensorId",
  108. "type": "uint256"
  109. },
  110. {
  111. "internalType": "string",
  112. "name": "date",
  113. "type": "string"
  114. },
  115. {
  116. "internalType": "string",
  117. "name": "hour",
  118. "type": "string"
  119. },
  120. {
  121. "internalType": "string",
  122. "name": "minute",
  123. "type": "string"
  124. }
  125. ],
  126. "stateMutability": "view",
  127. "type": "function"
  128. },
  129. {
  130. "inputs": [
  131. {
  132. "internalType": "string",
  133. "name": "_date",
  134. "type": "string"
  135. },
  136. {
  137. "internalType": "string",
  138. "name": "_hour",
  139. "type": "string"
  140. },
  141. {
  142. "internalType": "string",
  143. "name": "_minute",
  144. "type": "string"
  145. }
  146. ],
  147. "name": "dateToString",
  148. "outputs": [
  149. {
  150. "internalType": "string",
  151. "name": "",
  152. "type": "string"
  153. }
  154. ],
  155. "stateMutability": "pure",
  156. "type": "function"
  157. },
  158. {
  159. "inputs": [
  160. {
  161. "internalType": "uint256",
  162. "name": "_dataId",
  163. "type": "uint256"
  164. }
  165. ],
  166. "name": "getDataById",
  167. "outputs": [
  168. {
  169. "components": [
  170. {
  171. "internalType": "uint256",
  172. "name": "dataId",
  173. "type": "uint256"
  174. },
  175. {
  176. "internalType": "uint256",
  177. "name": "sensorId",
  178. "type": "uint256"
  179. },
  180. {
  181. "internalType": "string",
  182. "name": "date",
  183. "type": "string"
  184. },
  185. {
  186. "internalType": "string",
  187. "name": "hour",
  188. "type": "string"
  189. },
  190. {
  191. "internalType": "string",
  192. "name": "minute",
  193. "type": "string"
  194. },
  195. {
  196. "internalType": "uint256[]",
  197. "name": "datas",
  198. "type": "uint256[]"
  199. }
  200. ],
  201. "internalType": "struct MainContract.Data",
  202. "name": "",
  203. "type": "tuple"
  204. }
  205. ],
  206. "stateMutability": "view",
  207. "type": "function"
  208. },
  209. {
  210. "inputs": [],
  211. "name": "getLatestData",
  212. "outputs": [
  213. {
  214. "components": [
  215. {
  216. "internalType": "uint256",
  217. "name": "dataId",
  218. "type": "uint256"
  219. },
  220. {
  221. "internalType": "uint256",
  222. "name": "sensorId",
  223. "type": "uint256"
  224. },
  225. {
  226. "internalType": "string",
  227. "name": "date",
  228. "type": "string"
  229. },
  230. {
  231. "internalType": "string",
  232. "name": "hour",
  233. "type": "string"
  234. },
  235. {
  236. "internalType": "string",
  237. "name": "minute",
  238. "type": "string"
  239. },
  240. {
  241. "internalType": "uint256[]",
  242. "name": "datas",
  243. "type": "uint256[]"
  244. }
  245. ],
  246. "internalType": "struct MainContract.Data",
  247. "name": "",
  248. "type": "tuple"
  249. }
  250. ],
  251. "stateMutability": "view",
  252. "type": "function"
  253. },
  254. {
  255. "inputs": [
  256. {
  257. "internalType": "uint256",
  258. "name": "_sensorId",
  259. "type": "uint256"
  260. }
  261. ],
  262. "name": "getSensorById",
  263. "outputs": [
  264. {
  265. "components": [
  266. {
  267. "internalType": "uint256",
  268. "name": "sensorId",
  269. "type": "uint256"
  270. },
  271. {
  272. "internalType": "string",
  273. "name": "_sensorString",
  274. "type": "string"
  275. },
  276. {
  277. "internalType": "uint256[]",
  278. "name": "sensorDataList",
  279. "type": "uint256[]"
  280. }
  281. ],
  282. "internalType": "struct MainContract.Sensor",
  283. "name": "",
  284. "type": "tuple"
  285. },
  286. {
  287. "components": [
  288. {
  289. "internalType": "uint256",
  290. "name": "dataId",
  291. "type": "uint256"
  292. },
  293. {
  294. "internalType": "uint256",
  295. "name": "sensorId",
  296. "type": "uint256"
  297. },
  298. {
  299. "internalType": "string",
  300. "name": "date",
  301. "type": "string"
  302. },
  303. {
  304. "internalType": "string",
  305. "name": "hour",
  306. "type": "string"
  307. },
  308. {
  309. "internalType": "string",
  310. "name": "minute",
  311. "type": "string"
  312. },
  313. {
  314. "internalType": "uint256[]",
  315. "name": "datas",
  316. "type": "uint256[]"
  317. }
  318. ],
  319. "internalType": "struct MainContract.Data[]",
  320. "name": "",
  321. "type": "tuple[]"
  322. }
  323. ],
  324. "stateMutability": "view",
  325. "type": "function"
  326. },
  327. {
  328. "inputs": [],
  329. "name": "sensorCounter",
  330. "outputs": [
  331. {
  332. "internalType": "uint256",
  333. "name": "",
  334. "type": "uint256"
  335. }
  336. ],
  337. "stateMutability": "view",
  338. "type": "function"
  339. },
  340. {
  341. "inputs": [
  342. {
  343. "internalType": "uint256",
  344. "name": "",
  345. "type": "uint256"
  346. }
  347. ],
  348. "name": "sensorList",
  349. "outputs": [
  350. {
  351. "internalType": "uint256",
  352. "name": "sensorId",
  353. "type": "uint256"
  354. },
  355. {
  356. "internalType": "string",
  357. "name": "_sensorString",
  358. "type": "string"
  359. }
  360. ],
  361. "stateMutability": "view",
  362. "type": "function"
  363. },
  364. {
  365. "inputs": [
  366. {
  367. "internalType": "string",
  368. "name": "",
  369. "type": "string"
  370. }
  371. ],
  372. "name": "timeToData",
  373. "outputs": [
  374. {
  375. "internalType": "uint256",
  376. "name": "",
  377. "type": "uint256"
  378. }
  379. ],
  380. "stateMutability": "view",
  381. "type": "function"
  382. }
  383. ];
  384.  
  385. const web3 = new Web3(
  386. new Web3.providers.HttpProvider(
  387. 'https://ropsten.infura.io/v3/cbd9d5f7455940279cb13adf10ad5e77'
  388. )
  389. );
  390.  
  391. // const web3 = new Web3('http://localhost:7545');
  392.  
  393. var contract = new web3.eth.Contract(abi, contractAddress, {
  394. from: accountAddress,
  395. });
  396.  
  397. contract.defaultAccount = accountAddress;
  398.  
  399.  
  400. async function asyncCreateSensor(_sensorName) {
  401. let nonce = await web3.eth.getTransactionCount(accountAddress);
  402.  
  403. let data = await contract.methods.createSensor(_sensorName).encodeABI();
  404.  
  405. let gasPrice = await web3.eth.getGasPrice();
  406. gasPrice = web3.utils.toHex(gasPrice);
  407.  
  408. let gasPriceLimit = await web3.eth.estimateGas({
  409. "from": accountAddress,
  410. "nonce": nonce,
  411. "to": contractAddress,
  412. "data": data
  413. });
  414. gasPriceLimit = web3.utils.toHex(gasPriceLimit);
  415.  
  416. let rawTx = {
  417. "nonce": nonce,
  418. "from": accountAddress,
  419. "gasPrice": gasPrice,
  420. "gasLimit": gasPriceLimit,
  421. "to": contractAddress,
  422. "value": "0x00",
  423. "data": data
  424. }
  425. var tx = new Tx(rawTx, { 'chain': 'ropsten' });
  426. tx.sign(privateKey);
  427. var serializedTx = tx.serialize();
  428.  
  429. return await web3.eth.sendSignedTransaction('0x' + serializedTx.toString('hex'))
  430. .then((err, res) => {
  431. if (err) {
  432. return err;
  433. }
  434. return res;
  435. });
  436. }
  437.  
  438. async function asyncCreateData(_sensorId, _date, _hour, _minute, arr) {
  439.  
  440. let nonce = await web3.eth.getTransactionCount(accountAddress);
  441.  
  442. let data = await contract.methods.createData(_sensorId, _date, _hour, _minute, arr).encodeABI();
  443.  
  444. let gasPrice = await web3.eth.getGasPrice();
  445. gasPrice = web3.utils.toHex(gasPrice);
  446.  
  447. let gasPriceLimit = await web3.eth.estimateGas({
  448. "from": accountAddress,
  449. "nonce": nonce,
  450. "to": contractAddress,
  451. "data": data
  452. });
  453. gasPriceLimit = web3.utils.toHex(gasPriceLimit);
  454.  
  455. let rawTx = {
  456. "nonce": nonce,
  457. "from": accountAddress,
  458. "gasPrice": gasPrice,
  459. "gasLimit": gasPriceLimit,
  460. "to": contractAddress,
  461. "value": "0x00",
  462. "data": data
  463. }
  464. var tx = new Tx(rawTx, { 'chain': 'ropsten' });
  465. tx.sign(privateKey);
  466. var serializedTx = tx.serialize();
  467.  
  468. return await web3.eth.sendSignedTransaction('0x' + serializedTx.toString('hex'))
  469. .then((err, res) => {
  470. if (err) {
  471. return err;
  472. }
  473. return res;
  474. });
  475. }
  476.  
  477. async function asyncGetSensorById(_sensorId) {
  478. return await contract.methods.getSensorById(_sensorId).call();
  479. }
  480.  
  481. async function asyncGetData(_dataId) {
  482. return await contract.methods.getDataById(_dataId).call();
  483. }
  484.  
  485. async function asyncGetLatestData() {
  486. return await contract.methods.getLatestData().call();
  487. }
  488.  
  489.  
  490.  
  491.  
  492. module.exports = {
  493. asyncGetSensorById,
  494. asyncCreateSensor,
  495. asyncCreateData,
  496. asyncGetData,
  497. asyncGetLatestData
  498. };
  499.  
RAW Paste Data