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

fix: improved currency detection #1345

Merged
merged 7 commits into from
Feb 5, 2024
Merged

Conversation

KolevDarko
Copy link
Contributor

@KolevDarko KolevDarko commented Jan 25, 2024

The reason for this PR came about when trying to introduce native USDC and bridged USDC to Polygon and Optimism.
Similar fix was done to Arbitrum One here by @alexandre-abrioux.
However, the reason it worked for Arbitrum, and does not work for Polygon and Optimism is because the network name arbitrum-one has a dash in it, the full currency identifier is USDC-arbitrum-one.
Because of the two dashes, the currency parsing from CurrencyManager failed with CurrencyManager.fromSymbol and then was correctly detected by CurrencyManager.fromId.
But with chain names without dashes, the CurrencyManager.fromSymbol worked but it fetches the wrong USDC token, it doesn't prioritize the id, that's why I had to prioritize the CurrencyManager.fromId with this PR.

I think it's the more correct way of doing things because the id is exact match, if the exact match fails, then we can attempt to detect based on the symbol with or without a network etc.

Further Context on the USDC tokens in Asana Task

Comment on lines +78 to +81
const currencyFromId = this.fromId(currencyIdentifier);

if (currencyFromId) return currencyFromId;

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Main purpose of this PR is to prioritize the method CurrencyManager.fromId before fromSymbol, because it matches the direct id of the currency, it's more specific.

Comment on lines 74 to 76
if (network && currencyIdentifier.indexOf(network) === -1) {
currencyIdentifier = `${currencyIdentifier}-${network}`;
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

In making the improvement below, we need to handle the case where we provide the network as separate parameter but the network is not within the symbol.
There is an issue when we have both Fiat and ERC20 token with same symbol, like MNT.
MNT - fiat currency
MNT-mainnet - Ethereum token.
The simplest fix here was to add the network to the currencyIdentifier if it's not already there.

@alexandre-abrioux alexandre-abrioux dismissed their stale review January 25, 2024 16:17

found an issue with token id

@@ -709,4 +709,46 @@ describe('CurrencyManager', () => {
expect(path).toMatchObject([eur.hash, dai.hash.toLowerCase()]);
});
});

describe('Native and bridged USDC', () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

The describe and it should give details on the behaviour you are testing, not about the data.

Comment on lines +78 to +80
const currencyFromId = this.fromId(currencyIdentifier);

if (currencyFromId) return currencyFromId;
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't get this. If you are using this method by passing always IDs, why not use fromId directly?

Copy link
Member

Choose a reason for hiding this comment

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

You're right, we should always use fromId in user code when we are sure to have an ID as input.

In the case of from, we're not entirely sure what kind of input we have (ID, symbol, address). In this case, we want to prioritize looking by ID rather than symbol because it's more precise (like we already prioritize looking by address first). More context here: #1345 (comment)

I'm sure we are over-using from sometimes where we could replace it with fromId in user code, but the path with from should work too and needs to be fixed.

Copy link
Contributor

Choose a reason for hiding this comment

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

should we deprecate from?

Copy link
Member

@alexandre-abrioux alexandre-abrioux Feb 1, 2024

Choose a reason for hiding this comment

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

Yes, that could be a good start to add the @deprecated JSDOC 👍

name: 'USD Coin (PoS)',
symbol: 'USDCe',
network: 'matic',
id: 'USDC-matic',
Copy link
Contributor

Choose a reason for hiding this comment

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

can you give context why the ID wouldn't be USDCe-matic for this token?

Copy link
Member

Choose a reason for hiding this comment

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

I agree with @benjlevesque; maybe a small comment in the code explaining why we test a currency with an ID and a Symbol that differs would do. Here is some context about it, @KolevDarko feel free to reuse some of the explanation for the comment in the code: #1345 (comment)

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 added an elaborate comment above the describe block; there are many ways to explain this, I'm open to suggestions.

Copy link
Member

Choose a reason for hiding this comment

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

Looks good!

@KolevDarko
Copy link
Contributor Author

Found another way to solve the underlying problem I had

@KolevDarko KolevDarko closed this Jan 29, 2024
@MantisClone
Copy link
Member

@KolevDarko Although you ended up fixing the behavior another way, do you still think that this PR is still the "more correct way of doing things"?
CC @benjlevesque

I want to understand if this PR still has merit or benefit for the rest of the Request Network ecosystem.

@alexandre-abrioux
Copy link
Member

I agree with @MantisClone, I think we should keep this PR open and continue to push for this change. Retrieving a currency from the currency list with the CurrencyManager.from() function should first prioritize using the currency ID and then try looking in the list from the currency symbol.

Context

While maintaining an extended list of currencies at Request Finance for our day-to-day operations (https://api.request.finance/currency/) we came across the need to rename some currencies.

For instance, on Arbitrum, the community first used a bridged USDC token. Then in 2023, Circle deployed their own version of USDC on Aritrum, called the "native USDC token", see https://www.circle.com/blog/arbitrum-usdc-now-available.

At Request Finance, we renamed the old USDC token to USDCe, and we introduced the new native token as USDC.

Behind the scenes, the ID of the legacy token did not change to prevent breaking previously created Requests. So the symbol and ID of these currencies started to drift apart:

  • USDC-arbitrum (ID), the legacy token, now corresponds to USDCe (symbol)
  • USDCn-arbitrum (ID), the new token, corresponds to USDC (symbol)

Anywhere we retrieve a currency with the CurrencyManager.from() function, we should make sure to first look it up by ID, because it is always more precise.

Copy link
Member

@alexandre-abrioux alexandre-abrioux left a comment

Choose a reason for hiding this comment

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

I'll let you answer @benjlevesque's suggestions, after that it's all good for me 👍

@KolevDarko KolevDarko merged commit cc43c2e into master Feb 5, 2024
24 of 25 checks passed
@KolevDarko KolevDarko deleted the fix/currency-fromid-precedence branch February 5, 2024 16:25
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.

5 participants