-
Notifications
You must be signed in to change notification settings - Fork 13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(trading-sdk): add an option to toggle logs #233
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
6ab88ff
feat(trading-sdk): add an option to toggle logs
shoom3301 52915fd
chore: rename enableLogging
shoom3301 63f2ac8
Merge branch 'main' of https://github.com/cowprotocol/cow-sdk into fi…
shoom3301 1271cf2
chore: const -> function
shoom3301 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
jest.mock('cross-fetch', () => { | ||
// eslint-disable-next-line @typescript-eslint/no-var-requires | ||
const fetchMock = require('jest-fetch-mock') | ||
// Require the original module to not be mocked... | ||
const originalFetch = jest.requireActual('cross-fetch') | ||
return { | ||
__esModule: true, | ||
...originalFetch, | ||
default: fetchMock, | ||
} | ||
}) | ||
import { TradingSdk } from './tradingSdk' | ||
import { SupportedChainId } from '../common' | ||
import { TradeBaseParameters } from './types' | ||
import { OrderBookApi, OrderKind } from '../order-book' | ||
|
||
const defaultOrderParams: TradeBaseParameters = { | ||
sellToken: '0xfff9976782d46cc05630d1f6ebab18b2324d6b14', | ||
sellTokenDecimals: 18, | ||
buyToken: '0x0625afb445c3b6b7b929342a04a22599fd5dbb59', | ||
buyTokenDecimals: 18, | ||
amount: '100000000000000000', | ||
kind: OrderKind.SELL, | ||
} | ||
|
||
const quoteResponseMock = { | ||
quote: { | ||
sellToken: '0xfFf9976782d46CC05630D1f6eBAb18b2324d6B14', | ||
buyToken: '0x0625aFB445C3B6B7B929342a04A22599fd5dBB59', | ||
receiver: '0xc8c753ee51e8fc80e199ab297fb575634a1ac1d3', | ||
sellAmount: '1005456782512030400', | ||
buyAmount: '400000000000000000000', | ||
validTo: 1737468944, | ||
appData: | ||
'{"appCode":"test","metadata":{"orderClass":{"orderClass":"market"},"quote":{"slippageBips":50}},"version":"1.3.0"}', | ||
appDataHash: '0xe269b09f45b1d3c98d8e4e841b99a0779fbd3b77943d069b91ddc4fd9789e27e', | ||
feeAmount: '1112955650440102', | ||
kind: 'buy', | ||
partiallyFillable: false, | ||
sellTokenBalance: 'erc20', | ||
buyTokenBalance: 'erc20', | ||
signingScheme: 'eip712', | ||
}, | ||
from: '0xc8c753ee51e8fc80e199ab297fb575634a1ac1d3', | ||
expiration: '2025-01-21T14:07:44.176194885Z', | ||
id: 575498, | ||
verified: true, | ||
} | ||
|
||
describe('TradingSdk', () => { | ||
let orderBookApi: OrderBookApi | ||
|
||
describe('Logs', () => { | ||
beforeEach(() => { | ||
orderBookApi = { | ||
context: { | ||
chainId: SupportedChainId.GNOSIS_CHAIN, | ||
}, | ||
getQuote: jest.fn().mockResolvedValue(quoteResponseMock), | ||
sendOrder: jest.fn().mockResolvedValue('0x01'), | ||
} as unknown as OrderBookApi | ||
}) | ||
|
||
afterEach(() => { | ||
jest.resetAllMocks() | ||
}) | ||
|
||
it('When logs option is set to false, then should not display logs', async () => { | ||
const logSpy = jest.spyOn(console, 'log') | ||
|
||
const sdk = new TradingSdk( | ||
{ | ||
chainId: SupportedChainId.GNOSIS_CHAIN, | ||
appCode: 'test', | ||
signer: '0xa43ccc40ff785560dab6cb0f13b399d050073e8a54114621362f69444e1421ca', | ||
}, | ||
{ enableLogging: false, orderBookApi } | ||
) | ||
|
||
await sdk.getQuote(defaultOrderParams) | ||
|
||
expect(logSpy.mock.calls.length).toBe(0) | ||
}) | ||
|
||
it('When logs option is set to true, then should display logs', async () => { | ||
const logSpy = jest.spyOn(console, 'log') | ||
|
||
const sdk = new TradingSdk( | ||
{ | ||
chainId: SupportedChainId.GNOSIS_CHAIN, | ||
appCode: 'test', | ||
signer: '0xa43ccc40ff785560dab6cb0f13b399d050073e8a54114621362f69444e1421ca', | ||
}, | ||
{ enableLogging: true, orderBookApi } | ||
) | ||
|
||
await sdk.getQuote(defaultOrderParams) | ||
|
||
expect(logSpy.mock.calls[0][0]).toContain('[COW TRADING SDK]') | ||
}) | ||
}) | ||
}) |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Its a bit strange pattern to mutate a global var like this from the constructor. I don't care too much, so maybe I'm nitpicking, but technically, you create 2 instances of the SDK and the second one will override the option of the first one
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I was thinking about that.
It's a bit tricky. The sdk is provided in two ways:
TradingSdk
classpostLimitOrder
,getQuote
and othersBecause of that, it's a bit difficult to wire them up together in terms of logging.
I could put
logEnabled
flag to each function, but I fee it a bit overengineering.I understand that adding
enabled
property to thelog
function is a hacky way, but this is tradeoff in favor of simplicity.