-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
42 changed files
with
3,119 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { DynamicModule, Injectable, Module } from '@nestjs/common'; | ||
import { ModuleMetadata } from '@nestjs/common'; | ||
import { Test } from '@nestjs/testing'; | ||
import { getNetwork } from '@ethersproject/networks'; | ||
import { getDefaultProvider, BaseProvider } from '@ethersproject/providers'; | ||
import { RegistryFetchModule, RegistryFetchService } from '../../'; | ||
|
||
@Injectable() | ||
class TestService { | ||
provider: BaseProvider; | ||
|
||
constructor() { | ||
this.provider = getDefaultProvider(process.env.PROVIDERS_URLS); | ||
jest.spyOn(this.provider, 'detectNetwork').mockImplementation(async () => getNetwork('mainnet')); | ||
} | ||
} | ||
@Module({ | ||
providers: [TestService], | ||
exports: [TestService], | ||
}) | ||
class TestModule { | ||
static forRoot(): DynamicModule { | ||
return { | ||
module: TestModule, | ||
global: true, | ||
}; | ||
} | ||
} | ||
|
||
describe('Async module initializing', () => { | ||
const testModules = async (imports: ModuleMetadata['imports']) => { | ||
const moduleRef = await Test.createTestingModule({ imports }).compile(); | ||
const fetchService: RegistryFetchService = moduleRef.get(RegistryFetchService); | ||
|
||
expect(fetchService).toBeDefined(); | ||
}; | ||
|
||
test('forRootAsync', async () => { | ||
await testModules([ | ||
TestModule.forRoot(), | ||
RegistryFetchModule.forRootAsync({ | ||
async useFactory(testService: TestService) { | ||
return { provider: testService.provider }; | ||
}, | ||
inject: [TestService], | ||
}), | ||
]); | ||
}); | ||
|
||
test('forFeatureAsync', async () => { | ||
await testModules([ | ||
TestModule.forRoot(), | ||
RegistryFetchModule.forFeatureAsync({ | ||
async useFactory(testService: TestService) { | ||
return { provider: testService.provider }; | ||
}, | ||
inject: [TestService], | ||
}), | ||
]); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { Test } from '@nestjs/testing'; | ||
import { Registry__factory, REGISTRY_CONTRACT_ADDRESSES } from '@lido-nestjs/contracts'; | ||
import { getNetwork } from '@ethersproject/networks'; | ||
import { Interface } from '@ethersproject/abi'; | ||
import { getDefaultProvider } from '@ethersproject/providers'; | ||
import { keysResponse, usedStatuses, mergedKeys, mergedSignatures } from '../fixtures/key-batch.fixture'; | ||
import { RegistryFetchModule, RegistryKeyBatchFetchService } from '../../'; | ||
describe('Keys', () => { | ||
const provider = getDefaultProvider(process.env.PROVIDERS_URLS); | ||
if (!process.env.CHAIN_ID) { | ||
console.error("CHAIN_ID wasn't provides"); | ||
process.exit(1); | ||
} | ||
const address = REGISTRY_CONTRACT_ADDRESSES[process.env.CHAIN_ID]; | ||
let fetchService: RegistryKeyBatchFetchService; | ||
|
||
const mockCall = jest.spyOn(provider, 'call').mockImplementation(async () => ''); | ||
|
||
jest.spyOn(provider, 'detectNetwork').mockImplementation(async () => getNetwork('mainnet')); | ||
|
||
beforeEach(async () => { | ||
const imports = [RegistryFetchModule.forFeature({ provider })]; | ||
const moduleRef = await Test.createTestingModule({ imports }).compile(); | ||
fetchService = moduleRef.get(RegistryKeyBatchFetchService); | ||
}); | ||
|
||
afterEach(async () => { | ||
mockCall.mockReset(); | ||
}); | ||
|
||
test('fetch', async () => { | ||
mockCall.mockImplementation(async () => { | ||
const iface = new Interface(Registry__factory.abi); | ||
return iface.encodeFunctionResult('getSigningKeys', keysResponse); | ||
}); | ||
const result = await fetchService.fetch(address, 0, 0, usedStatuses.length); | ||
|
||
const [firstKey] = result; | ||
|
||
const isKeySatisfies = mergedKeys.startsWith(firstKey.key); | ||
const isSignaturesSatisfies = mergedSignatures.startsWith(firstKey.depositSignature); | ||
const isUseStatusSatisfies = firstKey.used === usedStatuses[0]; | ||
|
||
expect(result).toHaveLength(usedStatuses.length); | ||
|
||
expect(isKeySatisfies).toBeTruthy(); | ||
expect(isSignaturesSatisfies).toBeTruthy(); | ||
expect(isUseStatusSatisfies).toBeTruthy(); | ||
|
||
expect(mockCall).toBeCalledTimes(1); | ||
}); | ||
|
||
test('fetch. fromIndex > toIndex', async () => { | ||
await expect(() => fetchService.fetch(address, 0, 2, 1)).rejects.toThrow(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { Test } from '@nestjs/testing'; | ||
import { JsonRpcBatchProvider } from '@ethersproject/providers'; | ||
import { RegistryFetchModule, RegistryKeyFetchService } from '../../'; | ||
import { REGISTRY_CONTRACT_ADDRESSES } from '@lido-nestjs/contracts'; | ||
import * as dotenv from 'dotenv'; | ||
|
||
dotenv.config(); | ||
|
||
describe('Keys', () => { | ||
const provider = new JsonRpcBatchProvider(process.env.PROVIDERS_URLS); | ||
if (!process.env.CHAIN_ID) { | ||
console.error("CHAIN_ID wasn't provides"); | ||
process.exit(1); | ||
} | ||
const address = REGISTRY_CONTRACT_ADDRESSES[process.env.CHAIN_ID]; | ||
|
||
let fetchService: RegistryKeyFetchService; | ||
|
||
beforeEach(async () => { | ||
const imports = [RegistryFetchModule.forFeature({ provider })]; | ||
const moduleRef = await Test.createTestingModule({ imports }).compile(); | ||
fetchService = moduleRef.get(RegistryKeyFetchService); | ||
}); | ||
|
||
test('fetch one key', async () => { | ||
const key = await fetchService.fetchOne(address, 21, 0, { blockTag: 6912872 }); | ||
|
||
expect(key).toBeInstanceOf(Object); | ||
|
||
expect(typeof key.operatorIndex).toBe('number'); | ||
expect(typeof key.index).toBe('number'); | ||
expect(typeof key.key).toBe('string'); | ||
expect(typeof key.depositSignature).toBe('string'); | ||
}); | ||
|
||
test('fetch operator keys', async () => { | ||
const keys = await fetchService.fetch(address, 21, 0, -1, { | ||
blockTag: 6912872, | ||
}); | ||
|
||
expect(keys).toBeInstanceOf(Array); | ||
expect(keys.length).toBe(3); | ||
}, 15_000); | ||
|
||
test('fetch multiply operators', async () => { | ||
const keys = await fetchService.fetch(address, 21, 0, 2, { | ||
blockTag: 6912872, | ||
}); | ||
|
||
expect(keys).toBeInstanceOf(Array); | ||
expect(keys.length).toBe(2); | ||
|
||
expect(keys[0].operatorIndex).toBe(21); | ||
expect(keys[1].operatorIndex).toBe(21); | ||
|
||
expect(keys[0].index).toBe(0); | ||
expect(keys[1].index).toBe(1); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import { Test } from '@nestjs/testing'; | ||
import { REGISTRY_CONTRACT_ADDRESSES, Registry__factory } from '@lido-nestjs/contracts'; | ||
import { getNetwork } from '@ethersproject/networks'; | ||
import { Interface } from '@ethersproject/abi'; | ||
import { getDefaultProvider } from '@ethersproject/providers'; | ||
import { operator, operatorFields } from '../fixtures/operator.fixture'; | ||
import { key, keyFields } from '../fixtures/key.fixture'; | ||
import { RegistryFetchModule, RegistryKeyFetchService } from '../../'; | ||
|
||
describe('Keys', () => { | ||
const provider = getDefaultProvider(process.env.PROVIDERS_URLS); | ||
let fetchService: RegistryKeyFetchService; | ||
if (!process.env.CHAIN_ID) { | ||
console.error("CHAIN_ID wasn't provides"); | ||
process.exit(1); | ||
} | ||
const address = REGISTRY_CONTRACT_ADDRESSES[process.env.CHAIN_ID]; | ||
const mockCall = jest.spyOn(provider, 'call').mockImplementation(async () => ''); | ||
|
||
jest.spyOn(provider, 'detectNetwork').mockImplementation(async () => getNetwork('mainnet')); | ||
|
||
beforeEach(async () => { | ||
const imports = [RegistryFetchModule.forFeature({ provider })]; | ||
const moduleRef = await Test.createTestingModule({ imports }).compile(); | ||
fetchService = moduleRef.get(RegistryKeyFetchService); | ||
}); | ||
|
||
afterEach(async () => { | ||
mockCall.mockReset(); | ||
}); | ||
|
||
test('fetchOne', async () => { | ||
const expected = { operatorIndex: 0, index: 1, moduleAddress: address, ...key }; | ||
|
||
mockCall.mockImplementation(async () => { | ||
const iface = new Interface(Registry__factory.abi); | ||
return iface.encodeFunctionResult('getSigningKey', keyFields); | ||
}); | ||
const result = await fetchService.fetchOne(address, expected.operatorIndex, expected.index); | ||
|
||
expect(result).toEqual(expected); | ||
expect(mockCall).toBeCalledTimes(1); | ||
}); | ||
|
||
test('fetch', async () => { | ||
const expectedFirst = { operatorIndex: 0, index: 1, moduleAddress: address, ...key }; | ||
const expectedSecond = { operatorIndex: 0, index: 2, moduleAddress: address, ...key }; | ||
|
||
mockCall.mockImplementation(async () => { | ||
const iface = new Interface(Registry__factory.abi); | ||
return iface.encodeFunctionResult('getSigningKey', keyFields); | ||
}); | ||
const result = await fetchService.fetch( | ||
address, | ||
expectedFirst.operatorIndex, | ||
expectedFirst.index, | ||
expectedSecond.index + 1, | ||
); | ||
|
||
expect(result).toEqual([expectedFirst, expectedSecond]); | ||
expect(mockCall).toBeCalledTimes(2); | ||
}); | ||
|
||
test('fetch all operator keys', async () => { | ||
const expected = { operatorIndex: 1, index: 0, moduleAddress: address, ...key }; | ||
|
||
mockCall | ||
.mockImplementationOnce(async () => { | ||
const iface = new Interface(Registry__factory.abi); | ||
return iface.encodeFunctionResult( | ||
'getNodeOperator', | ||
operatorFields({ ...operator, moduleAddress: address, totalSigningKeys: 1 }), | ||
); | ||
}) | ||
.mockImplementation(async () => { | ||
const iface = new Interface(Registry__factory.abi); | ||
return iface.encodeFunctionResult('getSigningKey', keyFields); | ||
}); | ||
const result = await fetchService.fetch(address, expected.operatorIndex); | ||
|
||
expect(result).toEqual([expected]); | ||
expect(mockCall).toBeCalledTimes(2); | ||
}); | ||
|
||
test('fetch. fromIndex > toIndex', async () => { | ||
await expect(() => fetchService.fetch(address, 0, 2, 1)).rejects.toThrow(); | ||
}); | ||
}); |
Oops, something went wrong.