Skip to content

Commit

Permalink
chore(processor): Add code gen for test utils (stephenh#76)
Browse files Browse the repository at this point in the history
  • Loading branch information
Poytr1 authored Sep 6, 2022
1 parent d840bdb commit af95adb
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 36 deletions.
72 changes: 72 additions & 0 deletions sdk/src/target-ethers-sentio/codegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,56 @@ export function codeGenSentioFile(contract: Contract): string {
return imports + source
}

export function codeGenTestUtilsFile(contract: Contract): string {
const source = `
declare global {
var contractAddress: string
}
function mockField() {
return {
address: globalThis.contractAddress,
blockHash:
"0x0000000000000000000000000000000000000000000000000000000000000000",
blockNumber: 0,
logIndex: 0,
removed: false,
transactionHash:
"0x0000000000000000000000000000000000000000000000000000000000000000",
transactionIndex: 0,
}
}
${Object.values(contract.events)
.map((events) => {
if (events.length === 1) {
return generateMockEventLogFunction(events[0], contract.name, false)
} else {
return events.map((e) => generateMockEventLogFunction(e, contract.name, true)).join('\n')
}
})
.join('\n')}
`

const imports = createImportsForUsedIdentifiers(
{
'@ethersproject/providers': ['Log'],
'.': [
`get${contract.name}Contract`,
...Object.values(contract.events).flatMap((events) => {
if (events.length === 1) {
return `${events[0].name}EventObject`
} else {
return events.flatMap((e) => `${getFullSignatureAsSymbolForEvent(e)}_EventObject`)
}
}),
],
},
source
)

return imports + source
}

function generateOnEventFunction(event: EventDeclaration, contractName: string, includeArgTypes: boolean): string {
let eventName = event.name
if (includeArgTypes) {
Expand Down Expand Up @@ -219,3 +269,25 @@ function generateBoundViewFunction(func: FunctionDeclaration): string {
}
`
}

function generateMockEventLogFunction(event: EventDeclaration, contractName: string, includeArgTypes: boolean): string {
let eventName = event.name
if (includeArgTypes) {
eventName = getFullSignatureAsSymbolForEvent(event) + '_'
}

return `
export function mock${eventName}Log(event: ${eventName}EventObject): Log {
const contract = get${contractName}Contract(globalThis.contractAddress)
const encodedLog = contract.rawContract.interface.encodeEventLog(
contract.rawContract.interface.getEvent('${eventName}'),
Object.values(event)
)
return {
...mockField(),
data: encodedLog.data,
topics: encodedLog.topics,
}
}
`
}
6 changes: 5 additions & 1 deletion sdk/src/target-ethers-sentio/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Ethers from '@typechain/ethers-v5'
import { Config, extractAbi, extractDocumentation, FileDescription, parse, shortenFullJsonFilePath } from 'typechain'
import { dirname, join, relative } from 'path'
import { codeGenIndex, codeGenSentioFile } from './codegen'
import { codeGenIndex, codeGenSentioFile, codeGenTestUtilsFile } from './codegen'

export default class EthersSentio extends Ethers {
constructor(config: Config) {
Expand Down Expand Up @@ -40,6 +40,10 @@ export default class EthersSentio extends Ethers {
path: join(dirname(files[0].path), '..', contract.name.toLowerCase(), 'index.ts'),
contents: codeGenIndex(contract),
},
{
path: join(dirname(files[0].path), '..', contract.name.toLowerCase(), 'test-utils.ts'),
contents: codeGenTestUtilsFile(contract),
},
]
}
}
Expand Down
38 changes: 3 additions & 35 deletions sdk/src/test/erc20.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,46 +6,14 @@ import { HandlerType } from '..'

import { TestProcessorServer } from './test-processor-server'
import { firstCounterValue, firstGaugeValue } from './metric-utils'
import { ApprovalEventObject, getErc20Contract, TransferEventObject } from '../builtin/erc20'
import { Log } from '@ethersproject/providers'
import { BigNumber } from 'ethers'

const contractAddress = '0x1E4EDE388cbc9F4b5c79681B7f94d36a11ABEBC9'

function getEventLog(event: TransferEventObject | ApprovalEventObject): Log {
let encodedLog: {
data: string
topics: string[]
}
const erc20Contract = getErc20Contract(contractAddress)
if ('from' in event) {
encodedLog = erc20Contract.rawContract.interface.encodeEventLog(
erc20Contract.rawContract.interface.getEvent('Transfer'),
[event.from, event.to, event.value]
)
} else {
encodedLog = erc20Contract.rawContract.interface.encodeEventLog(
erc20Contract.rawContract.interface.getEvent('Approval'),
[event.owner, event.spender, event.value]
)
}
return {
address: contractAddress,
blockHash: '0x0000000000000000000000000000000000000000000000000000000000000000',
blockNumber: 0,
logIndex: 0,
removed: false,
transactionHash: '0x0000000000000000000000000000000000000000000000000000000000000000',
transactionIndex: 0,
data: encodedLog.data,
topics: encodedLog.topics,
}
}
import { mockTransferLog } from '../builtin/erc20/test-utils'

describe('Test Basic Examples', () => {
const service = new TestProcessorServer()

beforeAll(async () => {
globalThis.contractAddress = '0x1E4EDE388cbc9F4b5c79681B7f94d36a11ABEBC9'
service.setup()
require('./erc20')
await service.start()
Expand Down Expand Up @@ -81,7 +49,7 @@ describe('Test Basic Examples', () => {
})

test('Check log dispatch', async () => {
const logData = getEventLog({
const logData = mockTransferLog({
from: '0x0000000000000000000000000000000000000000',
to: '0xB329e39Ebefd16f40d38f07643652cE17Ca5Bac1',
value: BigNumber.from('0x9a71db64810aaa0000'),
Expand Down

0 comments on commit af95adb

Please sign in to comment.