Advertisement
abrhsn1998

thor.js

Nov 22nd, 2020 (edited)
2,212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var Transaction = require('thor-devkit').Transaction;
  2. var cry = require('thor-devkit').cry;
  3.  
  4. var thorify = require("thorify").thorify;
  5. var Web3 = require("web3");
  6. var web3 = thorify(new Web3(), "https://testnet.veblocks.net");
  7.  
  8.  
  9. var words = ''// My Mnemonic Word
  10.  
  11.  
  12. let node = cry.HDNode.fromMnemonic(words);
  13.  
  14. let privateKey = node.derive(0).privateKey.toString('hex');
  15. let prKey = node.derive(0).privateKey;
  16.  
  17. let acc = web3.eth.accounts.privateKeyToAccount(privateKey);
  18.  
  19. let wallet = web3.eth.accounts.wallet;
  20.  
  21. wallet.add(acc);
  22.  
  23. var contractAddress = "0x87834f70Bcd44753303a4bD7488B9694288Aca13";
  24.  
  25. var abi = [
  26.     {
  27.         "inputs": [],
  28.         "stateMutability": "payable",
  29.         "type": "constructor"
  30.     },
  31.     {
  32.         "inputs": [
  33.             {
  34.                 "internalType": "string",
  35.                 "name": "_time",
  36.                 "type": "string"
  37.             },
  38.             {
  39.                 "internalType": "uint256[]",
  40.                 "name": "_datas",
  41.                 "type": "uint256[]"
  42.             }
  43.         ],
  44.         "name": "createData",
  45.         "outputs": [
  46.             {
  47.                 "internalType": "uint256",
  48.                 "name": "",
  49.                 "type": "uint256"
  50.             }
  51.         ],
  52.         "stateMutability": "payable",
  53.         "type": "function"
  54.     },
  55.     {
  56.         "inputs": [],
  57.         "name": "dataCounter",
  58.         "outputs": [
  59.             {
  60.                 "internalType": "uint256",
  61.                 "name": "",
  62.                 "type": "uint256"
  63.             }
  64.         ],
  65.         "stateMutability": "view",
  66.         "type": "function"
  67.     },
  68.     {
  69.         "inputs": [
  70.             {
  71.                 "internalType": "uint256",
  72.                 "name": "",
  73.                 "type": "uint256"
  74.             },
  75.             {
  76.                 "internalType": "uint256",
  77.                 "name": "",
  78.                 "type": "uint256"
  79.             }
  80.         ],
  81.         "name": "dataList",
  82.         "outputs": [
  83.             {
  84.                 "internalType": "uint256",
  85.                 "name": "",
  86.                 "type": "uint256"
  87.             }
  88.         ],
  89.         "stateMutability": "view",
  90.         "type": "function"
  91.     },
  92.     {
  93.         "inputs": [
  94.             {
  95.                 "internalType": "uint256",
  96.                 "name": "_dataId",
  97.                 "type": "uint256"
  98.             }
  99.         ],
  100.         "name": "getData",
  101.         "outputs": [
  102.             {
  103.                 "internalType": "uint256[]",
  104.                 "name": "",
  105.                 "type": "uint256[]"
  106.             }
  107.         ],
  108.         "stateMutability": "view",
  109.         "type": "function"
  110.     },
  111.     {
  112.         "inputs": [
  113.             {
  114.                 "internalType": "string",
  115.                 "name": "_time",
  116.                 "type": "string"
  117.             }
  118.         ],
  119.         "name": "getDataId",
  120.         "outputs": [
  121.             {
  122.                 "internalType": "uint256",
  123.                 "name": "",
  124.                 "type": "uint256"
  125.             }
  126.         ],
  127.         "stateMutability": "view",
  128.         "type": "function"
  129.     },
  130.     {
  131.         "inputs": [],
  132.         "name": "getLatestData",
  133.         "outputs": [
  134.             {
  135.                 "internalType": "uint256[]",
  136.                 "name": "",
  137.                 "type": "uint256[]"
  138.             }
  139.         ],
  140.         "stateMutability": "view",
  141.         "type": "function"
  142.     },
  143.     {
  144.         "inputs": [
  145.             {
  146.                 "internalType": "string",
  147.                 "name": "",
  148.                 "type": "string"
  149.             }
  150.         ],
  151.         "name": "timeToData",
  152.         "outputs": [
  153.             {
  154.                 "internalType": "uint256",
  155.                 "name": "",
  156.                 "type": "uint256"
  157.             }
  158.         ],
  159.         "stateMutability": "view",
  160.         "type": "function"
  161.     }
  162. ];
  163.  
  164. var contract = new web3.eth.Contract(abi, contractAddress);
  165.  
  166. contract.defaultAccount = wallet[0].address;
  167.  
  168. async function createData(_time, arr) {
  169.  
  170.     let data = contract.methods.createData(_time, arr).encodeABI();
  171.  
  172.     let clauses = [{
  173.         'to': contractAddress,
  174.         'value': '0x0',
  175.         'data': data
  176.     }]
  177.  
  178.     let gas = Transaction.intrinsicGas(clauses)*2;
  179.     console.log(gas);
  180.  
  181.     // console.log(gas);
  182.     let blockRef = await web3.eth.getBlock('latest').then(res => {
  183.         return res.id.substr(0, 18);
  184.     });
  185.     // console.log(blockRef);
  186.     let body = {
  187.         chainTag: 0x27,
  188.         blockRef: blockRef,
  189.         expiration: 32,
  190.         clauses: clauses,
  191.         gasPriceCoef: 128,
  192.         gas: gas,
  193.         dependsOn: null,
  194.         nonce: '0x11'
  195.     }
  196.  
  197.     let tx = new Transaction(body);
  198.  
  199.     let signingHash = tx.signingHash();
  200.  
  201.     tx.signature = cry.secp256k1.sign(signingHash, prKey);
  202.  
  203.     let raw = tx.encode();
  204.  
  205.     // let decoded = Transaction.decode(raw);
  206.     return await web3.eth.sendSignedTransaction('0x' + raw.toString('hex')).then((err, res) => {
  207.         if (err) {
  208.             console.error(err);
  209.             return null;
  210.         }
  211.         return res;
  212.     });
  213.  
  214. }
  215.  
  216.  
  217. async function getData(_dataId) {
  218.     return await contract.methods.getData(_dataId).call();
  219. }
  220. async function getLatestData() {
  221.     return await contract.methods.getLatestData().call();
  222. }
  223.  
  224.  
  225. // createData('202022222222', [1, 2, 3, 4, 5, 6])
  226. (async () => {
  227.     console.log(await createData('202011242010', [1, 2, 3, 4, 5]));
  228. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement