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 support to dec34 #1100

Open
wants to merge 22 commits into
base: main
Choose a base branch
from
Open

Conversation

cosmic-vagabond
Copy link
Contributor

@cosmic-vagabond cosmic-vagabond commented Jan 7, 2025

Description

What has Changed?

Add support to Dec34 type to handle up to 34 decimal places for price calculation using asset with high decimal precision (e.g. WETH 18 decimals)

Test results

EVM token price (0.004)

$ elysd q oracle show-price WETH
price:
  asset: WETH
  block_height: "668964"
  price: "0.004000000000000000"
  provider: elys10d07y265gmmuvt4z0w9aw880jnsr700j6z2zm3
  source: elys
  timestamp: "1736437787"

Swap estimation retrieves correct price and amount:

$ elysd q amm swap-estimation-by-denom 1$WETH $weth $usdc

amount:
  amount: "3990"
  denom: ibc/F082B65C88E4B6D5EF1DB243CDA1D331D002759E938A0F5CD3FFDC5D53B3E349
available_liquidity:
  amount: "100000000000"
  denom: ibc/F082B65C88E4B6D5EF1DB243CDA1D331D002759E938A0F5CD3FFDC5D53B3E349
discount: "0.000000000000000000"
in_route:
- pool_id: "3"
  token_out_denom: ibc/F082B65C88E4B6D5EF1DB243CDA1D331D002759E938A0F5CD3FFDC5D53B3E349
out_route: []
price_impact: "0.99999999999900259975"
slippage: "0.000099999999999999999999999999999999"
spot_price: "0.00400040004000400040004000400040004"
swap_fee: "0.002000000000000000"
weight_balance_ratio: "-0.0005001251219032960793880326679998092"

Inverse price accurate:

$ elysd q amm swap-estimation-by-denom 1$USDC $usdc $weth 
amount:
  amount: "249972500399982666971"
  denom: weth-wei
available_liquidity:
  amount: "25000000000000000000000000"
  denom: weth-wei
discount: "0.000000000000000000"
in_route:
- pool_id: "3"
  token_out_denom: weth-wei
out_route: []
price_impact: "-999990000598.9906669506950695069507"
slippage: "0.00000999940000933304930493049304930493"
spot_price: "249.975"
swap_fee: "0.000000000000000000"
weight_balance_ratio: "0"

Author Checklist

All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues.

I have...

  • included the correct type prefix in the PR title
  • added ! to the type prefix if API or client breaking change
  • targeted the correct branch (see PR Targeting)
  • provided a link to the relevant issue or specification
  • followed the guidelines for building modules
  • included the necessary unit and integration tests
  • included comments for documenting Go code
  • updated the relevant documentation or specification
  • reviewed "Files changed" and left comments if necessary
  • confirmed all CI checks have passed

Reviewers Checklist

All items are required. Please add a note if the item is not applicable and please add
your handle next to the items reviewed if you only reviewed selected items.

I have...

  • confirmed the correct type prefix in the PR title
  • confirmed ! in the type prefix if API or client breaking change
  • confirmed all author checklist items have been addressed
  • reviewed state machine logic
  • reviewed API design and naming
  • reviewed documentation is accurate
  • reviewed tests and test coverage
  • manually tested (if applicable)

Deployment Notes

Are there any specific considerations to take into account when deploying these changes? This may include new dependencies, scripts that need to be executed, or any aspects that can only be evaluated in a deployed environment.

Screenshots and Videos

Please provide any relevant before and after screenshots by uploading them here. Additionally, demo videos can be highly beneficial in demonstrating the process.

…acyDec to work with decimal places less than -18
Copy link

codecov bot commented Jan 10, 2025

Codecov Report

Attention: Patch coverage is 40.97015% with 791 lines in your changes missing coverage. Please review.

Project coverage is 47.63%. Comparing base (bd617a1) to head (787d233).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1100      +/-   ##
==========================================
- Coverage   48.13%   47.63%   -0.50%     
==========================================
  Files         892      897       +5     
  Lines       35069    35744     +675     
