Skip to content

Commit

Permalink
feat: tokenconverter
Browse files Browse the repository at this point in the history
  • Loading branch information
kimurayu45z committed May 21, 2024
1 parent 5ae328c commit 4a19112
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 62 deletions.
58 changes: 29 additions & 29 deletions api/sunrise/tokenconverter/params.pulsar.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion proto/sunrise/tokenconverter/params.proto
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ message Params {

string denom_gov_token = 1;
string denom_fee_token = 2;
string supply_cap_fee_token = 3 [
string max_supply_fee_token = 3 [
(cosmos_proto.scalar) = "cosmos.Int",
(gogoproto.customtype) = "cosmossdk.io/math.Int",
(gogoproto.nullable) = false,
Expand Down
4 changes: 2 additions & 2 deletions x/tokenconverter/README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# tokenconverter

This module is for converting `SRG` to `SR` token.
This module is for converting `SSR` to `SR` token.

$$
\text{OutputSR} = \text{InputSSR} \times \min\left(1, \frac{\text{CurrentSupplySR}}{\text{CurrentSupplySSR}} \right) \ \text{if} \ \text{CurrentSupplySR} + \text{OutputSR} \le \text{MaxSupplySR}
\text{OutputSR} = \text{InputSSR} \times \min\left(1, \frac{\text{MaxSupplySR}}{\text{CurrentSupplySSR}} \right) \ \text{if} \ \text{CurrentSupplySR} + \text{OutputSR} \le \text{MaxSupplySR}
$$

## Ante Handler
Expand Down
10 changes: 4 additions & 6 deletions x/tokenconverter/keeper/calculation.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,19 @@ import (
func (k Keeper) CalculateAmountOutFeeToken(ctx context.Context, amountInGovToken math.Int) math.Int {
params := k.GetParams(ctx)

supplyFee := k.bankKeeper.GetSupply(ctx, params.DenomFeeToken)
supplyGov := k.bankKeeper.GetSupply(ctx, params.DenomGovToken)

output := types.CalculateAmountOutFeeToken(supplyFee.Amount, supplyGov.Amount, amountInGovToken)
output := types.CalculateAmountOutFeeToken(params.MaxSupplyFeeToken, supplyGov.Amount, amountInGovToken)

return output
}

func (k Keeper) CalculateAmountInGovToken(ctx context.Context, amountOutFeeToken math.Int) math.Int {
params := k.GetParams(ctx)

supplyFee := k.bankKeeper.GetSupply(ctx, params.DenomFeeToken)
supplyGov := k.bankKeeper.GetSupply(ctx, params.DenomGovToken)

input := types.CalculateAmountInGovToken(supplyFee.Amount, supplyGov.Amount, amountOutFeeToken)
input := types.CalculateAmountInGovToken(params.MaxSupplyFeeToken, supplyGov.Amount, amountOutFeeToken)

return input
}
Expand All @@ -45,8 +43,8 @@ func (k Keeper) Convert(ctx context.Context, amountInGovToken math.Int, amountOu

supplyFee := k.bankKeeper.GetSupply(ctx, params.DenomFeeToken)

if supplyFee.Amount.Add(amountOutFeeToken).GT(params.SupplyCapFeeToken) {
return types.ErrExceedsSupplyCap
if supplyFee.Amount.Add(amountOutFeeToken).GT(params.MaxSupplyFeeToken) {
return types.ErrExceedsMaxSupply
}

if err := k.bankKeeper.SendCoinsFromAccountToModule(ctx, address, types.ModuleName, sdk.NewCoins(govToken)); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion x/tokenconverter/types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ var (
ErrInvalidSigner = sdkerrors.Register(ModuleName, 1100, "expected gov account as only signer for proposal message")
ErrSample = sdkerrors.Register(ModuleName, 1101, "sample error")

ErrExceedsSupplyCap = sdkerrors.Register(ModuleName, 1111, "exceeds supply cap")
ErrExceedsMaxSupply = sdkerrors.Register(ModuleName, 1111, "exceeds max supply")

ErrInsufficientAmountOut = sdkerrors.Register(ModuleName, 1121, "insufficient amount out")
ErrExceededAmountIn = sdkerrors.Register(ModuleName, 1122, "exceeded amount in")
Expand Down
34 changes: 17 additions & 17 deletions x/tokenconverter/types/params.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions x/tokenconverter/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"cosmossdk.io/math"
)

func ratio(supplyFeeToken math.Int, supplyGovToken math.Int) math.LegacyDec {
feeDec := math.LegacyNewDecFromInt(supplyFeeToken)
func ratio(maxSupplyFeeToken math.Int, supplyGovToken math.Int) math.LegacyDec {
feeDec := math.LegacyNewDecFromInt(maxSupplyFeeToken)
govDec := math.LegacyNewDecFromInt(supplyGovToken)
ratio := feeDec.Quo(govDec)

Expand All @@ -17,14 +17,14 @@ func ratio(supplyFeeToken math.Int, supplyGovToken math.Int) math.LegacyDec {

}

func CalculateAmountOutFeeToken(supplyFeeToken math.Int, supplyGovToken math.Int, amountInGovToken math.Int) math.Int {
ratio := ratio(supplyFeeToken, supplyGovToken)
func CalculateAmountOutFeeToken(maxSupplyFeeToken math.Int, supplyGovToken math.Int, amountInGovToken math.Int) math.Int {
ratio := ratio(maxSupplyFeeToken, supplyGovToken)

return ratio.MulInt(amountInGovToken).TruncateInt()
}

func CalculateAmountInGovToken(supplyFeeToken math.Int, supplyGovToken math.Int, amountOutFeeToken math.Int) math.Int {
ratio := ratio(supplyFeeToken, supplyGovToken)
func CalculateAmountInGovToken(maxSupplyFeeToken math.Int, supplyGovToken math.Int, amountOutFeeToken math.Int) math.Int {
ratio := ratio(maxSupplyFeeToken, supplyGovToken)

return math.LegacyOneDec().Quo(ratio).MulInt(amountOutFeeToken).TruncateInt()
}

0 comments on commit 4a19112

Please sign in to comment.