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: add base parameter to /rates API #1527

Merged
merged 4 commits into from
Jun 29, 2023
Merged

feat: add base parameter to /rates API #1527

merged 4 commits into from
Jun 29, 2023

Conversation

mkurapov
Copy link
Contributor

@mkurapov mkurapov commented Jun 28, 2023

Changes proposed in this pull request

  • Making sure rates service can handle different exchange rates with multiple decimal points: before, there would be a bug where the currency conversion would fail because BigInt could not support decimal place results that would arise from exchange rates with more decimals than the assetScale
  • Adding base parameter to /rates, updating MASE to properly return with the base param

Context

Checklist

  • Related issues linked using fixes #number
  • Tests added/updated
  • Documentation added
  • Make sure that all checks pass

@netlify
Copy link

netlify bot commented Jun 28, 2023

Deploy Preview for brilliant-pasca-3e80ec canceled.

Name Link
🔨 Latest commit 98bddc5
🔍 Latest deploy log https://app.netlify.com/sites/brilliant-pasca-3e80ec/deploys/649c610b0c504100081c1e58

@github-actions github-actions bot added pkg: backend Changes in the backend package. pkg: mock-ase type: source Changes business logic type: tests Testing related labels Jun 28, 2023
(opts.sourceAmount * BigInt(scaledExchangeRate * shiftUp)) / BigInt(shiftUp)
)

return BigInt(Math.round(Number(opts.sourceAmount) * scaledExchangeRate))
Copy link
Contributor Author

@mkurapov mkurapov Jun 28, 2023

Choose a reason for hiding this comment

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

BigInt divided by BigInt ends up truncating and not rounding the result. I think for currency, rounding up or down to the nearest unit makes more sense (and is standard). We can't really get around using number, since we are dealing with decimals for the exchange rate

Copy link
Member

Choose a reason for hiding this comment

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

Do we need to be concerned about exceeding the MAX_SAFE_INTEGER (in the Number(opts.sourceAmount) case)?

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'm quite certain we will never hit payment amounts over 9007199254740991 in any realistic scenario, but I can add a log just to be extra safe

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We don't have any tooling right now for proper alerting/error management, and I feel like a log would just get swallowed. Going to merge this in for now.

@mkurapov mkurapov changed the title 1524/mk/rates feat: add base parameter to /rates API Jun 28, 2023
@mkurapov mkurapov marked this pull request as ready for review June 28, 2023 16:35
Comment on lines -25 to -32
expect(
convert({
exchangeRate: 0.5,
sourceAmount: 101n,
sourceAsset: asset(9),
destinationAsset: asset(9)
})
).toBe(50n)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

(opts.sourceAmount * BigInt(scaledExchangeRate * shiftUp)) / BigInt(shiftUp)
)

return BigInt(Math.round(Number(opts.sourceAmount) * scaledExchangeRate))
Copy link
Member

Choose a reason for hiding this comment

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

Do we need to be concerned about exceeding the MAX_SAFE_INTEGER (in the Number(opts.sourceAmount) case)?

@mkurapov mkurapov merged commit 7b8f4a8 into main Jun 29, 2023
@mkurapov mkurapov deleted the 1524/mk/rates branch June 29, 2023 12:17
Muasa-harman pushed a commit to Muasa-harman/rafiki that referenced this pull request Jul 13, 2023
* feat: support exchangeRates with multiple decimal points

* feat: add base parameter to rates service

* test: update tests with new /rates api

* feat: add base parameter to openapi spec
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pkg: backend Changes in the backend package. pkg: mock-ase type: source Changes business logic type: tests Testing related
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Allow /rates API to take in a base parameter
2 participants