==========================================
+ Hits        16880    17028     +148     
- Misses      16931    17439     +508     
- Partials     1258     1277      +19     
Components Coverage Δ
leveragelp_transactions 75.00% <ø> (ø)
leveragelp_lifecycle 82.72% <100.00%> (ø)
leveragelp_keeper 85.41% <ø> (ø)
leveragelp_queries 19.94% <20.00%> (ø)
accountedpool_transactions 100.00% <ø> (ø)
accountedpool_lifecycle ∅ <ø> (∅)
accountedpool_queries 81.25% <ø> (ø)
amm_transactions 80.28% <100.00%> (ø)
amm_lifecycle 90.66% <ø> (ø)
amm_keeper 73.59% <100.00%> (ø)
amm_queries 82.45% <66.66%> (ø)
assetprofile_transactions 76.85% <ø> (ø)
assetprofile_lifecycle ∅ <ø> (∅)
assetprofile_keeper 80.00% <ø> (ø)
assetprofile_queries 60.00% <ø> (ø)
burner_transactions 0.00% <ø> (ø)
burner_lifecycle ∅ <ø> (∅)
burner_keeper 100.00% <ø> (ø)
burner_queries 79.06% <ø> (ø)
commitment_transactions 74.63% <ø> (ø)
commitment_lifecycle ∅ <ø> (∅)
commitment_keeper 86.17% <ø> (ø)
commitment_queries 59.04% <ø> (ø)
epochs_transactions ∅ <ø> (∅)
epochs_lifecycle 92.00% <ø> (ø)
epochs_keeper 84.61% <ø> (ø)
epochs_queries 83.33% <ø> (ø)
estaking_transactions 68.93% <ø> (ø)
estaking_lifecycle 82.60% <ø> (ø)
estaking_keeper 72.80% <ø> (ø)
estaking_queries 62.35% <ø> (ø)
incentive_transactions ∅ <ø> (∅)
incentive_lifecycle ∅ <ø> (∅)
incentive_keeper ∅ <ø> (∅)
incentive_queries ∅ <ø> (∅)
masterchef_transactions 86.85% <ø> (ø)
masterchef_lifecycle 75.09% <72.72%> (-0.34%) ⬇️
masterchef_keeper 100.00% <ø> (ø)
masterchef_queries 38.74% <45.00%> (ø)
oracle_transactions 27.27% <ø> (ø)
oracle_lifecycle 30.00% <ø> (ø)
oracle_keeper 61.11% <ø> (ø)
oracle_queries 34.53% <ø> (ø)
parameter_transactions 18.86% <ø> (ø)
parameter_lifecycle ∅ <ø> (∅)
parameter_keeper 75.00% <ø> (ø)
parameter_queries 57.14% <ø> (ø)
stablestake_transactions 81.53% <ø> (ø)
stablestake_lifecycle 100.00% <ø> (ø)
stablestake_keeper 90.47% <ø> (ø)
stablestake_queries 100.00% <ø> (ø)
perpetual_transactions 76.10% <ø> (ø)
perpetual_lifecycle 90.90% <ø> (ø)
perpetual_keeper 61.68% <ø> (ø)
perpetual_queries 64.86% <100.00%> (ø)
tier_transactions 100.00% <ø> (ø)
tier_lifecycle 100.00% <ø> (ø)
tier_keeper 90.90% <ø> (ø)
tier_queries 79.92% <77.27%> (ø)
tokenomics_transactions 71.87% <ø> (ø)
tokenomics_lifecycle ∅ <ø> (∅)
tokenomics_keeper 80.00% <ø> (ø)
tokenomics_queries 79.06% <ø> (ø)
transferhook_transactions ∅ <ø> (∅)
transferhook_lifecycle ∅ <ø> (∅)
transferhook_keeper 100.00% <ø> (ø)
transferhook_queries 57.14% <ø> (ø)
tradeshield_transactions 74.29% <ø> (ø)
tradeshield_lifecycle ∅ <ø> (∅)
tradeshield_keeper 90.90% <100.00%> (ø)
tradeshield_queries 67.56% <ø> (ø)

types/dec34.go Outdated Show resolved Hide resolved
types/dec34.go Outdated Show resolved Hide resolved
regenmath "github.com/regen-network/regen-ledger/types/v2/math"
)

type Dec34 regenmath.Dec
Copy link
Contributor

Choose a reason for hiding this comment

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

https://github.com/cosmos/cosmos-sdk/blob/v0.50.9/x/group/internal/math/dec.go#L14

Considering the comments above, we shall use wrapper?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@avkr003 regenmath.Dec is already a wrapper around apd.Decimal here we are essentially defining a new type that is a regenmath.Dec and therefore a wrapper of apd.Decimal as well

Copy link
Contributor

Choose a reason for hiding this comment

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

@cosmic-vagabond instead of using their library lets copy paste their whole file. I think they did the same from the cosmos SDK / group module or cosmos copy pasted from them. I am guessing there must some reasoning they didn't import but created own data structures (can't think of though, may be easier to migrate later or easier auditing as less exposure to external libraries)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@avkr003 this seems to be a minor change we can come back to based on our priorities, I would be happy to work on it in another PR as this one is quite big and becomes extremely difficult to maintain consider the other changes that gets added to the repo.

proto/elys/amm/tx.proto Show resolved Hide resolved
import (
"cosmossdk.io/math"
"github.com/cockroachdb/apd/v2"
regenmath "github.com/regen-network/regen-ledger/types/v2/math"
Copy link
Contributor

Choose a reason for hiding this comment

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

necessary to use regen network code? Directly use apd? cosmos in group module (example)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

regenmath "github.com/regen-network/regen-ledger/types/v2/math"
)

type Dec34 regenmath.Dec
Copy link
Contributor

Choose a reason for hiding this comment

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

@cosmic-vagabond instead of using their library lets copy paste their whole file. I think they did the same from the cosmos SDK / group module or cosmos copy pasted from them. I am guessing there must some reasoning they didn't import but created own data structures (can't think of though, may be easier to migrate later or easier auditing as less exposure to external libraries)

sdkmath "cosmossdk.io/math"
)

func OneTokenUnit(decimal uint64) sdkmath.Int {
Copy link
Contributor

Choose a reason for hiding this comment

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

The function name isn't obvious. It's returning 10^6 or 10^18 or based on right? Rename to GetPowerOfTen (basically exactly what it does)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

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.

2 participants