Best JavaScript code snippet using wpt
sync_block.js
Source:sync_block.js
1const axios = require('axios');2const config = require('./config.js');3const Ont = require('ontology-ts-sdk');4const BigNumber = require('bignumber.js');5const moment = require('moment');6const restfulUrl = config.url + ":20334";7function HexToInt(num) {8 return parseInt('0x'+num.match(/../g).reverse().join(''));9}10const fetchLastBlock = async function () {11 const url = restfulUrl + '/api/v1/block/height';12 const res = await axios.get(url); 13 var height;14 if(res && res.data && res.data.Result) {15 height = res.data.Result;16 } else {17 height = 0;18 }19 return height;20}21const fetchScEventsByBlock = async function (height) {22 const url = restfulUrl + '/api/v1/smartcode/event/transactions/' + height;23 // console.log(url);24 const res = await axios.get(url);25 if(!res || !res.data) {26 return;27 }28 const txs = res.data.Result;29 if (txs!= "") {30 // console.log('txs are : ' + JSON.stringify(txs));31 }32 33 const events = []34 for (const tx of txs) {35 for (const notify of tx.Notify) {36 if (notify.ContractAddress === config.myContractHash) {37 notify.TxHash = tx['TxHash']38 events.push(notify)39 // break;40 }41 }42 }43 return events;44}45exports.syncBet = async function (current_hash) {46 try { 47 var height = await fetchLastBlock();48 }49 catch (e) { throw e };50 51 try {52 var notifyList = await fetchScEventsByBlock(height); 53 }54 catch (e) { throw e }; 55 if (notifyList.length > 0) {56 for (const notify of notifyList) {57 const states = notify['States'];58 59 const contractHash = notify['ContractAddress']; 60 // console.log("\n*** the whole notify is " + JSON.stringify(notify));61 if (contractHash == config.myContractHash) {62 if (notify.TxHash == current_hash) {63 const key = Ont.utils.hexstr2str(states[0]);64 if (key == 'bet') {65 // console.log(JSON.stringify(states[2][9]));66 const bet = HexToInt(states[1]);67 // const for_rep = HexToInt(states[2][0]);68 // const against_rep = HexToInt(states[2][1]); 69 const for_staked = HexToInt(states[2][2]) / 1e8;70 const against_staked = HexToInt(states[2][3]) / 1e8;71 const change = HexToInt(states[2][6]) * HexToInt(states[2][7]);72 const target_price = HexToInt(states[2][8]) / 1e8;73 const date = Ont.utils.hexstr2str(states[2][9]);74 const ticker = Ont.utils.hexstr2str(states[2][10]);75 const for_avg_rep = HexToInt(states[2][11]) / 1e8 - 1e8;76 var against_avg_rep;77 if (against_staked == 0) { against_avg_rep = 0; }78 else { against_avg_rep = HexToInt(states[2][12]) / 1e8 - 1e8; }79 const prob = HexToInt(states[2][13]) / 1e8;80 const val = [bet, ticker, change, target_price, date, for_staked, against_staked, for_avg_rep, against_avg_rep, prob];81 return val;82 }83 } 84 } 85 }86 }87}88exports.syncVote = async function (current_hash) {89 const height = await fetchLastBlock();90 91 const notifyList = await fetchScEventsByBlock(height);92 93 if (notifyList.length > 0) {94 for (const notify of notifyList) {95 const states = notify['States'];96 const contractHash = notify['ContractAddress']; 97 // console.log("\n*** the whole notify is " + JSON.stringify(notify));98 if (contractHash == config.myContractHash) {99 if (notify.TxHash == current_hash) {100 const key = Ont.utils.hexstr2str(states[0]);101 if (key == 'vote') {102 // console.log(JSON.stringify(states[2][9]));103 const bet = HexToInt(states[1]);104 // const for_rep = HexToInt(states[2][0]);105 // const against_rep = HexToInt(states[2][1]); 106 const for_staked = HexToInt(states[2][2]) / 1e8;107 const against_staked = HexToInt(states[2][3]) / 1e8;108 const change = HexToInt(states[2][6]) * HexToInt(states[2][7]);109 const target_price = HexToInt(states[2][8]) / 1e8;110 const date = Ont.utils.hexstr2str(states[2][9]);111 const ticker = Ont.utils.hexstr2str(states[2][10]);112 const for_avg_rep = HexToInt(states[2][11]) / 1e8 - 1e8;113 var against_avg_rep;114 if (against_staked == 0) { against_avg_rep = 0; }115 else { against_avg_rep = HexToInt(states[2][12]) / 1e8 - 1e8; }116 const prob = HexToInt(states[2][13]) / 1e8;117 const val = [bet, ticker, change, target_price, date, for_staked, against_staked, for_avg_rep, against_avg_rep, prob];118 return val;119 }120 } 121 } 122 }123 }124}125exports.syncFeed = async function (current_hash) {126 const height = await fetchLastBlock();127 128 try { 129 var notifyList = await fetchScEventsByBlock(height);130 }131 catch (e) { throw e };132 133 const betEvents = [];134 if (notifyList.length > 0) {135 for (const notify of notifyList) {136 const states = notify['States'];137 const contractHash = notify['ContractAddress']; 138 // console.log("\n*** the whole notify is " + JSON.stringify(notify));139 140 if (notify.TxHash === current_hash) {141 const key = Ont.utils.hexstr2str(states[0]);142 if (key == 'feed') {143 var map = new Map();144 for (let i = 0; i < states[1].length; i++) {145 // console.log(JSON.stringify(states[1][i]));146 const bet = HexToInt(states[1][i][0]);147 const ticker = Ont.utils.hexstr2str(states[1][i][1], 16);148 console.log("raw " + states[1][i][2]);149 console.log("after parse " + HexToInt(states[1][i][2]));150 const target_price = HexToInt(states[1][i][2]) / 1e8;151 const change = parseInt(states[1][i][3], 16) * parseInt(states[1][i][4], 16);152 const for_avg_rep = (HexToInt(states[1][i][5]) / 1e8) - 1e8;153 const for_staked = HexToInt(states[1][i][7]) / 1e8;154 const against_staked = HexToInt(states[1][i][8]) / 1e8;155 const against_avg_rep = (HexToInt(states[1][i][6]) / 1e8) - 1e8;156 if (against_staked == 0) { against_avg_rep = 0; }157 const date = Ont.utils.hexstr2str(states[1][i][9], 16)158 const prob = HexToInt(states[1][i][10]) / 1e8;159 const val = [ticker, target_price, change, for_avg_rep, against_avg_rep, for_staked, against_staked, date, prob];160 map.set(bet, val);161 }162 163 return map;164 }165 } 166 167 }168 }169}170exports.syncRecord = async function (current_hash) {171 const height = await fetchLastBlock();172 173 const notifyList = await fetchScEventsByBlock(height);174 if (notifyList.length > 0) {175 for (const notify of notifyList) {176 // e means each event 177 const states = notify['States'];178 const contractHash = notify['ContractAddress']; 179 // console.log("\n*** the whole notify is " + JSON.stringify(notify));180 if (contractHash == config.myContractHash) {181 if (notify.TxHash === current_hash) {182 const key = Ont.utils.hexstr2str(states[0]);183 if (key == 'record') {184 const bets = states[1];185 const results = states[2];186 const net = states[3];187 for (let i = 0; i < bets.length; i++) {188 bets[i] = HexToInt(bets[i]);189 results[i] = HexToInt(results[i]);190 net[i] = HexToInt(net[i]);191 }192 const val = [bets, results, net];193 return val; 194 }195 if (key == 'No bets') { return [ null, null, null ]; } 196 } 197 } 198 }199 }200}201exports.syncHistory = async function (current_hash) {202 const height = await fetchLastBlock();203 204 const notifyList = await fetchScEventsByBlock(height);205 if (notifyList.length > 0) {206 for (const notify of notifyList) {207 // e means each event 208 const states = notify['States'];209 const contractHash = notify['ContractAddress']; 210 // console.log("\n*** the whole notify is " + JSON.stringify(notify));211 if (contractHash == config.myContractHash) {212 if (notify.TxHash === current_hash) {213 const key = Ont.utils.hexstr2str(states[0]);214 if (key == 'Final result') {215 var result = HexToInt(states[1]);216 switch (result) {217 case -1: result = 'Incorrect';218 case 0: result = 'No price change';219 case 1: result = 'Correct';220 case 2: result = 'Incomplete';221 }222 return result;223 }224 } 225 } 226 }227 }...
useExtractPlant.js
Source:useExtractPlant.js
...8 const bnGasUsed = new BN(gasUsed);9 const sunSize = Math.round(Math.log(bnGasUsed.toNumber()));10 const noZeroHash = tx.substring(2);11 const [_habitableStart, _habitableEnd, _astroidsStart, _astroidsEnd] = noZeroHash.match(re);12 const habitableStart = normalize(sunSize, (CANVAS_SIZE / 2), hexToInt(_habitableStart));13 const habitableEnd = normalize(habitableStart, (CANVAS_SIZE / 2), hexToInt(_habitableEnd));14 const astroidsStart = normalize(sunSize, (CANVAS_SIZE / 2), hexToInt(_astroidsStart));15 const astroidsEnd = normalize(astroidsStart, (CANVAS_SIZE / 2), hexToInt(_astroidsEnd));16 return { habitableStart, habitableEnd, astroidsStart, astroidsEnd, sunSize };17 }18 const getPlanet = ({ tx }, sunSize) => {19 const noZeroHash = tx.substring(2);20 const [_color1, _color2, _color3, _theta, _distance, _speed, _radius] = noZeroHash.match(re);21 const theta = normalize(0, 1, hexToInt(_theta));22 const distance = normalize(sunSize, CANVAS_SIZE / 2, hexToInt(_distance));23 const speed = normalize(1, 6, hexToInt(_speed));24 const radius = normalize(5, sunSize - 2, hexToInt(_radius));25 const tailLength = 90 * (radius / sunSize);26 return {27 color: {28 r: hexToInt(_color1),29 g: hexToInt(_color2),30 b: hexToInt(_color3),31 },32 theta,33 distance,34 speed,35 radius,36 tailLength37 };38 }39 const getAstroid = (tx, sun) => {40 const noZeroHash = tx.substring(2);41 const [42 _distance,43 _theta,44 _size,45 _point1X,46 _point1Y,47 _point2X,48 _point2Y,49 _point3X,50 _point3Y,51 _point4X,52 _point4Y,53 _point5X,54 _point5Y,55 _point6X,56 _point6Y,57 ] = noZeroHash.match(re);58 const theta = normalize(0, 1, hexToInt(_theta));59 const distance = normalize(sun.astroidsStart, sun.astroidsEnd, hexToInt(_distance));60 const size = normalize(4, 10, hexToInt(_size));61 const points = []62 const isShape = size >= 4;63 if (isShape) {64 points.push({65 x: Math.round(normalize(1, size, hexToInt(_point1X))),66 y: Math.round(normalize(1, size, hexToInt(_point1Y))),67 })68 points.push({69 x: Math.round(normalize(1, size, hexToInt(_point2X))),70 y: Math.round(normalize(1, size, hexToInt(_point2Y))),71 })72 points.push({73 x: Math.round(normalize(1, size, hexToInt(_point3X))),74 y: Math.round(normalize(1, size, hexToInt(_point3Y))),75 })76 points.push({77 x: Math.round(normalize(1, size, hexToInt(_point4X))),78 y: Math.round(normalize(1, size, hexToInt(_point4Y))),79 })80 points.push({81 x: Math.round(normalize(1, size, hexToInt(_point5X))),82 y: Math.round(normalize(1, size, hexToInt(_point5Y))),83 })84 points.push({85 x: Math.round(normalize(1, size, hexToInt(_point6X))),86 y: Math.round(normalize(1, size, hexToInt(_point6Y))),87 })88 }89 return {90 // color: {91 // r: hexToInt(_color1),92 // g: hexToInt(_color2),93 // b: hexToInt(_color3),94 // },95 color: {96 r: 195,97 g: 160,98 b: 107,99 },100 theta,101 distance,102 size,103 points,104 isShape,105 };106 }107 return {...
HexToInt.spec.ts
Source:HexToInt.spec.ts
1import { Endianness } from "../Config";2import { HexToInt } from "./HexToInt";3describe("HexToInt mapper", () => {4 let hexToInt: HexToInt;5 describe("Big endianness", () => {6 beforeEach(() => {7 hexToInt = new HexToInt({ endianness: Endianness.BIG_ENDIAN });8 });9 it("should correctly parse hex to int", () => {10 const inputObj = "babe";11 hexToInt.signed = false;12 const transformRes = hexToInt.transform(inputObj);13 expect(transformRes).toEqual(47806);14 });15 it("should correctly parse signed hex to int", () => {16 const inputObj = "babe";17 hexToInt.signed = true;18 const transformRes = hexToInt.transform(inputObj);19 expect(transformRes).toEqual(-17730);20 });21 it("should allow for prepended 0x on value", () => {22 const inputObj = "0xbabe";23 hexToInt.signed = false;24 const transformRes = hexToInt.transform(inputObj);25 expect(transformRes).toEqual(47806);26 });27 });28 describe("Little endianness", () => {29 beforeEach(() => {30 hexToInt = new HexToInt({ endianness: Endianness.LITTLE_ENDIAN });31 });32 it("should correctly parse hex to int", () => {33 const inputObj = "babe";34 hexToInt.signed = false;35 const transformRes = hexToInt.transform(inputObj);36 expect(transformRes).toEqual(48826);37 });38 it("should correctly parse signed hex to int", () => {39 const inputObj = "babe";40 hexToInt.signed = true;41 const transformRes = hexToInt.transform(inputObj);42 expect(transformRes).toEqual(-16710);43 });44 it("should allow for prepended 0x on value", () => {45 const inputObj = "0xbabe";46 hexToInt.signed = false;47 const transformRes = hexToInt.transform(inputObj);48 expect(transformRes).toEqual(48826);49 });50 });51 describe("Configuration", () => {52 it("should correctly return config based on initial configuration", () => {53 const hexToIntWithConfig = new HexToInt({54 endianness: Endianness.LITTLE_ENDIAN,55 signed: true,56 });57 expect(hexToIntWithConfig.config()).toEqual({58 id: "HEXTOINT",59 params: {60 endianness: Endianness.LITTLE_ENDIAN,61 signed: true,62 },63 });64 });65 });66 describe("Invalid input", () => {67 beforeEach(() => {68 hexToInt = new HexToInt();69 });70 it("should return 0 upon empty value input", () => {71 const inputObj = undefined as any;72 const transformRes = hexToInt.transform(inputObj);73 expect(transformRes).toEqual(0);74 });75 it("should return 0 for invalid input", () => {76 const inputObj = "gege";77 hexToInt.signed = true;78 const transformRes = hexToInt.transform(inputObj);79 expect(transformRes).toEqual(0);80 });81 });...
Using AI Code Generation
1var wptools = require('wptools');2var hexToInt = wptools.hexToInt;3console.log(hexToInt('0x000000'));4console.log(hexToInt('0x000001'));5console.log(hexToInt('0x0000FF'));6console.log(hexToInt('0x000100'));7console.log(hexToInt('0x00FF00'));8console.log(hexToInt('0x010000'));9console.log(hexToInt('0x00FFFF'));10console.log(hexToInt('0x010101'));11console.log(hexToInt('0xFFFFFF'));12console.log(hexToInt('0x123456'));13console.log(hexToInt('0xABCDEF'));14console.log(hexToInt('0x012345'));15console.log(hexToInt('0x0A0B0C'));16console.log(hexToInt('0x0'));17console.log(hexToInt('0x1'));18console.log(hexToInt('0xF'));19console.log(hexToInt('0x10'));20console.log(hexToInt('0xFF'));21console.log(hexToInt('0x100'));22console.log(hexToInt('0xFFF'));23console.log(hexToInt('0x1000'));24console.log(hexToInt('0xFFFF'));25console.log(hexToInt('0x10000'));26console.log(hexToInt('0xFFFFF'));27console.log(hexToInt('0x100000'));28console.log(hexToInt('0xFFFFFF'));29console.log(hexToInt('0x1000000'));30console.log(hexToInt('0x10000000'));31console.log(hexToInt('0xFFFFFFFF'));32console.log(hexToInt('0x100000000'));33console.log(hexToInt('0x1000000000'));34console.log(hexToInt('0xFFFFFFFFFF'));35console.log(hexToInt('0x10000000000'));36console.log(hexToInt('0x100000000000'));37console.log(hexToInt('0xFFFFFFFFFFFF'));38console.log(hexToInt('0x1000000000000'));39console.log(hexToInt('0x10000000000000'));40console.log(hexToInt('0xFFFFFFFFFFFFFF'));41console.log(hexToInt('0x100000000000000'));42console.log(hexToInt('0x1000000000000000'));43console.log(hexToInt('0xFFFFFFFFFFFFFFFF'));44console.log(hexToInt('0x10000000000000000'));45console.log(hexToInt('0x100000000000000000'));46console.log(hexToInt('0xFFFFFFFFFFFFFFFFFF'));47console.log(hexToInt('0x1000000000000000000'));48console.log(hexToInt('0x100
Using AI Code Generation
1const wptools = require('wptools');2var hex = "0x000000";3wptools.hexToInt(hex).then((result) => {4 console.log(result);5}).catch((err) => {6 console.log(err);7});8## hexToInt(hex) - Promise9var hex = "0x000000";10wptools.hexToInt(hex).then((result) => {11 console.log(result);12}).catch((err) => {13 console.log(err);14});15## hexToAscii(hex) - Promise16var hex = "0x000000";17wptools.hexToAscii(hex).then((result) => {18 console.log(result);19}).catch((err) => {20 console.log(err);21});22## hexToUtf8(hex) - Promise23var hex = "0x000000";24wptools.hexToUtf8(hex).then((result) => {25 console.log(result);26}).catch((err) => {27 console.log(err);28});29## hexToUtf16(hex) - Promise30var hex = "0x000000";31wptools.hexToUtf16(hex).then((result) => {32 console.log(result);33}).catch((err) => {34 console.log(err);35});36## hexToBase64(hex) - Promise
Using AI Code Generation
1var wptools = require('wptools');2var wiki = wptools.page('Sachin Tendulkar');3wiki.get(function(err, resp){4 console.log(resp.data['coordinates'][0].lat);5 console.log(resp.data['coordinates'][0].lon);6 console.log(resp.data['coordinates'][0].globe);7 console.log(resp.data['coordinates'][0].primary);8 console.log(resp.data['coordinates'][0].dim);9 console.log(resp.data['coordinates'][0].type);10 console.log(resp.data['coordinates'][0].name);11 console.log(resp.data['coordinates'][0].display_name);12 console.log(resp.data['coordinates'][0].icon);13 console.log(resp.data['coordinates'][0].url);14});15[MIT](LICENSE)
Using AI Code Generation
1var wptools = require('wptools');2var hex = wptools.hexToInt("0x000000");3### hexToRgb(hex)4var wptools = require('wptools');5var rgb = wptools.hexToRgb("0x000000");6### hexToRgba(hex, alpha)7var wptools = require('wptools');8var rgba = wptools.hexToRgba("0x000000", 0.5);9### rgbToHex(r, g, b)10var wptools = require('wptools');11var hex = wptools.rgbToHex(0, 0, 0);12### rgbToHsl(r, g, b)13var wptools = require('wptools');14var hsl = wptools.rgbToHsl(0, 0, 0);15### rgbToHsv(r, g, b)16var wptools = require('wptools');17var hsv = wptools.rgbToHsv(0, 0, 0);
Using AI Code Generation
1var wptools = require('wp-tools');2var hex = '0x1F6D1';3var decimal = wptools.hexToInt(hex);4var wptools = require('wp-tools');5var decimal = 128737;6var hex = wptools.intToHex(decimal);7var wptools = require('wp-tools');8var str = 'hello';9var unicode = wptools.toUnicode(str);10var wptools = require('wp-tools');11var unicode = '\u0068\u0065\u006C\u006C\u006F';12var str = wptools.fromUnicode(unicode);13var wptools = require('wp-tools');14var str = 'hello';
Using AI Code Generation
1var wpt = require('webpagetest');2var api = new wpt('A.a1f7a6f8d6e7e0b9c81a7d9a9b9d7f1e');3api.runTest(url, {location: 'Dulles:Chrome'}, function(err, data) {4 if (err) return console.error(err);5 console.log(data);6 api.getTestResults(data.data.testId, function(err, data) {7 if (err) return console.error(err);8 console.log(data);9 });10});
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!