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

refactor: api clients and add stx-20 balances #5315

Closed
wants to merge 1 commit into from

Conversation

fbwoolf
Copy link
Contributor

@fbwoolf fbwoolf commented Apr 25, 2024

Try out Leather build 89f255eExtension build, Test report, Storybook, Chromatic

@kyranjamie hoping to get your feedback here on refactoring our api clients via axios and abandoning the StacksClient and BitcoinClient pattern. I used implementing STX-20 balances, and refactored one Stacks balance query, to show how we could do this with all our current queries and setup a better pattern to follow. Open to feedback, but I don't think it would be too hard to refactor all of them here.

@fbwoolf fbwoolf requested a review from kyranjamie April 25, 2024 01:00
@@ -19,6 +16,7 @@ function trackApiError(url: string, statusCode: number) {
* @deprecated Use `axios` directly instead. Fetch only needed for interation
* with generated stacks blockchain api library
*/
// TEMPORARY RELOCATION
export async function wrappedFetch(input: RequestInfo, init: RequestInit = {}) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This will be removed if we refactor them all.


import type { DefaultNetworkConfigurations } from '@shared/constants';

// import type { AddressBalanceResponse } from '@shared/models/account.model';
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 removed this and am using the one imported from the api-types bc the need for this might be outdated? I'm not seeing why we would need to keep this?

Copy link
Collaborator

@kyranjamie kyranjamie left a comment

Choose a reason for hiding this comment

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

One thing to check, we use interceptors to track certain api failures

https://github.com/leather-wallet/extension/blob/0899e9d0d375a2636071907886b3758c14dd8690/src/app/common/api/fetch-wrapper.ts#L34-L45

think these work independent to each client. So, does axios.create break this?

Comment on lines +3 to +7
export function stx20ApiClient() {
return axios.create({
baseURL: 'https://api.stx20.com/api/v1',
});
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think we want to call this once, and export, vs every time.

E.g, if we want to modify all requests for stx20.com, we'd use stx20Client.interceptors, which afaik depends on a single instance

Copy link
Collaborator

Choose a reason for hiding this comment

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

mm, but yeah we can't do this for custom networks

Copy link
Collaborator

@kyranjamie kyranjamie Apr 25, 2024

Choose a reason for hiding this comment

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

so now I'm questioning if it is a good idea to use all the separate axios clients / vs a single client where we don't set baseURL. Thoughts?

Copy link
Contributor Author

@fbwoolf fbwoolf Apr 29, 2024

Choose a reason for hiding this comment

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

Thinking on the refactor here, I might close this and separate out the STX-20 balance work just to get that merged first. I'm not sure we absolutely need to refactor the api clients rn, but I'd like to think on how to set this all up a bit better.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You are right, this should only be called once with the config nec to handle that service.
https://blog.logrocket.com/understanding-axios-create/

@@ -0,0 +1,16 @@
import type { AddressBalanceResponse } from '@stacks/stacks-blockchain-api-types';
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can you check this lib is up to date?

Comment on lines +10 to +14
export function hiroApiClient(network: DefaultNetworkConfigurations) {
return axios.create({
baseURL: defaultNetworksKeyedById[network].chain.stacks.url,
headers: { ...leatherHeaders },
});
Copy link
Collaborator

Choose a reason for hiding this comment

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

Mmm, actually (contrary to my earlier recommendation), this wouldn't work with custom networks, should just pass the URL

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 see this now, thx.

Comment on lines +16 to +19
export interface Stx20Token extends Omit<Stx20Balance, 'balance'> {
balance: Money;
marketData: null;
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Per comment in other PR, do wonder if these should be paired ahead of time at model-level?

import { Stx20TokenAssetItemLayout } from './stx20-token-asset-item.layout';

interface Stx20TokenAssetListProps {
stx20Tokens: Stx20Token[];
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit, for me it's clear enough from component name this is for stx20, so just "tokens" is good as a prop name?

@fbwoolf fbwoolf closed this Apr 30, 2024
@fbwoolf fbwoolf deleted the refactor/api-clients-with-stx-20 branch July 12, 2024 16:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Refactor StacksClient and BitcoinClient Display STX-20 balances
2 participants