Best JavaScript code snippet using differencify
agent.spec.js
Source:agent.spec.js
1const {2 ethers,3 createTransactionEvent,4 Finding,5 FindingType,6 FindingSeverity,7} = require('forta-agent');8const {9 initialize,10 handleTransaction,11 TORNADO_CASH_ADDRESSES,12} = require('./agent');13const config = {14 developerAbbreviation: 'DEVTEST',15 protocolName: 'PROTOTEST',16 protocolAbbreviation: 'PT',17 botType: 'tornado-cash-monitor',18 name: 'test-bot',19 observationIntervalInBlocks: 10,20 contracts: {21 contractName1: {22 address: '0xBC4CA0EdA7647A8aB7C2061c2E118A18a936f13D',23 type: 'Info',24 severity: 'Info',25 },26 },27};28// tests29describe('handleTransaction', () => {30 let botState;31 let addressName;32 let testAddressInfo;33 let mockTrace;34 let mockTxEvent;35 let iface;36 const tornadoCashAddress = TORNADO_CASH_ADDRESSES[0].toLowerCase();37 beforeEach(async () => {38 // set up test configuration parameters that won't change with each test39 // grab the first entry from the 'addressList' key in the configuration file40 const { contracts } = config;41 [addressName] = Object.keys(contracts);42 testAddressInfo = contracts[addressName];43 botState = await initialize(config);44 // pull out the initialized interface to use for crafting test data45 ({ iface } = botState);46 // initialize mock trace object with default values47 mockTrace = [48 {49 action: {50 to: ethers.constants.AddressZero,51 input: ethers.constants.HashZero,52 value: '0x0',53 from: ethers.constants.AddressZero,54 },55 transactionHash: '0xFAKETRANSACTIONHASH',56 },57 ];58 // initialize mock transaction event with default values59 mockTxEvent = createTransactionEvent({60 block: {61 number: 10000,62 },63 addresses: {},64 transaction: {65 to: ethers.constants.AddressZero,66 from: ethers.constants.AddressZero,67 value: '0',68 data: ethers.constants.HashZero,69 },70 traces: mockTrace,71 });72 });73 it('returns no findings if no deposit/withdraw function from a tornado cash proxy was called', async () => {74 // run bot with empty mockTxEvent75 const findings = await handleTransaction(botState, mockTxEvent);76 // expect the suspiciousAddresses object to be empty77 expect(Object.keys(botState.suspiciousAddresses).length).toBe(0);78 // expect no findings79 expect(findings).toStrictEqual([]);80 });81 it('returns no findings when a deposit/withdraw function from tornado cash is called but there are no subsequent interactions with monitored addresses', async () => {82 // setup some mock function arguments to be encoded as mock function data83 const mockFunctionArgs = [84 '0x1111111111111111111111111111111111111111',85 ethers.utils.hexZeroPad('0xff', 32),86 '0xff',87 ];88 const mockFunctionData = iface.encodeFunctionData('deposit', mockFunctionArgs);89 // update mock trace object with encoded function data90 mockTrace[0].action.input = mockFunctionData;91 mockTrace[0].action.to = tornadoCashAddress;92 mockTrace[0].action.from = ethers.utils.AddressZero;93 // update mock transaction event with new mock trace94 mockTxEvent.traces = mockTrace;95 mockTxEvent.transaction.to = tornadoCashAddress;96 mockTxEvent.transaction.from = ethers.utils.AddressZero;97 mockTxEvent.addresses = {98 [ethers.utils.AddressZero]: true,99 [tornadoCashAddress]: true,100 };101 // run the bot102 const findings = await handleTransaction(botState, mockTxEvent);103 // expect the suspiciousAddresses object to contain one entry104 expect(Object.keys(botState.suspiciousAddresses).length).toBe(1);105 // expect no findings since there were no transactions involving a monitored address106 expect(findings).toStrictEqual([]);107 });108 it('returns no findings when suspicous addresses have been found but subsequent interactions with monitored addresses occur outside the observation interval', async () => {109 const mockSuspiciousAddress = '0x2222222222222222222222222222222222222222';110 // setup some mock function arguments to be encoded as mock function data111 const mockFunctionArgs = [112 '0x1111111111111111111111111111111111111111',113 ethers.utils.hexZeroPad('0xff', 32),114 '0xff',115 ];116 const mockFunctionData = iface.encodeFunctionData('deposit', mockFunctionArgs);117 // update mock trace object with encoded function data118 mockTrace[0].action.input = mockFunctionData;119 mockTrace[0].action.to = tornadoCashAddress;120 mockTrace[0].action.from = mockSuspiciousAddress;121 // update mock transaction event with new mock trace122 mockTxEvent.traces = mockTrace;123 mockTxEvent.transaction.to = tornadoCashAddress;124 mockTxEvent.transaction.from = mockSuspiciousAddress;125 mockTxEvent.addresses = {126 [mockSuspiciousAddress]: true,127 [tornadoCashAddress]: true,128 };129 // run the bot130 let findings = await handleTransaction(botState, mockTxEvent);131 // expect the suspiciousAddresses object to contain one entry132 expect(Object.keys(botState.suspiciousAddresses).length).toBe(1);133 // expect no findings since there have not been any transactions involving a monitored address134 expect(findings).toStrictEqual([]);135 // update mock trace object to include a monitored address136 mockTrace[0].action.input = ethers.constants.HashZero;137 mockTrace[0].action.to = testAddressInfo.address;138 mockTrace[0].action.from = mockSuspiciousAddress;139 // update mock transaction event with new mock trace140 mockTxEvent.traces = mockTrace;141 mockTxEvent.transaction.to = testAddressInfo.address;142 mockTxEvent.transaction.from = mockSuspiciousAddress;143 mockTxEvent.addresses = {144 [mockSuspiciousAddress]: true,145 [testAddressInfo.address]: true,146 };147 // update the blockNumber to be one greater than the observation interval specified in the148 // config file149 mockTxEvent.block.number = mockTxEvent.block.number + config.observationIntervalInBlocks + 1;150 // run the bot151 findings = await handleTransaction(botState, mockTxEvent);152 // expect the suspiciousAddresses object to contain no entries as the only entry should have153 // been removed since the current block number minus the block number the suspicious address154 // was added to the list at is greater than the observation interval155 expect(Object.keys(botState.suspiciousAddresses).length).toBe(0);156 // expect no findings157 expect(findings).toStrictEqual([]);158 });159 it('returns a finding when a suspicious address was found and subsequent interactions with monitored functions have occurred within the observaion interval', async () => {160 const mockSuspiciousAddress = '0x2222222222222222222222222222222222222222';161 // setup some mock function arguments to be encoded as mock function data162 const mockFunctionArgs = [163 '0x1111111111111111111111111111111111111111',164 ethers.utils.hexZeroPad('0xff', 32),165 '0xff',166 ];167 const mockFunctionData = iface.encodeFunctionData('deposit', mockFunctionArgs);168 // update mock trace object with encoded function data169 mockTrace[0].action.input = mockFunctionData;170 mockTrace[0].action.to = tornadoCashAddress;171 mockTrace[0].action.from = mockSuspiciousAddress;172 // update mock transaction event with new mock trace173 mockTxEvent.traces = mockTrace;174 mockTxEvent.transaction.to = tornadoCashAddress;175 mockTxEvent.transaction.from = mockSuspiciousAddress;176 mockTxEvent.addresses = {177 [mockSuspiciousAddress]: true,178 [tornadoCashAddress]: true,179 };180 // run the bot181 let findings = await handleTransaction(botState, mockTxEvent);182 // expect the suspiciousAddresses object to contain one entry183 expect(Object.keys(botState.suspiciousAddresses).length).toBe(1);184 // expect no findings since there have not been any transactions involving a monitored address185 expect(findings).toStrictEqual([]);186 // update mock trace object to include a monitored address187 mockTrace[0].action.input = ethers.constants.HashZero;188 mockTrace[0].action.to = testAddressInfo.address;189 mockTrace[0].action.from = mockSuspiciousAddress;190 // update mock transaction event with new mock trace191 mockTxEvent.traces = mockTrace;192 mockTxEvent.transaction.to = testAddressInfo.address;193 mockTxEvent.transaction.from = mockSuspiciousAddress;194 mockTxEvent.addresses = {195 [mockSuspiciousAddress]: true,196 [testAddressInfo.address]: true,197 };198 // update the blockNumber199 mockTxEvent.block.number += 1;200 // run the bot201 findings = await handleTransaction(botState, mockTxEvent);202 const expectedFinding = [Finding.fromObject({203 name: `${config.protocolName} Tornado Cash Monitor`,204 description: `The ${addressName} address (${testAddressInfo.address}) was involved in a`205 + ` transaction with an address ${mockSuspiciousAddress} that has previously interacted`206 + ' with Tornado Cash',207 alertId: `${config.developerAbbreviation}-${config.protocolAbbreviation}-TORNADO-CASH-MONITOR`,208 type: FindingType[testAddressInfo.type],209 severity: FindingSeverity[testAddressInfo.severity],210 metadata: {211 monitoredAddress: testAddressInfo.address,212 name: addressName,213 suspiciousAddress: mockSuspiciousAddress,214 tornadoCashContractAddresses: TORNADO_CASH_ADDRESSES.join(','),215 },216 })];217 expect(findings).toStrictEqual(expectedFinding);218 });...
mockTracing.ts
Source:mockTracing.ts
1import { TracingInterface } from './types';2import {3 Exp, unknown, LVal4} from './exp';5class MockTrace implements TracingInterface {6 constructor() {7 }8 pushArgs(exps: Exp[]) {9 }10 popArgs(): Exp[] {11 let u = unknown();12 return [u, u, u, u, u, u]; // should be enough!13 }14 getTrace(): Exp {15 return unknown();16 }17 newTrace() {18 }19 exitBlock() {20 }21 /**22 * Creates the trace expression 'let name = { unknown };' and enters the23 * block containing the 'unknown'.24 */25 traceNamed(name: string): void {26 }27 traceLet(name: string, named: Exp): void {28 }29 traceFunctionCall(name: string, theArgs: Exp[]): void {30 }31 traceFunctionBody(labelName: string): Exp[] {32 return [];33 }34 traceSet(name: LVal, named: Exp): void {35 }36 traceIfTrue(cond: Exp) {37 }38 traceIfFalse(cond: Exp) {39 }40 traceWhile(cond: Exp) {41 }42 traceLoop() {43 }44 traceCallback(event: string, eventArg: Exp, callbackArgs: string[], clos: Exp): MockTrace {45 return new MockTrace();46 }47 tracePrimApp(event: string, eventArgs: Exp[]) {48 }49 traceLabel(name: string): void {50 }51 traceBreak(name: string, value: Exp): void {52 }53 traceReturn(e1: Exp): void {54 }55 prettyPrint(): void {56 }57}58export function newMockTrace(): TracingInterface {59 return new MockTrace();...
Using AI Code Generation
1const { mockTrace } = require('differencify');2const { expect } = require('chai');3describe('test', () => {4 it('should pass', () => {5 mockTrace({6 });7 expect(true).to.equal(true);8 });9});10{11}
Using AI Code Generation
1const { mockTrace } = require('differencify');2describe('test', () => {3 it('should take screenshot', async () => {4 await mockTrace('test', async () => {5 });6 });7});8{9}10describe('test', () => {11 it('should take screenshot', async () => {12 await mockTrace('test', async () => {13 });14 });15});16{17}18describe('test', () => {19 it('should take screenshot', async () => {20 await mockTrace('test', async () => {21 });22 });23});24{25}26describe('test', () => {27 it('should take screenshot', async () => {28 await mockTrace('test', async () => {29 });30 });31});32### `mockTrace(name, callback)`
Using AI Code Generation
1const differencify = require('differencify');2const { mockTrace } = differencify;3const { By, until } = require('selenium-webdriver');4const { getDriver } = require('./testUtils');5describe('differencify', () => {6 let driver;7 let differencifyInstance;8 beforeAll(async () => {9 driver = await getDriver();10 differencifyInstance = differencify.init(driver);11 });12 afterAll(async () => {13 await driver.quit();14 });15 it('should render the page', async () => {16 await driver.wait(until.elementLocated(By.css('h1')), 2000);17 const screenshot = await differencifyInstance.takeScreenshot('test');18 expect(screenshot).toMatchImageSnapshot();19 });20});21### init(driver, options)
Using AI Code Generation
1const { mockTrace } = require('differencify');2describe('Test', () => {3 it('should mock trace', async () => {4 expect(mockTraceResult).toMatchSnapshot();5 });6});7Object {8 Object {9 },10 Object {11 Object {12 },13 },14}15`;16const { mockTrace } = require('differencify');17describe('Test', () => {18 it('should mock trace', async () => {19 viewport: {20 },21 });22 expect(mockTraceResult).toMatchSnapshot();23 });24});25Object {26 Object {27 },28 Object {29 Object {30 },
Using AI Code Generation
1const { mockTrace } = require('differencify');2const { createPage } = require('differencify/dist/utils/browser');3describe('test', () => {4 beforeAll(async () => {5 const page = await createPage();6 await mockTrace(page, { viewport: { width: 1280, height: 800 } });7 });8 it('test', async () => {9 });10});11const { mockTrace } = require('differencify');12const { createPage } = require('differencify/dist/utils/browser');13describe('test', () => {14 beforeAll(async () => {15 const page = await createPage();16 await mockTrace(page, { viewport: { width: 1280, height: 800 } });17 });18 it('test', async () => {19 });20});21const { mockTrace } = require('differencify');22const { createPage } = require('differencify/dist/utils/browser');23describe('test', () => {24 beforeAll(async () => {25 const page = await createPage();26 await mockTrace(page, { viewport: { width: 1280, height: 800 } });27 });28 it('test', async () => {29 });30});31const { mockTrace } = require('differencify');32const { createPage } = require('differencify/dist/utils/browser');33describe('test', () => {34 beforeAll(async () => {35 const page = await createPage();36 await mockTrace(page, { viewport: { width: 1280, height: 800 } });37 });38 it('test', async () => {39 });40});41const { mockTrace } = require('differencify');42const { createPage } = require('differencify/dist/utils/browser');
Using AI Code Generation
1const { mockTrace } = require('differencify');2const { trace } = require('puppeteer');3const { join } = require('path');4const tracePath = join(__dirname, 'traces');5mockTrace(tracePath, url).then(() => {6 console.log('Tracing finished');7});8const { join } = require('path');9const { trace } = require('puppeteer');10const { expect } = require('chai');11const tracePath = join(__dirname, 'traces');12describe('Tracing', () => {13 it('should match the baseline', async () => {14 const browser = await trace.launch();15 const page = await browser.newPage();16 const result = await trace(page, tracePath, url);17 expect(result).to.equal(true);18 await browser.close();19 });20});
Using AI Code Generation
1const { mockTrace } = require('differencify');2const differencify = require('differencify');3differencify.init({4});5differencify.setConfig({6});7differencify.setTestName('test name');8differencify.checkWindow({9});10const differencify = require('differencify');11differencify.init({12});13differencify.setConfig({14});15differencify.setTestName('test name');16differencify.checkWindow({17});18const differencify = require('differencify');19differencify.init({20});21differencify.setConfig({22});23differencify.setTestName('test name');24differencify.checkWindow({25});26const differencify = require('differencify');27differencify.init({28});29differencify.setConfig({30});31differencify.setTestName('test name');32differencify.checkWindow({33});34const differencify = require('differencify');35differencify.init({36});37differencify.setConfig({38});39differencify.setTestName('test name');40differencify.checkWindow({41});42const differencify = require('differencify');43differencify.init({44});45differencify.setConfig({46});47differencify.setTestName('test name');48differencify.checkWindow({49});50const differencify = require('differencify');51differencify.init({52});53differencify.setConfig({54});
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!!