Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Implement service using IoC #50

Merged
merged 3 commits into from
Jul 9, 2024
Merged

Implement service using IoC #50

merged 3 commits into from
Jul 9, 2024

Conversation

anxolin
Copy link
Contributor

@anxolin anxolin commented Jul 8, 2024

Continues on #49

Integrates inversify, an IoC framework.

It is used to replace the factory method added #48 to inject the slippage service using IoC.

I also added a unit test for the service, where I mocked the repository to be able to test the service in isolation

When reviewing this PR

I didn't care about the implementation of the repository (is mocked) or the service (I made also mocked implementation that returns random numbers).

The point is to decouple the layers, and integrate this framework.
Some future PR will make the real implementation for the repositories and services.

Test

Make sure they tests passes:
nx run services:test --watch

image

Make sure the service is available:
http://localhost:3010/chains/1/markets/0x6b175474e89094c44da98b954eedeac495271d0f-0x2260fac5e5542a773aa44fbcfedf7c193bc2c599/slippageTolerance

If you keep refreshing, you'll see the result keeps changing

Screen.Recording.2024-07-08.at.22.52.19.mov

@anxolin anxolin changed the title Implement using IoC Implement service using IoC Jul 8, 2024
Copy link

socket-security bot commented Jul 8, 2024

New and removed dependencies detected. Learn more about Socket for GitHub ↗︎

Package New capabilities Transitives Size Publisher
npm/inversify@6.0.2 None 0 1.52 MB jameskmonger
npm/redis-errors@1.2.0 None 0 8.85 kB bridgear
npm/reflect-metadata@0.2.2 None 0 241 kB rbuckton
npm/resolve@1.22.2 environment, filesystem +1 154 kB ljharb
npm/rfdc@1.3.0 None 0 24 kB davidmarkclements
npm/rimraf@3.0.2 filesystem Transitive: environment +4 41.1 kB isaacs
npm/rxjs@7.8.1 None 0 4.5 MB blesh
npm/safe-buffer@5.1.2 None 0 31.7 kB feross
npm/safer-buffer@2.1.2 None 0 42.3 kB chalker
npm/secure-json-parse@2.7.0 None 0 40.7 kB matteo.collina
npm/semver@7.3.4 None +1 101 kB isaacs
npm/signal-exit@3.0.7 None 0 9.96 kB isaacs
npm/source-map-js@1.2.0 None 0 140 kB 7rulnik
npm/source-map@0.6.1 None 0 805 kB tromey
npm/string-width@4.2.3 None +2 58.4 kB sindresorhus
npm/strip-ansi@6.0.1 None +1 9.64 kB sindresorhus
npm/strip-json-comments@3.1.1 None 0 6.96 kB sindresorhus
npm/ts-jest@29.1.1 environment, filesystem, unsafe Transitive: eval, network, shell +51 2.15 MB kul
npm/ts-node@10.9.1 environment, filesystem, unsafe 0 747 kB cspotcode
npm/tslib@2.6.0 None 0 83.5 kB typescript-bot
npm/type-check@0.4.0 None +1 57.9 kB gkz
npm/typechain@8.2.0 filesystem +4 270 kB ethereum-ts-bot
npm/typeorm-fastify-plugin@1.0.5 None 0 11.4 kB jclemens24
npm/typeorm@0.3.17 environment, eval, filesystem Transitive: network +9 21.1 MB pleerock
npm/typescript@5.4.5 None 0 32.4 MB typescript-bot
npm/url-parse@1.5.10 None +2 78.5 kB swaagie
npm/util-deprecate@1.0.2 None 0 5.48 kB tootallnate
npm/v8-compile-cache@2.3.0 environment, filesystem, unsafe 0 16.8 kB zertosh
npm/vite-plugin-dts@3.1.0 unsafe 0 82.9 kB qmhc
npm/vite-tsconfig-paths@4.2.0 filesystem 0 95.5 kB aleclarson
npm/vite@4.5.3 environment, eval, filesystem, network, shell, unsafe 0 3.34 MB vitebot
npm/walker@1.0.8 filesystem 0 5.8 kB daaku
npm/which-typed-array@1.1.15 None +4 92.7 kB ljharb
npm/yargs-parser@21.1.1 environment, filesystem 0 128 kB oss-bot
npm/yargs@17.7.2 environment, filesystem +5 379 kB oss-bot

🚮 Removed packages: npm/@rushstack/node-core-library@3.59.5, npm/@volar/language-core@1.8.0, npm/@volar/source-map@1.8.0, npm/@vue/language-core@1.8.4, npm/@vue/shared@3.3.4, npm/array-back@3.1.0, npm/ieee754@1.2.1, npm/is-core-module@2.12.1, npm/make-error@1.3.6, npm/pino-abstract-transport@1.0.0, npm/punycode@2.3.0

View full report↗︎

@@ -1,3 +1,5 @@
import 'reflect-metadata';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't seem to be used anywhere. Is importing enough to "activate" it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, indeed. This allows us to use the introspection to make annotations work

});

it('should return always 1234', async () => {
expect(await slippageService.getSlippageBps('0x0', '0x0')).toEqual(0);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test description doesn't match the result

Suggested change
expect(await slippageService.getSlippageBps('0x0', '0x0')).toEqual(0);
expect(await slippageService.getSlippageBps('0x0', '0x0')).toEqual(1234);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just changed a silly implementation for another. This will change soon

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Anyways, fixed (set to 0 in the description)

}

// TODO: Find good name for the implementation, as Leandro don't like "Impl" suffix, just don't want to couple it to add Coingecko in its name (as that would couple it to the data-source, and the adding a name to specify the algorithm might complicate the name, as this will use some custom logic based on standard deviation, so for now as we plan to have just one implementation, I want to keep it simple. But happy to get ideas here)
export const slippageServiceSymbol = Symbol.for('SlippageService');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the symbol? Is that a common pattern with injectable?
Also, since it's a const, should it be all CAPS?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, it was required. Followed their getting started guide. I think its similar to Spring, when you give a name to a bean. Here they use symbols so its not loose strings, and this way they can check for collisions

image

Base automatically changed from add-repository to add-service July 9, 2024 15:18
Base automatically changed from add-service to main July 9, 2024 15:30
@anxolin anxolin marked this pull request as ready for review July 9, 2024 19:22
@anxolin
Copy link
Contributor Author

anxolin commented Jul 9, 2024

@shoom3301 @alfetopito I will merge this PR and just iterate. I will give it a try in a bit to the other framework proposed by Sasha, I think it will be easier to just iterate from the state of this PR

@anxolin anxolin merged commit a0f7e73 into main Jul 9, 2024
6 checks passed
@anxolin anxolin deleted the IoC branch July 9, 2024 19:25
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants