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(on-ramp): upgrade on-ramp-sdk to v1.22.0 with abort controller support #6679

Merged
merged 10 commits into from
Jul 13, 2023
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
66 changes: 54 additions & 12 deletions app/components/UI/FiatOnRampAggregator/hooks/useSDKMethod.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useCallback, useEffect, useMemo, useState } from 'react';
import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
import { RegionsService } from '@consensys/on-ramp-sdk';
import { useFiatOnRampSDK, SDK } from '../sdk';
import Logger from '../../../../util/Logger';
Expand Down Expand Up @@ -30,6 +30,24 @@ function validMethodParams<T extends keyof RegionsService>(
});
}

/**
* Determines if the provided method is called with all its parameters.
* @param method - The method to check the parameters against.
* @param params - The parameters to check against the RegionsService interface.
* @returns A boolean indicating whether or not the provided parameters are all the parameters of the specified method.
*/
function hasAllParams<T extends keyof RegionsService>(
sethkfman marked this conversation as resolved.
Show resolved Hide resolved
method: T,
params: PartialParameters<RegionsService[T]> | [],
): boolean {
const { parameters } = SDK.getSignature(
RegionsService,
RegionsService.prototype[method],
);

return parameters.length === params.length;
}

interface config<T> {
method: T;
onMount?: boolean;
Expand Down Expand Up @@ -69,39 +87,62 @@ export default function useSDKMethod<T extends keyof RegionsService>(
...customParams: PartialParameters<RegionsService[T]> | []
) => Promise<any> | ReturnType<RegionsService[T]>,
] {
const method = typeof config === 'string' ? config : config.method;
const onMount = typeof config === 'string' ? true : config.onMount ?? true;

const { sdk } = useFiatOnRampSDK();
const [data, setData] = useState<Awaited<
ReturnType<RegionsService[T]>
> | null>(null);
const [error, setError] = useState<string | null>(null);
const [isFetching, setIsFetching] = useState<boolean>(true);
const [isFetching, setIsFetching] = useState<boolean>(onMount);
const stringifiedParams = useMemo(() => JSON.stringify(params), [params]);
const method = typeof config === 'string' ? config : config.method;
const onMount = typeof config === 'string' ? true : config.onMount ?? true;
const abortControllerRef = useRef<AbortController>();

const query = useCallback(
async (...customParams: PartialParameters<RegionsService[T]> | []) => {
const queryParams = customParams.length > 0 ? customParams : params;
const hasCustomParams = customParams.length > 0;
const queryParams = hasCustomParams ? customParams : params;

if (!validMethodParams(method, queryParams)) {
return;
}

const hasEveryParameter = hasAllParams(method, queryParams);
let abortController;

try {
abortControllerRef?.current?.abort();

if (!hasEveryParameter) {
abortController = new AbortController();
abortControllerRef.current = abortController;
}

setIsFetching(true);
setError(null);
setData(null);

if (sdk) {
const response = await sdk[method](
const methodParams = abortController
? [...queryParams, abortController]
: queryParams;
const response = (await sdk[method](
// @ts-expect-error spreading params error
...queryParams,
);
// @ts-expect-error response type error
...methodParams,
)) as Awaited<ReturnType<RegionsService[T]>>;
setData(response);
setIsFetching(false);

return response;
}
} catch (responseError) {
if (abortController?.signal.aborted) {
return;
}

Logger.error(responseError as Error, `useSDKMethod::${method} failed`);
setError((responseError as Error).message);
} finally {
setIsFetching(false);
}
},
Expand All @@ -112,9 +153,10 @@ export default function useSDKMethod<T extends keyof RegionsService>(
useEffect(() => {
if (onMount) {
query();
} else {
setIsFetching(false);
}
return () => {
abortControllerRef.current?.abort();
};
}, [query, onMount]);

return [{ data, error, isFetching }, query];
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@
"tough-cookie": "4.1.3"
},
"dependencies": {
"@consensys/on-ramp-sdk": "1.20.0",
"@consensys/on-ramp-sdk": "1.22.0",
"@eth-optimism/contracts": "0.0.0-2021919175625",
"@ethereumjs/common": "^2.3.1",
"@ethereumjs/tx": "^3.2.1",
Expand Down
45 changes: 23 additions & 22 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2613,15 +2613,18 @@
resolved "https://registry.yarnpkg.com/@colors/colors/-/colors-1.5.0.tgz#bb504579c1cae923e6576a4f5da43d25f97bdbd9"
integrity sha512-ooWCrlZP11i8GImSjTHYHLkvFDP48nS4+204nGb1RiX/WXYHmJA2III9/e2DWVabCESdW7hBAEzHRqUn9OUVvQ==

"@consensys/on-ramp-sdk@1.20.0":
version "1.20.0"
resolved "https://registry.yarnpkg.com/@consensys/on-ramp-sdk/-/on-ramp-sdk-1.20.0.tgz#31d65aa74edeaed161e9e5551f39f54df365cfd7"
integrity sha512-QJyu72qt4o1kXUq+5xMzGe2OFpWo0bXdHU4tXK4Rr4JYs0/0RD2mA7t/kC2+gPdjNaAx1NyzJcPv7Kef4uzK/w==
"@consensys/on-ramp-sdk@1.22.0":
version "1.22.0"
resolved "https://registry.yarnpkg.com/@consensys/on-ramp-sdk/-/on-ramp-sdk-1.22.0.tgz#19a43e354b4a2f504c28891870f0ad631c039495"
integrity sha512-Sof1eUSosU/Xum8U3PA9uEv4JMjV2c/89FbNX4lneqGCOaJAtsT5JcKP2bwVS0ftX1USb9/1KUmajhNlMcF5qw==
dependencies:
async "^3.2.3"
axios "^0.21.0"
axios "^0.27.0"
axios-retry "^3.1.2"
crypto-js "^4.1.1"
jsonpath-plus "^7.2.0"
lodash "^4.17.21"
reflect-metadata "^0.1.13"
uuid "^9.0.0"

"@cspotcode/source-map-support@^0.8.0":
Expand Down Expand Up @@ -10141,9 +10144,9 @@ aws4@^1.8.0:
integrity sha512-xh1Rl34h6Fi1DC2WWKfxUTVqRsNnr6LsKz2+hfwDxQJWmrx8+c7ylaqBMcHfl1U1r2dsifOvKX3LQuLNZ+XSvA==

axios-retry@^3.1.2:
version "3.2.5"
resolved "https://registry.yarnpkg.com/axios-retry/-/axios-retry-3.2.5.tgz#64952992837c7d9a12eec156a2694a7945f60895"
integrity sha512-a8umkKbfIkTiYJQLx3v3TzKM85TGKB8ZQYz4zwykt2fpO64TsRlUhjaPaAb3fqMWCXFm2YhWcd8V5FHDKO9bSA==
version "3.5.0"
resolved "https://registry.yarnpkg.com/axios-retry/-/axios-retry-3.5.0.tgz#32206b3e6555169488eded232527e36c8ce6e545"
integrity sha512-g48qNrLX30VU6ECWltpFCPegKK6dWzMDYv2o83W2zUL/Zh/SLXbT6ksGoKqYZHtghzqeeXhZBcSXJkO1fPbCcw==
dependencies:
"@babel/runtime" "^7.15.4"
is-retry-allowed "^2.2.0"
Expand All @@ -10157,13 +10160,6 @@ axios@1.4.0:
form-data "^4.0.0"
proxy-from-env "^1.1.0"

axios@^0.21.0:
version "0.21.4"
resolved "https://registry.yarnpkg.com/axios/-/axios-0.21.4.tgz#c67b90dc0568e5c1cf2b0b858c43ba28e2eda575"
integrity sha512-ut5vewkiu8jjGBdqpM44XxjuCjq9LAKeHVmoVfHVzy8eHgxxq8SbAVQNovDA8mVi05kP0Ea/n/UzcSHcTJQfNg==
dependencies:
follow-redirects "^1.14.0"

axios@^0.24.0:
version "0.24.0"
resolved "https://registry.yarnpkg.com/axios/-/axios-0.24.0.tgz#804e6fa1e4b9c5288501dd9dff56a7a0940d20d6"
Expand All @@ -10178,7 +10174,7 @@ axios@^0.26.1:
dependencies:
follow-redirects "^1.14.8"

axios@^0.x:
axios@^0.27.0, axios@^0.x:
version "0.27.2"
resolved "https://registry.yarnpkg.com/axios/-/axios-0.27.2.tgz#207658cc8621606e586c85db4b41a750e756d972"
integrity sha512-t+yRIyySRTp/wua5xEr+z1q60QmLq8ABsS5O9Me1AsE5dfKqgnCFzwiCZZ/cGNd1lq4/7akDWMxdhVlucjmnOQ==
Expand Down Expand Up @@ -15586,16 +15582,16 @@ focus-lock@^0.10.1:
dependencies:
tslib "^2.0.3"

follow-redirects@^1.14.0, follow-redirects@^1.14.8:
version "1.14.9"
resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.14.9.tgz#dd4ea157de7bfaf9ea9b3fbd85aa16951f78d8d7"
integrity sha512-MQDfihBQYMcyy5dhRDJUHcw7lb2Pv/TuE6xP1vyraLukNDHKbDxDNaOE3NbCAdKQApno+GPRyo1YAp89yCjK4w==

follow-redirects@^1.14.4, follow-redirects@^1.14.9, follow-redirects@^1.15.0:
version "1.15.2"
resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.15.2.tgz#b460864144ba63f2681096f274c4e57026da2c13"
integrity sha512-VQLG33o04KaQ8uYi2tVNbdrWp1QWxNNea+nmIB4EVM28v0hmP17z7aG1+wAkNzVq4KeXTq3221ye5qTJP91JwA==

follow-redirects@^1.14.8:
version "1.14.9"
resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.14.9.tgz#dd4ea157de7bfaf9ea9b3fbd85aa16951f78d8d7"
integrity sha512-MQDfihBQYMcyy5dhRDJUHcw7lb2Pv/TuE6xP1vyraLukNDHKbDxDNaOE3NbCAdKQApno+GPRyo1YAp89yCjK4w==

for-each@^0.3.3:
version "0.3.3"
resolved "https://registry.yarnpkg.com/for-each/-/for-each-0.3.3.tgz#69b447e88a0a5d32c3e7084f3f1710034b21376e"
Expand Down Expand Up @@ -18482,6 +18478,11 @@ jsonify@~0.0.0:
resolved "https://registry.yarnpkg.com/jsonify/-/jsonify-0.0.0.tgz#2c74b6ee41d93ca51b7b5aaee8f503631d252a73"
integrity sha1-LHS27kHZPKUbe1qu6PUDYx0lKnM=

jsonpath-plus@^7.2.0:
version "7.2.0"
resolved "https://registry.yarnpkg.com/jsonpath-plus/-/jsonpath-plus-7.2.0.tgz#7ad94e147b3ed42f7939c315d2b9ce490c5a3899"
integrity sha512-zBfiUPM5nD0YZSBT/o/fbCUlCcepMIdP0CJZxM1+KgA4f2T206f6VAg9e7mX35+KlMaIc5qXW34f3BnwJ3w+RA==

jsonschema@^1.2.4:
version "1.4.0"
resolved "https://registry.yarnpkg.com/jsonschema/-/jsonschema-1.4.0.tgz#1afa34c4bc22190d8e42271ec17ac8b3404f87b2"
Expand Down Expand Up @@ -23902,7 +23903,7 @@ redux@4.1.1, redux@^4.0.0, redux@^4.0.5:
dependencies:
"@babel/runtime" "^7.9.2"

reflect-metadata@0.1.13:
reflect-metadata@0.1.13, reflect-metadata@^0.1.13:
version "0.1.13"
resolved "https://registry.yarnpkg.com/reflect-metadata/-/reflect-metadata-0.1.13.tgz#67ae3ca57c972a2aa1642b10fe363fe32d49dc08"
integrity sha512-Ts1Y/anZELhSsjMcU605fU9RE4Oi3p5ORujwbIKXfWa+0Zxs510Qrmrce5/Jowq3cHSZSJqBjypxmHarc+vEWg==
Expand Down