Skip to content
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: remove lodash #1351

Merged
merged 2 commits into from
Feb 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions packages/integration-test/test/scheduled/native-token.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { PaymentNetworkFactory } from '@requestnetwork/payment-detection';
import { ExtensionTypes, RequestLogicTypes } from '@requestnetwork/types';
import { AdvancedLogic } from '@requestnetwork/advanced-logic';
import { CurrencyManager } from '@requestnetwork/currency';
import { omit } from 'lodash';

const advancedLogic = new AdvancedLogic(new CurrencyManager(CurrencyManager.getDefaultList()));

Expand Down Expand Up @@ -34,9 +33,10 @@ describe('PaymentNetworkFactory and createExtensionsDataForCreation', () => {
'aurora-testnet',
);
await expect(async () => {
await paymentNetwork.createExtensionsDataForCreation(
omit(createCreationActionParams, 'paymentNetworkName'),
);
await paymentNetwork.createExtensionsDataForCreation({
...createCreationActionParams,
paymentNetworkName: undefined,
});
}).rejects.toThrowError(
'The network name is mandatory for the creation of the extension pn-native-token.',
);
Expand Down
2 changes: 0 additions & 2 deletions packages/payment-detection/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@
"graphql": "16.8.1",
"graphql-request": "6.1.0",
"graphql-tag": "2.12.6",
"lodash": "4.17.21",
"satoshi-bitcoin": "1.0.4",
"tslib": "2.5.0"
},
Expand All @@ -64,7 +63,6 @@
"@jridgewell/gen-mapping": "0.3.2",
"@requestnetwork/advanced-logic": "0.39.0",
"@types/jest": "29.5.6",
"@types/lodash": "4.14.161",
"jest": "29.5.0",
"jest-junit": "16.0.0",
"source-map-support": "0.5.19",
Expand Down
4 changes: 1 addition & 3 deletions packages/payment-detection/src/thegraph/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { GraphQLClient } from 'graphql-request';
import { Block_Height, Maybe, getSdk } from './generated/graphql';
import { getSdk as getNearSdk } from './generated/graphql-near';
import { RequestConfig } from 'graphql-request/src/types';
import { omit } from 'lodash';

const HOSTED_THE_GRAPH_URL =
'https://api.thegraph.com/subgraphs/name/requestnetwork/request-payments-';
Expand Down Expand Up @@ -53,7 +52,7 @@ const extractClientOptions = (

// build query options
const queryOptions: TheGraphQueryOptions = {};
const { minIndexedBlock } = optionsObject;
const { minIndexedBlock, ...clientOptions } = optionsObject;
if (minIndexedBlock) {
queryOptions.blockFilter = { number_gte: minIndexedBlock };
} else if (url.match(/^https:\/\/gateway-\w+\.network\.thegraph\.com\//)) {
Expand All @@ -62,7 +61,6 @@ const extractClientOptions = (
}

// build client options
const clientOptions: RequestConfig = omit(optionsObject, 'minIndexedBlock');
return [clientOptions, queryOptions];
};

Expand Down
28 changes: 10 additions & 18 deletions packages/payment-detection/src/thegraph/info-retriever.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { PaymentTypes } from '@requestnetwork/types';
import { ICurrencyManager } from '@requestnetwork/currency';
import { utils } from 'ethers';
import { pick, mapValues } from 'lodash';
import type { TheGraphClient } from './client';
import type { EscrowEventResultFragment, PaymentEventResultFragment } from './generated/graphql';
import { formatAddress, unpadAmountFromChainlink } from '../utils';
import { formatAddress, transformNonNull, unpadAmountFromChainlink } from '../utils';
import { TransferEventsParams, ITheGraphBaseInfoRetriever } from '../types';

/**
Expand Down Expand Up @@ -85,22 +84,15 @@ export class TheGraphInfoRetriever<TGraphQuery extends TransferEventsParams = Tr
feeAmount,
block: payment.block,
to: formatAddress(payment.to, 'to'),
...mapValues(
pick(
payment,
'txHash',
'gasUsed',
'gasPrice',
'amountInCrypto',
'feeAmountInCrypto',
'maxRateTimespan',
),
(val) => (val !== null ? String(val) : undefined),
),
// Make sure the checksum is right for addresses.
...mapValues(pick(payment, 'from', 'feeAddress', 'tokenAddress'), (str, key) =>
str ? formatAddress(str, key) : undefined,
),
...transformNonNull(payment, 'txHash', String),
...transformNonNull(payment, 'gasUsed', String),
...transformNonNull(payment, 'gasPrice', String),
...transformNonNull(payment, 'amountInCrypto', String),
...transformNonNull(payment, 'feeAmountInCrypto', String),
...transformNonNull(payment, 'maxRateTimespan', String),
...transformNonNull(payment, 'from', formatAddress),
...transformNonNull(payment, 'feeAddress', formatAddress),
...transformNonNull(payment, 'tokenAddress', formatAddress),
},
};
}
Expand Down
6 changes: 3 additions & 3 deletions packages/payment-detection/src/thegraph/superfluid.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { GraphQLClient } from 'graphql-request';
import { getSdk } from './generated/graphql-superfluid';
import { RequestConfig } from 'graphql-request/src/types';
import { omit } from 'lodash';

const BASE_URL = `https://api.thegraph.com`;
const NETWORK_TO_URL: Record<string, string> = {
Expand All @@ -24,7 +23,9 @@ export const getTheGraphSuperfluidClient = (
network: string,
options?: TheGraphClientOptions,
): TheGraphSuperfluidClient => {
const baseUrl = options?.baseUrl || network === 'private' ? 'http://localhost:8000' : BASE_URL;
const { baseUrl: _baseUrl, ...clientOptions } = options ?? {};

const baseUrl = _baseUrl || network === 'private' ? 'http://localhost:8000' : BASE_URL;
// Note: it is also possible to use the IPFS hash of the subgraph
// eg. /subgraphs/id/QmcCaSkefrmhe4xQj6Y6BBbHiFkbrn6UGDEBUWER7nt399
// which is a better security but would require an update of the
Expand All @@ -33,6 +34,5 @@ export const getTheGraphSuperfluidClient = (
const url = `${baseUrl}/subgraphs/name/superfluid-finance/protocol-v1-${
NETWORK_TO_URL[network] || network
}`;
const clientOptions = omit(options, 'baseUrl');
return getSdk(new GraphQLClient(url, clientOptions));
};
13 changes: 13 additions & 0 deletions packages/payment-detection/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,3 +204,16 @@ export const formatAddress: {
});
}
};

/** applies a transformation if val[key] is not null */
export const transformNonNull = <T extends Record<string, unknown>, TKey extends keyof T, TOut>(
val: T,
key: TKey,
transform: (val: T[TKey], key: keyof T) => TOut = (val) => val as TOut,
): { [P in TKey]: TOut } | Partial<Record<TKey, TOut>> =>
({
[key as TKey]:
val[key] !== undefined && val[key] !== null ? transform(val[key], key) : undefined,
}) as {
[P in TKey]: TOut;
};
32 changes: 32 additions & 0 deletions packages/payment-detection/test/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import {
getPaymentReference,
formatAddress,
} from '../src';
import { transformNonNull } from '../src/utils';
import { logger, errors } from 'ethers';

describe('conversion: padding amounts for Chainlink', () => {
const currencyManager = CurrencyManager.getDefault();
Expand Down Expand Up @@ -372,3 +374,33 @@ describe('formatAddress', () => {
expect(val).toBeUndefined();
});
});

fdescribe(transformNonNull, () => {
it('ignores a null value', () => {
expect(transformNonNull({ foo: null }, 'foo')).toEqual({ foo: undefined });
});
it('ignores an undefined value', () => {
expect(transformNonNull({ foo: undefined }, 'foo')).toEqual({ foo: undefined });
});
it('keeps an empty string', () => {
expect(transformNonNull({ foo: '' }, 'foo')).toEqual({ foo: '' });
});
it('keeps a value', () => {
expect(transformNonNull({ foo: 'hello' }, 'foo')).toEqual({ foo: 'hello' });
});
it('transforms a value', () => {
expect(transformNonNull({ foo: 'hello' }, 'foo', (s) => s.toUpperCase())).toEqual({
foo: 'HELLO',
});
});

it('can be used with formatAddress, passing down the key', () => {
expect(() => transformNonNull({ foo: '0xhello' }, 'foo', formatAddress)).toThrowError(
logger.makeError('invalid address', errors.INVALID_ARGUMENT, {
argument: 'address',
value: '0xhello',
key: 'foo',
}),
);
});
});
1 change: 0 additions & 1 deletion packages/toolbox/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@
"@requestnetwork/utils": "0.40.0",
"ethers": "5.5.1",
"inquirer": "8.2.0",
"lodash": "4.17.21",
"tslib": "2.5.0",
"yargs": "17.6.2"
},
Expand Down
4 changes: 4 additions & 0 deletions packages/types/src/payment-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,10 @@ export interface IERC20FeePaymentEventParameters extends IERC20PaymentEventParam
feeAmountInCrypto?: string;
amountInCrypto?: string;
tokenAddress?: string;

gasUsed?: string;
gasPrice?: string;
maxRateTimespan?: string;
Comment on lines +221 to +224
Copy link
Contributor Author

Choose a reason for hiding this comment

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

the new typing makes it clear that these were missing

}

export type ERC20PaymentNetworkEventParameters =
Expand Down
7 changes: 1 addition & 6 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5287,11 +5287,6 @@
dependencies:
"@types/node" "*"

"@types/[email protected]":
version "4.14.161"
resolved "https://registry.npmjs.org/@types/lodash/-/lodash-4.14.161.tgz"
integrity sha512-EP6O3Jkr7bXvZZSZYlsgt5DIjiGr0dXP1/jVEwVLTFgg0d+3lWVQkRavYVQszV7dYUwvg0B8R0MBDpcmXg7XIA==

"@types/lru-cache@^5.1.0":
version "5.1.1"
resolved "https://registry.npmjs.org/@types/lru-cache/-/lru-cache-5.1.1.tgz"
Expand Down Expand Up @@ -15898,7 +15893,7 @@ [email protected]:
resolved "https://registry.npmjs.org/lodash/-/lodash-4.17.20.tgz"
integrity sha512-PlhdFcillOINfeV7Ni6oF1TAEayyZBoZ8bcshTHqOYJYlrqzRK5hagpagky5o4HfCzzd1TRkXPMFq6cKk9rGmA==

lodash@4.17.21, lodash@^4.14.2, lodash@^4.17.11, lodash@^4.17.12, lodash@^4.17.14, lodash@^4.17.15, lodash@^4.17.20, lodash@^4.17.21, lodash@^4.17.4, lodash@~4.17.0:
lodash@^4.14.2, lodash@^4.17.11, lodash@^4.17.12, lodash@^4.17.14, lodash@^4.17.15, lodash@^4.17.20, lodash@^4.17.21, lodash@^4.17.4, lodash@~4.17.0:
version "4.17.21"
resolved "https://registry.npmjs.org/lodash/-/lodash-4.17.21.tgz"
integrity sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==
Expand Down
Loading